From 80b92505bc3b79c417cf8f677d20abe675078318 Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Sun, 18 Dec 2022 13:04:42 +0100 Subject: [PATCH] fix(template): remove the need for escaping a dot literal --- src/template/text.go | 21 +++++++++++++++++---- src/template/text_test.go | 6 ++++++ website/docs/configuration/templates.mdx | 10 ---------- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/template/text.go b/src/template/text.go index 78e5fd2d..f8d4e54f 100644 --- a/src/template/text.go +++ b/src/template/text.go @@ -108,10 +108,23 @@ func (t *Text) cleanTemplate() { return false } - var result string - var property string - var inProperty bool - for _, char := range t.Template { + var result, property string + var inProperty, inTemplate bool + for i, char := range t.Template { + // define start or end of template + if !inTemplate && char == '{' { + if i-1 >= 0 && rune(t.Template[i-1]) == '{' { + inTemplate = true + } + } else if inTemplate && char == '}' { + if i-1 >= 0 && rune(t.Template[i-1]) == '}' { + inTemplate = false + } + } + if !inTemplate { + result += string(char) + continue + } switch char { case '.': var lastChar rune diff --git a/src/template/text_test.go b/src/template/text_test.go index 69b13ec7..ddfa2ede 100644 --- a/src/template/text_test.go +++ b/src/template/text_test.go @@ -20,6 +20,12 @@ func TestRenderTemplate(t *testing.T) { ShouldError bool Context interface{} }{ + { + Case: "dot literal", + Expected: "Hello .NET \uE77F", + Template: "{{ .Text }} .NET \uE77F", + Context: struct{ Text string }{Text: "Hello"}, + }, { Case: "color override with dots", Expected: "πŸ˜ΊπŸ’¬<#FF8000> Meow! What should I do next? ...", diff --git a/website/docs/configuration/templates.mdx b/website/docs/configuration/templates.mdx index af356273..6a2b44f7 100644 --- a/website/docs/configuration/templates.mdx +++ b/website/docs/configuration/templates.mdx @@ -90,16 +90,6 @@ end ## Template logic -:::tip -Oh My Posh replaces all unknown `.Var` variables in the template with the right references. If you need a literal `.Var`, use `\.Var` -in the template. - -```json -"template": " \\.NET \uE77F " -``` - -::: - | Template | Description |