m/fzf
1
0
mirror of https://github.com/junegunn/fzf.git synced 2025-11-17 07:43:39 -05:00

Replace RuneWidth to StringWidth to handle grapheme clusters

Fix #2482
This commit is contained in:
Junegunn Choi
2021-05-14 11:43:32 +09:00
parent 4cd621e877
commit 3f75a8369f
6 changed files with 110 additions and 108 deletions

View File

@@ -10,7 +10,8 @@ import (
"time"
"unicode/utf8"
"github.com/junegunn/fzf/src/util"
"github.com/mattn/go-runewidth"
"github.com/rivo/uniseg"
"golang.org/x/term"
)
@@ -50,7 +51,7 @@ func (r *LightRenderer) stderrInternal(str string, allowNLCR bool) {
}
bytes = bytes[sz:]
}
r.queued += string(runes)
r.queued.WriteString(string(runes))
}
func (r *LightRenderer) csi(code string) {
@@ -58,9 +59,9 @@ func (r *LightRenderer) csi(code string) {
}
func (r *LightRenderer) flush() {
if len(r.queued) > 0 {
fmt.Fprint(os.Stderr, r.queued)
r.queued = ""
if r.queued.Len() > 0 {
fmt.Fprint(os.Stderr, r.queued.String())
r.queued.Reset()
}
}
@@ -82,7 +83,7 @@ type LightRenderer struct {
escDelay int
fullscreen bool
upOneLine bool
queued string
queued strings.Builder
y int
x int
maxHeightFunc func(int) int
@@ -889,20 +890,26 @@ func wrapLine(input string, prefixLength int, max int, tabstop int) []wrappedLin
lines := []wrappedLine{}
width := 0
line := ""
for _, r := range input {
w := util.RuneWidth(r, prefixLength+width, 8)
width += w
str := string(r)
if r == '\t' {
gr := uniseg.NewGraphemes(input)
for gr.Next() {
rs := gr.Runes()
str := string(rs)
var w int
if len(rs) == 1 && rs[0] == '\t' {
w = tabstop - (prefixLength+width)%tabstop
str = repeat(' ', w)
} else {
w = runewidth.StringWidth(str)
}
width += w
if prefixLength+width <= max {
line += str
} else {
lines = append(lines, wrappedLine{string(line), width - w})
line = str
prefixLength = 0
width = util.RuneWidth(r, prefixLength, 8)
width = w
}
}
lines = append(lines, wrappedLine{string(line), width})