Skip to content

Commit

Permalink
fix(term): ansi: truncate wide characters
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Mar 14, 2024
1 parent a83b192 commit 7e3a0e0
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
2 changes: 1 addition & 1 deletion exp/term/ansi/truncate.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func Truncate(s string, length int, tail string) string {

// Is this gonna be too wide?
// If so write the tail and stop collecting.
if curWidth+width >= length && !ignoring {
if curWidth+width > length && !ignoring {
ignoring = true
buf.WriteString(tail)
}
Expand Down
2 changes: 2 additions & 0 deletions exp/term/ansi/truncate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ var tcases = []struct {
{"dcs", "\x1BPq#0;2;0;0;0#1;2;100;100;0#2;2;0;100;0#1~~@@vv@@~~@@~~$#2??}}GG}}??}}??-#1!14@\x1B\\foobar", "…", 4, "\x1BPq#0;2;0;0;0#1;2;100;100;0#2;2;0;100;0#1~~@@vv@@~~@@~~$#2??}}GG}}??}}??-#1!14@\x1B\\foo…"},
{"emoji_tail", "\x1b[36mHello there!\x1b[m", "😃", 8, "\x1b[36mHello 😃\x1b[m"},
{"unicode", "\x1b[35mClaire‘s Boutique\x1b[0m", "", 8, "\x1b[35mClaire‘s\x1b[0m"},
{"wide_chars", "こんにちは", "…", 7, "こんに…"},
{"style_wide_chars", "\x1b[35mこんにちは\x1b[m", "…", 7, "\x1b[35mこんに…\x1b[m"},
}

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

0 comments on commit 7e3a0e0

Please sign in to comment.