diff --git a/go.mod b/go.mod index f0b7bc8..be5bd99 100644 --- a/go.mod +++ b/go.mod @@ -6,8 +6,8 @@ require ( github.com/gdamore/tcell/v2 v2.4.1-0.20210905002822-f057f0a857a1 github.com/lucasb-eyer/go-colorful v1.2.0 github.com/mattn/go-runewidth v0.0.13 - github.com/rivo/uniseg v0.2.0 - golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 // indirect + github.com/rivo/uniseg v0.3.1 + golang.org/x/sys v0.0.0-20210309074719-68d13333faf2 // indirect golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d // indirect golang.org/x/text v0.3.7 // indirect ) diff --git a/go.sum b/go.sum index 1cb0aae..8b10259 100644 --- a/go.sum +++ b/go.sum @@ -6,8 +6,9 @@ github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69 github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU= github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= -github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/rivo/uniseg v0.3.1 h1:SDPP7SHNl1L7KrEFCSJslJ/DM9DT02Nq2C61XrfHMmk= +github.com/rivo/uniseg v0.3.1/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 h1:id054HUawV2/6IGm2IV8KZQjqtwAOo2CYlOToYqa0d0= golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/textview.go b/textview.go index 7c85a02..294cf70 100644 --- a/textview.go +++ b/textview.go @@ -812,10 +812,7 @@ func (t *TextView) reindexBuffer(width int) { extract := runewidth.Truncate(str, width, "") if len(extract) == 0 { // We'll extract at least one grapheme cluster. - gr := uniseg.NewGraphemes(str) - gr.Next() - _, to := gr.Positions() - extract = str[:to] + extract, _, _, _ = uniseg.FirstGraphemeClusterInString(str, -1) } if t.wordWrap && len(extract) < len(str) { // Add any spaces from the next line. diff --git a/util.go b/util.go index 3c95203..1bcbf03 100644 --- a/util.go +++ b/util.go @@ -445,10 +445,14 @@ func TaggedStringWidth(text string) int { // text. It splits the text into its grapheme clusters, calculates each // cluster's width, and adds them up to a total. func stringWidth(text string) (width int) { - g := uniseg.NewGraphemes(text) - for g.Next() { - var chWidth int - for _, r := range g.Runes() { + state := -1 + for len(text) > 0 { + var ( + chWidth int + cl string + ) + cl, text, _, state = uniseg.FirstGraphemeClusterInString(text, state) + for _, r := range cl { chWidth = runewidth.RuneWidth(r) if chWidth > 0 { break // Our best guess at this point is to use the width of the first non-zero-width rune. @@ -582,23 +586,34 @@ func Escape(text string) string { // returns true. This function returns true if the iteration was stopped before // the last character. func iterateString(text string, callback func(main rune, comb []rune, textPos, textWidth, screenPos, screenWidth int) bool) bool { - var screenPos int + var screenPos, textPos int - gr := uniseg.NewGraphemes(text) - for gr.Next() { - r := gr.Runes() - from, to := gr.Positions() - width := stringWidth(gr.Str()) - var comb []rune - if len(r) > 1 { - comb = r[1:] + state := -1 + for len(text) > 0 { + var cluster string + cluster, text, _, state = uniseg.FirstGraphemeClusterInString(text, state) + + var width int + runes := make([]rune, 0, len(cluster)) + for _, r := range cluster { + runes = append(runes, r) + w := runewidth.RuneWidth(r) + if width == 0 && w > 0 { + width = w // Our best guess at this point is to use the width of the first non-zero-width rune. + } } - if callback(r[0], comb, from, to-from, screenPos, width) { + var comb []rune + if len(runes) > 1 { + comb = runes[1:] + } + + if callback(runes[0], comb, textPos, len(cluster), screenPos, width) { return true } screenPos += width + textPos += len(cluster) } return false