From 89ff7b020338bbc856cfba10b1d9c60994555ddb Mon Sep 17 00:00:00 2001 From: Joe Lim <50560759+joelim-work@users.noreply.github.com> Date: Fri, 19 Sep 2025 19:44:43 +1000 Subject: [PATCH] Implement smarter history navigation (#2161) * Remove unnecessary space in history file * Refactor history navigation * Implement smarter history navigation * Skip duplicate commands in history navigation * Return to Normal mode if it started from there * Reset history state when changing prompt * Update documentation --- app.go | 62 ++++++++++++++-------------------- doc.md | 3 +- doc.txt | 4 ++- eval.go | 103 ++++++++++++++++++++++++++++++++------------------------ lf.1 | 6 ++-- ui.go | 4 +-- 6 files changed, 96 insertions(+), 86 deletions(-) diff --git a/app.go b/app.go index f21343c4..23daa2bd 100644 --- a/app.go +++ b/app.go @@ -15,29 +15,25 @@ import ( "time" ) -type cmdItem struct { - prefix string - value string -} - type app struct { - ui *ui - nav *nav - ticker *time.Ticker - quitChan chan struct{} - cmd *exec.Cmd - cmdIn io.WriteCloser - cmdOutBuf []byte - cmdHistory []cmdItem - cmdHistoryBeg int - cmdHistoryInd int - menuCompActive bool - menuCompTmp []string - menuComps []compMatch - menuCompInd int - selectionOut []string - watch *watch - quitting bool + ui *ui + nav *nav + ticker *time.Ticker + quitChan chan struct{} + cmd *exec.Cmd + cmdIn io.WriteCloser + cmdOutBuf []byte + cmdHistory []string + cmdHistoryBeg int + cmdHistoryInd int + cmdHistoryInput *string + menuCompActive bool + menuCompTmp []string + menuComps []compMatch + menuCompInd int + selectionOut []string + watch *watch + quitting bool } func newApp(ui *ui, nav *nav) *app { @@ -196,14 +192,11 @@ func (app *app) readHistory() error { scanner := bufio.NewScanner(f) for scanner.Scan() { - toks := strings.SplitN(scanner.Text(), " ", 2) - if toks[0] != ":" && toks[0] != "$" && toks[0] != "%" && toks[0] != "!" && toks[0] != "&" { + cmd := scanner.Text() + if len(cmd) < 1 || !slices.Contains([]string{":", "$", "!", "%", "&"}, cmd[:1]) { continue } - if len(toks) < 2 { - continue - } - app.cmdHistory = append(app.cmdHistory, cmdItem{toks[0], toks[1]}) + app.cmdHistory = append(app.cmdHistory, cmd) } app.cmdHistoryBeg = len(app.cmdHistory) @@ -220,8 +213,7 @@ func (app *app) writeHistory() error { return nil } - local := make([]cmdItem, len(app.cmdHistory)-app.cmdHistoryBeg) - copy(local, app.cmdHistory[app.cmdHistoryBeg:]) + local := slices.Clone(app.cmdHistory[app.cmdHistoryBeg:]) app.cmdHistory = nil if err := app.readHistory(); err != nil { @@ -229,6 +221,9 @@ func (app *app) writeHistory() error { } app.cmdHistory = append(app.cmdHistory, local...) + if len(app.cmdHistory) > 1000 { + app.cmdHistory = app.cmdHistory[len(app.cmdHistory)-1000:] + } if err := os.MkdirAll(filepath.Dir(gHistoryPath), os.ModePerm); err != nil { return fmt.Errorf("creating data directory: %s", err) @@ -240,13 +235,8 @@ func (app *app) writeHistory() error { } defer f.Close() - if len(app.cmdHistory) > 1000 { - app.cmdHistory = app.cmdHistory[len(app.cmdHistory)-1000:] - } - for _, cmd := range app.cmdHistory { - _, err = fmt.Fprintf(f, "%s %s\n", cmd.prefix, cmd.value) - if err != nil { + if _, err = fmt.Fprintln(f, cmd); err != nil { return fmt.Errorf("writing history file: %s", err) } } diff --git a/doc.md b/doc.md index 6fb91ce0..4af2ca24 100644 --- a/doc.md +++ b/doc.md @@ -669,7 +669,8 @@ Interrupt the current shell-pipe command and return to the Normal mode. ## cmd-history-next (default `` and ``), cmd-history-prev (default `` and ``) -Go to the next/previous item in the history. +Go to the next/previous entry in the command history. +If part of the command is already typed, then only matching entries will be considered, and consecutive duplicate entries are skipped. ## cmd-left (default `` and ``), cmd-right (default `` and ``) diff --git a/doc.txt b/doc.txt index c6f62c18..57a46977 100644 --- a/doc.txt +++ b/doc.txt @@ -696,7 +696,9 @@ Interrupt the current shell-pipe command and return to the Normal mode. cmd-history-next (default and ), cmd-history-prev (default and ) -Go to the next/previous item in the history. +Go to the next/previous entry in the command history. If part of the +command is already typed, then only matching entries will be considered, +and consecutive duplicate entries are skipped. cmd-left (default and ), cmd-right (default and ) diff --git a/eval.go b/eval.go index 1eda0070..00a94ff0 100644 --- a/eval.go +++ b/eval.go @@ -672,9 +672,8 @@ func splitKeys(s string) (keys []string) { } func update(app *app) { - app.ui.menu = "" - app.ui.menuSelect = nil - app.menuCompActive = false + exitCompMenu(app) + app.cmdHistoryInput = nil switch { case gOpts.incsearch && app.ui.cmdPrefix == "/": @@ -755,12 +754,10 @@ func resetIncCmd(app *app) { func normal(app *app) { resetIncCmd(app) + exitCompMenu(app) app.cmdHistoryInd = 0 - - app.ui.menu = "" - app.ui.menuSelect = nil - app.menuCompActive = false + app.cmdHistoryInput = nil app.ui.cmdAccLeft = nil app.ui.cmdAccRight = nil @@ -956,17 +953,24 @@ func insert(app *app, arg string) { switch arg { case "!", "$", "%", "&": app.ui.cmdPrefix = arg + app.cmdHistoryInd = 0 + app.cmdHistoryInput = nil return } fallthrough default: - app.ui.menu = "" - app.ui.menuSelect = nil - app.menuCompActive = false + exitCompMenu(app) + app.cmdHistoryInput = nil app.ui.cmdAccLeft = append(app.ui.cmdAccLeft, []rune(arg)...) } } +func exitCompMenu(app *app) { + app.ui.menu = "" + app.ui.menuSelect = nil + app.menuCompActive = false +} + func (e *callExpr) eval(app *app, args []string) { os.Setenv("lf_count", strconv.Itoa(e.count)) @@ -1847,25 +1851,19 @@ func (e *callExpr) eval(app *app, args []string) { case "cmd-menu-complete-back": app.menuComplete(-1) case "cmd-menu-accept": - app.ui.menu = "" - app.ui.menuSelect = nil - app.menuCompActive = false + exitCompMenu(app) case "cmd-menu-discard": if app.menuCompActive { app.ui.cmdAccLeft = []rune(strings.Join(app.menuCompTmp, " ")) } - app.ui.menu = "" - app.ui.menuSelect = nil - app.menuCompActive = false + exitCompMenu(app) case "cmd-enter": s := string(append(app.ui.cmdAccLeft, app.ui.cmdAccRight...)) if len(s) == 0 && app.ui.cmdPrefix != "filter: " && app.ui.cmdPrefix != ">" { return } - app.ui.menu = "" - app.ui.menuSelect = nil - app.menuCompActive = false + exitCompMenu(app) app.ui.cmdAccLeft = nil app.ui.cmdAccRight = nil @@ -1873,8 +1871,8 @@ func (e *callExpr) eval(app *app, args []string) { switch app.ui.cmdPrefix { case ":": log.Printf("command: %s", s) + app.cmdHistory = append(app.cmdHistory, app.ui.cmdPrefix+s) app.ui.cmdPrefix = "" - app.cmdHistory = append(app.cmdHistory, cmdItem{":", s}) p := newParser(strings.NewReader(s)) for p.parse() { p.expr.eval(app, nil) @@ -1884,25 +1882,25 @@ func (e *callExpr) eval(app *app, args []string) { } case "$": log.Printf("shell: %s", s) + app.cmdHistory = append(app.cmdHistory, app.ui.cmdPrefix+s) app.ui.cmdPrefix = "" - app.cmdHistory = append(app.cmdHistory, cmdItem{"$", s}) app.runShell(s, nil, "$") case "%": log.Printf("shell-pipe: %s", s) - app.cmdHistory = append(app.cmdHistory, cmdItem{"%", s}) + app.cmdHistory = append(app.cmdHistory, app.ui.cmdPrefix+s) app.runShell(s, nil, "%") case ">": io.WriteString(app.cmdIn, s+"\n") app.cmdOutBuf = nil case "!": log.Printf("shell-wait: %s", s) + app.cmdHistory = append(app.cmdHistory, app.ui.cmdPrefix+s) app.ui.cmdPrefix = "" - app.cmdHistory = append(app.cmdHistory, cmdItem{"!", s}) app.runShell(s, nil, "!") case "&": log.Printf("shell-async: %s", s) + app.cmdHistory = append(app.cmdHistory, app.ui.cmdPrefix+s) app.ui.cmdPrefix = "" - app.cmdHistory = append(app.cmdHistory, cmdItem{"&", s}) app.runShell(s, nil, "&") case "/": dir := app.nav.currDir() @@ -2027,33 +2025,48 @@ func (e *callExpr) eval(app *app, args []string) { if !slices.Contains([]string{":", "$", "!", "%", "&"}, app.ui.cmdPrefix) { return } - if app.cmdHistoryInd > 0 { - app.cmdHistoryInd-- + input := app.ui.cmdPrefix + string(app.ui.cmdAccLeft) + if app.cmdHistoryInput == nil { + app.cmdHistoryInput = &input } - if app.cmdHistoryInd == 0 { - normal(app) - app.ui.cmdPrefix = ":" - return + for i := app.cmdHistoryInd - 1; i >= 0; i-- { + if i == 0 { + if *app.cmdHistoryInput == "" { + normal(app) + } else { + exitCompMenu(app) + app.ui.cmdAccLeft = nil + app.cmdHistoryInd = 0 + } + break + } + cmd := app.cmdHistory[len(app.cmdHistory)-i] + if strings.HasPrefix(cmd, *app.cmdHistoryInput) && cmd != input { + exitCompMenu(app) + app.ui.cmdPrefix = cmd[:1] + app.ui.cmdAccLeft = []rune(cmd[1:]) + app.cmdHistoryInd = i + break + } } - historyInd := app.cmdHistoryInd - cmd := app.cmdHistory[len(app.cmdHistory)-historyInd] - normal(app) - app.cmdHistoryInd = historyInd - app.ui.cmdPrefix = cmd.prefix - app.ui.cmdAccLeft = []rune(cmd.value) case "cmd-history-prev": if !slices.Contains([]string{":", "$", "!", "%", "&", ""}, app.ui.cmdPrefix) { return } - if app.cmdHistoryInd == len(app.cmdHistory) { - return + input := app.ui.cmdPrefix + string(app.ui.cmdAccLeft) + if app.cmdHistoryInput == nil { + app.cmdHistoryInput = &input + } + for i := app.cmdHistoryInd + 1; i <= len(app.cmdHistory); i++ { + cmd := app.cmdHistory[len(app.cmdHistory)-i] + if strings.HasPrefix(cmd, *app.cmdHistoryInput) && cmd != input { + exitCompMenu(app) + app.ui.cmdPrefix = cmd[:1] + app.ui.cmdAccLeft = []rune(cmd[1:]) + app.cmdHistoryInd = i + break + } } - historyInd := app.cmdHistoryInd + 1 - cmd := app.cmdHistory[len(app.cmdHistory)-historyInd] - normal(app) - app.cmdHistoryInd = historyInd - app.ui.cmdPrefix = cmd.prefix - app.ui.cmdAccLeft = []rune(cmd.value) case "cmd-left": if len(app.ui.cmdAccLeft) == 0 { return @@ -2083,6 +2096,8 @@ func (e *callExpr) eval(app *app, args []string) { switch app.ui.cmdPrefix { case "!", "$", "%", "&": app.ui.cmdPrefix = ":" + app.cmdHistoryInd = 0 + app.cmdHistoryInput = nil case ">", "rename: ", "filter: ": // Don't mess with programs waiting for input. // Exiting on backspace is also inconvenient for 'rename' and 'filter', diff --git a/lf.1 b/lf.1 index 79b9db67..f055b044 100644 --- a/lf.1 +++ b/lf.1 @@ -1,6 +1,6 @@ .\" Automatically generated by Pandoc 3.7.0.2 .\" -.TH "LF" "1" "2025\-09\-19" "r38" "DOCUMENTATION" +.TH "LF" "1" "2025\-09\-19" "r39" "DOCUMENTATION" .SH NAME lf \- terminal file manager .SH SYNOPSIS @@ -635,7 +635,9 @@ Execute the current line. .SS cmd\-interrupt (default \f[CR]\f[R]) Interrupt the current shell\-pipe command and return to the Normal mode. .SS cmd\-history\-next (default \f[CR]\f[R] and \f[CR]\f[R]), cmd\-history\-prev (default \f[CR]\f[R] and \f[CR]\f[R]) -Go to the next/previous item in the history. +Go to the next/previous entry in the command history. +If part of the command is already typed, then only matching entries will +be considered, and consecutive duplicate entries are skipped. .SS cmd\-left (default \f[CR]\f[R] and \f[CR]\f[R]), cmd\-right (default \f[CR]\f[R] and \f[CR]\f[R]) Move the cursor to the left/right. .SS cmd\-home (default \f[CR]\f[R] and \f[CR]\f[R]), cmd\-end (default \f[CR]\f[R] and \f[CR]\f[R]) diff --git a/ui.go b/ui.go index 4877565f..95bac38f 100644 --- a/ui.go +++ b/ui.go @@ -1267,7 +1267,7 @@ func listJumps(jumps []string, ind int) string { return b.String() } -func listHistory(history []cmdItem) string { +func listHistory(history []string) string { t := new(tabwriter.Writer) b := new(bytes.Buffer) @@ -1276,7 +1276,7 @@ func listHistory(history []cmdItem) string { t.Init(b, 0, gOpts.tabstop, 2, '\t', 0) fmt.Fprintln(t, "number\tcommand") for i, cmd := range history { - fmt.Fprintf(t, "%*d\t%s%s\n", maxlength, i+1, cmd.prefix, cmd.value) + fmt.Fprintf(t, "%*d\t%s\n", maxlength, i+1, cmd) } t.Flush()