fix(template): remove the need for escaping a dot literal

This commit is contained in:
Jan De Dobbeleer 2022-12-18 13:04:42 +01:00 committed by Jan De Dobbeleer
parent c5b7c7fbfc
commit 80b92505bc
3 changed files with 23 additions and 14 deletions

View file

@ -108,10 +108,23 @@ func (t *Text) cleanTemplate() {
return false return false
} }
var result string var result, property string
var property string var inProperty, inTemplate bool
var inProperty bool for i, char := range t.Template {
for _, 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 { switch char {
case '.': case '.':
var lastChar rune var lastChar rune

View file

@ -20,6 +20,12 @@ func TestRenderTemplate(t *testing.T) {
ShouldError bool ShouldError bool
Context interface{} Context interface{}
}{ }{
{
Case: "dot literal",
Expected: "Hello .NET \uE77F",
Template: "{{ .Text }} .NET \uE77F",
Context: struct{ Text string }{Text: "Hello"},
},
{ {
Case: "color override with dots", Case: "color override with dots",
Expected: "😺💬<#FF8000> Meow! What should I do next? ...</>", Expected: "😺💬<#FF8000> Meow! What should I do next? ...</>",

View file

@ -90,16 +90,6 @@ end
## Template logic ## 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 "
```
:::
<!-- markdownlint-disable MD013 --> <!-- markdownlint-disable MD013 -->
| Template | Description | | Template | Description |