2026-11-30 20:49:26 +01:00
|
|
|
;; This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
|
|
;; The line editor's keys, history and completion, without a terminal.
|
|
|
|
|
;;
|
|
|
|
|
;; bjo test tests/line.bjo
|
|
|
|
|
|
|
|
|
|
(import (std prelude))
|
|
|
|
|
(import (std simpletest))
|
|
|
|
|
(import (bjoterm terminal))
|
|
|
|
|
(import (bjoterm line))
|
|
|
|
|
|
|
|
|
|
(import/extern (temp-dir (: System.IO.Path.GetTempPath (-> string))))
|
|
|
|
|
|
|
|
|
|
;; ---- events ----------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
(: (-> string (Vec TermEvent)))
|
|
|
|
|
(defun (typed s)
|
|
|
|
|
(vec-map (fun (c) (TermKey (KeyChar c) no-mods)) (string->vec s)))
|
|
|
|
|
|
|
|
|
|
(: (-> char TermEvent))
|
|
|
|
|
(defun (ctrl c)
|
|
|
|
|
(TermKey (KeyChar c) (Mods (shift? #f) (alt? #f) (ctrl? #t))))
|
|
|
|
|
|
|
|
|
|
(: (-> char TermEvent))
|
|
|
|
|
(defun (alt c)
|
|
|
|
|
(TermKey (KeyChar c) (Mods (shift? #f) (alt? #t) (ctrl? #f))))
|
|
|
|
|
|
|
|
|
|
(: (-> Key TermEvent))
|
|
|
|
|
(defun (key k)
|
|
|
|
|
(TermKey k no-mods))
|
|
|
|
|
|
|
|
|
|
(: TermEvent)
|
|
|
|
|
(def enter (key KeyEnter))
|
|
|
|
|
|
|
|
|
|
;; The events, one after the other.
|
|
|
|
|
(: (-> (Vec (Vec TermEvent)) (Vec TermEvent)))
|
|
|
|
|
(defun (keys parts)
|
|
|
|
|
(let go ((i 0) (out []))
|
|
|
|
|
(if (= i (vec-length parts))
|
|
|
|
|
out
|
|
|
|
|
(go (+ i 1) (vec-append out (vec-ref parts i))))))
|
|
|
|
|
|
2026-12-01 13:07:56 +01:00
|
|
|
;; Suggests "/usr/local/share" for a line that ends with a prefix of it.
|
2026-12-01 13:28:16 +01:00
|
|
|
(: (-> string (Vec string) (Option Suggestion)))
|
2026-12-01 13:07:56 +01:00
|
|
|
(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)))
|
2026-12-01 13:28:16 +01:00
|
|
|
(Some (suggestion-after text (string-drop full (string-length word))))
|
|
|
|
|
(if (= text "cd lo")
|
|
|
|
|
(Some (Suggestion (ghost " -> /usr/local") (line "cd /usr/local")))
|
|
|
|
|
None)))
|
2026-12-01 13:07:56 +01:00
|
|
|
|
2026-11-30 20:49:26 +01:00
|
|
|
;; ---- running the editor ----------------------------------------------------
|
|
|
|
|
|
|
|
|
|
(: (-> (Option LineResult) string))
|
|
|
|
|
(defun (result-text r)
|
|
|
|
|
(match r
|
|
|
|
|
((Some (LineText text)) (str "text " (written text)))
|
|
|
|
|
((Some LineEof) "eof")
|
|
|
|
|
((Some LineCancelled) "cancelled")
|
2026-12-01 13:07:56 +01:00
|
|
|
((Some (LineKey ev text)) (str "key " (event->string ev) " " (written text)))
|
2026-11-30 20:49:26 +01:00
|
|
|
(None "not finished")))
|
|
|
|
|
|
|
|
|
|
;; The result, or the buffer when the events end first.
|
|
|
|
|
(: (-> LineEditor (Vec TermEvent) string))
|
|
|
|
|
(defun (run-on ed events)
|
|
|
|
|
(match (simulate-line ed events)
|
|
|
|
|
((Tuple None buffer) (str "buffer " (written buffer)))
|
|
|
|
|
((Tuple r _) (result-text r))))
|
|
|
|
|
|
|
|
|
|
(: (-> (Vec TermEvent) string))
|
|
|
|
|
(defun (run-fresh events)
|
|
|
|
|
(run-on (make-line-editor) events))
|
|
|
|
|
|
|
|
|
|
(: (-> string (Vec (Vec TermEvent)) string void))
|
|
|
|
|
(defun (expect-line name parts wanted)
|
|
|
|
|
(expect name (run-fresh (keys parts)) wanted))
|
|
|
|
|
|
|
|
|
|
;; Open brackets make the input incomplete, as in a Lisp REPL.
|
|
|
|
|
(: (-> string bool))
|
|
|
|
|
(defun (balanced? text)
|
|
|
|
|
(def depth (loop (:for c (in-vec (string->vec text)))
|
|
|
|
|
(:acc d (summing (cond ((= c #\() 1) ((= c #\)) -1) (else 0))))))
|
|
|
|
|
(<= depth 0))
|
|
|
|
|
|
|
|
|
|
(: (Vec string))
|
|
|
|
|
(def words ["vec-map" "vec-add" "vector" "display"])
|
|
|
|
|
|
|
|
|
|
;; Completes the last word from `words`.
|
|
|
|
|
(: (-> string Completion))
|
|
|
|
|
(defun (complete-word before)
|
|
|
|
|
(def parts (string-split before " "))
|
|
|
|
|
(def word (if (vec-empty? parts) "" (vec-ref parts (- (vec-length parts) 1))))
|
|
|
|
|
(Completion (prefix word)
|
|
|
|
|
(candidates (vec-map (fun (w) (str w " "))
|
2026-12-01 13:28:16 +01:00
|
|
|
(vec-filter (fun (w) (string-starts-with? w word)) words)))
|
|
|
|
|
(descriptions [])))
|
2026-11-30 20:49:26 +01:00
|
|
|
|
|
|
|
|
(defun (main args)
|
|
|
|
|
;; Typing and accepting.
|
|
|
|
|
(expect-line "typing and Enter" [(typed "hello") [enter]] "text \"hello\"")
|
|
|
|
|
(expect-line "the events end first" [(typed "hel")] "buffer \"hel\"")
|
|
|
|
|
(expect-line "C-d on an empty buffer" [[(ctrl #\d)]] "eof")
|
|
|
|
|
(expect-line "C-d deletes under the cursor" [(typed "ab") [(ctrl #\a) (ctrl #\d)] [enter]] "text \"b\"")
|
|
|
|
|
(expect-line "C-c cancels" [(typed "abc") [(ctrl #\c)]] "cancelled")
|
|
|
|
|
(expect-line "the terminal hangs up" [(typed "abc") [TermEnd]] "eof")
|
|
|
|
|
(expect-line "a paste is inserted" [(typed "a") [(TermPaste "x\ny")] (typed "b") [enter]] "text \"ax\\nyb\"")
|
|
|
|
|
|
|
|
|
|
;; Moving.
|
|
|
|
|
(expect-line "Left and insert" [(typed "helo") [(key KeyLeft)] (typed "l") [enter]] "text \"hello\"")
|
|
|
|
|
(expect-line "C-a and C-e" [(typed "bc") [(ctrl #\a)] (typed "a") [(ctrl #\e)] (typed "d") [enter]]
|
|
|
|
|
"text \"abcd\"")
|
|
|
|
|
(expect-line "M-b and M-f" [(typed "one two") [(alt #\b) (alt #\b) (alt #\f)] (typed "!") [enter]]
|
|
|
|
|
"text \"one! two\"")
|
|
|
|
|
(expect-line "C-Left by word"
|
|
|
|
|
[(typed "one two") [(TermKey KeyLeft (Mods (shift? #f) (alt? #f) (ctrl? #t)))] (typed "_") [enter]]
|
|
|
|
|
"text \"one _two\"")
|
|
|
|
|
|
|
|
|
|
;; Deleting and the kill buffer.
|
|
|
|
|
(expect-line "Backspace" [(typed "abc") [(key KeyBackspace)] [enter]] "text \"ab\"")
|
|
|
|
|
(expect-line "Delete" [(typed "abc") [(ctrl #\a) (key KeyDelete)] [enter]] "text \"bc\"")
|
|
|
|
|
(expect-line "C-k and C-y" [(typed "abcd") [(ctrl #\a) (ctrl #\f) (ctrl #\k)] [(ctrl #\a) (ctrl #\y)] [enter]]
|
|
|
|
|
"text \"bcda\"")
|
|
|
|
|
(expect-line "C-u kills to the start" [(typed "abcd") [(ctrl #\b) (ctrl #\u)] [enter]] "text \"d\"")
|
|
|
|
|
(expect-line "C-w kills a word" [(typed "foo bar") [(ctrl #\w)] [enter]] "text \"foo \"")
|
|
|
|
|
(expect-line "kills one after another add up" [(typed "a b c") [(ctrl #\w) (ctrl #\w) (ctrl #\y)] [enter]]
|
|
|
|
|
"text \"a b c\"")
|
|
|
|
|
;; The second C-w takes only "b": the cursor is before the space.
|
|
|
|
|
(expect-line "a key between kills starts a new kill"
|
|
|
|
|
[(typed "a b c") [(ctrl #\w) (key KeyLeft) (ctrl #\w) (ctrl #\e) (ctrl #\y)] [enter]]
|
|
|
|
|
"text \"a b\"")
|
|
|
|
|
(expect-line "M-d kills the next word" [(typed "one two") [(ctrl #\a) (alt #\d)] [enter]] "text \" two\"")
|
|
|
|
|
(expect-line "C-t swaps at the end" [(typed "ab") [(ctrl #\t)] [enter]] "text \"ba\"")
|
|
|
|
|
(expect-line "C-t swaps in the middle" [(typed "abc") [(ctrl #\a) (ctrl #\f) (ctrl #\t)] [enter]]
|
|
|
|
|
"text \"bac\"")
|
|
|
|
|
|
|
|
|
|
;; Grapheme clusters.
|
|
|
|
|
(expect-line "a combining mark joins the letter before it"
|
|
|
|
|
[(typed "e") [(TermKey (KeyChar (int->char 769)) no-mods) (key KeyBackspace)] [enter]]
|
|
|
|
|
"text \"\"")
|
|
|
|
|
(expect-line "an emoji with a modifier is one cluster"
|
|
|
|
|
[[(TermPaste "a👍🏽b")] [(key KeyLeft) (key KeyLeft) (key KeyBackspace)] [enter]]
|
|
|
|
|
"text \"👍🏽b\"")
|
|
|
|
|
|
|
|
|
|
;; Several lines.
|
|
|
|
|
(expect-line "M-Enter starts a new line" [(typed "a") [(TermKey KeyEnter (Mods (shift? #f) (alt? #t) (ctrl? #f)))]
|
|
|
|
|
(typed "b") [enter]]
|
|
|
|
|
"text \"a\\nb\"")
|
|
|
|
|
(expect-line "C-j starts a new line" [(typed "a") [(ctrl #\j)] (typed "b") [enter]] "text \"a\\nb\"")
|
|
|
|
|
(expect "Enter in incomplete input starts a new line"
|
|
|
|
|
(run-on (make-line-editor #:input-done? (Some balanced?))
|
|
|
|
|
(keys [(typed "(+ 1") [enter] (typed "2)") [enter]]))
|
|
|
|
|
"text \"(+ 1\\n2)\"")
|
|
|
|
|
(expect-line "Up moves to the line above, at the same column"
|
|
|
|
|
[(typed "abc") [(ctrl #\j)] (typed "de") [(key KeyUp)] (typed "X") [enter]]
|
|
|
|
|
"text \"abXc\\nde\"")
|
|
|
|
|
(expect-line "Down moves to the line below, as far as it reaches"
|
|
|
|
|
[(typed "abcdef") [(ctrl #\j)] (typed "x") [(key KeyUp) (ctrl #\e) (key KeyDown)] (typed "Y") [enter]]
|
|
|
|
|
"text \"abcdef\\nxY\"")
|
|
|
|
|
(expect-line "C-a goes to the start of the line" [(typed "ab") [(ctrl #\j)] (typed "cd") [(ctrl #\a)] (typed "X") [enter]]
|
|
|
|
|
"text \"ab\\nXcd\"")
|
|
|
|
|
(expect-line "C-k at the end of a line joins the next"
|
|
|
|
|
[(typed "ab") [(ctrl #\j)] (typed "cd") [(key KeyUp) (ctrl #\e) (ctrl #\k)] [enter]]
|
|
|
|
|
"text \"abcd\"")
|
|
|
|
|
|
|
|
|
|
;; History.
|
|
|
|
|
(def ed (make-line-editor #:history-size 3))
|
|
|
|
|
(ignore (run-on ed (keys [(typed "one") [enter]])))
|
|
|
|
|
(ignore (run-on ed (keys [(typed "two") [enter]])))
|
|
|
|
|
(ignore (run-on ed (keys [(typed "two") [enter]])))
|
|
|
|
|
(ignore (run-on ed (keys [[enter]])))
|
|
|
|
|
(expect "accepted lines go to the history, not repeats or empty ones" (line-history ed) ["one" "two"])
|
|
|
|
|
(expect "Up shows the last entry" (run-on ed (keys [[(key KeyUp)] [enter]])) "text \"two\"")
|
|
|
|
|
(expect "Up twice, and Down" (run-on ed (keys [[(key KeyUp) (key KeyUp) (key KeyUp) (key KeyDown)] [enter]]))
|
|
|
|
|
"text \"two\"")
|
|
|
|
|
(expect "Down past the newest shows the draft"
|
|
|
|
|
(run-on ed (keys [(typed "draft") [(ctrl #\p) (ctrl #\p) (ctrl #\n) (ctrl #\n) (ctrl #\n)]]))
|
|
|
|
|
"buffer \"draft\"")
|
|
|
|
|
(line-history-add! ed "three")
|
|
|
|
|
(line-history-add! ed "four")
|
|
|
|
|
(expect "the oldest entry goes above the size" (line-history ed) ["two" "three" "four"])
|
|
|
|
|
|
|
|
|
|
;; Search.
|
|
|
|
|
(def searched (make-line-editor))
|
|
|
|
|
(loop (:for e (in-vec ["make build" "make test" "git status" "make install"]))
|
|
|
|
|
(:do (line-history-add! searched e)))
|
|
|
|
|
(expect "C-r finds the newest match" (run-on searched (keys [[(ctrl #\r)] (typed "make") [enter]]))
|
|
|
|
|
"text \"make install\"")
|
|
|
|
|
(expect "C-r again finds an older one" (run-on searched (keys [[(ctrl #\r)] (typed "make") [(ctrl #\r)] [enter]]))
|
|
|
|
|
"text \"make test\"")
|
|
|
|
|
(expect "a longer query narrows the match" (run-on searched (keys [[(ctrl #\r)] (typed "make b") [enter]]))
|
|
|
|
|
"text \"make build\"")
|
|
|
|
|
(expect "C-g ends the search and restores the input"
|
|
|
|
|
(run-on searched (keys [(typed "x") [(ctrl #\r)] (typed "git") [(ctrl #\g)]]))
|
|
|
|
|
"buffer \"x\"")
|
|
|
|
|
(expect "another key keeps the match and does what it does"
|
|
|
|
|
(run-on searched (keys [[(ctrl #\r)] (typed "status") [(ctrl #\e)] (typed "!") [enter]]))
|
|
|
|
|
"text \"git status!\"")
|
|
|
|
|
;; The test before added "git status!" to the history.
|
|
|
|
|
(expect "no match keeps the last one"
|
|
|
|
|
(run-on searched (keys [[(ctrl #\r)] (typed "gitx") [(key KeyRight)]]))
|
|
|
|
|
"buffer \"git status!\"")
|
|
|
|
|
|
|
|
|
|
;; Completion.
|
|
|
|
|
(def completing (make-line-editor #:completer (Some complete-word)))
|
|
|
|
|
(expect "Tab inserts the common prefix" (run-on completing (keys [(typed "x ve") [(key KeyTab)]]))
|
|
|
|
|
"buffer \"x vec\"")
|
|
|
|
|
(expect "one candidate is inserted whole" (run-on completing (keys [(typed "x vec-m") [(key KeyTab)]]))
|
|
|
|
|
"buffer \"x vec-map \"")
|
|
|
|
|
(expect "no candidate changes nothing" (run-on completing (keys [(typed "zz") [(key KeyTab) (key KeyTab)]]))
|
|
|
|
|
"buffer \"zz\"")
|
2026-12-01 14:23:40 +01:00
|
|
|
(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))])])
|
2026-11-30 20:49:26 +01:00
|
|
|
(expect "Tab without a completer inserts a tab" (run-fresh (keys [(typed "a") [(key KeyTab)] [enter]]))
|
|
|
|
|
"text \"a\\t\"")
|
|
|
|
|
|
2026-12-01 13:07:56 +01:00
|
|
|
;; 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\"")
|
2026-12-01 13:28:16 +01:00
|
|
|
(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\"")
|
2026-12-01 13:07:56 +01:00
|
|
|
(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!"))
|
|
|
|
|
|
2026-12-01 14:58:26 +01:00
|
|
|
;; Highlighting.
|
|
|
|
|
(expect "a highlight styles its characters, the last one winning"
|
|
|
|
|
(cluster-styles (make-line-editor #:highlighter
|
|
|
|
|
(Some (fun (text)
|
|
|
|
|
[(Highlight (start 0) (end 3) (style [StyleBold]))
|
|
|
|
|
(Highlight (start 1) (end 2) (style [StyleUnderline]))])))
|
|
|
|
|
["a" "b" "c" "d"])
|
|
|
|
|
[[StyleBold] [StyleUnderline] [StyleBold] []])
|
|
|
|
|
(expect "offsets count characters, not clusters"
|
|
|
|
|
(cluster-styles (make-line-editor #:highlighter
|
|
|
|
|
(Some (fun (text) [(Highlight (start 2) (end 3) (style [StyleBold]))])))
|
|
|
|
|
["e\u{301}" "x"])
|
|
|
|
|
[[] [StyleBold]])
|
|
|
|
|
|
2026-12-01 13:28:16 +01:00
|
|
|
;; 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"))
|
|
|
|
|
|
2026-11-30 20:49:26 +01:00
|
|
|
;; The history file.
|
|
|
|
|
(def path (path-combine (temp-dir) "bjoterm-history-test.txt"))
|
|
|
|
|
(def saved (make-line-editor))
|
|
|
|
|
(line-history-add! saved "plain")
|
|
|
|
|
(line-history-add! saved "two\nlines")
|
|
|
|
|
(line-history-add! saved "a \\ backslash and \\n")
|
|
|
|
|
(expect "the history is saved" (line-history-save saved path) (Ok unit))
|
|
|
|
|
(def loaded (make-line-editor))
|
|
|
|
|
(expect "and loaded" (line-history-load! loaded path) (Ok unit))
|
|
|
|
|
(expect "with lines and backslashes as they were" (line-history loaded) (line-history saved))
|
|
|
|
|
(ignore (file-delete path))
|
|
|
|
|
(expect "a history file that does not exist is empty"
|
|
|
|
|
(line-history-load! (make-line-editor) (path-combine (temp-dir) "bjoterm-no-such-history.txt"))
|
|
|
|
|
(Ok unit))
|
2026-11-30 21:24:16 +01:00
|
|
|
|
|
|
|
|
;; Reflow: where the cursor is after a terminal that reflows narrows.
|
|
|
|
|
(expect "rows that still fit move nothing" (cursor-row-after-reflow [[1 1 1] [1 1]] 1 2 10) 1)
|
|
|
|
|
(expect "a row above that wraps moves the cursor down"
|
|
|
|
|
(cursor-row-after-reflow [[1 1 1 1 1 1] [1 1]] 1 1 4) 2)
|
|
|
|
|
(expect "every row above counts" (cursor-row-after-reflow [[1 1 1 1 1] [1 1 1 1 1] [1]] 2 0 2) 6)
|
|
|
|
|
(expect "the cursor's own row wraps before it" (cursor-row-after-reflow [[1 1 1 1 1 1]] 0 5 4) 1)
|
|
|
|
|
(expect "but not after it" (cursor-row-after-reflow [[1 1 1 1 1 1]] 0 2 4) 0)
|
|
|
|
|
(expect "a cursor after a full row is on the next" (cursor-row-after-reflow [[1 1 1 1]] 0 4 4) 1)
|
|
|
|
|
(expect "a wide item that does not fit goes to the next row"
|
|
|
|
|
(cursor-row-after-reflow [[1 1 1 2] [1]] 1 0 4) 2)
|
|
|
|
|
(expect "an empty row is one row" (cursor-row-after-reflow [[] [] [1]] 2 1 4) 2)
|
2026-11-30 21:35:16 +01:00
|
|
|
|
|
|
|
|
;; The rows drawn of an input taller than the screen.
|
|
|
|
|
(expect "input that fits is drawn whole" (input-window 5 10 3 0) (Tuple 0 5))
|
|
|
|
|
(expect "at the start, a marker below" (input-window 20 6 2 0) (Tuple 0 5))
|
|
|
|
|
(expect "at the end, a marker above" (input-window 20 6 19 0) (Tuple 15 5))
|
|
|
|
|
(expect "between, a marker each side" (input-window 20 6 10 0) (Tuple 7 4))
|
|
|
|
|
(expect "the window stays while the cursor is in it" (input-window 20 6 9 7) (Tuple 7 4))
|
|
|
|
|
(expect "and moves the least it must" (input-window 20 6 5 7) (Tuple 5 4))
|
|
|
|
|
(expect "a screen of 2 rows has no markers" (input-window 20 2 10 0) (Tuple 9 2))
|
2026-11-30 20:49:26 +01:00
|
|
|
(println "line ok"))
|