Skip to content

Commit

Permalink
fix: transform shouldn't operate on ANSI sequences
Browse files Browse the repository at this point in the history
  • Loading branch information
meowgorithm committed Mar 22, 2024
1 parent 734aea5 commit b71dd12
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
8 changes: 4 additions & 4 deletions style.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -408,10 +412,6 @@ func (s Style) Render(strs ...string) string {
}
}

if transform != nil {
return transform(str)
}

return str
}

Expand Down
15 changes: 12 additions & 3 deletions style_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
}
}
Expand Down

0 comments on commit b71dd12

Please sign in to comment.