Skip to content

Commit

Permalink
refactor(ansi): drop grapheme states
Browse files Browse the repository at this point in the history
We don't need to keep track of grapheme states, see rivo/uniseg#58
  • Loading branch information
aymanbagabas committed Aug 2, 2024
1 parent 055ac53 commit be935ba
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 19 deletions.
2 changes: 0 additions & 2 deletions ansi/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 1 addition & 3 deletions ansi/truncate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand Down
6 changes: 1 addition & 5 deletions ansi/width.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ func StringWidth(s string) int {
}

var (
gstate = -1
pstate = parser.GroundState // initial state
cluster string
width int
Expand All @@ -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
Expand All @@ -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
}

Expand Down
14 changes: 5 additions & 9 deletions ansi/wrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
Expand All @@ -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 {
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit be935ba

Please sign in to comment.