From b71dd126641028b1d5de397303b1fb3ddebe42b3 Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Fri, 22 Mar 2024 16:12:37 -0400 Subject: [PATCH] fix: transform shouldn't operate on ANSI sequences --- style.go | 8 ++++---- style_test.go | 15 ++++++++++++--- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/style.go b/style.go index 6efbc3b5..ad6b52a3 100644 --- a/style.go +++ b/style.go @@ -231,6 +231,10 @@ func (s Style) Render(strs ...string) string { transform = s.getAsTransform(transformKey) ) + if transform != nil { + str = transform(str) + } + if len(s.rules) == 0 { return s.maybeConvertTabs(str) } @@ -408,10 +412,6 @@ func (s Style) Render(strs ...string) string { } } - if transform != nil { - return transform(str) - } - return str } diff --git a/style_test.go b/style_test.go index a8c799fb..84620e02 100644 --- a/style_test.go +++ b/style_test.go @@ -410,11 +410,19 @@ func TestStringTransform(t *testing.T) { fn func(string) string expected string }{ + // No-op. + { + "hello", + func(s string) string { return s }, + "hello", + }, + // Uppercase. { "raow", strings.ToUpper, "RAOW", }, + // English and Chinese. { "The quick brown 狐 jumped over the lazy 犬", func(s string) string { @@ -433,9 +441,10 @@ func TestStringTransform(t *testing.T) { "犬 yzal eht revo depmuj 狐 nworb kciuq ehT", }, } { - res := NewStyle().Transform(tc.fn).Render(tc.input) - if res != tc.expected { - t.Errorf("Test #%d:\nExpected: %q\nGot: %q", i+1, tc.expected, res) + res := NewStyle().Bold(true).Transform(tc.fn).Render(tc.input) + expected := "\x1b[1m" + tc.expected + "\x1b[0m" + if res != expected { + t.Errorf("Test #%d:\nExpected: %q\nGot: %q", i+1, expected, res) } } }