line-read: suggestions, keys for the caller, initial input; 0.1.2
#:suggester shows a suggestion after the input, dimmed, as fish does. Right, C-f, End and C-e at the end accept it; M-f, C-Right and M-Right one word. #:command-key? hands keys to the caller as (LineKey key input), with the input erased. line-read and simulate-line take #:initial. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
dc8a3325b8
commit
3021b14aac
7 changed files with 198 additions and 38 deletions
|
|
@ -42,6 +42,17 @@
|
|||
out
|
||||
(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)))
|
||||
(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))
|
||||
|
||||
;; ---- running the editor ----------------------------------------------------
|
||||
|
||||
(: (-> (Option LineResult) string))
|
||||
|
|
@ -50,6 +61,7 @@
|
|||
((Some (LineText text)) (str "text " (written text)))
|
||||
((Some LineEof) "eof")
|
||||
((Some LineCancelled) "cancelled")
|
||||
((Some (LineKey ev text)) (str "key " (event->string ev) " " (written text)))
|
||||
(None "not finished")))
|
||||
|
||||
;; The result, or the buffer when the events end first.
|
||||
|
|
@ -202,6 +214,56 @@
|
|||
(expect "Tab without a completer inserts a tab" (run-fresh (keys [(typed "a") [(key KeyTab)] [enter]]))
|
||||
"text \"a\\t\"")
|
||||
|
||||
;; Suggestions.
|
||||
(def suggesting (make-line-editor #:suggester (Some suggest-path)))
|
||||
(expect "Right accepts the suggestion"
|
||||
(run-on suggesting (keys [(typed "cd /usr/l") [(key KeyRight)]]))
|
||||
"buffer \"cd /usr/local/share\"")
|
||||
(expect "and C-f, End and C-e"
|
||||
(vec-map (fun (k) (run-on suggesting (keys [(typed "cd /usr/l") [k]])))
|
||||
[(ctrl #\f) (key KeyEnd) (ctrl #\e)])
|
||||
["buffer \"cd /usr/local/share\"" "buffer \"cd /usr/local/share\"" "buffer \"cd /usr/local/share\""])
|
||||
(expect "M-f accepts one word"
|
||||
(run-on suggesting (keys [(typed "cd /usr/l") [(alt #\f)]]))
|
||||
"buffer \"cd /usr/local\"")
|
||||
(expect "and again the next"
|
||||
(run-on suggesting (keys [(typed "cd /usr/l") [(alt #\f) (alt #\f)]]))
|
||||
"buffer \"cd /usr/local/share\"")
|
||||
(expect "C-Right accepts one word"
|
||||
(run-on suggesting (keys [(typed "cd /usr/l") [(TermKey KeyRight (Mods (shift? #f) (alt? #f) (ctrl? #t)))]]))
|
||||
"buffer \"cd /usr/local\"")
|
||||
(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 "Right with no suggestion does nothing at the end"
|
||||
(run-on suggesting (keys [(typed "ls") [(key KeyRight)]]))
|
||||
"buffer \"ls\"")
|
||||
(expect "Enter does not accept it"
|
||||
(run-on suggesting (keys [(typed "cd /usr/l") [enter]]))
|
||||
"text \"cd /usr/l\"")
|
||||
(def seen (make-box []))
|
||||
(def with-history
|
||||
(make-line-editor #:suggester (Some (fun (text history) (box-set! seen history) None))))
|
||||
(ignore (run-on with-history (keys [(typed "one") [enter]])))
|
||||
(ignore (run-on with-history (keys [(typed "t") [(key KeyRight)]])))
|
||||
(expect "the suggester gets the history" (box-ref seen) ["one"])
|
||||
|
||||
;; Keys that the caller claims.
|
||||
(def claiming
|
||||
(make-line-editor #:command-key?
|
||||
(Some (fun (ev text) (and (string-empty? text)
|
||||
(= ev (TermKey KeyLeft (Mods (shift? #f) (alt? #t) (ctrl? #f)))))))))
|
||||
(expect "a claimed key returns LineKey"
|
||||
(run-on claiming (keys [[(TermKey KeyLeft (Mods (shift? #f) (alt? #t) (ctrl? #f)))]]))
|
||||
"key key M-<left> \"\"")
|
||||
(expect "an unclaimed one edits"
|
||||
(run-on claiming (keys [(typed "ab") [(TermKey KeyLeft (Mods (shift? #f) (alt? #t) (ctrl? #f)))]
|
||||
(typed "x") [enter]]))
|
||||
"text \"xab\"")
|
||||
(expect "#:initial starts with the input"
|
||||
(simulate-line (make-line-editor) (keys [(typed "!") [enter]]) #:initial "hi")
|
||||
(Tuple (Some (LineText "hi!")) "hi!"))
|
||||
|
||||
;; The history file.
|
||||
(def path (path-combine (temp-dir) "bjoterm-history-test.txt"))
|
||||
(def saved (make-line-editor))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue