Skip to content

Commit

Permalink
fix(term): ansi: StringWidth: properly handle utf8 transitions
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Mar 13, 2024
1 parent 722ff55 commit 4ea0020
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
8 changes: 6 additions & 2 deletions exp/term/ansi/width.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ func Strip(s string) string {
// This implements a subset of the Parser to only collect runes and
// printable characters.
for i := 0; i < len(s); i++ {
state, action := Table.Transition(pstate, s[i])
// log.Printf("pstate: %s, state: %s, action: %s, code: %c", StateNames[pstate], StateNames[state], ActionNames[action], s[i])
var state, action byte
if pstate != Utf8State {
state, action = Table.Transition(pstate, s[i])
}

// log.Printf("pstate: %s, state: %s, action: %s, code: %c, buf: %q", StateNames[pstate], StateNames[state], ActionNames[action], s[i], buf.String())
switch {
case pstate == Utf8State:
// During this state, collect rw bytes to form a valid rune in the
Expand Down
3 changes: 3 additions & 0 deletions exp/term/ansi/width_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ var cases = []struct {
{"newline", "hello\nworld", 10},
{"tab", "hello\tworld", 10},
{"controlnewline", "\x1b[31mhello\x1b[0m\nworld", 10},
{"style", "\x1B[38;2;249;38;114mfoo", 3},
{"unicode", "\x1b[35m“box”\x1b[0m", 5},
{"just_unicode", "Claire‘s Boutique", 17},
}

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

0 comments on commit 4ea0020

Please sign in to comment.