From 6dd5b52bbd9d5fa09ba366d11f95de400055966b Mon Sep 17 00:00:00 2001 From: Justen Walker Date: Thu, 23 Jul 2020 21:30:27 -0400 Subject: [PATCH] fix(templatehelper): no styles in Ascii mode If the color profile doesn't 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. --- templatehelper.go | 25 ++++++++++++++++ templatehelper_test.go | 43 +++++++++++++++++++++++++++ testdata/templatehelper.tpl | 14 +++++++++ testdata/templatehelper_ansi.txt | 14 +++++++++ testdata/templatehelper_ansi256.txt | 14 +++++++++ testdata/templatehelper_ascii.txt | 14 +++++++++ testdata/templatehelper_truecolor.txt | 14 +++++++++ 7 files changed, 138 insertions(+) create mode 100644 templatehelper_test.go create mode 100644 testdata/templatehelper.tpl create mode 100644 testdata/templatehelper_ansi.txt create mode 100644 testdata/templatehelper_ansi256.txt create mode 100644 testdata/templatehelper_ascii.txt create mode 100644 testdata/templatehelper_truecolor.txt diff --git a/templatehelper.go b/templatehelper.go index 60ec48a..698a394 100644 --- a/templatehelper.go +++ b/templatehelper.go @@ -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 := p.String(values[len(values)-1].(string)) @@ -53,3 +56,25 @@ func styleFunc(p Profile, 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) +} diff --git a/templatehelper_test.go b/templatehelper_test.go new file mode 100644 index 0000000..5e0022a --- /dev/null +++ b/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)) + } + }) + } +} diff --git a/testdata/templatehelper.tpl b/testdata/templatehelper.tpl new file mode 100644 index 0000000..f3eead9 --- /dev/null +++ b/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" }} \ No newline at end of file diff --git a/testdata/templatehelper_ansi.txt b/testdata/templatehelper_ansi.txt new file mode 100644 index 0000000..285c638 --- /dev/null +++ b/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 \ No newline at end of file diff --git a/testdata/templatehelper_ansi256.txt b/testdata/templatehelper_ansi256.txt new file mode 100644 index 0000000..1e28230 --- /dev/null +++ b/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 \ No newline at end of file diff --git a/testdata/templatehelper_ascii.txt b/testdata/templatehelper_ascii.txt new file mode 100644 index 0000000..e7b9b21 --- /dev/null +++ b/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 \ No newline at end of file diff --git a/testdata/templatehelper_truecolor.txt b/testdata/templatehelper_truecolor.txt new file mode 100644 index 0000000..81426c8 --- /dev/null +++ b/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 \ No newline at end of file