Skip to content

Commit c432282

Browse files
feat(path): allow cross platform pattern matching
resolves #6166
1 parent 425b8b1 commit c432282

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

Diff for: src/segments/path.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -619,14 +619,16 @@ func (pt *Path) replaceMappedLocations(inputPath string) (string, string) {
619619

620620
for _, key := range keys {
621621
if strings.HasPrefix(key, regexPrefix) {
622-
match, OK := regex.FindStringMatch(key[len(regexPrefix):], inputPath, 1)
622+
input := strings.ReplaceAll(inputPath, `\`, `/`)
623+
match, OK := regex.FindStringMatch(key[len(regexPrefix):], input, 1)
623624
if !OK {
624625
continue
625626
}
626627

627628
// Replace the first match with the mapped location.
628-
inputPath = strings.Replace(inputPath, match, pt.mappedLocations[key], 1)
629-
return pt.parsePath(inputPath)
629+
input = strings.Replace(input, match, pt.mappedLocations[key], 1)
630+
input = path.Clean(input)
631+
return pt.parsePath(input)
630632
}
631633

632634
keyRoot, keyRelative := pt.parsePath(key)

Diff for: src/segments/path_windows_test.go

+28-3
Original file line numberDiff line numberDiff line change
@@ -420,9 +420,34 @@ var testFullAndFolderPathCases = []testFullAndFolderPathCase{
420420
}
421421

422422
var testFullPathCustomMappedLocationsCases = []testFullPathCustomMappedLocationsCase{
423-
{Pwd: `\a\b\c\d`, MappedLocations: map[string]string{`\a\b`: "#"}, GOOS: runtime.WINDOWS, PathSeparator: `\`, Expected: `#\c\d`},
424-
{Pwd: `\a\b\1234\d\e`, MappedLocations: map[string]string{`re:(\\a\\b\\[0-9]+\\d).*`: "#"}, GOOS: runtime.WINDOWS, PathSeparator: `\`, Expected: `#\e`},
425-
{Pwd: `\a\b\1234\f\e`, MappedLocations: map[string]string{`re:(\\a\\b\\[0-9]+\\d).*`: "#"}, GOOS: runtime.WINDOWS, PathSeparator: `\`, Expected: `\a\b\1234\f\e`},
423+
{
424+
Pwd: `\a\b\c\d`,
425+
MappedLocations: map[string]string{`\a\b`: "#"},
426+
GOOS: runtime.WINDOWS,
427+
PathSeparator: `\`,
428+
Expected: `#\c\d`,
429+
},
430+
{
431+
Pwd: `\a\b\1234\d\e`,
432+
MappedLocations: map[string]string{`re:(/a/b/[0-9]+/d).*`: "#"},
433+
GOOS: runtime.WINDOWS,
434+
PathSeparator: `\`,
435+
Expected: `#\e`,
436+
},
437+
{
438+
Pwd: `\a\b\1234\f\e`,
439+
MappedLocations: map[string]string{`re:(/a/b/[0-9]+/d).*`: "#"},
440+
GOOS: runtime.WINDOWS,
441+
PathSeparator: `\`,
442+
Expected: `\a\b\1234\f\e`,
443+
},
444+
{
445+
Pwd: `C:\Users\taylo\Documents\GitHub\project`,
446+
MappedLocations: map[string]string{`re:(.*Users/taylo/Documents/GitHub).*`: "github"},
447+
GOOS: runtime.WINDOWS,
448+
PathSeparator: `\`,
449+
Expected: `github\project`,
450+
},
426451
}
427452

428453
var testSplitPathCases = []testSplitPathCase{

Diff for: website/docs/segments/system/path.mdx

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ For example, to swap out `C:\Users\Leet\GitHub` with a GitHub icon, you can do t
8888
user Bill, who has a user account `Bill` on Windows and `bill` on Linux, `~/Foo` might match
8989
`C:\Users\Bill\Foo` or `C:\Users\Bill\foo` on Windows but only `/home/bill/Foo` on Linux.
9090
- For more complicated cases, you can use the `re:` prefix to use a regular expression with a capture group for matching.
91-
For example, `"re:(C:\\[0-9]+\\Foo).*": "#"` will match `C:\123\Foo\Bar` and replace it with `#\Bar`. The regex is not
92-
cross platform out of the box, unlike the first statement for non-regex matches.
91+
For example, `"re:(C:/[0-9]+/Foo).*": "#"` will match `C:\123\Foo\Bar` and replace it with `#\Bar`. The path used for matching
92+
will always use `/`, regardless of the operating system, allowing cross platform matching.
9393

9494
## Style
9595

0 commit comments

Comments
 (0)