From 4b7fb7ecd65b489ddc9c218deb851daa350d991f Mon Sep 17 00:00:00 2001 From: Oliver <480930+rivo@users.noreply.github.com> Date: Wed, 27 Jul 2022 20:09:10 +0100 Subject: [PATCH 1/2] Upgraded to new rivo/uniseg version. --- go.mod | 2 +- go.sum | 3 ++- textview.go | 5 +---- util.go | 43 +++++++++++++++++++++++++++++-------------- 4 files changed, 33 insertions(+), 20 deletions(-) diff --git a/go.mod b/go.mod index f9ca7a9..542bc2c 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ 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 + github.com/rivo/uniseg v0.3.0 golang.org/x/sys v0.0.0-20210309074719-68d13333faf2 // indirect golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d // indirect ) diff --git a/go.sum b/go.sum index 9a2f811..cc4e0a9 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.0 h1:eyC18g7xB83Dv/xlJXLgNkRidVoR7nqFZBJvqo/K188= +github.com/rivo/uniseg v0.3.0/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-20210309074719-68d13333faf2 h1:46ULzRKLh1CwgRq2dC5SlBzEqqNCi8rreOZnNrbqcIY= golang.org/x/sys v0.0.0-20210309074719-68d13333faf2/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/textview.go b/textview.go index 99281d0..d4b7e2d 100644 --- a/textview.go +++ b/textview.go @@ -807,10 +807,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..05ad241 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 From c6cff75ed57b44392e20f7f447fd9826d8005c85 Mon Sep 17 00:00:00 2001 From: Oliver <480930+rivo@users.noreply.github.com> Date: Thu, 28 Jul 2022 10:46:20 +0100 Subject: [PATCH 2/2] Adapted to slight change in rivo/uniseg. --- go.mod | 2 +- go.sum | 4 ++-- textview.go | 2 +- util.go | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index 542bc2c..f8469de 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ 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.3.0 + 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 ) diff --git a/go.sum b/go.sum index cc4e0a9..81828d5 100644 --- a/go.sum +++ b/go.sum @@ -7,8 +7,8 @@ github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i 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/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= -github.com/rivo/uniseg v0.3.0 h1:eyC18g7xB83Dv/xlJXLgNkRidVoR7nqFZBJvqo/K188= -github.com/rivo/uniseg v0.3.0/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= +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-20210309074719-68d13333faf2 h1:46ULzRKLh1CwgRq2dC5SlBzEqqNCi8rreOZnNrbqcIY= golang.org/x/sys v0.0.0-20210309074719-68d13333faf2/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/textview.go b/textview.go index d4b7e2d..6ea4e73 100644 --- a/textview.go +++ b/textview.go @@ -807,7 +807,7 @@ func (t *TextView) reindexBuffer(width int) { extract := runewidth.Truncate(str, width, "") if len(extract) == 0 { // We'll extract at least one grapheme cluster. - extract, _, _ = uniseg.FirstGraphemeClusterInString(str, -1) + 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 05ad241..1bcbf03 100644 --- a/util.go +++ b/util.go @@ -451,7 +451,7 @@ func stringWidth(text string) (width int) { chWidth int cl string ) - cl, text, state = uniseg.FirstGraphemeClusterInString(text, state) + cl, text, _, state = uniseg.FirstGraphemeClusterInString(text, state) for _, r := range cl { chWidth = runewidth.RuneWidth(r) if chWidth > 0 { @@ -591,7 +591,7 @@ func iterateString(text string, callback func(main rune, comb []rune, textPos, t state := -1 for len(text) > 0 { var cluster string - cluster, text, state = uniseg.FirstGraphemeClusterInString(text, state) + cluster, text, _, state = uniseg.FirstGraphemeClusterInString(text, state) var width int runes := make([]rune, 0, len(cluster))