line-read: a menu of the candidates below the input; 0.2.2

As fish does: Tab that can add nothing opens a menu, Tab and Shift-Tab
choose a candidate into the input, Escape restores it, and another key
closes the menu. Names are shown without their common directory.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Linus Björnstam 2026-12-01 14:23:40 +01:00
parent 702fcedf2d
commit 4eef978b6b
3 changed files with 194 additions and 58 deletions

View file

@ -214,6 +214,41 @@
"buffer \"x vec-map \"")
(expect "no candidate changes nothing" (run-on completing (keys [(typed "zz") [(key KeyTab) (key KeyTab)]]))
"buffer \"zz\"")
(def menu-ed (make-line-editor #:completer (Some (fun (before)
(Completion (prefix "d/a") (candidates ["d/ab " "d/ac "])
(descriptions []))))))
(def tab (key KeyTab))
(def back-tab (TermKey KeyTab (Mods (shift? #t) (alt? #f) (ctrl? #f))))
(expect "Tab that adds nothing opens the menu, and changes nothing"
(run-on menu-ed (keys [(typed "x d/a") [tab]])) "buffer \"x d/a\"")
(expect "the next Tab chooses the first"
(run-on menu-ed (keys [(typed "x d/a") [tab tab]])) "buffer \"x d/ab \"")
(expect "and the next the second"
(run-on menu-ed (keys [(typed "x d/a") [tab tab tab]])) "buffer \"x d/ac \"")
(expect "and then the first again"
(run-on menu-ed (keys [(typed "x d/a") [tab tab tab tab]])) "buffer \"x d/ab \"")
(expect "Shift-Tab goes back"
(run-on menu-ed (keys [(typed "x d/a") [tab back-tab]])) "buffer \"x d/ac \"")
(expect "Escape restores the input"
(run-on menu-ed (keys [(typed "x d/a") [tab tab (key KeyEscape)]])) "buffer \"x d/a\"")
(expect "another key keeps the choice and does what it does"
(run-on menu-ed (keys [(typed "x d/a") [tab tab] (typed "z")])) "buffer \"x d/ab z\"")
(expect "Enter keeps the choice, and does not run the line"
(run-on menu-ed (keys [(typed "x d/a") [tab tab enter]])) "buffer \"x d/ab \"")
(expect "Enter with nothing chosen runs it"
(run-on menu-ed (keys [(typed "x d/a") [tab enter]])) "text \"x d/a\"")
(expect "the text after the cursor stays"
(run-on menu-ed (keys [(typed "x d/a;y") [(key KeyLeft) (key KeyLeft) tab tab]]))
"buffer \"x d/ab ;y\"")
(expect "the menu shows names without their common directory"
(menu-lines ["d/ab " "d/ac "] [] -1 40 None) ["ab ac"])
(expect "but keeps one that would be empty"
(menu-lines ["d/" "d/e/"] [] -1 40 None) ["d/ d/e/"])
(expect "the chosen one is in reverse"
(menu-lines ["ab" "cd"] [] 1 40 None) [(str "ab " (styled "cd" [StyleReverse]))])
(expect "a long menu shows the rows around the chosen one"
(menu-lines ["a" "b" "c" "d" "e"] [] 3 3 (Some 3))
["c" (styled "d" [StyleReverse]) (styled "\u{2026} 3 more rows" [(StyleFg (Ansi 8))])])
(expect "Tab without a completer inserts a tab" (run-fresh (keys [(typed "a") [(key KeyTab)] [enter]]))
"text \"a\\t\"")