mirror of
https://github.com/rivo/tview.git
synced 2025-04-24 13:48:56 +08:00
Upgraded to new rivo/uniseg version.
This commit is contained in:
parent
73bf2902b5
commit
4b7fb7ecd6
2
go.mod
2
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
|
||||
)
|
||||
|
3
go.sum
3
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=
|
||||
|
@ -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.
|
||||
|
43
util.go
43
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
|
||||
|
Loading…
x
Reference in New Issue
Block a user