mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-02-01 13:21:09 -08:00
perf(template): cache template functions
This commit is contained in:
parent
48528a3fd3
commit
9e1942035f
|
@ -1,6 +1,8 @@
|
|||
package main
|
||||
|
||||
import "github.com/jandedobbeleer/oh-my-posh/src/cli"
|
||||
import (
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/cli"
|
||||
)
|
||||
|
||||
func main() {
|
||||
cli.Execute()
|
||||
|
|
|
@ -10,7 +10,7 @@ import (
|
|||
func BenchmarkInit(b *testing.B) {
|
||||
cmd := cli.RootCmd
|
||||
// needs to be a non-existing file as we panic otherwise
|
||||
cmd.SetArgs([]string{"init", "fish", "--print", "--config", "err.omp.json"})
|
||||
cmd.SetArgs([]string{"init", "fish", "--print"})
|
||||
out := bytes.NewBufferString("")
|
||||
cmd.SetOut(out)
|
||||
|
||||
|
@ -22,7 +22,7 @@ func BenchmarkInit(b *testing.B) {
|
|||
func BenchmarkPrimary(b *testing.B) {
|
||||
cmd := cli.RootCmd
|
||||
// needs to be a non-existing file as we panic otherwise
|
||||
cmd.SetArgs([]string{"print", "primary", "--config", "err.omp.json", "--pwd", "/Users/jan/Code/oh-my-posh/src", "--shell", "fish"})
|
||||
cmd.SetArgs([]string{"print", "primary", "--pwd", "/Users/jan/Code/oh-my-posh/src", "--shell", "fish"})
|
||||
out := bytes.NewBufferString("")
|
||||
cmd.SetOut(out)
|
||||
|
||||
|
|
|
@ -48,6 +48,8 @@ var (
|
|||
}
|
||||
|
||||
shell string
|
||||
|
||||
tmplFunc = template.New("cache").Funcs(funcMap())
|
||||
)
|
||||
|
||||
type Text struct {
|
||||
|
@ -87,7 +89,7 @@ func (t *Text) Render() (string, error) {
|
|||
|
||||
t.cleanTemplate()
|
||||
|
||||
tmpl, err := template.New(t.Template).Funcs(funcMap()).Parse(t.Template)
|
||||
tmpl, err := tmplFunc.Parse(t.Template)
|
||||
if err != nil {
|
||||
t.Env.Error(err)
|
||||
return "", errors.New(InvalidTemplate)
|
||||
|
@ -106,6 +108,7 @@ func (t *Text) Render() (string, error) {
|
|||
if len(msg) == 0 {
|
||||
return "", errors.New(IncorrectTemplate)
|
||||
}
|
||||
|
||||
return "", errors.New(msg["MSG"])
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue