refactor: migrate from uniseg to displaywidth (#2559)

* migrate from uniseg to displaywidth

* use firstGraphemeCluster
This commit is contained in:
valoq authored and GitHub committed 2026-05-01 14:06:58 +10:00
1 parent 54bcbca6ce
commit a88d55e077
6 files changed
+39 -34

No files matched your search

+4 -4
View File
@@ -14,8 +14,8 @@ import (
"unicode" "unicode"
"unicode/utf8" "unicode/utf8"
"github.com/clipperhouse/displaywidth"
"github.com/gdamore/tcell/v3" "github.com/gdamore/tcell/v3"
"github.com/rivo/uniseg"
) )
func applyBoolOpt(opt *bool, e *setExpr) error { func applyBoolOpt(opt *bool, e *setExpr) error {
@@ -471,7 +471,7 @@ func (e *setExpr) eval(app *app, _ []string) {
case "timefmt": case "timefmt":
gOpts.timefmt = e.val gOpts.timefmt = e.val
case "truncatechar": case "truncatechar":
if uniseg.StringWidth(e.val) != 1 { if displaywidth.String(e.val) != 1 {
app.ui.echoerr("truncatechar: value should be a single character") app.ui.echoerr("truncatechar: value should be a single character")
return return
} }
@@ -2029,9 +2029,9 @@ func (e *callExpr) eval(app *app, _ []string) {
update(app) update(app)
case "cmd-transpose": case "cmd-transpose":
var c []string var c []string
gr := uniseg.NewGraphemes(app.ui.cmdAccLeft) gr := displaywidth.StringGraphemes(app.ui.cmdAccLeft)
for gr.Next() { for gr.Next() {
c = append(c, gr.Str()) c = append(c, gr.Value())
} }
first := firstGraphemeCluster(app.ui.cmdAccRight) first := firstGraphemeCluster(app.ui.cmdAccRight)
+1 -2
View File
@@ -3,16 +3,15 @@ module github.com/gokcehan/lf
go 1.25.0 go 1.25.0
require ( require (
github.com/clipperhouse/displaywidth v0.11.0
github.com/djherbis/times v1.6.0 github.com/djherbis/times v1.6.0
github.com/fsnotify/fsnotify v1.9.0 github.com/fsnotify/fsnotify v1.9.0
github.com/gdamore/tcell/v3 v3.3.0 github.com/gdamore/tcell/v3 v3.3.0
github.com/rivo/uniseg v0.4.7
golang.org/x/sys v0.43.0 golang.org/x/sys v0.43.0
golang.org/x/term v0.42.0 golang.org/x/term v0.42.0
) )
require ( require (
github.com/clipperhouse/displaywidth v0.11.0 // indirect
github.com/clipperhouse/uax29/v2 v2.7.0 // indirect github.com/clipperhouse/uax29/v2 v2.7.0 // indirect
github.com/gdamore/encoding v1.0.1 // indirect github.com/gdamore/encoding v1.0.1 // indirect
github.com/lucasb-eyer/go-colorful v1.4.0 // indirect github.com/lucasb-eyer/go-colorful v1.4.0 // indirect
-2
View File
@@ -12,8 +12,6 @@ github.com/gdamore/tcell/v3 v3.3.0 h1:lGo3VwiV8iYMH4TdwBDwEOoDctnOLns6QfrXvZkKNe
github.com/gdamore/tcell/v3 v3.3.0/go.mod h1:8CJpEjUiAlFIrs7jUhzobiZsSLBAtXo8Dq9mCTrVnqo= github.com/gdamore/tcell/v3 v3.3.0/go.mod h1:8CJpEjUiAlFIrs7jUhzobiZsSLBAtXo8Dq9mCTrVnqo=
github.com/lucasb-eyer/go-colorful v1.4.0 h1:UtrWVfLdarDgc44HcS7pYloGHJUjHV/4FwW4TvVgFr4= github.com/lucasb-eyer/go-colorful v1.4.0 h1:UtrWVfLdarDgc44HcS7pYloGHJUjHV/4FwW4TvVgFr4=
github.com/lucasb-eyer/go-colorful v1.4.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= github.com/lucasb-eyer/go-colorful v1.4.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
+12 -13
View File
@@ -11,12 +11,11 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
"slices"
"strconv" "strconv"
"strings" "strings"
"unicode" "unicode"
"github.com/rivo/uniseg" "github.com/clipperhouse/displaywidth"
) )
var ( var (
@@ -42,18 +41,18 @@ func replaceTilde(s string) string {
// firstGraphemeCluster returns the string containing the first grapheme cluster // firstGraphemeCluster returns the string containing the first grapheme cluster
// of the input. // of the input.
func firstGraphemeCluster(s string) string { func firstGraphemeCluster(s string) string {
gr := uniseg.NewGraphemes(s) gr := displaywidth.StringGraphemes(s)
gr.Next() gr.Next()
return gr.Str() return gr.Value()
} }
// lastGraphemeCluster returns the string containing the last grapheme cluster // lastGraphemeCluster returns the string containing the last grapheme cluster
// of the input. // of the input.
func lastGraphemeCluster(s string) string { func lastGraphemeCluster(s string) string {
gr := uniseg.NewGraphemes(s) gr := displaywidth.StringGraphemes(s)
var last string var last string
for gr.Next() { for gr.Next() {
last = gr.Str() last = gr.Value()
} }
return last return last
} }
@@ -64,14 +63,14 @@ func truncateRight(s string, maxWidth int) string {
buf := make([]byte, 0, len(s)) buf := make([]byte, 0, len(s))
width := 0 width := 0
gr := uniseg.NewGraphemes(s) gr := displaywidth.StringGraphemes(s)
for gr.Next() { for gr.Next() {
width += gr.Width() width += gr.Width()
if width > maxWidth { if width > maxWidth {
break break
} }
buf = append(buf, gr.Bytes()...) buf = append(buf, gr.Value()...)
} }
return string(buf) return string(buf)
@@ -87,9 +86,9 @@ func truncateLeft(s string, maxWidth int) string {
var clusters []cluster var clusters []cluster
totalWidth := 0 totalWidth := 0
gr := uniseg.NewGraphemes(s) gr := displaywidth.StringGraphemes(s)
for gr.Next() { for gr.Next() {
clusters = append(clusters, cluster{slices.Clone(gr.Bytes()), gr.Width()}) clusters = append(clusters, cluster{[]byte(gr.Value()), gr.Width()})
totalWidth += gr.Width() totalWidth += gr.Width()
} }
@@ -432,19 +431,19 @@ func getFileExtension(file fs.FileInfo) string {
// if it exceeds the allowed width. // if it exceeds the allowed width.
func truncateFilename(file fs.FileInfo, maxWidth, truncatePct int, truncateChar string) string { func truncateFilename(file fs.FileInfo, maxWidth, truncatePct int, truncateChar string) string {
filename := sanitizeName(file.Name()) filename := sanitizeName(file.Name())
if uniseg.StringWidth(filename) <= maxWidth { if displaywidth.String(filename) <= maxWidth {
return filename return filename
} }
ext := sanitizeName(getFileExtension(file)) ext := sanitizeName(getFileExtension(file))
avail := maxWidth - uniseg.StringWidth(truncateChar) - uniseg.StringWidth(ext) avail := maxWidth - displaywidth.String(truncateChar) - displaywidth.String(ext)
if avail < 0 { if avail < 0 {
return truncateRight(truncateChar+ext, maxWidth) return truncateRight(truncateChar+ext, maxWidth)
} }
basename := strings.TrimSuffix(filename, ext) basename := strings.TrimSuffix(filename, ext)
left := truncateRight(basename, avail*truncatePct/100) left := truncateRight(basename, avail*truncatePct/100)
right := truncateLeft(basename, avail-uniseg.StringWidth(left)) right := truncateLeft(basename, avail-displaywidth.String(left))
return left + truncateChar + right + ext return left + truncateChar + right + ext
} }
+1 -1
View File
@@ -298,7 +298,7 @@ func sanitizeForDisplay(s string) string {
// sanitizeName sanitizes a filename, path, or symlink target for display. // sanitizeName sanitizes a filename, path, or symlink target for display.
// Unlike sanitizeForDisplay it also replaces tabs, because tabs in names // Unlike sanitizeForDisplay it also replaces tabs, because tabs in names
// are expanded by the renderer to tabstop width while uniseg.StringWidth // are expanded by the renderer to tabstop width while displaywidth.String
// counts them as width 1, causing column overflow. // counts them as width 1, causing column overflow.
func sanitizeName(s string) string { func sanitizeName(s string) string {
return strings.Map(func(r rune) rune { return strings.Map(func(r rune) rune {
+21 -12
View File
@@ -17,8 +17,8 @@ import (
"time" "time"
"unicode/utf8" "unicode/utf8"
"github.com/clipperhouse/displaywidth"
"github.com/gdamore/tcell/v3" "github.com/gdamore/tcell/v3"
"github.com/rivo/uniseg"
"golang.org/x/term" "golang.org/x/term"
) )
@@ -46,6 +46,15 @@ func isPrintable(gc string) bool {
return !isControlChar(r) return !isControlChar(r)
} }
// firstGrapheme returns the first grapheme cluster in s and its display width.
func firstGrapheme(s string) (string, int) {
gr := displaywidth.StringGraphemes(s)
if !gr.Next() {
return "", 0
}
return gr.Value(), gr.Width()
}
// printLength returns the display width of s in terminal cells. // printLength returns the display width of s in terminal cells.
// //
// It ignores supported terminal control sequences (see [readTermSequence]) // It ignores supported terminal control sequences (see [readTermSequence])
@@ -61,7 +70,7 @@ func printLength(s string) int {
continue continue
} }
gc, _, w, _ := uniseg.FirstGraphemeClusterInString(s[i:], -1) gc, w := firstGrapheme(s[i:])
i += len(gc) i += len(gc)
if gc == "\t" { if gc == "\t" {
@@ -98,7 +107,7 @@ func (win *win) print(screen tcell.Screen, x, y int, st tcell.Style, s string) t
continue continue
} }
gc, _, _, _ := uniseg.FirstGraphemeClusterInString(s[i:], -1) gc := firstGraphemeCluster(s[i:])
if gc == "\t" { if gc == "\t" {
w := gOpts.tabstop - (x+off+printLength(b.String()))%gOpts.tabstop w := gOpts.tabstop - (x+off+printLength(b.String()))%gOpts.tabstop
b.WriteString(strings.Repeat(" ", w)) b.WriteString(strings.Repeat(" ", w))
@@ -387,7 +396,7 @@ func (win *win) printDir(ui *ui, dir *dir, context *dirContext, dirStyle *dirSty
} }
// subtract space for icon // subtract space for icon
maxFilenameWidth := maxWidth - uniseg.StringWidth(icon) maxFilenameWidth := maxWidth - displaywidth.String(icon)
// subtract space for tag if not merged with selection marker // subtract space for tag if not merged with selection marker
if !gOpts.mergeindicators { if !gOpts.mergeindicators {
maxFilenameWidth-- maxFilenameWidth--
@@ -401,14 +410,14 @@ func (win *win) printDir(ui *ui, dir *dir, context *dirContext, dirStyle *dirSty
} }
filename := truncateFilename(f, maxFilenameWidth, gOpts.truncatepct, gOpts.truncatechar) filename := truncateFilename(f, maxFilenameWidth, gOpts.truncatepct, gOpts.truncatechar)
spacing := maxFilenameWidth - uniseg.StringWidth(filename) spacing := maxFilenameWidth - displaywidth.String(filename)
if spacing > 0 { if spacing > 0 {
filename += strings.Repeat(" ", spacing) filename += strings.Repeat(" ", spacing)
} }
if showInfo { if showInfo {
filename += info filename += info
customOff += nameOff + uniseg.StringWidth(icon) + maxFilenameWidth customOff += nameOff + displaywidth.String(icon) + maxFilenameWidth
} }
if i == dir.pos { if i == dir.pos {
@@ -451,7 +460,7 @@ func (win *win) printDir(ui *ui, dir *dir, context *dirContext, dirStyle *dirSty
win.print(ui.screen, nameOff, i, iconStyle, icon) win.print(ui.screen, nameOff, i, iconStyle, icon)
} }
win.print(ui.screen, nameOff+uniseg.StringWidth(icon), i, st, filename+" ") win.print(ui.screen, nameOff+displaywidth.String(icon), i, st, filename+" ")
// print over the empty space we reserved for the custom info // print over the empty space we reserved for the custom info
if showInfo && custom != "" { if showInfo && custom != "" {
@@ -1173,16 +1182,16 @@ func (ui *ui) draw(nav *nav) {
case ">": case ">":
maxWidth := ui.msgWin.w - 1 // leave space for cursor at the end maxWidth := ui.msgWin.w - 1 // leave space for cursor at the end
prefix := truncateRight(ui.cmdPrefix, maxWidth) prefix := truncateRight(ui.cmdPrefix, maxWidth)
left := truncateLeft(ui.cmdAccLeft, maxWidth-uniseg.StringWidth(prefix)-printLength(ui.msg)) left := truncateLeft(ui.cmdAccLeft, maxWidth-displaywidth.String(prefix)-printLength(ui.msg))
ui.msgWin.printLine(ui.screen, 0, 0, st, prefix+ui.msg) ui.msgWin.printLine(ui.screen, 0, 0, st, prefix+ui.msg)
ui.msgWin.print(ui.screen, uniseg.StringWidth(prefix)+printLength(ui.msg), 0, st, left+ui.cmdAccRight) ui.msgWin.print(ui.screen, displaywidth.String(prefix)+printLength(ui.msg), 0, st, left+ui.cmdAccRight)
ui.screen.ShowCursor(ui.msgWin.x+uniseg.StringWidth(prefix)+printLength(ui.msg)+uniseg.StringWidth(left), ui.msgWin.y) ui.screen.ShowCursor(ui.msgWin.x+displaywidth.String(prefix)+printLength(ui.msg)+displaywidth.String(left), ui.msgWin.y)
default: default:
maxWidth := ui.msgWin.w - 1 // leave space for cursor at the end maxWidth := ui.msgWin.w - 1 // leave space for cursor at the end
prefix := truncateRight(ui.cmdPrefix, maxWidth) prefix := truncateRight(ui.cmdPrefix, maxWidth)
left := truncateLeft(ui.cmdAccLeft, maxWidth-uniseg.StringWidth(prefix)) left := truncateLeft(ui.cmdAccLeft, maxWidth-displaywidth.String(prefix))
ui.msgWin.printLine(ui.screen, 0, 0, st, prefix+left+ui.cmdAccRight) ui.msgWin.printLine(ui.screen, 0, 0, st, prefix+left+ui.cmdAccRight)
ui.screen.ShowCursor(ui.msgWin.x+uniseg.StringWidth(prefix)+uniseg.StringWidth(left), ui.msgWin.y) ui.screen.ShowCursor(ui.msgWin.x+displaywidth.String(prefix)+displaywidth.String(left), ui.msgWin.y)
} }
ui.drawPreview(nav, &context) ui.drawPreview(nav, &context)