From f6f817d53552ac5edf1da81bda1bc84a834370be Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Fri, 12 Jul 2024 11:21:41 -0400 Subject: [PATCH] fix(ansi): reset uniseq grapheme state after We need to reset the grapheme state after encountering a non-printable character or when we're not using uniseg for string width (ASCII printables have a width of 1 cell). Fixes: https://github.com/charmbracelet/x/issues/122 Fixes: https://github.com/charmbracelet/x/issues/123 Fixes: https://github.com/charmbracelet/lipgloss/discussions/332 --- ansi/width.go | 4 ++++ ansi/width_test.go | 1 + 2 files changed, 5 insertions(+) diff --git a/ansi/width.go b/ansi/width.go index 857639f8..119f1931 100644 --- a/ansi/width.go +++ b/ansi/width.go @@ -90,6 +90,10 @@ func StringWidth(s string) int { continue } width++ + fallthrough + default: + // Reset uniseg state when we're not in a printable state. + gstate = -1 } pstate = state diff --git a/ansi/width_test.go b/ansi/width_test.go index 8a51373c..2c41bc7b 100644 --- a/ansi/width_test.go +++ b/ansi/width_test.go @@ -31,6 +31,7 @@ var cases = []struct { {"unicode", "\x1b[35m“box”\x1b[0m", "“box”", 5}, {"just_unicode", "Claire’s Boutique", "Claire’s Boutique", 17}, {"unclosed_ansi", "Hey, \x1b[7m\n猴", "Hey, \n猴", 7}, + {"double_asian_runes", " 你\x1b[8m好.", " 你好.", 6}, } func TestStrip(t *testing.T) {