From 7297e4f5223ef0974cb4c9e992ce4631f0fa52bc Mon Sep 17 00:00:00 2001 From: Joe Lim <50560759+joelim-work@users.noreply.github.com> Date: Wed, 27 Aug 2025 19:49:07 +1000 Subject: [PATCH] Add options for customizing the appearance of the menu (#2123) * Refactor for loop * Save menu selection * Add options to customize menu style * Add documentation --- app.go | 4 ++-- doc.md | 15 +++++++++++++++ doc.txt | 15 +++++++++++++++ eval.go | 6 ++++++ lf.1 | 11 ++++++++++- opts.go | 6 ++++++ ui.go | 54 +++++++++++++++++++++++++++++++++--------------------- 7 files changed, 87 insertions(+), 24 deletions(-) diff --git a/app.go b/app.go index e59b8d6c..3b4162f9 100644 --- a/app.go +++ b/app.go @@ -674,7 +674,7 @@ func (app *app) doComplete() (matches []compMatch) { } app.ui.cmdAccLeft = []rune(result) - app.ui.menu = listMatches(app.ui.screen, matches, -1) + app.ui.menu, app.ui.menuSelect = listMatches(app.ui.screen, matches, -1) return } @@ -697,7 +697,7 @@ func (app *app) menuComplete(direction int) { app.menuCompTmp[len(app.menuCompTmp)-1] = app.menuComps[app.menuCompInd].result app.ui.cmdAccLeft = []rune(strings.Join(app.menuCompTmp, " ")) } - app.ui.menu = listMatches(app.ui.screen, app.menuComps, app.menuCompInd) + app.ui.menu, app.ui.menuSelect = listMatches(app.ui.screen, app.menuComps, app.menuCompInd) } func (app *app) watchDir(dir *dir) { diff --git a/doc.md b/doc.md index 74a97e8f..637b6c23 100644 --- a/doc.md +++ b/doc.md @@ -177,6 +177,9 @@ The following options can be used to customize the behavior of lf: info []string (default '') infotimefmtnew string (default 'Jan _2 15:04') infotimefmtold string (default 'Jan _2 2006') + menufmt string (default "\033[0m") + menuheaderfmt string (default "\033[1m") + menuselectfmt string (default "\033[7m") mouse bool (default false) number bool (default false) numberfmt string (default "\033[33m") @@ -868,6 +871,18 @@ Format string of the file time shown in the info column when it matches this yea Format string of the file time shown in the info column when it doesn't match this year. +## menufmt (string) (default `\033[0m`) + +Format string of the menu. + +## menuheaderfmt (string) (default `\033[1m`) + +Format string of the header row in the menu. + +## menuselectfmt (string) (default `\033[7m`) + +Format string of the currently selected item in the menu. + ## mouse (bool) (default false) Send mouse events as input. diff --git a/doc.txt b/doc.txt index b01705ae..5a5e2ead 100644 --- a/doc.txt +++ b/doc.txt @@ -167,6 +167,9 @@ The following options can be used to customize the behavior of lf: info []string (default '') infotimefmtnew string (default 'Jan _2 15:04') infotimefmtold string (default 'Jan _2 2006') + menufmt string (default "\033[0m") + menuheaderfmt string (default "\033[1m") + menuselectfmt string (default "\033[7m") mouse bool (default false) number bool (default false) numberfmt string (default "\033[33m") @@ -924,6 +927,18 @@ infotimefmtold (string) (default Jan _2 2006) Format string of the file time shown in the info column when it doesn't match this year. +menufmt (string) (default \033[0m) + +Format string of the menu. + +menuheaderfmt (string) (default \033[1m) + +Format string of the header row in the menu. + +menuselectfmt (string) (default \033[7m) + +Format string of the currently selected item in the menu. + mouse (bool) (default false) Send mouse events as input. diff --git a/eval.go b/eval.go index 2302d635..f10e66d7 100644 --- a/eval.go +++ b/eval.go @@ -276,6 +276,12 @@ func (e *setExpr) eval(app *app, args []string) { gOpts.infotimefmtnew = e.val case "infotimefmtold": gOpts.infotimefmtold = e.val + case "menufmt": + gOpts.menufmt = e.val + case "menuheaderfmt": + gOpts.menuheaderfmt = e.val + case "menuselectfmt": + gOpts.menuselectfmt = e.val case "numberfmt": gOpts.numberfmt = e.val case "period": diff --git a/lf.1 b/lf.1 index 42104fe7..fc593ca9 100644 --- a/lf.1 +++ b/lf.1 @@ -1,6 +1,6 @@ .\" Automatically generated by Pandoc 3.7.0.2 .\" -.TH "LF" "1" "2025\-08\-26" "r37" "DOCUMENTATION" +.TH "LF" "1" "2025\-08\-27" "r37" "DOCUMENTATION" .SH NAME lf \- terminal file manager .SH SYNOPSIS @@ -182,6 +182,9 @@ incsearch bool (default false) info []string (default \(aq\(aq) infotimefmtnew string (default \(aqJan _2 15:04\(aq) infotimefmtold string (default \(aqJan _2 2006\(aq) +menufmt string (default \(dq\(rs033[0m\(dq) +menuheaderfmt string (default \(dq\(rs033[1m\(dq) +menuselectfmt string (default \(dq\(rs033[7m\(dq) mouse bool (default false) number bool (default false) numberfmt string (default \(dq\(rs033[33m\(dq) @@ -808,6 +811,12 @@ this year. .SS infotimefmtold (string) (default \f[CR]Jan _2 2006\f[R]) Format string of the file time shown in the info column when it doesn\(aqt match this year. +.SS menufmt (string) (default \f[CR]\(rs033[0m\f[R]) +Format string of the menu. +.SS menuheaderfmt (string) (default \f[CR]\(rs033[1m\f[R]) +Format string of the header row in the menu. +.SS menuselectfmt (string) (default \f[CR]\(rs033[7m\f[R]) +Format string of the currently selected item in the menu. .SS mouse (bool) (default false) Send mouse events as input. .SS number (bool) (default false) diff --git a/opts.go b/opts.go index 6ebe0527..8b65f932 100644 --- a/opts.go +++ b/opts.go @@ -75,6 +75,9 @@ var gOpts struct { info []string infotimefmtnew string infotimefmtold string + menufmt string + menuheaderfmt string + menuselectfmt string mouse bool number bool numberfmt string @@ -232,6 +235,9 @@ func init() { gOpts.info = nil gOpts.infotimefmtnew = "Jan _2 15:04" gOpts.infotimefmtold = "Jan _2 2006" + gOpts.menufmt = "\033[0m" + gOpts.menuheaderfmt = "\033[1m" + gOpts.menuselectfmt = "\033[7m" gOpts.mouse = false gOpts.number = false gOpts.numberfmt = "\033[33m" diff --git a/ui.go b/ui.go index 58932549..0d807b65 100644 --- a/ui.go +++ b/ui.go @@ -639,6 +639,11 @@ func getWins(screen tcell.Screen) []*win { return wins } +type menuSelect struct { + x, y int + s string +} + type ui struct { screen tcell.Screen sxScreen sixelScreen @@ -655,6 +660,7 @@ type ui struct { tevChan chan tcell.Event evChan chan tcell.Event menu string + menuSelect *menuSelect cmdPrefix string cmdAccLeft []rune cmdAccRight []rune @@ -1071,13 +1077,20 @@ func (ui *ui) drawMenu() { ui.sxScreen.forceClear = true for i, line := range lines { - st := tcell.StyleDefault + var st tcell.Style if i == 0 { - st = st.Bold(true) + st = parseEscapeSequence(gOpts.menuheaderfmt) + } else { + st = parseEscapeSequence(gOpts.menufmt) } ui.menuWin.printLine(ui.screen, 0, i, st, line) } + + if ui.menuSelect != nil { + st := parseEscapeSequence(gOpts.menuselectfmt) + ui.menuWin.print(ui.screen, ui.menuSelect.x, ui.menuSelect.y, st, ui.menuSelect.s) + } } func (ui *ui) dirOfWin(nav *nav, wind int) *dir { @@ -1666,14 +1679,11 @@ func anyKey() { os.Stdin.Read(b) } -func listMatches(screen tcell.Screen, matches []compMatch, selectedInd int) string { - mlen := len(matches) - if mlen < 2 { - return "" +func listMatches(screen tcell.Screen, matches []compMatch, selectedInd int) (string, *menuSelect) { + if len(matches) < 2 { + return "", nil } - var b strings.Builder - wtot, _ := screen.Size() wcol := 0 for _, m := range matches { @@ -1682,21 +1692,23 @@ func listMatches(screen tcell.Screen, matches []compMatch, selectedInd int) stri wcol += gOpts.tabstop - wcol%gOpts.tabstop ncol := max(wtot/wcol, 1) - b.WriteString("possible matches\n") + var b strings.Builder + b.WriteString("possible matches") - for i := 0; i < mlen; { - for j := 0; j < ncol && i < mlen; i, j = i+1, j+1 { - name := matches[i].name - w := runeSliceWidth([]rune(name)) - - if i == selectedInd { - fmt.Fprintf(&b, "\033[7m%s\033[0m%*s", name, wcol-w, "") - } else { - fmt.Fprintf(&b, "%s%*s", name, wcol-w, "") - } + for i, match := range matches { + if i%ncol == 0 { + b.WriteByte('\n') } - b.WriteByte('\n') + w := runeSliceWidth([]rune(match.name)) + fmt.Fprintf(&b, "%s%*s", match.name, wcol-w, "") } - return b.String() + b.WriteByte('\n') + + var selection *menuSelect + if selectedInd != -1 { + selection = &menuSelect{selectedInd % ncol * wcol, selectedInd/ncol + 1, matches[selectedInd].name} + } + + return b.String(), selection }