fix(template): allow literal dot word

This commit is contained in:
Jan De Dobbeleer 2022-06-28 20:23:07 +02:00 committed by Jan De Dobbeleer
parent 5f8cf3e353
commit 0b0f119420
2 changed files with 14 additions and 2 deletions

View file

@ -77,13 +77,16 @@ func (t *Text) cleanTemplate() {
*knownVariables = append(*knownVariables, splitted[0])
return splitted[0], true
}
knownVariables := []string{"Root", "PWD", "Folder", "Shell", "ShellVersion", "UserName", "HostName", "Env", "Data", "Code", "OS", "WSL", "Segments"}
matches := regex.FindAllNamedRegexMatch(`(?: |{|\()(?P<var>(\.[a-zA-Z_][a-zA-Z0-9]*)+)`, t.Template)
matches := regex.FindAllNamedRegexMatch(`(?: |{|\()(?P<VAR>(\.[a-zA-Z_][a-zA-Z0-9]*)+)`, t.Template)
for _, match := range matches {
if variable, OK := unknownVariable(match["var"], &knownVariables); OK {
if variable, OK := unknownVariable(match["VAR"], &knownVariables); OK {
pattern := fmt.Sprintf(`\.%s\b`, variable)
dataVar := fmt.Sprintf(".Data.%s", variable)
t.Template = regex.ReplaceAllString(pattern, t.Template, dataVar)
}
}
// allow literal dots in template
t.Template = strings.ReplaceAll(t.Template, `\.`, ".")
}

View file

@ -40,6 +40,15 @@ New-Alias -Name 'Set-PoshContext' -Value 'Set-EnvVar' -Scope Global -Force
## 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 -->
| Template | Description |
| -------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |