Skip to content

Commit

Permalink
fix(templatehelper): no styles in Ascii mode
Browse files Browse the repository at this point in the history
If the color profile doesnt support ANSI color codes, then it likely
shouldn't support any Style options either. This returns a no-op funcmap from
termenv.TemplateFuncs when the color profile is Ascii.
  • Loading branch information
justenwalker committed Jul 24, 2020
1 parent ae220b9 commit a8fb303
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 0 deletions.
25 changes: 25 additions & 0 deletions templatehelper.go
Expand Up @@ -6,6 +6,9 @@ import (

// TemplateFuncs contains a few useful template helpers
func TemplateFuncs(p Profile) template.FuncMap {
if p == Ascii {
return noopTemplateFuncs
}
return template.FuncMap{
"Color": func(values ...interface{}) string {
s := String(values[len(values)-1].(string))
Expand Down Expand Up @@ -53,3 +56,25 @@ func styleFunc(f func(Style) Style) func(...interface{}) string {
return f(s).String()
}
}

var noopTemplateFuncs = template.FuncMap{
"Color": noColorFunc,
"Foreground": noColorFunc,
"Background": noColorFunc,
"Bold": noStyleFunc,
"Faint": noStyleFunc,
"Italic": noStyleFunc,
"Underline": noStyleFunc,
"Overline": noStyleFunc,
"Blink": noStyleFunc,
"Reverse": noStyleFunc,
"CrossOut": noStyleFunc,
}

func noColorFunc(values ...interface{}) string {
return values[len(values)-1].(string)
}

func noStyleFunc(values ...interface{}) string {
return values[0].(string)
}
43 changes: 43 additions & 0 deletions templatehelper_test.go
@@ -0,0 +1,43 @@
package termenv

import (
"bytes"
"fmt"
"io/ioutil"
"testing"
"text/template"
)

func TestTemplateFuncs(t *testing.T) {
tests := []struct {
name string
profile Profile
}{
{"ascii", Ascii},
{"ansi", ANSI},
{"ansi256", ANSI256},
{"truecolor", TrueColor},
}
const templateFile = "./testdata/templatehelper.tpl"
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
tpl, err := template.New("templatehelper.tpl").Funcs(TemplateFuncs(test.profile)).ParseFiles(templateFile)
if err != nil {
t.Fatalf("unexpected error parsing template: %v", err)
}
var buf bytes.Buffer
if err = tpl.Execute(&buf, nil); err != nil {
t.Fatalf("unexpected error executing template: %v", err)
}
actual := buf.Bytes()
filename := fmt.Sprintf("./testdata/templatehelper_%s.txt", test.name)
expected, err := ioutil.ReadFile(filename)
if err != nil {
t.Fatalf("unexpected error reading golden file %q: %v", filename, err)
}
if !bytes.Equal(buf.Bytes(), expected) {
t.Fatalf("template output does not match golden file.\n--- Expected ---\n%s\n--- Actual ---\n%s\n", string(expected), string(actual))
}
})
}
}
14 changes: 14 additions & 0 deletions testdata/templatehelper.tpl
@@ -0,0 +1,14 @@
Plain
{{ Color "#ff0000" "Red" }}
{{ Color "#ff0000" "#00ff00" "Red on Blue" }}
{{ Foreground "#00ffff" "Cyan" }}
{{ Background "#ff00ff" "Magenta Bg" }}
{{ Foreground "#00ffff" (Background "#ff00ff" "Cyan on Magenta Bg") }}
{{ Bold "Bold" }}
{{ Faint "Faint" }}
{{ Italic "Italic" }}
{{ Underline "Underline" }}
{{ Overline "Overline" }}
{{ Blink "Blink" }}
{{ Reverse "Reverse" }}
{{ CrossOut "CrossOut" }}
14 changes: 14 additions & 0 deletions testdata/templatehelper_ansi.txt
@@ -0,0 +1,14 @@
Plain
Red
Red on Blue
Cyan
Magenta Bg
Cyan on Magenta Bg
Bold
Faint
Italic
Underline
Overline
Blink
Reverse
CrossOut
14 changes: 14 additions & 0 deletions testdata/templatehelper_ansi256.txt
@@ -0,0 +1,14 @@
Plain
Red
Red on Blue
Cyan
Magenta Bg
Cyan on Magenta Bg
Bold
Faint
Italic
Underline
Overline
Blink
Reverse
CrossOut
14 changes: 14 additions & 0 deletions testdata/templatehelper_ascii.txt
@@ -0,0 +1,14 @@
Plain
Red
Red on Blue
Cyan
Magenta Bg
Cyan on Magenta Bg
Bold
Faint
Italic
Underline
Overline
Blink
Reverse
CrossOut
14 changes: 14 additions & 0 deletions testdata/templatehelper_truecolor.txt
@@ -0,0 +1,14 @@
Plain
Red
Red on Blue
Cyan
Magenta Bg
Cyan on Magenta Bg
Bold
Faint
Italic
Underline
Overline
Blink
Reverse
CrossOut

0 comments on commit a8fb303

Please sign in to comment.