fix(template): parse entire template

resolves #2937
This commit is contained in:
Jan De Dobbeleer 2022-10-13 20:12:06 +02:00 committed by Jan De Dobbeleer
parent 040a73e0f0
commit 9a98823166
2 changed files with 49 additions and 47 deletions

View file

@ -4,7 +4,6 @@ import (
"bytes"
"errors"
"oh-my-posh/environment"
"oh-my-posh/regex"
"strings"
"text/template"
)
@ -97,12 +96,10 @@ func (t *Text) cleanTemplate() {
return false
}
walkAndReplace := func(node string) string {
var result string
var property string
var inProperty bool
// var literal bool
for _, char := range node {
for _, char := range t.Template {
switch char {
case '.':
var lastChar rune
@ -140,13 +137,6 @@ func (t *Text) cleanTemplate() {
result += string(char)
}
}
return result
}
// matches := regex.FindAllNamedRegexMatch(`(?: |{|\()(?P<VAR>(\.[a-zA-Z_][a-zA-Z0-9]*)+)`, t.Template)
matches := regex.FindAllNamedRegexMatch(`(?P<NODE>{{[^{]+}})`, t.Template)
for _, match := range matches {
node := walkAndReplace(match["NODE"])
t.Template = strings.Replace(t.Template, match["NODE"], node, 1)
}
t.Template = result
}

View file

@ -20,6 +20,18 @@ func TestRenderTemplate(t *testing.T) {
ShouldError bool
Context interface{}
}{
{
Case: "tillig's regex",
Expected: " ⎈ hello :: world ",
Template: " ⎈ {{ replaceP \"([a-f0-9]{2})[a-f0-9]{6}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{10}([a-f0-9]{2})\" .Context \"$1..$2\" }}{{ if .Namespace }} :: {{ .Namespace }}{{ end }} ", //nolint:lll
Context: struct {
Context string
Namespace string
}{
Context: "hello",
Namespace: "world",
},
},
{
Case: "Env like property name",
Expected: "hello world",