Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(templatehelper): no styles in Ascii mode #14

Merged
merged 1 commit into from Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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))
}
Comment on lines +38 to +40
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not the biggest fan of doing this kind of comparison and output in tests, but I didn't want to introduce a test-only dependency unless you're ok with it.

A couple I had in mind to make these sorts of tests more readable/maintainable:

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, that's probably a sensible suggestion. I haven't looked into goldie yet, but I've used testify before.

})
}
}
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