From fe2aaae02b68f16fe55ccbaf2f3bfe2a6dcd5c2b Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Fri, 2 Aug 2024 14:27:53 -0400 Subject: [PATCH] refactor(ansi): drop grapheme states We don't need to keep track of grapheme states, see https://github.com/rivo/uniseg/issues/58 --- ansi/parser.go | 2 -- ansi/truncate.go | 4 +--- ansi/width.go | 6 +----- ansi/wrap.go | 14 +++++--------- 4 files changed, 7 insertions(+), 19 deletions(-) diff --git a/ansi/parser.go b/ansi/parser.go index 0d04744d..3e75b759 100644 --- a/ansi/parser.go +++ b/ansi/parser.go @@ -238,9 +238,7 @@ func (p *Parser) performAction(dispatcher ParserDispatcher, action parser.Action // Collect intermediate bytes // we only store the last intermediate byte p.Cmd &^= 0xff << parser.IntermedShift - // p.Cmd[2] &^= 0xff p.Cmd |= int(b) << parser.IntermedShift - // p.Cmd[2] |= b } case parser.ParamAction: diff --git a/ansi/truncate.go b/ansi/truncate.go index 6006c61b..db0782c8 100644 --- a/ansi/truncate.go +++ b/ansi/truncate.go @@ -26,7 +26,6 @@ func Truncate(s string, length int, tail string) string { var buf bytes.Buffer curWidth := 0 ignoring := false - gstate := -1 pstate := parser.GroundState // initial state b := []byte(s) i := 0 @@ -41,7 +40,7 @@ func Truncate(s string, length int, tail string) string { if state == parser.Utf8State { // This action happens when we transition to the Utf8State. var width int - cluster, _, width, gstate = uniseg.FirstGraphemeCluster(b[i:], gstate) + cluster, _, width, _ = uniseg.FirstGraphemeCluster(b[i:], -1) // increment the index by the length of the cluster i += len(cluster) @@ -65,7 +64,6 @@ func Truncate(s string, length int, tail string) string { curWidth += width buf.Write(cluster) - gstate = -1 // reset grapheme state otherwise, width calculation might be off // Done collecting, now we're back in the ground state. pstate = parser.GroundState continue diff --git a/ansi/width.go b/ansi/width.go index 4d658a17..80890e42 100644 --- a/ansi/width.go +++ b/ansi/width.go @@ -68,7 +68,6 @@ func StringWidth(s string) int { } var ( - gstate = -1 pstate = parser.GroundState // initial state cluster string width int @@ -78,7 +77,7 @@ func StringWidth(s string) int { state, action := parser.Table.Transition(pstate, s[i]) if state == parser.Utf8State { var w int - cluster, _, w, gstate = uniseg.FirstGraphemeClusterInString(s[i:], gstate) + cluster, _, w, _ = uniseg.FirstGraphemeClusterInString(s[i:], -1) width += w i += len(cluster) - 1 pstate = parser.GroundState @@ -89,9 +88,6 @@ func StringWidth(s string) int { width++ } - // Reset uniseg state when we're not in a printable state. - gstate = -1 - pstate = state } diff --git a/ansi/wrap.go b/ansi/wrap.go index 7d32aeac..2cab5cec 100644 --- a/ansi/wrap.go +++ b/ansi/wrap.go @@ -27,7 +27,6 @@ func Hardwrap(s string, limit int, preserveSpace bool) string { buf bytes.Buffer curWidth int forceNewline bool - gstate = -1 pstate = parser.GroundState // initial state b = []byte(s) ) @@ -42,7 +41,7 @@ func Hardwrap(s string, limit int, preserveSpace bool) string { state, action := parser.Table.Transition(pstate, b[i]) if state == parser.Utf8State { var width int - cluster, _, width, gstate = uniseg.FirstGraphemeCluster(b[i:], gstate) + cluster, _, width, _ = uniseg.FirstGraphemeCluster(b[i:], -1) i += len(cluster) if curWidth+width > limit { @@ -58,7 +57,6 @@ func Hardwrap(s string, limit int, preserveSpace bool) string { buf.Write(cluster) curWidth += width - gstate = -1 // reset grapheme state otherwise, width calculation might be off pstate = parser.GroundState continue } @@ -120,7 +118,6 @@ func Wordwrap(s string, limit int, breakpoints string) string { space bytes.Buffer curWidth int wordLen int - gstate = -1 pstate = parser.GroundState // initial state b = []byte(s) ) @@ -154,7 +151,7 @@ func Wordwrap(s string, limit int, breakpoints string) string { state, action := parser.Table.Transition(pstate, b[i]) if state == parser.Utf8State { var width int - cluster, _, width, gstate = uniseg.FirstGraphemeCluster(b[i:], gstate) + cluster, _, width, _ = uniseg.FirstGraphemeCluster(b[i:], -1) i += len(cluster) r, _ := utf8.DecodeRune(cluster) @@ -247,9 +244,8 @@ func Wrap(s string, limit int, breakpoints string) string { buf bytes.Buffer word bytes.Buffer space bytes.Buffer - curWidth int // written width of the line - wordLen int // word buffer len without ANSI escape codes - gstate = -1 + curWidth int // written width of the line + wordLen int // word buffer len without ANSI escape codes pstate = parser.GroundState // initial state b = []byte(s) ) @@ -283,7 +279,7 @@ func Wrap(s string, limit int, breakpoints string) string { state, action := parser.Table.Transition(pstate, b[i]) if state == parser.Utf8State { var width int - cluster, _, width, gstate = uniseg.FirstGraphemeCluster(b[i:], gstate) + cluster, _, width, _ = uniseg.FirstGraphemeCluster(b[i:], -1) i += len(cluster) r, _ := utf8.DecodeRune(cluster)