fix(templates): only match alphanumeric strings

relates to #2956
This commit is contained in:
Jan De Dobbeleer 2022-10-19 20:53:48 +02:00 committed by Jan De Dobbeleer
parent 6824a0a350
commit ae671c711d
2 changed files with 11 additions and 4 deletions

View file

@ -4,6 +4,7 @@ import (
"bytes"
"errors"
"oh-my-posh/environment"
"oh-my-posh/regex"
"strings"
"text/template"
)
@ -82,15 +83,16 @@ func (t *Text) cleanTemplate() {
}
knownVariable := func(variable string) bool {
if variable == "." {
return true
}
variable = strings.TrimPrefix(variable, ".")
splitted := strings.Split(variable, ".")
if len(splitted) == 0 {
return false
return true
}
variable = splitted[0]
// check if alphanumeric
if !regex.MatchString(`^[a-zA-Z0-9]+$`, variable) {
return true
}
for _, b := range knownVariables {
if variable == b {
return true

View file

@ -225,6 +225,11 @@ func TestCleanTemplate(t *testing.T) {
Expected string
Template string
}{
{
Case: "Literal dots",
Expected: " ... ",
Template: " ... ",
},
{
Case: "Literal dot",
Expected: "hello . what's up",