diff --git a/src/segments/aws_test.go b/src/segments/aws_test.go index 43623b03..6bbc510a 100644 --- a/src/segments/aws_test.go +++ b/src/segments/aws_test.go @@ -3,7 +3,6 @@ package segments import ( "oh-my-posh/mock" "oh-my-posh/properties" - "oh-my-posh/template" "testing" "github.com/stretchr/testify/assert" @@ -45,7 +44,7 @@ func TestAWSSegment(t *testing.T) { Region: "eu-west", Template: "profile: {{.Profile}}{{if .Region}} in {{.Region}}{{end}}", }, - {Case: "template: invalid", ExpectedString: template.InvalidTemplate, ExpectedEnabled: true, Profile: "c", Template: "{{ .Burp"}, + {Case: "template: invalid", ExpectedString: "{{ .Burp", ExpectedEnabled: true, Profile: "c", Template: "{{ .Burp"}, } for _, tc := range cases { diff --git a/src/segments/path.go b/src/segments/path.go index 04cbdb11..d8bf983e 100644 --- a/src/segments/path.go +++ b/src/segments/path.go @@ -394,9 +394,22 @@ func (pt *Path) replaceMappedLocations() (string, string) { // mapped locations can override predefined locations keyValues := pt.props.GetKeyValueMap(MappedLocations, make(map[string]string)) for key, val := range keyValues { - if key != "" { - mappedLocations[pt.normalize(key)] = val + if len(key) == 0 { + continue } + tmpl := &template.Text{ + Template: key, + Context: pt, + Env: pt.env, + } + path, err := tmpl.Render() + if err != nil { + pt.env.Log(platform.Error, "replaceMappedLocations", err.Error()) + } + if len(path) == 0 { + continue + } + mappedLocations[pt.normalize(path)] = val } // sort map keys in reverse order diff --git a/src/segments/path_test.go b/src/segments/path_test.go index 9d70150f..b206ff6d 100644 --- a/src/segments/path_test.go +++ b/src/segments/path_test.go @@ -842,6 +842,7 @@ func TestFullPathCustomMappedLocations(t *testing.T) { PathSeparator string Expected string }{ + {Pwd: "/a/b/c/d", MappedLocations: map[string]string{"{{ .Env.HOME }}/d": "#"}, Expected: "#"}, {Pwd: "/a/b/c/d", MappedLocations: map[string]string{"/a/b/c/d": "#"}, Expected: "#"}, {Pwd: "\\a\\b\\c\\d", MappedLocations: map[string]string{"\\a\\b": "#"}, GOOS: platform.WINDOWS, PathSeparator: "\\", Expected: "#\\c\\d"}, {Pwd: "/a/b/c/d", MappedLocations: map[string]string{"/a/b": "#"}, Expected: "#/c/d"}, @@ -868,6 +869,11 @@ func TestFullPathCustomMappedLocations(t *testing.T) { } env.On("Flags").Return(args) env.On("Shell").Return(shell.PLAIN) + env.On("TemplateCache").Return(&platform.TemplateCache{ + Env: map[string]string{ + "HOME": "/a/b/c", + }, + }) path := &Path{ env: env, props: properties.Map{ diff --git a/src/template/text.go b/src/template/text.go index 6fd5423d..d3f26dd8 100644 --- a/src/template/text.go +++ b/src/template/text.go @@ -42,6 +42,9 @@ func (c *Context) init(t *Text) { } func (t *Text) Render() (string, error) { + if !strings.Contains(t.Template, "{{") || !strings.Contains(t.Template, "}}") { + return t.Template, nil + } t.cleanTemplate() tmpl, err := template.New(t.Template).Funcs(funcMap()).Parse(t.Template) if err != nil {