mirror of
https://github.com/rivo/tview.git
synced 2025-04-28 13:48:53 +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/gdamore/tcell/v2 v2.4.1-0.20210905002822-f057f0a857a1
|
||||||
github.com/lucasb-eyer/go-colorful v1.2.0
|
github.com/lucasb-eyer/go-colorful v1.2.0
|
||||||
github.com/mattn/go-runewidth v0.0.13
|
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/sys v0.0.0-20210309074719-68d13333faf2 // indirect
|
||||||
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d // 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/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 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
|
||||||
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
|
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.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-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 h1:46ULzRKLh1CwgRq2dC5SlBzEqqNCi8rreOZnNrbqcIY=
|
||||||
golang.org/x/sys v0.0.0-20210309074719-68d13333faf2/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
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, "")
|
extract := runewidth.Truncate(str, width, "")
|
||||||
if len(extract) == 0 {
|
if len(extract) == 0 {
|
||||||
// We'll extract at least one grapheme cluster.
|
// We'll extract at least one grapheme cluster.
|
||||||
gr := uniseg.NewGraphemes(str)
|
extract, _, _ = uniseg.FirstGraphemeClusterInString(str, -1)
|
||||||
gr.Next()
|
|
||||||
_, to := gr.Positions()
|
|
||||||
extract = str[:to]
|
|
||||||
}
|
}
|
||||||
if t.wordWrap && len(extract) < len(str) {
|
if t.wordWrap && len(extract) < len(str) {
|
||||||
// Add any spaces from the next line.
|
// 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
|
// text. It splits the text into its grapheme clusters, calculates each
|
||||||
// cluster's width, and adds them up to a total.
|
// cluster's width, and adds them up to a total.
|
||||||
func stringWidth(text string) (width int) {
|
func stringWidth(text string) (width int) {
|
||||||
g := uniseg.NewGraphemes(text)
|
state := -1
|
||||||
for g.Next() {
|
for len(text) > 0 {
|
||||||
var chWidth int
|
var (
|
||||||
for _, r := range g.Runes() {
|
chWidth int
|
||||||
|
cl string
|
||||||
|
)
|
||||||
|
cl, text, state = uniseg.FirstGraphemeClusterInString(text, state)
|
||||||
|
for _, r := range cl {
|
||||||
chWidth = runewidth.RuneWidth(r)
|
chWidth = runewidth.RuneWidth(r)
|
||||||
if chWidth > 0 {
|
if chWidth > 0 {
|
||||||
break // Our best guess at this point is to use the width of the first non-zero-width rune.
|
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
|
// returns true. This function returns true if the iteration was stopped before
|
||||||
// the last character.
|
// the last character.
|
||||||
func iterateString(text string, callback func(main rune, comb []rune, textPos, textWidth, screenPos, screenWidth int) bool) bool {
|
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)
|
state := -1
|
||||||
for gr.Next() {
|
for len(text) > 0 {
|
||||||
r := gr.Runes()
|
var cluster string
|
||||||
from, to := gr.Positions()
|
cluster, text, state = uniseg.FirstGraphemeClusterInString(text, state)
|
||||||
width := stringWidth(gr.Str())
|
|
||||||
var comb []rune
|
var width int
|
||||||
if len(r) > 1 {
|
runes := make([]rune, 0, len(cluster))
|
||||||
comb = r[1:]
|
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
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
screenPos += width
|
screenPos += width
|
||||||
|
textPos += len(cluster)
|
||||||
}
|
}
|
||||||
|
|
||||||
return false
|
return false
|
||||||
|
Loading…
x
Reference in New Issue
Block a user