chore!: change to use v3 of Tcell (#2286)

* Remove SGR 8

* Remove `<backspace2>`

* Migrate to v3 of Tcell

* Refactor key handling functionality

* Bump Tcell version

* Replace `SetContent` API with `Put` API

* bump dependencies

* Use new color package

* Fix cursor position for grapheme clusters

* Some editing fixes to support grapheme clusters

* Ignore carriage return when printing

* Move grapheme cluster handling into misc.go

* Fix length of filenames containing emoji

* Use slices.Concat

* Use `string` instead of `[]rune` for `win.print`

* Add note about modifier limitations

* Update `CHANGELOG.md`
This commit is contained in:
Joe Lim authored and GitHub committed 2026-03-04 17:02:26 +11:00
1 parent a446855e76
commit 6ec60cf9d8
21 files changed
+558 -463

No files matched your search

+104 -350
View File
@@ -14,144 +14,13 @@ import (
"text/tabwriter"
"text/template"
"time"
"unicode"
"unicode/utf8"
"github.com/gdamore/tcell/v2"
"github.com/mattn/go-runewidth"
"github.com/gdamore/tcell/v3"
"github.com/rivo/uniseg"
"golang.org/x/term"
)
var gKeyVal = map[tcell.Key]string{
tcell.KeyEnter: "<enter>",
tcell.KeyBackspace: "<backspace>",
tcell.KeyTab: "<tab>",
tcell.KeyBacktab: "<backtab>",
tcell.KeyEsc: "<esc>",
tcell.KeyBackspace2: "<backspace2>",
tcell.KeyDelete: "<delete>",
tcell.KeyInsert: "<insert>",
tcell.KeyUp: "<up>",
tcell.KeyDown: "<down>",
tcell.KeyLeft: "<left>",
tcell.KeyRight: "<right>",
tcell.KeyHome: "<home>",
tcell.KeyEnd: "<end>",
tcell.KeyUpLeft: "<upleft>",
tcell.KeyUpRight: "<upright>",
tcell.KeyDownLeft: "<downleft>",
tcell.KeyDownRight: "<downright>",
tcell.KeyCenter: "<center>",
tcell.KeyPgDn: "<pgdn>",
tcell.KeyPgUp: "<pgup>",
tcell.KeyClear: "<clear>",
tcell.KeyExit: "<exit>",
tcell.KeyCancel: "<cancel>",
tcell.KeyPause: "<pause>",
tcell.KeyPrint: "<print>",
tcell.KeyF1: "<f-1>",
tcell.KeyF2: "<f-2>",
tcell.KeyF3: "<f-3>",
tcell.KeyF4: "<f-4>",
tcell.KeyF5: "<f-5>",
tcell.KeyF6: "<f-6>",
tcell.KeyF7: "<f-7>",
tcell.KeyF8: "<f-8>",
tcell.KeyF9: "<f-9>",
tcell.KeyF10: "<f-10>",
tcell.KeyF11: "<f-11>",
tcell.KeyF12: "<f-12>",
tcell.KeyF13: "<f-13>",
tcell.KeyF14: "<f-14>",
tcell.KeyF15: "<f-15>",
tcell.KeyF16: "<f-16>",
tcell.KeyF17: "<f-17>",
tcell.KeyF18: "<f-18>",
tcell.KeyF19: "<f-19>",
tcell.KeyF20: "<f-20>",
tcell.KeyF21: "<f-21>",
tcell.KeyF22: "<f-22>",
tcell.KeyF23: "<f-23>",
tcell.KeyF24: "<f-24>",
tcell.KeyF25: "<f-25>",
tcell.KeyF26: "<f-26>",
tcell.KeyF27: "<f-27>",
tcell.KeyF28: "<f-28>",
tcell.KeyF29: "<f-29>",
tcell.KeyF30: "<f-30>",
tcell.KeyF31: "<f-31>",
tcell.KeyF32: "<f-32>",
tcell.KeyF33: "<f-33>",
tcell.KeyF34: "<f-34>",
tcell.KeyF35: "<f-35>",
tcell.KeyF36: "<f-36>",
tcell.KeyF37: "<f-37>",
tcell.KeyF38: "<f-38>",
tcell.KeyF39: "<f-39>",
tcell.KeyF40: "<f-40>",
tcell.KeyF41: "<f-41>",
tcell.KeyF42: "<f-42>",
tcell.KeyF43: "<f-43>",
tcell.KeyF44: "<f-44>",
tcell.KeyF45: "<f-45>",
tcell.KeyF46: "<f-46>",
tcell.KeyF47: "<f-47>",
tcell.KeyF48: "<f-48>",
tcell.KeyF49: "<f-49>",
tcell.KeyF50: "<f-50>",
tcell.KeyF51: "<f-51>",
tcell.KeyF52: "<f-52>",
tcell.KeyF53: "<f-53>",
tcell.KeyF54: "<f-54>",
tcell.KeyF55: "<f-55>",
tcell.KeyF56: "<f-56>",
tcell.KeyF57: "<f-57>",
tcell.KeyF58: "<f-58>",
tcell.KeyF59: "<f-59>",
tcell.KeyF60: "<f-60>",
tcell.KeyF61: "<f-61>",
tcell.KeyF62: "<f-62>",
tcell.KeyF63: "<f-63>",
tcell.KeyF64: "<f-64>",
tcell.KeyCtrlA: "<c-a>",
tcell.KeyCtrlB: "<c-b>",
tcell.KeyCtrlC: "<c-c>",
tcell.KeyCtrlD: "<c-d>",
tcell.KeyCtrlE: "<c-e>",
tcell.KeyCtrlF: "<c-f>",
tcell.KeyCtrlG: "<c-g>",
tcell.KeyCtrlJ: "<c-j>",
tcell.KeyCtrlK: "<c-k>",
tcell.KeyCtrlL: "<c-l>",
tcell.KeyCtrlN: "<c-n>",
tcell.KeyCtrlO: "<c-o>",
tcell.KeyCtrlP: "<c-p>",
tcell.KeyCtrlQ: "<c-q>",
tcell.KeyCtrlR: "<c-r>",
tcell.KeyCtrlS: "<c-s>",
tcell.KeyCtrlT: "<c-t>",
tcell.KeyCtrlU: "<c-u>",
tcell.KeyCtrlV: "<c-v>",
tcell.KeyCtrlW: "<c-w>",
tcell.KeyCtrlX: "<c-x>",
tcell.KeyCtrlY: "<c-y>",
tcell.KeyCtrlZ: "<c-z>",
tcell.KeyCtrlSpace: "<c-space>",
tcell.KeyCtrlUnderscore: "<c-_>",
tcell.KeyCtrlRightSq: "<c-]>",
tcell.KeyCtrlBackslash: "<c-\\>",
tcell.KeyCtrlCarat: "<c-^>",
}
var gValKey map[string]tcell.Key
func init() {
gValKey = make(map[string]tcell.Key, len(gKeyVal))
for k, v := range gKeyVal {
gValKey[v] = k
}
}
type win struct {
w, h, x, y int
}
@@ -169,69 +38,65 @@ func (win *win) renew(w, h, x, y int) {
// It ignores supported terminal control sequences (see [readTermSequence])
// and accounts for tab expansions using the `tabstop` option.
func printLength(s string) int {
ind := 0
off := 0
length := 0
i := 0
slen := len(s)
for i := 0; i < slen; i++ {
for i < slen {
seq := readTermSequence(s[i:])
if seq != "" {
i += len(seq) - 1
i += len(seq)
continue
}
r, w := utf8.DecodeRuneInString(s[i:])
i += w - 1
gc, _, w, _ := uniseg.FirstGraphemeClusterInString(s[i:], -1)
i += len(gc)
if r == '\t' {
ind += gOpts.tabstop - (ind-off)%gOpts.tabstop
if gc == "\t" {
length += gOpts.tabstop - length%gOpts.tabstop
} else {
ind += runewidth.RuneWidth(r)
length += w
}
}
return ind
return length
}
func (win *win) print(screen tcell.Screen, x, y int, st tcell.Style, s string) tcell.Style {
off := x
var comb []rune
slen := len(s)
for i := 0; i < slen; i++ {
seq := readTermSequence(s[i:])
if seq != "" {
st = applyTermSequence(seq, st)
i += len(seq) - 1
continue
}
r, w := utf8.DecodeRuneInString(s[i:])
for {
rc, wc := utf8.DecodeRuneInString(s[i+w:])
if !unicode.Is(unicode.Mn, rc) {
break
}
comb = append(comb, rc)
i += wc
}
if x < win.w {
screen.SetContent(win.x+x, win.y+y, r, comb, st)
comb = nil
}
i += w - 1
if r == '\t' {
ind := gOpts.tabstop - (x-off)%gOpts.tabstop
for i := 0; i < ind && x+i < win.w; i++ {
screen.SetContent(win.x+x+i, win.y+y, ' ', nil, st)
}
x += ind
} else {
x += runewidth.RuneWidth(r)
var b strings.Builder
off := 0
put := func() {
if b.Len() > 0 {
s := b.String()
screen.PutStrStyled(win.x+x+off, win.y+y, s, st)
off += printLength(s)
b.Reset()
}
}
i := 0
slen := len(s)
for i < slen {
seq := readTermSequence(s[i:])
if seq != "" {
put()
st = applyTermSequence(seq, st)
i += len(seq)
continue
}
gc, _, _, _ := uniseg.FirstGraphemeClusterInString(s[i:], -1)
if gc == "\t" {
w := gOpts.tabstop - (x+off+printLength(b.String()))%gOpts.tabstop
b.WriteString(strings.Repeat(" ", w))
} else if gc != "\r" {
b.WriteString(gc)
}
i += len(gc)
}
put()
return st
}
@@ -515,7 +380,7 @@ func (win *win) printDir(ui *ui, dir *dir, context *dirContext, dirStyle *dirSty
}
filename := []rune(truncateFilename(f, maxFilenameWidth, gOpts.truncatepct, []rune(gOpts.truncatechar)[0]))
for j := runeSliceWidth(filename); j < maxFilenameWidth; j++ {
for j := uniseg.StringWidth(string(filename)); j < maxFilenameWidth; j++ {
filename = append(filename, ' ')
}
@@ -636,15 +501,12 @@ type menuSelect struct {
type ui struct {
screen tcell.Screen // primary screen used for drawing and event polling
sxScreen sixelScreen // sixel preview state
polling bool // whether event polling is running
wins []*win // pane windows from `ratios` (last is `preview` when enabled)
promptWin *win // prompt line window
msgWin *win // status line window
menuWin *win // menu window
msg string // message/output shown in msgWin
exprChan chan expr // expr queue
keyChan chan string // key queue (send via `push`)
tevChan chan tcell.Event // terminal events from screen.PollEvent
evChan chan tcell.Event // merged event queue (keyChan + tevChan)
menu string // rendered (multiline) menu text (i.e. completions, binds, marks)
menuSelect *menuSelect // selected menu entry (completion menu only)
@@ -652,8 +514,8 @@ type ui struct {
cmdAccLeft []rune // command buffer left of cursor
cmdAccRight []rune // command buffer right of cursor
cmdYankBuf []rune // yank buffer for command line editing
keyAcc []rune // keys typed so far for mapping lookup
keyCount []rune // count prefix for next command
keyAcc string // keys typed so far for mapping lookup
keyCount string // count prefix for next command
styles styleMap // parsed styles
icons iconMap // parsed icons
ruler *template.Template // compiled `rulerfile`
@@ -667,14 +529,11 @@ func newUI(screen tcell.Screen) *ui {
ui := &ui{
screen: screen,
polling: true,
wins: getWins(screen),
promptWin: newWin(wtot, 1, 0, 0),
msgWin: newWin(wtot, 1, 0, htot-1),
menuWin: newWin(wtot, 1, 0, htot-2),
exprChan: make(chan expr, 1000),
keyChan: make(chan string, 1000),
tevChan: make(chan tcell.Event, 1000),
evChan: make(chan tcell.Event, 1000),
styles: parseStyles(),
icons: parseIcons(),
@@ -683,8 +542,6 @@ func newUI(screen tcell.Screen) *ui {
}
ui.ruler, ui.rulerErr = parseRuler(gOpts.rulerfile)
go ui.pollEvents()
return ui
}
@@ -698,18 +555,6 @@ func (ui *ui) winAt(x, y int) (int, *win) {
return -1, nil
}
func (ui *ui) pollEvents() {
var ev tcell.Event
for {
ev = ui.screen.PollEvent()
if ev == nil {
ui.polling = false
return
}
ui.tevChan <- ev
}
}
func (ui *ui) renew() {
ui.wins = getWins(ui.screen)
@@ -909,7 +754,7 @@ func (ui *ui) drawRuler(nav *nav) {
tot := len(dir.files)
ind := min(dir.ind+1, tot)
hid := len(dir.allFiles) - tot
acc := string(ui.keyCount) + string(ui.keyAcc)
acc := ui.keyCount + ui.keyAcc
var percentage string
beg := max(dir.ind-dir.pos, 0)
@@ -1113,7 +958,7 @@ func (ui *ui) drawRulerFile(nav *nav) {
data := rulerData{
SPACER: "\x1f",
Message: ui.msg,
Keys: string(ui.keyCount) + string(ui.keyAcc),
Keys: ui.keyCount + ui.keyAcc,
Progress: progress,
Copy: copiedPaths,
Cut: cutPaths,
@@ -1172,35 +1017,35 @@ func (ui *ui) drawBox() {
w, h := ui.screen.Size()
for i := 1; i < w-1; i++ {
ui.screen.SetContent(i, 1, tcell.RuneHLine, nil, st)
ui.screen.SetContent(i, h-2, tcell.RuneHLine, nil, st)
ui.screen.PutStrStyled(i, 1, string(tcell.RuneHLine), st)
ui.screen.PutStrStyled(i, h-2, string(tcell.RuneHLine), st)
}
for i := 2; i < h-2; i++ {
ui.screen.SetContent(0, i, tcell.RuneVLine, nil, st)
ui.screen.SetContent(w-1, i, tcell.RuneVLine, nil, st)
ui.screen.PutStrStyled(0, i, string(tcell.RuneVLine), st)
ui.screen.PutStrStyled(w-1, i, string(tcell.RuneVLine), st)
}
if gOpts.roundbox {
ui.screen.SetContent(0, 1, '╭', nil, st)
ui.screen.SetContent(w-1, 1, '╮', nil, st)
ui.screen.SetContent(0, h-2, '╰', nil, st)
ui.screen.SetContent(w-1, h-2, '╯', nil, st)
ui.screen.PutStrStyled(0, 1, "╭", st)
ui.screen.PutStrStyled(w-1, 1, "╮", st)
ui.screen.PutStrStyled(0, h-2, "╰", st)
ui.screen.PutStrStyled(w-1, h-2, "╯", st)
} else {
ui.screen.SetContent(0, 1, tcell.RuneULCorner, nil, st)
ui.screen.SetContent(w-1, 1, tcell.RuneURCorner, nil, st)
ui.screen.SetContent(0, h-2, tcell.RuneLLCorner, nil, st)
ui.screen.SetContent(w-1, h-2, tcell.RuneLRCorner, nil, st)
ui.screen.PutStrStyled(0, 1, string(tcell.RuneULCorner), st)
ui.screen.PutStrStyled(w-1, 1, string(tcell.RuneURCorner), st)
ui.screen.PutStrStyled(0, h-2, string(tcell.RuneLLCorner), st)
ui.screen.PutStrStyled(w-1, h-2, string(tcell.RuneLRCorner), st)
}
wacc := 0
for wind := range len(ui.wins) - 1 {
wacc += ui.wins[wind].w + 1
ui.screen.SetContent(wacc, 1, tcell.RuneTTee, nil, st)
ui.screen.PutStrStyled(wacc, 1, string(tcell.RuneTTee), st)
for i := 2; i < h-2; i++ {
ui.screen.SetContent(wacc, i, tcell.RuneVLine, nil, st)
ui.screen.PutStrStyled(wacc, i, string(tcell.RuneVLine), st)
}
ui.screen.SetContent(wacc, h-2, tcell.RuneBTee, nil, st)
ui.screen.PutStrStyled(wacc, h-2, string(tcell.RuneBTee), st)
}
}
@@ -1292,7 +1137,7 @@ func (ui *ui) draw(nav *nav) {
prefix := runeSliceWidthRange([]rune(ui.cmdPrefix), 0, maxWidth)
left := runeSliceWidthLastRange(ui.cmdAccLeft, maxWidth-runeSliceWidth(prefix))
ui.msgWin.printLine(ui.screen, 0, 0, st, string(prefix)+string(left)+string(ui.cmdAccRight))
ui.screen.ShowCursor(ui.msgWin.x+runeSliceWidth(prefix)+runeSliceWidth(left), ui.msgWin.y)
ui.screen.ShowCursor(ui.msgWin.x+runeSliceWidth(prefix)+uniseg.StringWidth(string(left)), ui.msgWin.y)
}
ui.drawPreview(nav, &context)
@@ -1482,71 +1327,6 @@ func listFilesInCurrDir(nav *nav) string {
return b.String()
}
func (ui *ui) pollEvent() tcell.Event {
select {
case val := <-ui.keyChan:
var ch rune
var mod tcell.ModMask
k := tcell.KeyRune
if key, ok := gValKey[val]; ok {
return tcell.NewEventKey(key, ch, mod)
}
switch {
case utf8.RuneCountInString(val) == 1:
ch, _ = utf8.DecodeRuneInString(val)
case val == "<lt>":
ch = '<'
case val == "<gt>":
ch = '>'
case val == "<space>":
ch = ' '
case reModKey.MatchString(val):
matches := reModKey.FindStringSubmatch(val)
switch matches[1] {
case "c":
mod = tcell.ModCtrl
case "s":
mod = tcell.ModShift
case "a":
mod = tcell.ModAlt
}
val = matches[2]
if utf8.RuneCountInString(val) == 1 {
ch, _ = utf8.DecodeRuneInString(val)
break
} else if key, ok := gValKey["<"+val+">"]; ok {
k = key
break
}
fallthrough
default:
k = tcell.KeyESC
ui.echoerrf("unknown key: %s", val)
}
return tcell.NewEventKey(k, ch, mod)
case ev := <-ui.tevChan:
return ev
}
}
func addSpecialKeyModifier(val string, mod tcell.ModMask) string {
switch {
case !strings.HasPrefix(val, "<"):
return val
case mod == tcell.ModCtrl && !strings.HasPrefix(val, "<c-"):
return "<c-" + val[1:]
case mod == tcell.ModShift:
return "<s-" + val[1:]
case mod == tcell.ModAlt:
return "<a-" + val[1:]
default:
return val
}
}
// readNormalEvent is used to read a normal event on the client side. For keys,
// digits are interpreted as command counts but this is only done for digits
// preceding any non-digit characters (e.g. "42y2k" as 42 times "y2k").
@@ -1565,57 +1345,47 @@ func (ui *ui) readNormalEvent(ev tcell.Event, nav *nav) expr {
return nil
}
// KeyRune is a regular character
if tev.Key() == tcell.KeyRune {
switch {
case tev.Rune() == '<':
ui.keyAcc = append(ui.keyAcc, []rune("<lt>")...)
case tev.Rune() == '>':
ui.keyAcc = append(ui.keyAcc, []rune("<gt>")...)
case tev.Rune() == ' ':
ui.keyAcc = append(ui.keyAcc, []rune("<space>")...)
case tev.Modifiers() == tcell.ModAlt:
ui.keyAcc = append(ui.keyAcc, '<', 'a', '-', tev.Rune(), '>')
case unicode.IsDigit(tev.Rune()) && len(ui.keyAcc) == 0:
ui.keyCount = append(ui.keyCount, tev.Rune())
default:
ui.keyAcc = append(ui.keyAcc, tev.Rune())
isDigitKey := func(*tcell.EventKey) bool {
if tev.Key() != tcell.KeyRune || tev.Modifiers() != tcell.ModNone {
return false
}
} else {
val := gKeyVal[tev.Key()]
val = addSpecialKeyModifier(val, tev.Modifiers())
if val == "<esc>" && len(ui.keyAcc) != 0 {
ui.keyAcc = nil
ui.keyCount = nil
ui.menu = ""
return draw
}
ui.keyAcc = append(ui.keyAcc, []rune(val)...)
s := tev.Str()
return len(s) == 1 && s[0] >= '0' && s[0] <= '9'
}
if len(ui.keyAcc) == 0 {
switch {
case tev.Key() == tcell.KeyEsc && ui.keyAcc != "":
ui.keyAcc = ""
ui.keyCount = ""
ui.menu = ""
return draw
case isDigitKey(tev) && ui.keyAcc == "":
ui.keyCount += tev.Str()
return draw
default:
ui.keyAcc += readKey(tev)
}
binds, ok := findBinds(keys, string(ui.keyAcc))
binds, ok := findBinds(keys, ui.keyAcc)
switch len(binds) {
case 0:
ui.echoerrf("unknown mapping: %s", string(ui.keyAcc))
ui.keyAcc = nil
ui.keyCount = nil
ui.echoerrf("unknown mapping: %s", ui.keyAcc)
ui.keyAcc = ""
ui.keyCount = ""
ui.menu = ""
return draw
default:
if ok {
if len(ui.keyCount) > 0 {
c, err := strconv.Atoi(string(ui.keyCount))
if ui.keyCount != "" {
c, err := strconv.Atoi(ui.keyCount)
if err != nil {
log.Printf("converting command count: %s", err)
}
count = c
}
expr := keys[string(ui.keyAcc)]
expr := keys[ui.keyAcc]
if count != 0 {
switch e := expr.(type) {
@@ -1626,14 +1396,14 @@ func (ui *ui) readNormalEvent(ev tcell.Event, nav *nav) expr {
}
}
ui.keyAcc = nil
ui.keyCount = nil
ui.keyAcc = ""
ui.keyCount = ""
ui.menu = ""
return expr
}
if gOpts.showbinds {
// mode and already typed keys are obvious here; no need to clutter the menu
ui.menu = listMatchingBinds(binds, string(ui.keyAcc))
ui.menu = listMatchingBinds(binds, ui.keyAcc)
}
return draw
}
@@ -1680,8 +1450,8 @@ func (ui *ui) readNormalEvent(ev tcell.Event, nav *nav) expr {
}
if button != "<m-1>" && button != "<m-2>" {
ui.echoerrf("unknown mapping: %s", button)
ui.keyAcc = nil
ui.keyCount = nil
ui.keyAcc = ""
ui.keyCount = ""
ui.menu = ""
return draw
}
@@ -1756,21 +1526,12 @@ func (ui *ui) readNormalEvent(ev tcell.Event, nav *nav) expr {
func readCmdEvent(ev tcell.Event) expr {
if tev, ok := ev.(*tcell.EventKey); ok {
if tev.Key() == tcell.KeyRune {
if tev.Modifiers() == tcell.ModAlt {
val := string([]rune{'<', 'a', '-', tev.Rune(), '>'})
if expr, ok := gOpts.cmdkeys[val]; ok {
return expr
}
} else {
return &callExpr{"cmd-insert", []string{string(tev.Rune())}, 1}
}
} else {
val := gKeyVal[tev.Key()]
val = addSpecialKeyModifier(val, tev.Modifiers())
if expr, ok := gOpts.cmdkeys[val]; ok {
return expr
}
if tev.Key() == tcell.KeyRune && tev.Modifiers()&tcell.ModAlt == 0 {
return &callExpr{"cmd-insert", []string{tev.Str()}, 1}
}
if expr, ok := gOpts.cmdkeys[readKey(tev)]; ok {
return expr
}
}
return nil
@@ -1788,12 +1549,10 @@ func (ui *ui) readEvent(ev tcell.Event, nav *nav) expr {
return ui.readNormalEvent(ev, nav)
}
func (ui *ui) readExpr() {
go func() {
for {
ui.evChan <- ui.pollEvent()
}
}()
func (ui *ui) readEvents() {
for ev := range ui.screen.EventQ() {
ui.evChan <- ev
}
}
func (ui *ui) suspend() error {
@@ -1802,12 +1561,7 @@ func (ui *ui) suspend() error {
}
func (ui *ui) resume() error {
err := ui.screen.Resume()
if !ui.polling {
go ui.pollEvents()
ui.polling = true
}
return err
return ui.screen.Resume()
}
func (ui *ui) exportSizes() {