Change mattn/go-runewidth dependency to rivo/uniseg for accurate results
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Related #3588 #3588 #3567
authored by Junegunn Choi · committed by Junegunn Choi · parent 6654620 · Browse files · 11 files·+59 added−51 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
11 files+59 added−51 removed
- other:+59 added−51 removed
Provenance
- Commit
16f6473- Tree
71f117d- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
6654620
11 files changed+59 added−51 removed
modified:man/man1/fzf.1+7 added-3 removed
258 unmodified lines259 .BR none260 .br261 262 If you use a terminal emulator where each box-drawing character takes263-2 columns, try setting \fBRUNEWIDTH_EASTASIAN\fR environment variable to264-\fB0\fR or \fB1\fR. If the border is still not properly rendered, set265-\fB--no-unicode\fR.263+2 columns, try setting \fB--ambidouble\fR. If the border is still not properly264+rendered, set \fB--no-unicode\fR.265 266 .TP267 .BI "--border-label" [=LABEL]268 Label to print on the horizontal border line. Should be used with one of the43 unmodified lines312 Use ASCII characters instead of Unicode drawing characters to draw borders,313 the spinner and the horizontal separator.314 315 .TP316+.B "--ambidouble"317+Set this option if your terminal displays characters with East Asian Width318+Class Ambiguous (e.g. box-drawing characters for borders) as 2 columns.319+320+.TP321 .BI "--margin=" MARGIN322 Comma-separated expression for margins around the finder.323 .br324 1132 unmodified linesmodified:src/tui/light.go+7 added-8 removed
8 unmodified lines9 "strings"10 "time"11 "unicode/utf8"12 13- "github.com/junegunn/go-runewidth"14- "github.com/rivo/uniseg"13+ "github.com/junegunn/uniseg"14 15 "golang.org/x/term"16 )17 784 unmodified lines802 color := ColBorder803 if w.preview {804 color = ColPreviewBorder805 }807- hw := runewidth.RuneWidth(w.border.top)806+ hw := runeWidth(w.border.top)807 if top {808 w.Move(0, 0)809 w.CPrint(color, repeat(w.border.top, w.width/hw))810 }29 unmodified lines840 color := ColBorder841 if w.preview {842 color = ColPreviewBorder843 }845- hw := runewidth.RuneWidth(w.border.top)846- tcw := runewidth.RuneWidth(w.border.topLeft) + runewidth.RuneWidth(w.border.topRight)847- bcw := runewidth.RuneWidth(w.border.bottomLeft) + runewidth.RuneWidth(w.border.bottomRight)844+ hw := runeWidth(w.border.top)845+ tcw := runeWidth(w.border.topLeft) + runeWidth(w.border.topRight)846+ bcw := runeWidth(w.border.bottomLeft) + runeWidth(w.border.bottomRight)847 rem := (w.width - tcw) % hw848 w.CPrint(color, string(w.border.topLeft)+repeat(w.border.top, (w.width-tcw)/hw)+repeat(' ', rem)+string(w.border.topRight))849 if !onlyHorizontal {851- vw := runewidth.RuneWidth(w.border.left)850+ vw := runeWidth(w.border.left)851 for y := 1; y < w.height-1; y++ {852 w.Move(y, 0)853 w.CPrint(color, string(w.border.left))854 w.CPrint(color, repeat(' ', w.width-vw*2))163 unmodified lines1018 str = repeat(' ', w)1019 } else if rs[0] == '\r' {1020 w++1021 } else {1023- w = runewidth.StringWidth(str)1022+ w = uniseg.StringWidth(str)1023 }1024 width += w1025 1026 if prefixLength+width <= max {92 unmodified linesmodified:src/tui/tcell.go+5 added-6 removed
8 unmodified lines9 "github.com/gdamore/tcell/v2"10 "github.com/gdamore/tcell/v2/encoding"11 "github.com/junegunn/fzf/src/util"12 13- "github.com/junegunn/go-runewidth"14- "github.com/rivo/uniseg"13+ "github.com/junegunn/uniseg"14 )15 16 func HasFullscreenRenderer() bool {17 return true718 unmodified lines736 } else {737 style = w.normal.style()738 }739 741- hw := runewidth.RuneWidth(w.borderStyle.top)740+ hw := runeWidth(w.borderStyle.top)741 switch shape {742 case BorderRounded, BorderSharp, BorderBold, BorderBlock, BorderThinBlock, BorderDouble, BorderHorizontal, BorderTop:743 max := right - 2*hw744 if shape == BorderHorizontal || shape == BorderTop {26 unmodified lines771 }772 }773 switch shape {774 case BorderRounded, BorderSharp, BorderBold, BorderBlock, BorderThinBlock, BorderDouble, BorderVertical, BorderRight:776- vw := runewidth.RuneWidth(w.borderStyle.right)775+ vw := runeWidth(w.borderStyle.right)776 for y := top; y < bot; y++ {777 _screen.SetContent(right-vw, y, w.borderStyle.right, nil, style)778 }779 }780 }781 switch shape {782 case BorderRounded, BorderSharp, BorderBold, BorderBlock, BorderThinBlock, BorderDouble:783 _screen.SetContent(left, top, w.borderStyle.topLeft, nil, style)785- _screen.SetContent(right-runewidth.RuneWidth(w.borderStyle.topRight), top, w.borderStyle.topRight, nil, style)784+ _screen.SetContent(right-runeWidth(w.borderStyle.topRight), top, w.borderStyle.topRight, nil, style)785 _screen.SetContent(left, bot-1, w.borderStyle.bottomLeft, nil, style)787- _screen.SetContent(right-runewidth.RuneWidth(w.borderStyle.bottomRight), bot-1, w.borderStyle.bottomRight, nil, style)786+ _screen.SetContent(right-runeWidth(w.borderStyle.bottomRight), bot-1, w.borderStyle.bottomRight, nil, style)787 }788 }modified:src/tui/tui.go+6 added
3 unmodified lines4 "fmt"5 "os"6 "strconv"7 "time"8+9+ "github.com/junegunn/uniseg"10 )11 12 // Types of user action13 type EventType int799 unmodified lines813 ColPreviewBorder = pair(theme.PreviewBorder, theme.PreviewBg)814 ColPreviewScrollbar = pair(theme.PreviewScrollbar, theme.PreviewBg)815 ColPreviewSpinner = pair(theme.Spinner, theme.PreviewBg)816 }817+818+func runeWidth(r rune) int {819+ return uniseg.StringWidth(string(r))820+}modified:src/util/util.go+3 added-4 removed
4 unmodified lines5 "os"6 "strings"7 "time"8 9- "github.com/junegunn/go-runewidth"9+ "github.com/junegunn/uniseg"10 "github.com/mattn/go-isatty"11- "github.com/rivo/uniseg"11 )12 13 // StringWidth returns string width where each CR/LF character takes 1 column14 func StringWidth(s string) int {16- return runewidth.StringWidth(s) + strings.Count(s, "\n") + strings.Count(s, "\r")15+ return uniseg.StringWidth(s) + strings.Count(s, "\n") + strings.Count(s, "\r")16 }17 18 // RunesWidth returns runes width19 func RunesWidth(runes []rune, prefixWidth int, tabstop int, limit int) (int, int) {143 unmodified lines163 rest := limit % length164 output := strings.Repeat(str, times)165 if rest > 0 {166 for _, r := range str {168- rest -= runewidth.RuneWidth(r)167+ rest -= uniseg.StringWidth(string(r))168 if rest < 0 {169 break170 }171 output += string(r)7 unmodified linesmodified:src/options.go+21 added-21 removed
9 unmodified lines10 11 "github.com/junegunn/fzf/src/algo"12 "github.com/junegunn/fzf/src/tui"13 "github.com/junegunn/fzf/src/util"14+ "github.com/junegunn/uniseg"15 15- "github.com/junegunn/go-runewidth"16 "github.com/mattn/go-shellwords"17 )18 19 const usage = `usage: fzf [options]316 unmodified lines336 BorderShape tui.BorderShape337 BorderLabel labelOpts338 PreviewLabel labelOpts339 Unicode bool340+ Ambidouble bool341 Tabstop int342 ListenAddr *listenAddress343 Unsafe bool344 ClearOnExit bool61 unmodified lines406 Scrollbar: nil,407 Margin: defaultMargin(),408 Padding: defaultMargin(),409 Unicode: true,410+ Ambidouble: os.Getenv("RUNEWIDTH_EASTASIAN") == "1",411 Tabstop: 8,412 BorderLabel: labelOpts{},413 PreviewLabel: labelOpts{},414 Unsafe: false,1179 unmodified lines1594 opts.History.maxSize = historyMax1595 }1596 }1597 validateJumpLabels := false1596- validatePointer := false1597- validateMarker := false1598 for i := 0; i < len(allArgs); i++ {1599 arg := allArgs[i]1600 switch arg {1601 case "-h", "--help":171 unmodified lines1773 case "--prompt":1774 opts.Prompt = nextString(allArgs, &i, "prompt string required")1775 case "--pointer":1776 opts.Pointer = firstLine(nextString(allArgs, &i, "pointer sign string required"))1777- validatePointer = true1777 case "--marker":1778 opts.Marker = firstLine(nextString(allArgs, &i, "selected sign string required"))1780- validateMarker = true1779 case "--sync":1780 opts.Sync = true1781 case "--no-sync":1782 opts.Sync = false59 unmodified lines1842 case "--no-unicode":1843 opts.Unicode = false1844 case "--unicode":1845 opts.Unicode = true1846+ case "--ambidouble":1847+ opts.Ambidouble = true1848+ case "--no-ambidouble":1849+ opts.Ambidouble = false1850 case "--margin":1851 opts.Margin = parseMargin(1852 "margin",1853 nextString(allArgs, &i, "margin required (TRBL / TB,RL / T,RL,B / T,R,B,L)"))50 unmodified lines1904 } else if match, value := optString(arg, "--prompt="); match {1905 opts.Prompt = value1906 } else if match, value := optString(arg, "--pointer="); match {1907 opts.Pointer = firstLine(value)1906- validatePointer = true1908 } else if match, value := optString(arg, "--marker="); match {1909 opts.Marker = firstLine(value)1909- validateMarker = true1910 } else if match, value := optString(arg, "-n", "--nth="); match {1911 opts.Nth = splitNth(value)1912 } else if match, value := optString(arg, "--with-nth="); match {1913 opts.WithNth = splitNth(value)98 unmodified lines2012 errorExit("non-ascii jump labels are not allowed")2013 }2014 }2015 }2016-2017- if validatePointer {2018- if err := validateSign(opts.Pointer, "pointer"); err != nil {2019- errorExit(err.Error())2020- }2021- }2022-2023- if validateMarker {2024- if err := validateSign(opts.Marker, "marker"); err != nil {2025- errorExit(err.Error())2026- }2027- }2016 }2017 2018 func validateSign(sign string, signOptName string) error {2019 if sign == "" {2020 return fmt.Errorf("%v cannot be empty", signOptName)2021 }2034- if runewidth.StringWidth(sign) > 2 {2022+ if uniseg.StringWidth(sign) > 2 {2023 return fmt.Errorf("%v display width should be up to 2", signOptName)2024 }2025 return nil2026 }2027 2028 func postProcessOptions(opts *Options) {2029+ if opts.Ambidouble {2030+ uniseg.EastAsianAmbiguousWidth = 22031+ }2032+2033+ if err := validateSign(opts.Pointer, "pointer"); err != nil {2034+ errorExit(err.Error())2035+ }2036+2037+ if err := validateSign(opts.Marker, "marker"); err != nil {2038+ errorExit(err.Error())2039+ }2040+2041 if !opts.Version && !tui.IsLightRendererSupported() && opts.Height.size > 0 {2042 errorExit("--height option is currently not supported on this platform")2043 }2044 2 unmodified lines2047 if len(runes) > 2 {2048 errorExit("--scrollbar should be given one or two characters")2049 }2050 for _, r := range runes {2051- if runewidth.RuneWidth(r) != 1 {2051+ if uniseg.StringWidth(string(r)) != 1 {2052 errorExit("scrollbar display width should be 1")2053 }2054 }2055 }124 unmodified linesmodified:src/terminal.go+2 added-3 removed
15 unmodified lines16 "sync"17 "syscall"18 "time"19 20- "github.com/junegunn/go-runewidth"21- "github.com/rivo/uniseg"20+ "github.com/junegunn/uniseg"21 22 "github.com/junegunn/fzf/src/tui"23 "github.com/junegunn/fzf/src/util"24 )771 unmodified lines796 }797 t.separator, t.separatorLen = t.ansiLabelPrinter(bar, &tui.ColSeparator, true)798 }799 if t.unicode {801- t.borderWidth = runewidth.RuneWidth('│')800+ t.borderWidth = uniseg.StringWidth("│")801 }802 if opts.Scrollbar == nil {803 if t.unicode && t.borderWidth == 1 {804 t.scrollbar = "│"3379 unmodified linesmodified:BUILD.md+2 added-2 removed
32 unmodified lines3334353637383738394041427 unmodified lines32 unmodified lines
Third-party libraries used--------------------------
- [mattn/go-runewidth](https://github.com/mattn/go-runewidth) - Licensed under [MIT](http://mattn.mit-license.org)- [rivo/uniseg](https://github.com/rivo/uniseg) - Licensed under [MIT](https://raw.githubusercontent.com/rivo/uniseg/master/LICENSE.txt)- [mattn/go-shellwords](https://github.com/mattn/go-shellwords) - Licensed under [MIT](http://mattn.mit-license.org)- [mattn/go-isatty](https://github.com/mattn/go-isatty) - Licensed under [MIT](http://mattn.mit-license.org)7 unmodified linesmodified:CHANGELOG.md+2 added
29 unmodified lines30 fi31 ' --preview 'seq {} 10000' --preview-window up32 ```33 - And we're phasing out `{fzf:prompt}` and `{fzf:action}`34+- Changed [mattn/go-runewidth](https://github.com/mattn/go-runewidth) dependency to [rivo/uniseg](https://github.com/rivo/uniseg) for accurate results35+ - Set `--ambidouble` if your terminal displays characters with East Asian Width Class Ambiguous (e.g. box-drawing characters for borders) as 2 columns36 - Bug fixes37 38 0.45.039 ------1814 unmodified linesmodified:go.mod+2 added-2 removed
1 module github.com/junegunn/fzf2 3 require (4 github.com/gdamore/tcell/v2 v2.5.45- github.com/junegunn/go-runewidth v0.0.15-0.20240119074001-7d2ea235ec545+ github.com/junegunn/uniseg v0.0.0-20240120174029-b504da4f6ed26 github.com/mattn/go-isatty v0.0.177 github.com/mattn/go-shellwords v1.0.128- github.com/rivo/uniseg v0.4.48 github.com/saracen/walker v0.1.39 golang.org/x/sys v0.16.010 golang.org/x/term v0.16.011 )1 unmodified line13 require (14 github.com/gdamore/encoding v1.0.0 // indirect15 github.com/lucasb-eyer/go-colorful v1.2.0 // indirect16 github.com/mattn/go-runewidth v0.0.14 // indirect17+ github.com/rivo/uniseg v0.4.4 // indirect18 golang.org/x/sync v0.5.0 // indirect19 golang.org/x/text v0.5.0 // indirect20 )21 1 unmodified linemodified:go.sum+2 added-2 removed
1 github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=2 github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=3 github.com/gdamore/tcell/v2 v2.5.4 h1:TGU4tSjD3sCL788vFNeJnTdzpNKIw1H5dgLnJRQVv/k=4 github.com/gdamore/tcell/v2 v2.5.4/go.mod h1:dZgRy5v4iMobMEcWNYBtREnDZAT9DYmfqIkrgEMxLyw=5-github.com/junegunn/go-runewidth v0.0.15-0.20240119074001-7d2ea235ec54 h1:eBbXiL4VPihi5C8DhESYtax8HYfgCDUkzVYigADhkzU=6-github.com/junegunn/go-runewidth v0.0.15-0.20240119074001-7d2ea235ec54/go.mod h1:Mq6NazeZhIIQPMFoInCi35AktcN/MuW2elHsDK5N52w=5+github.com/junegunn/uniseg v0.0.0-20240120174029-b504da4f6ed2 h1:oEwPBh29BPu1MaTsz2dM9bDrkOgKBoYFC0u6uY2izWo=6+github.com/junegunn/uniseg v0.0.0-20240120174029-b504da4f6ed2/go.mod h1:ywqF55XaSE3/uS2tkJqVFKiE0oIYAXRvU2N7DU4y3XQ=7 github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=8 github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=9 github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=10 github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=42 unmodified lines