Skip to content

Commit b2c8f14

Browse files
feat(path): allow pattern matching in maped_locations
resolves #6280
1 parent 5c0625d commit b2c8f14

File tree

7 files changed

+88
-62
lines changed

7 files changed

+88
-62
lines changed

Diff for: src/regex/regex.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -110,21 +110,21 @@ func MatchString(pattern, text string) bool {
110110
return re.MatchString(text)
111111
}
112112

113-
func FindStringMatch(pattern, text string, index int) string {
113+
func FindStringMatch(pattern, text string, index int) (string, bool) {
114114
re, err := GetCompiledRegex(pattern)
115115
if err != nil {
116-
return text
116+
return text, false
117117
}
118118

119119
matches := re.FindStringSubmatch(text)
120120
if len(matches) <= index {
121-
return text
121+
return text, false
122122
}
123123

124124
match := matches[index]
125125
if len(match) == 0 {
126-
return text
126+
return text, false
127127
}
128128

129-
return match
129+
return match, true
130130
}

Diff for: src/regex/regex_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func TestFindStringMatch(t *testing.T) {
6666
}
6767

6868
for _, tc := range cases {
69-
got := FindStringMatch(tc.Pattern, tc.Text, tc.Index)
69+
got, _ := FindStringMatch(tc.Pattern, tc.Text, tc.Index)
7070
assert.Equal(t, tc.Expected, got, tc.Case)
7171
}
7272
}

Diff for: src/segments/golang.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func (g *Golang) parseWorkFile() (string, error) {
8080
}
8181

8282
contents := g.env.FileContent(goWork.Path)
83-
version := regex.FindStringMatch(`go (\d(\.\d{1,2})?(\.\d{1,2})?)`, contents, 1)
83+
version, _ := regex.FindStringMatch(`go (\d(\.\d{1,2})?(\.\d{1,2})?)`, contents, 1)
8484
if len(version) > 0 {
8585
return version, nil
8686
}

Diff for: src/segments/path.go

+22-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ import (
1616
"github.com/jandedobbeleer/oh-my-posh/src/template"
1717
)
1818

19+
const (
20+
regexPrefix = "re:"
21+
)
22+
1923
type Folder struct {
2024
Name string
2125
Path string
@@ -570,12 +574,16 @@ func (pt *Path) setMappedLocations() {
570574
continue
571575
}
572576

577+
if !strings.HasPrefix(location, regexPrefix) {
578+
location = pt.normalize(location)
579+
}
580+
573581
// When two templates resolve to the same key, the values are compared in ascending order and the latter is taken.
574-
if v, exist := mappedLocations[pt.normalize(location)]; exist && value <= v {
582+
if v, exist := mappedLocations[location]; exist && value <= v {
575583
continue
576584
}
577585

578-
mappedLocations[pt.normalize(location)] = value
586+
mappedLocations[location] = value
579587
}
580588

581589
pt.mappedLocations = mappedLocations
@@ -610,7 +618,19 @@ func (pt *Path) replaceMappedLocations(inputPath string) (string, string) {
610618
}
611619

612620
for _, key := range keys {
621+
if strings.HasPrefix(key, regexPrefix) {
622+
match, OK := regex.FindStringMatch(key[len(regexPrefix):], inputPath, 1)
623+
if !OK {
624+
continue
625+
}
626+
627+
// Replace the first match with the mapped location.
628+
inputPath = strings.Replace(inputPath, match, pt.mappedLocations[key], 1)
629+
return pt.parsePath(inputPath)
630+
}
631+
613632
keyRoot, keyRelative := pt.parsePath(key)
633+
614634
matchSubFolders := strings.HasSuffix(keyRelative, pt.pathSeparator+"*")
615635

616636
if matchSubFolders {

0 commit comments

Comments
 (0)