From 9a9882316610e8919be0c4b86024db3b51d09b59 Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Thu, 13 Oct 2022 20:12:06 +0200 Subject: [PATCH] fix(template): parse entire template resolves #2937 --- src/template/text.go | 84 +++++++++++++++++---------------------- src/template/text_test.go | 12 ++++++ 2 files changed, 49 insertions(+), 47 deletions(-) diff --git a/src/template/text.go b/src/template/text.go index 4c03f761..42dfed5c 100644 --- a/src/template/text.go +++ b/src/template/text.go @@ -4,7 +4,6 @@ import ( "bytes" "errors" "oh-my-posh/environment" - "oh-my-posh/regex" "strings" "text/template" ) @@ -97,56 +96,47 @@ 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 { - switch char { - case '.': - var lastChar rune - if len(result) > 0 { - lastChar = rune(result[len(result)-1]) - } - // only replace if we're in a valid property start - // with a space, { or ( character - switch lastChar { - case ' ', '{', '(': - property += string(char) - inProperty = true - default: - result += string(char) - } - case ' ', '}', ')': // space or } - if !inProperty { - result += string(char) - continue - } - // end of a variable, needs to be appended - if !knownVariable(property) { - result += ".Data" + property - } else { - result += property - } - property = "" - result += string(char) - inProperty = false + var result string + var property string + var inProperty bool + for _, char := range t.Template { + switch char { + case '.': + var lastChar rune + if len(result) > 0 { + lastChar = rune(result[len(result)-1]) + } + // only replace if we're in a valid property start + // with a space, { or ( character + switch lastChar { + case ' ', '{', '(': + property += string(char) + inProperty = true default: - if inProperty { - property += string(char) - continue - } result += string(char) } + case ' ', '}', ')': // space or } + if !inProperty { + result += string(char) + continue + } + // end of a variable, needs to be appended + if !knownVariable(property) { + result += ".Data" + property + } else { + result += property + } + property = "" + result += string(char) + inProperty = false + default: + if inProperty { + property += string(char) + continue + } + result += string(char) } - return result } - // matches := regex.FindAllNamedRegexMatch(`(?: |{|\()(?P(\.[a-zA-Z_][a-zA-Z0-9]*)+)`, t.Template) - matches := regex.FindAllNamedRegexMatch(`(?P{{[^{]+}})`, t.Template) - for _, match := range matches { - node := walkAndReplace(match["NODE"]) - t.Template = strings.Replace(t.Template, match["NODE"], node, 1) - } + t.Template = result } diff --git a/src/template/text_test.go b/src/template/text_test.go index 55bc8026..30c721d2 100644 --- a/src/template/text_test.go +++ b/src/template/text_test.go @@ -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",