oh-my-posh/src/template/text.go

146 lines
2.9 KiB
Go
Raw Normal View History

2022-01-26 06:54:36 -08:00
package template
2021-02-07 01:54:36 -08:00
import (
"bytes"
2021-04-11 06:24:03 -07:00
"errors"
"oh-my-posh/environment"
"strings"
2021-02-07 01:54:36 -08:00
"text/template"
)
const (
// Errors to show when the template handling fails
2022-01-26 06:54:36 -08:00
InvalidTemplate = "invalid template text"
IncorrectTemplate = "unable to create text based on template"
2021-02-07 01:54:36 -08:00
)
2022-01-26 06:54:36 -08:00
type Text struct {
Template string
Context interface{}
Env environment.Environment
TemplatesResult string
2021-02-07 01:54:36 -08:00
}
type Data interface{}
type Context struct {
*environment.TemplateCache
2022-01-18 00:48:47 -08:00
// Simple container to hold ANY object
Data
Templates string
2022-01-18 00:48:47 -08:00
}
2022-01-26 06:54:36 -08:00
func (c *Context) init(t *Text) {
c.Data = t.Context
c.Templates = t.TemplatesResult
if cache := t.Env.TemplateCache(); cache != nil {
c.TemplateCache = cache
return
2021-06-15 12:23:08 -07:00
}
}
2022-01-26 06:54:36 -08:00
func (t *Text) Render() (string, error) {
t.cleanTemplate()
tmpl, err := template.New(t.Template).Funcs(funcMap()).Parse(t.Template)
2021-02-07 01:54:36 -08:00
if err != nil {
t.Env.Log(environment.Error, "Render", err.Error())
2022-01-26 06:54:36 -08:00
return "", errors.New(InvalidTemplate)
2021-02-07 01:54:36 -08:00
}
context := &Context{}
context.init(t)
2021-02-07 01:54:36 -08:00
buffer := new(bytes.Buffer)
defer buffer.Reset()
err = tmpl.Execute(buffer, context)
2021-02-07 01:54:36 -08:00
if err != nil {
t.Env.Log(environment.Error, "Render", err.Error())
2022-01-26 06:54:36 -08:00
return "", errors.New(IncorrectTemplate)
2021-02-07 01:54:36 -08:00
}
text := buffer.String()
// issue with missingkey=zero ignored for map[string]interface{}
// https://github.com/golang/go/issues/24963
text = strings.ReplaceAll(text, "<no value>", "")
return text, nil
}
2022-01-26 06:54:36 -08:00
func (t *Text) cleanTemplate() {
knownVariables := []string{
"Root",
"PWD",
"Folder",
"Shell",
"ShellVersion",
"UserName",
"HostName",
"Env",
"Data",
"Code",
"OS",
"WSL",
"Segments",
"Templates",
}
knownVariable := func(variable string) bool {
if variable == "." {
return true
}
variable = strings.TrimPrefix(variable, ".")
splitted := strings.Split(variable, ".")
if len(splitted) == 0 {
return false
}
variable = splitted[0]
for _, b := range knownVariables {
if variable == b {
return true
}
}
return 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:
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)
}
}
t.Template = result
2021-02-07 01:54:36 -08:00
}