Skip to content
Myr

Replace RuneWidth to StringWidth to handle grapheme clusters

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Fix #2482

authored by Junegunn Choi · committed by Junegunn Choi · parent 4cd621e · Browse files · 6 files+108 added−106 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

6 files+108 added−106 removed

  • other:+108 added−106 removed

6 files+108 added−106 removed

modified:src/tui/light.go+19 added-12 removed
8 unmodified lines
9 "strings"10 "time"11 "unicode/utf8"1213 "github.com/junegunn/fzf/src/util"13 "github.com/mattn/go-runewidth"14 "github.com/rivo/uniseg"1516 "golang.org/x/term"17)18
31 unmodified lines
50 }51 }52 bytes = bytes[sz:]53 }53 r.queued += string(runes)54 r.queued.WriteString(string(runes))55}5657func (r *LightRenderer) csi(code string) {58 r.stderr("\x1b[" + code)59}6061func (r *LightRenderer) flush() {61 if len(r.queued) > 0 {62 fmt.Fprint(os.Stderr, r.queued)63 r.queued = ""62 if r.queued.Len() > 0 {63 fmt.Fprint(os.Stderr, r.queued.String())64 r.queued.Reset()65 }66}6768// Light renderer
13 unmodified lines
82 tabstop int83 escDelay int84 fullscreen bool85 upOneLine bool85 queued string86 queued strings.Builder87 y int88 x int89 maxHeightFunc func(int) int90
798 unmodified lines
889func wrapLine(input string, prefixLength int, max int, tabstop int) []wrappedLine {890 lines := []wrappedLine{}891 width := 0892 line := ""892 for _, r := range input {893 w := util.RuneWidth(r, prefixLength+width, 8)894 width += w895 str := string(r)896 if r == '\t' {893 gr := uniseg.NewGraphemes(input)894 for gr.Next() {895 rs := gr.Runes()896 str := string(rs)897 var w int898 if len(rs) == 1 && rs[0] == '\t' {899 w = tabstop - (prefixLength+width)%tabstop900 str = repeat(' ', w)901 } else {902 w = runewidth.StringWidth(str)903 }904 width += w905906 if prefixLength+width <= max {907 line += str908 } else {909 lines = append(lines, wrappedLine{string(line), width - w})910 line = str911 prefixLength = 0905 width = util.RuneWidth(r, prefixLength, 8)912 width = w913 }914 }915 lines = append(lines, wrappedLine{string(line), width})916 return lines
71 unmodified lines
modified:src/tui/tcell.go+39 added-43 removed
3 unmodified lines
45import (6 "os"7 "time"8 "unicode/utf8"89 "runtime"1011 "github.com/gdamore/tcell"12 "github.com/gdamore/tcell/encoding"1314 "github.com/mattn/go-runewidth"15 "github.com/rivo/uniseg"16)1718func HasFullscreenRenderer() bool {19 return true
461 unmodified lines
481 w.printString(text, w.normal)482}483484func (w *TcellWindow) printString(text string, pair ColorPair) {485 t := text485 lx := 0486 a := pair.Attr()487488 style := pair.style()
5 unmodified lines
494 Blink(a&Attr(tcell.AttrBlink) != 0).495 Dim(a&Attr(tcell.AttrDim) != 0)496 }497499 for {500 if len(t) == 0 {501 break502 }503 r, size := utf8.DecodeRuneInString(t)504 t = t[size:]498 gr := uniseg.NewGraphemes(text)499 for gr.Next() {500 rs := gr.Runes()501506 if r < rune(' ') { // ignore control characters507 continue508 }509510 if r == '\n' {511 w.lastY++512 lx = 0513 } else {514515 if r == '\u000D' { // skip carriage return502 if len(rs) == 1 {503 r := rs[0]504 if r < rune(' ') { // ignore control characters505 continue506 } else if r == '\n' {507 w.lastY++508 lx = 0509 continue510 } else if r == '\u000D' { // skip carriage return511 continue512 }518519 var xPos = w.left + w.lastX + lx520 var yPos = w.top + w.lastY521 if xPos < (w.left+w.width) && yPos < (w.top+w.height) {522 _screen.SetContent(xPos, yPos, r, nil, style)523 }524 lx += runewidth.RuneWidth(r)513 }514 var xPos = w.left + w.lastX + lx515 var yPos = w.top + w.lastY516 if xPos < (w.left+w.width) && yPos < (w.top+w.height) {517 _screen.SetContent(xPos, yPos, rs[0], rs[1:], style)518 }519 lx += runewidth.StringWidth(string(rs))520 }521 w.lastX += lx522}523
18 unmodified lines
542 Reverse(a&Attr(tcell.AttrReverse) != 0).543 Underline(a&Attr(tcell.AttrUnderline) != 0).544 Italic(a&Attr(tcell.AttrItalic) != 0)545552 for _, r := range text {553 if r == '\n' {546 gr := uniseg.NewGraphemes(text)547 for gr.Next() {548 rs := gr.Runes()549 if len(rs) == 1 && rs[0] == '\n' {550 w.lastY++551 w.lastX = 0552 lx = 0557 } else {558 var xPos = w.left + w.lastX + lx553 continue554 }555560 // word wrap:561 if xPos >= (w.left + w.width) {562 w.lastY++563 w.lastX = 0564 lx = 0565 xPos = w.left566 }567 var yPos = w.top + w.lastY556 // word wrap:557 xPos := w.left + w.lastX + lx558 if xPos >= (w.left + w.width) {559 w.lastY++560 w.lastX = 0561 lx = 0562 xPos = w.left563 }564569 if yPos >= (w.top + w.height) {570 return FillSuspend571 }572573 _screen.SetContent(xPos, yPos, r, nil, style)574 lx += runewidth.RuneWidth(r)565 yPos := w.top + w.lastY566 if yPos >= (w.top + w.height) {567 return FillSuspend568 }569570 _screen.SetContent(xPos, yPos, rs[0], rs[1:], style)571 lx += runewidth.StringWidth(string(rs))572 }573 w.lastX += lx574 if w.lastX == w.width {575 w.lastY++
75 unmodified lines
modified:src/util/util.go+20 added-13 removed
5 unmodified lines
6 "time"78 "github.com/mattn/go-isatty"9 "github.com/mattn/go-runewidth"10 "github.com/rivo/uniseg"11)1212var _runeWidths = make(map[rune]int)1314// RuneWidth returns rune width15func RuneWidth(r rune, prefixWidth int, tabstop int) int {16 if r == '\t' {17 return tabstop - prefixWidth%tabstop18 } else if w, found := _runeWidths[r]; found {19 return w20 } else if r == '\n' || r == '\r' {21 return 113// RunesWidth returns runes width14func RunesWidth(runes []rune, prefixWidth int, tabstop int, limit int) (int, int) {15 width := 016 gr := uniseg.NewGraphemes(string(runes))17 idx := 018 for gr.Next() {19 rs := gr.Runes()20 var w int21 if len(rs) == 1 && rs[0] == '\t' {22 w = tabstop - (prefixWidth+width)%tabstop23 } else {24 w = runewidth.StringWidth(string(rs))25 }26 width += w27 if limit > 0 && width > limit {28 return width, idx29 }30 idx += len(rs)31 }23 w := runewidth.RuneWidth(r)24 _runeWidths[r] = w25 return w32 return width, -133}3435// Max returns the largest integer36func Max(first int, second int) int {
95 unmodified lines
modified:src/options.go+3 added-5 removed
1534 unmodified lines
1535func validateSign(sign string, signOptName string) error {1536 if sign == "" {1537 return fmt.Errorf("%v cannot be empty", signOptName)1538 }1539 widthSum := 01539 for _, r := range sign {1540 if !unicode.IsGraphic(r) {1541 return fmt.Errorf("invalid character in %v", signOptName)1542 }1544 widthSum += runewidth.RuneWidth(r)1545 if widthSum > 2 {1546 return fmt.Errorf("%v display width should be up to 2", signOptName)1547 }1543 }1544 if runewidth.StringWidth(sign) > 2 {1545 return fmt.Errorf("%v display width should be up to 2", signOptName)1546 }1547 return nil1548}15491550func postProcessOptions(opts *Options) {
72 unmodified lines
modified:src/terminal.go+26 added-32 removed
1package fzf23import (4 "bufio"5 "bytes"5 "fmt"6 "io/ioutil"7 "os"8 "os/signal"
4 unmodified lines
13 "sync"14 "syscall"15 "time"1617 "github.com/mattn/go-runewidth"18 "github.com/rivo/uniseg"1920 "github.com/junegunn/fzf/src/tui"21 "github.com/junegunn/fzf/src/util"22)23
650 unmodified lines
674 return sels675}676677func (t *Terminal) displayWidth(runes []rune) int {676 l := 0677 for _, r := range runes {678 l += util.RuneWidth(r, l, t.tabstop)679 }680 return l678 width, _ := util.RunesWidth(runes, 0, t.tabstop, 0)679 return width680}681682const (683 minWidth = 4
455 unmodified lines
1139 }1140 t.prevLines[i] = newLine1141}11421144func (t *Terminal) trimRight(runes []rune, width int) ([]rune, int) {1143func (t *Terminal) trimRight(runes []rune, width int) ([]rune, bool) {1144 // We start from the beginning to handle tab characters1146 l := 01147 for idx, r := range runes {1148 l += util.RuneWidth(r, l, t.tabstop)1149 if l > width {1150 return runes[:idx], len(runes) - idx1151 }1145 width, overflowIdx := util.RunesWidth(runes, 0, t.tabstop, width)1146 if overflowIdx >= 0 {1147 return runes[:overflowIdx], true1148 }1153 return runes, 01149 return runes, false1150}11511152func (t *Terminal) displayWidthWithLimit(runes []rune, prefixWidth int, limit int) int {1157 l := 01158 for _, r := range runes {1159 l += util.RuneWidth(r, l+prefixWidth, t.tabstop)1160 if l > limit {1161 // Early exit1162 return l1163 }1164 }1165 return l1153 width, _ := util.RunesWidth(runes, prefixWidth, t.tabstop, limit)1154 return width1155}11561157func (t *Terminal) trimLeft(runes []rune, width int) ([]rune, int32) {1158 width = util.Max(0, width)
191 unmodified lines
1350 var fillRet tui.FillReturn1351 prefixWidth := 01352 _, _, ansi = extractColor(line, ansi, func(str string, ansi *ansiState) bool {1353 trimmed := []rune(str)1365 trimmedLen := 01354 isTrimmed := false1355 if !t.previewOpts.wrap {1367 trimmed, trimmedLen = t.trimRight(trimmed, maxWidth-t.pwindow.X())1356 trimmed, isTrimmed = t.trimRight(trimmed, maxWidth-t.pwindow.X())1357 }1358 str, width := t.processTabs(trimmed, prefixWidth)1359 prefixWidth += width1360 if t.theme.Colored && ansi != nil && ansi.colored() {
1 unmodified line
1362 fillRet = t.pwindow.CFill(ansi.fg, ansi.bg, ansi.attr, str)1363 } else {1364 fillRet = t.pwindow.CFill(tui.ColPreview.Fg(), tui.ColPreview.Bg(), tui.AttrRegular, str)1365 }1377 return trimmedLen == 0 &&1366 return !isTrimmed &&1367 (fillRet == tui.FillContinue || t.previewOpts.wrap && fillRet == tui.FillNextLine)1368 })1369 t.previewer.scrollable = t.previewer.scrollable || t.pwindow.Y() == height-1 && t.pwindow.X() == t.pwindow.Width()1370 if fillRet == tui.FillNextLine {
47 unmodified lines
1418 t.pwindow.CPrint(tui.ColInfo.WithAttr(tui.Reverse), message)1419}14201421func (t *Terminal) processTabs(runes []rune, prefixWidth int) (string, int) {1433 var strbuf bytes.Buffer1422 var strbuf strings.Builder1423 l := prefixWidth1435 for _, r := range runes {1436 w := util.RuneWidth(r, l, t.tabstop)1437 l += w1438 if r == '\t' {1424 gr := uniseg.NewGraphemes(string(runes))1425 for gr.Next() {1426 rs := gr.Runes()1427 str := string(rs)1428 var w int1429 if len(rs) == 1 && rs[0] == '\t' {1430 w = t.tabstop - l%t.tabstop1431 strbuf.WriteString(strings.Repeat(" ", w))1432 } else {1433 w = runewidth.StringWidth(str)1441 strbuf.WriteRune(r)1434 strbuf.WriteString(str)1435 }1436 l += w1437 }1438 return strbuf.String(), l1439}1440
1325 unmodified lines
modified:go.mod+1 added-1 removed
4 unmodified lines
5 github.com/lucasb-eyer/go-colorful v1.2.0 // indirect6 github.com/mattn/go-isatty v0.0.127 github.com/mattn/go-runewidth v0.0.128 github.com/mattn/go-shellwords v1.0.119 github.com/rivo/uniseg v0.2.0 // indirect9 github.com/rivo/uniseg v0.2.010 github.com/saracen/walker v0.1.211 golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect12 golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab5713 golang.org/x/term v0.0.0-20210317153231-de623e64d2a6
4 unmodified lines