line-read: suggestions that replace the line, descriptions in completion; 0.2.0

A suggester returns a Suggestion: the text shown and the line it makes,
which can differ from the input in case or be another line. A Completion
has descriptions, shown beside the candidates on the second Tab.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Linus Björnstam 2026-12-01 13:28:16 +01:00
parent 3021b14aac
commit 2b7749013d
3 changed files with 104 additions and 27 deletions

View file

@ -43,15 +43,17 @@
(go (+ i 1) (vec-append out (vec-ref parts i))))))
;; Suggests "/usr/local/share" for a line that ends with a prefix of it.
(: (-> string (Vec string) (Option string)))
(: (-> string (Vec string) (Option Suggestion)))
(defun (suggest-path text history)
(def full "/usr/local/share")
(def word (match (string-split-once text " ")
((Some (Tuple _ w)) w)
(None "")))
(if (and (not (string-empty? word)) (string-starts-with? full word) (not (= word full)))
(Some (string-drop full (string-length word)))
None))
(Some (suggestion-after text (string-drop full (string-length word))))
(if (= text "cd lo")
(Some (Suggestion (ghost " -> /usr/local") (line "cd /usr/local")))
None)))
;; ---- running the editor ----------------------------------------------------
@ -96,7 +98,8 @@
(def word (if (vec-empty? parts) "" (vec-ref parts (- (vec-length parts) 1))))
(Completion (prefix word)
(candidates (vec-map (fun (w) (str w " "))
(vec-filter (fun (w) (string-starts-with? w word)) words)))))
(vec-filter (fun (w) (string-starts-with? w word)) words)))
(descriptions [])))
(defun (main args)
;; Typing and accepting.
@ -235,6 +238,12 @@
(expect "Right before the end moves"
(run-on suggesting (keys [(typed "cd /usr/l") [(key KeyLeft) (key KeyRight)] (typed "x")]))
"buffer \"cd /usr/lx\"")
(expect "a suggestion of another line replaces the input"
(run-on suggesting (keys [(typed "cd lo") [(key KeyRight)]]))
"buffer \"cd /usr/local\"")
(expect "and so does accepting a word of it"
(run-on suggesting (keys [(typed "cd lo") [(alt #\f)]]))
"buffer \"cd /usr/local\"")
(expect "Right with no suggestion does nothing at the end"
(run-on suggesting (keys [(typed "ls") [(key KeyRight)]]))
"buffer \"ls\"")
@ -264,6 +273,15 @@
(simulate-line (make-line-editor) (keys [(typed "!") [enter]]) #:initial "hi")
(Tuple (Some (LineText "hi!")) "hi!"))
;; The list of candidates.
(expect "without descriptions, in columns" (completion-listing ["ab" "cd" "ef"] [] 20) "ab cd ef\n")
(expect "with them, one a row"
(completion-listing ["ab" "cdef"] ["first" ""] 40)
(str "ab " (styled "first" [(StyleFg (Ansi 8))]) "\ncdef\n"))
(expect "a long description is cut"
(completion-listing ["ab"] ["a description that is long"] 16)
(str "ab " (styled "a descripti\u{2026}" [(StyleFg (Ansi 8))]) "\n"))
;; The history file.
(def path (path-combine (temp-dir) "bjoterm-history-test.txt"))
(def saved (make-line-editor))