Skip to content

Commit

Permalink
fix(ansi): tab is a zero-width character (#238)
Browse files Browse the repository at this point in the history
Tabs should be treated as zero-width characters when wrapping text.

Fixes: #237
  • Loading branch information
aymanbagabas authored Nov 4, 2024
1 parent 12fe954 commit 4179bb1
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
4 changes: 3 additions & 1 deletion ansi/wrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ func Hardwrap(s string, limit int, preserveSpace bool) string {
}

buf.WriteByte(b[i])
curWidth++
if action == parser.PrintAction {
curWidth++
}
default:
buf.WriteByte(b[i])
}
Expand Down
3 changes: 2 additions & 1 deletion ansi/wrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var cases = []struct {
{"simple", "foobarfoo", 4, "foob\narfo\no", true},
{"lf", "f\no\nobar", 3, "f\no\noba\nr", true},
{"lf_space", "foo bar\n baz", 3, "foo\n ba\nr\n b\naz", true},
{"tab", "foo\tbar", 3, "foo\n\tba\nr", true},
{"tab", "foo\tbar", 3, "foo\n\tbar", true},
{"unicode_space", "foo\xc2\xa0bar", 3, "foo\nbar", false},
{"style_nochange", "\x1B[38;2;249;38;114mfoo\x1B[0m\x1B[38;2;248;248;242m \x1B[0m\x1B[38;2;230;219;116mbar\x1B[0m", 7, "\x1B[38;2;249;38;114mfoo\x1B[0m\x1B[38;2;248;248;242m \x1B[0m\x1B[38;2;230;219;116mbar\x1B[0m", true},
{"style", "\x1B[38;2;249;38;114m(\x1B[0m\x1B[38;2;248;248;242mjust another test\x1B[38;2;249;38;114m)\x1B[0m", 3, "\x1B[38;2;249;38;114m(\x1B[0m\x1B[38;2;248;248;242mju\nst \nano\nthe\nr t\nest\x1B[38;2;249;38;114m\n)\x1B[0m", true},
Expand Down Expand Up @@ -210,6 +210,7 @@ var wrapCases = []struct {
{"style_code_dont_affect_length", "\x1B[38;2;249;38;114mfoo\x1B[0m\x1B[38;2;248;248;242m \x1B[0m\x1B[38;2;230;219;116mbar\x1B[0m", "\x1B[38;2;249;38;114mfoo\x1B[0m\x1B[38;2;248;248;242m \x1B[0m\x1B[38;2;230;219;116mbar\x1B[0m", 7},
{"style_code_dont_get_wrapped", "\x1B[38;2;249;38;114m(\x1B[0m\x1B[38;2;248;248;242mjust another test\x1B[38;2;249;38;114m)\x1B[0m", "\x1b[38;2;249;38;114m(\x1b[0m\x1b[38;2;248;248;242mjust\nanother\ntest\x1b[38;2;249;38;114m)\x1b[0m", 7},
{"osc8_wrap", "สวัสดีสวัสดี\x1b]8;;https://example.com\x1b\\ สวัสดีสวัสดี\x1b]8;;\x1b\\", "สวัสดีสวัสดี\x1b]8;;https://example.com\x1b\\\nสวัสดีสวัสดี\x1b]8;;\x1b\\", 8},
{"tab", "foo\tbar", "foo\nbar", 3},
}

func TestWrap(t *testing.T) {
Expand Down

0 comments on commit 4179bb1

Please sign in to comment.