mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-02-02 05:41:10 -08:00
parent
f2d926b78c
commit
54672cefdf
|
@ -3,7 +3,6 @@ package segments
|
||||||
import (
|
import (
|
||||||
"oh-my-posh/mock"
|
"oh-my-posh/mock"
|
||||||
"oh-my-posh/properties"
|
"oh-my-posh/properties"
|
||||||
"oh-my-posh/template"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
@ -45,7 +44,7 @@ func TestAWSSegment(t *testing.T) {
|
||||||
Region: "eu-west",
|
Region: "eu-west",
|
||||||
Template: "profile: {{.Profile}}{{if .Region}} in {{.Region}}{{end}}",
|
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 {
|
for _, tc := range cases {
|
||||||
|
|
|
@ -394,9 +394,22 @@ func (pt *Path) replaceMappedLocations() (string, string) {
|
||||||
// mapped locations can override predefined locations
|
// mapped locations can override predefined locations
|
||||||
keyValues := pt.props.GetKeyValueMap(MappedLocations, make(map[string]string))
|
keyValues := pt.props.GetKeyValueMap(MappedLocations, make(map[string]string))
|
||||||
for key, val := range keyValues {
|
for key, val := range keyValues {
|
||||||
if key != "" {
|
if len(key) == 0 {
|
||||||
mappedLocations[pt.normalize(key)] = val
|
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
|
// sort map keys in reverse order
|
||||||
|
|
|
@ -842,6 +842,7 @@ func TestFullPathCustomMappedLocations(t *testing.T) {
|
||||||
PathSeparator string
|
PathSeparator string
|
||||||
Expected 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/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": "#"}, GOOS: platform.WINDOWS, PathSeparator: "\\", Expected: "#\\c\\d"},
|
||||||
{Pwd: "/a/b/c/d", MappedLocations: map[string]string{"/a/b": "#"}, 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("Flags").Return(args)
|
||||||
env.On("Shell").Return(shell.PLAIN)
|
env.On("Shell").Return(shell.PLAIN)
|
||||||
|
env.On("TemplateCache").Return(&platform.TemplateCache{
|
||||||
|
Env: map[string]string{
|
||||||
|
"HOME": "/a/b/c",
|
||||||
|
},
|
||||||
|
})
|
||||||
path := &Path{
|
path := &Path{
|
||||||
env: env,
|
env: env,
|
||||||
props: properties.Map{
|
props: properties.Map{
|
||||||
|
|
|
@ -42,6 +42,9 @@ func (c *Context) init(t *Text) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Text) Render() (string, error) {
|
func (t *Text) Render() (string, error) {
|
||||||
|
if !strings.Contains(t.Template, "{{") || !strings.Contains(t.Template, "}}") {
|
||||||
|
return t.Template, nil
|
||||||
|
}
|
||||||
t.cleanTemplate()
|
t.cleanTemplate()
|
||||||
tmpl, err := template.New(t.Template).Funcs(funcMap()).Parse(t.Template)
|
tmpl, err := template.New(t.Template).Funcs(funcMap()).Parse(t.Template)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in a new issue