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:
parent
3021b14aac
commit
72a7c09b3a
3 changed files with 104 additions and 27 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
(package
|
(package
|
||||||
(name (bjoterm))
|
(name (bjoterm))
|
||||||
(version "0.1.2")
|
(version "0.2.0")
|
||||||
(description "The terminal for Bjolang programs: raw mode, key events and escape sequences. Linux.")
|
(description "The terminal for Bjolang programs: raw mode, key events and escape sequences. Linux.")
|
||||||
(license "MPL-2.0")
|
(license "MPL-2.0")
|
||||||
|
|
||||||
|
|
|
||||||
103
src/line.bjo
103
src/line.bjo
|
|
@ -29,7 +29,8 @@
|
||||||
;; - The history belongs to the editor. Accepted lines that are not empty
|
;; - The history belongs to the editor. Accepted lines that are not empty
|
||||||
;; and not the same as the last one are added to it.
|
;; and not the same as the last one are added to it.
|
||||||
;; - `#:suggester` shows a suggestion after the input, dimmed, as fish does.
|
;; - `#:suggester` shows a suggestion after the input, dimmed, as fish does.
|
||||||
;; Keys that move right at the end of the input accept it.
|
;; Keys that move right at the end of the input accept it, which makes
|
||||||
|
;; the input the suggested line.
|
||||||
;; - `#:command-key?` hands keys to the caller: `line-read` erases the input
|
;; - `#:command-key?` hands keys to the caller: `line-read` erases the input
|
||||||
;; and returns `LineKey`.
|
;; and returns `LineKey`.
|
||||||
;; - The design is in plan.org, phase 2.
|
;; - The design is in plan.org, phase 2.
|
||||||
|
|
@ -40,10 +41,10 @@
|
||||||
(import (bjoterm text))
|
(import (bjoterm text))
|
||||||
|
|
||||||
(export
|
(export
|
||||||
LineEditor LineResult Completion
|
LineEditor LineResult Completion Suggestion suggestion-after
|
||||||
make-line-editor line-read
|
make-line-editor line-read
|
||||||
line-history line-history-add! line-history-load! line-history-save
|
line-history line-history-add! line-history-load! line-history-save
|
||||||
simulate-line cursor-row-after-reflow input-window)
|
simulate-line cursor-row-after-reflow input-window completion-listing)
|
||||||
|
|
||||||
;; ===========================================================================
|
;; ===========================================================================
|
||||||
;; Types
|
;; Types
|
||||||
|
|
@ -63,12 +64,27 @@
|
||||||
;; the word being written. It must be a suffix of the text.
|
;; the word being written. It must be a suffix of the text.
|
||||||
;; - `candidates`: each a whole replacement for `prefix`. A candidate can end
|
;; - `candidates`: each a whole replacement for `prefix`. A candidate can end
|
||||||
;; with a space, to start the next word.
|
;; with a space, to start the next word.
|
||||||
(type (: Completion (Record (: prefix string) (: candidates (Vec string)))))
|
;; - `descriptions`: empty, or one per candidate, shown beside it in the
|
||||||
|
;; list of candidates. An empty one shows nothing.
|
||||||
|
(type (: Completion (Record (: prefix string) (: candidates (Vec string)) (: descriptions (Vec string)))))
|
||||||
|
|
||||||
|
;; What `#:suggester` returns.
|
||||||
|
;; - `ghost`: the text shown dimmed after the input.
|
||||||
|
;; - `line`: the input after the suggestion is accepted. When it is the
|
||||||
|
;; input and `ghost`, one word of `ghost` can be accepted at a time;
|
||||||
|
;; otherwise accepting a word accepts it all. It can differ from the
|
||||||
|
;; input in case, or be another line that `ghost` describes.
|
||||||
|
(type (: Suggestion (Record (: ghost string) (: line string))))
|
||||||
|
|
||||||
|
;; The suggestion of `rest` after `text`.
|
||||||
|
(: (-> string string Suggestion))
|
||||||
|
(defun (suggestion-after text rest)
|
||||||
|
(Suggestion (ghost rest) (line (str text rest))))
|
||||||
|
|
||||||
;; A line editor: its options and its history.
|
;; A line editor: its options and its history.
|
||||||
(type (: LineEditor #:opaque
|
(type (: LineEditor #:opaque
|
||||||
(Record (: completer (Option (-> string Completion)))
|
(Record (: completer (Option (-> string Completion)))
|
||||||
(: suggester (Option (-> string (Vec string) (Option string))))
|
(: suggester (Option (-> string (Vec string) (Option Suggestion))))
|
||||||
(: command-key? (Option (-> TermEvent string bool)))
|
(: command-key? (Option (-> TermEvent string bool)))
|
||||||
(: input-done? (Option (-> string bool)))
|
(: input-done? (Option (-> string bool)))
|
||||||
(: history-size int)
|
(: history-size int)
|
||||||
|
|
@ -101,7 +117,7 @@
|
||||||
;; What one key does.
|
;; What one key does.
|
||||||
(type (: Step (Union (: Edited State)
|
(type (: Step (Union (: Edited State)
|
||||||
(: Finished State LineResult)
|
(: Finished State LineResult)
|
||||||
(: ShowList State (Vec string))
|
(: ShowList State (Vec string) (Vec string))
|
||||||
(: ClearScreen State)
|
(: ClearScreen State)
|
||||||
(: SuspendNow State)
|
(: SuspendNow State)
|
||||||
(: Bell State))))
|
(: Bell State))))
|
||||||
|
|
@ -115,8 +131,8 @@
|
||||||
;; - `#:input-done?`: called by Enter with the whole input. #f makes Enter
|
;; - `#:input-done?`: called by Enter with the whole input. #f makes Enter
|
||||||
;; start a new line; a REPL returns #f while brackets are open.
|
;; start a new line; a REPL returns #f while brackets are open.
|
||||||
;; - `#:suggester`: called with the whole input and the history, oldest
|
;; - `#:suggester`: called with the whole input and the history, oldest
|
||||||
;; first, while the cursor is at the end of the input. Returns the text to
|
;; first, while the cursor is at the end of the input. Returns a
|
||||||
;; show after the input, or None. Called after each key, so it must be fast.
|
;; `Suggestion`, or None. Called after each key, so it must be fast.
|
||||||
;; - `#:command-key?`: called with each key and the whole input. #t makes
|
;; - `#:command-key?`: called with each key and the whole input. #t makes
|
||||||
;; `line-read` return `(LineKey key input)`.
|
;; `line-read` return `(LineKey key input)`.
|
||||||
;; - `#:history-size`: the most entries kept; the oldest go first.
|
;; - `#:history-size`: the most entries kept; the oldest go first.
|
||||||
|
|
@ -127,7 +143,7 @@
|
||||||
;; above the input when the terminal narrows; a wrong #f leaves a copy of
|
;; above the input when the terminal narrows; a wrong #f leaves a copy of
|
||||||
;; the input above it.
|
;; the input above it.
|
||||||
(: (-> (#:completer (Option (-> string Completion)))
|
(: (-> (#:completer (Option (-> string Completion)))
|
||||||
(#:suggester (Option (-> string (Vec string) (Option string))))
|
(#:suggester (Option (-> string (Vec string) (Option Suggestion))))
|
||||||
(#:command-key? (Option (-> TermEvent string bool)))
|
(#:command-key? (Option (-> TermEvent string bool)))
|
||||||
(#:input-done? (Option (-> string bool)))
|
(#:input-done? (Option (-> string bool)))
|
||||||
(#:history-size int)
|
(#:history-size int)
|
||||||
|
|
@ -297,7 +313,7 @@
|
||||||
(def ghost (if whole?
|
(def ghost (if whole?
|
||||||
[]
|
[]
|
||||||
(match (suggestion ed st)
|
(match (suggestion ed st)
|
||||||
((Some s) (clusters s))
|
((Some s) (clusters (record-ref s ghost)))
|
||||||
(None []))))
|
(None []))))
|
||||||
(def lay (layout-state st prompt cont width ghost))
|
(def lay (layout-state st prompt cont width ghost))
|
||||||
(def shown (window-of lay width (if whole? None (Some height)) top))
|
(def shown (window-of lay width (if whole? None (Some height)) top))
|
||||||
|
|
@ -348,9 +364,9 @@
|
||||||
(leave lay)
|
(leave lay)
|
||||||
(terminal-suspend term)
|
(terminal-suspend term)
|
||||||
(go next))
|
(go next))
|
||||||
((ShowList next items)
|
((ShowList next items descriptions)
|
||||||
(leave lay)
|
(leave lay)
|
||||||
(write-string out (columns items (fst (terminal-size term))))
|
(write-string out (completion-listing items descriptions (fst (terminal-size term))))
|
||||||
(go next))
|
(go next))
|
||||||
((Finished _ (LineKey ev text))
|
((Finished _ (LineKey ev text))
|
||||||
(def (Tuple width _) (terminal-size term))
|
(def (Tuple width _) (terminal-size term))
|
||||||
|
|
@ -370,6 +386,46 @@
|
||||||
(_ unit))
|
(_ unit))
|
||||||
result))))
|
result))))
|
||||||
|
|
||||||
|
;; The list of candidates that a second Tab writes, each row ended by "\n".
|
||||||
|
;; - Without descriptions: in columns, as `columns`.
|
||||||
|
;; - With descriptions: one candidate a row, its description after it,
|
||||||
|
;; dimmed and cut to fit `width`. The candidates' column is as wide as
|
||||||
|
;; the widest, up to half of `width`.
|
||||||
|
(: (-> (Vec string) (Vec string) int string))
|
||||||
|
(defun (completion-listing items descriptions width)
|
||||||
|
(if (or (vec-empty? descriptions) (all? string-empty? descriptions))
|
||||||
|
(columns items width)
|
||||||
|
(let* ((widest (loop (:for item (in-vec items))
|
||||||
|
(:acc w (folding 0 (max w (text-width item))))))
|
||||||
|
(cell (min (+ widest 2) (max 1 (quotient width 2))))
|
||||||
|
(out (stringbuilder-empty)))
|
||||||
|
(loop (:for item (in-vec items))
|
||||||
|
(:for i (range 0 (vec-length items)))
|
||||||
|
(:do (def d (if (< i (vec-length descriptions)) (vec-ref descriptions i) ""))
|
||||||
|
(def w (text-width item))
|
||||||
|
(stringbuilder-add-string! out item)
|
||||||
|
(unless (string-empty? d)
|
||||||
|
(def room (- width (max cell (+ w 2))))
|
||||||
|
(when (> room 3)
|
||||||
|
(stringbuilder-add-string! out (string-pad-left "" (max 2 (- cell w))))
|
||||||
|
(stringbuilder-add-string! out (styled (fit-width d room) ghost-style))))
|
||||||
|
(stringbuilder-add-string! out "\n")))
|
||||||
|
(stringbuilder->string out))))
|
||||||
|
|
||||||
|
;; `s` cut to `width` columns, with … at the end when it is cut.
|
||||||
|
(: (-> string int string))
|
||||||
|
(defun (fit-width s width)
|
||||||
|
(if (<= (text-width s) width)
|
||||||
|
s
|
||||||
|
(let go ((cs (vec->list (text-clusters s))) (w 0) (out []))
|
||||||
|
(match cs
|
||||||
|
(Nil (string-join out ""))
|
||||||
|
((Cons c more)
|
||||||
|
(def cw (cluster-width c))
|
||||||
|
(if (> (+ w cw) (- width 1))
|
||||||
|
(str (string-join out "") "\u{2026}")
|
||||||
|
(go more (+ w cw) (vec-add out c))))))))
|
||||||
|
|
||||||
;; `items` in columns as wide as the widest, filling rows first, each row
|
;; `items` in columns as wide as the widest, filling rows first, each row
|
||||||
;; ended by "\n".
|
;; ended by "\n".
|
||||||
(: (-> (Vec string) int string))
|
(: (-> (Vec string) int string))
|
||||||
|
|
@ -408,7 +464,7 @@
|
||||||
((Bell next) (go next (+ i 1)))
|
((Bell next) (go next (+ i 1)))
|
||||||
((ClearScreen next) (go next (+ i 1)))
|
((ClearScreen next) (go next (+ i 1)))
|
||||||
((SuspendNow next) (go next (+ i 1)))
|
((SuspendNow next) (go next (+ i 1)))
|
||||||
((ShowList next _) (go next (+ i 1)))))))
|
((ShowList next _ _) (go next (+ i 1)))))))
|
||||||
|
|
||||||
;; ===========================================================================
|
;; ===========================================================================
|
||||||
;; Layout
|
;; Layout
|
||||||
|
|
@ -585,9 +641,9 @@
|
||||||
;; Editing
|
;; Editing
|
||||||
;; ===========================================================================
|
;; ===========================================================================
|
||||||
|
|
||||||
;; The suggestion for `st`: the text after the input, not empty. None during
|
;; The suggestion for `st`, with a ghost that is not empty. None during
|
||||||
;; a search, when the cursor is not at the end, or the input is empty.
|
;; a search, when the cursor is not at the end, or the input is empty.
|
||||||
(: (-> LineEditor State (Option string)))
|
(: (-> LineEditor State (Option Suggestion)))
|
||||||
(defun (suggestion ed st)
|
(defun (suggestion ed st)
|
||||||
(def text (record-ref st text))
|
(def text (record-ref st text))
|
||||||
(match (record-ref ed suggester)
|
(match (record-ref ed suggester)
|
||||||
|
|
@ -598,18 +654,21 @@
|
||||||
(not (= (record-ref st cursor) (vec-length text))))
|
(not (= (record-ref st cursor) (vec-length text))))
|
||||||
None
|
None
|
||||||
(match (f (joined text) (record-ref ed history))
|
(match (f (joined text) (record-ref ed history))
|
||||||
((Some s) (if (string-empty? s) None (Some s)))
|
((Some s) (if (string-empty? (record-ref s ghost)) None (Some s)))
|
||||||
(None None))))))
|
(None None))))))
|
||||||
|
|
||||||
;; At the end of the input with a suggestion, adds all of it, or with
|
;; At the end of the input with a suggestion, accepts all of it, or with
|
||||||
;; `word?` up to the end of its first word. Else `otherwise`.
|
;; `word?` up to the end of the ghost's first word. Else `otherwise`.
|
||||||
(: (-> LineEditor State bool (-> Step) Step))
|
(: (-> LineEditor State bool (-> Step) Step))
|
||||||
(defun (accept-suggestion ed st word? otherwise)
|
(defun (accept-suggestion ed st word? otherwise)
|
||||||
(match (suggestion ed st)
|
(match (suggestion ed st)
|
||||||
(None (otherwise))
|
(None (otherwise))
|
||||||
((Some s)
|
((Some sg)
|
||||||
(if (not word?)
|
(def s (record-ref sg ghost))
|
||||||
(Edited (insert st s))
|
(def line (record-ref sg line))
|
||||||
|
(if (or (not word?) (not (= line (str (joined (record-ref st text)) s))))
|
||||||
|
(let ((cs (clusters line)))
|
||||||
|
(Edited (record-set st (text cs) (cursor (vec-length cs)))))
|
||||||
(let* ((cs (clusters s))
|
(let* ((cs (clusters s))
|
||||||
(n (vec-length cs))
|
(n (vec-length cs))
|
||||||
(gap (let go ((i 0))
|
(gap (let go ((i 0))
|
||||||
|
|
@ -782,7 +841,7 @@
|
||||||
(def common (common-prefix candidates))
|
(def common (common-prefix candidates))
|
||||||
(cond
|
(cond
|
||||||
((> (vec-length (clusters common)) k) (Edited (replace-with common)))
|
((> (vec-length (clusters common)) k) (Edited (replace-with common)))
|
||||||
(tabbed? (ShowList st candidates))
|
(tabbed? (ShowList st candidates (record-ref c descriptions)))
|
||||||
(else (Bell (record-set st (tabbed? #t))))))))))
|
(else (Bell (record-set st (tabbed? #t))))))))))
|
||||||
|
|
||||||
;; The next older match of `query` from history index `from` down, or None.
|
;; The next older match of `query` from history index `from` down, or None.
|
||||||
|
|
|
||||||
|
|
@ -43,15 +43,17 @@
|
||||||
(go (+ i 1) (vec-append out (vec-ref parts i))))))
|
(go (+ i 1) (vec-append out (vec-ref parts i))))))
|
||||||
|
|
||||||
;; Suggests "/usr/local/share" for a line that ends with a prefix of it.
|
;; 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)
|
(defun (suggest-path text history)
|
||||||
(def full "/usr/local/share")
|
(def full "/usr/local/share")
|
||||||
(def word (match (string-split-once text " ")
|
(def word (match (string-split-once text " ")
|
||||||
((Some (Tuple _ w)) w)
|
((Some (Tuple _ w)) w)
|
||||||
(None "")))
|
(None "")))
|
||||||
(if (and (not (string-empty? word)) (string-starts-with? full word) (not (= word full)))
|
(if (and (not (string-empty? word)) (string-starts-with? full word) (not (= word full)))
|
||||||
(Some (string-drop full (string-length word)))
|
(Some (suggestion-after text (string-drop full (string-length word))))
|
||||||
None))
|
(if (= text "cd lo")
|
||||||
|
(Some (Suggestion (ghost " -> /usr/local") (line "cd /usr/local")))
|
||||||
|
None)))
|
||||||
|
|
||||||
;; ---- running the editor ----------------------------------------------------
|
;; ---- running the editor ----------------------------------------------------
|
||||||
|
|
||||||
|
|
@ -96,7 +98,8 @@
|
||||||
(def word (if (vec-empty? parts) "" (vec-ref parts (- (vec-length parts) 1))))
|
(def word (if (vec-empty? parts) "" (vec-ref parts (- (vec-length parts) 1))))
|
||||||
(Completion (prefix word)
|
(Completion (prefix word)
|
||||||
(candidates (vec-map (fun (w) (str w " "))
|
(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)
|
(defun (main args)
|
||||||
;; Typing and accepting.
|
;; Typing and accepting.
|
||||||
|
|
@ -235,6 +238,12 @@
|
||||||
(expect "Right before the end moves"
|
(expect "Right before the end moves"
|
||||||
(run-on suggesting (keys [(typed "cd /usr/l") [(key KeyLeft) (key KeyRight)] (typed "x")]))
|
(run-on suggesting (keys [(typed "cd /usr/l") [(key KeyLeft) (key KeyRight)] (typed "x")]))
|
||||||
"buffer \"cd /usr/lx\"")
|
"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"
|
(expect "Right with no suggestion does nothing at the end"
|
||||||
(run-on suggesting (keys [(typed "ls") [(key KeyRight)]]))
|
(run-on suggesting (keys [(typed "ls") [(key KeyRight)]]))
|
||||||
"buffer \"ls\"")
|
"buffer \"ls\"")
|
||||||
|
|
@ -264,6 +273,15 @@
|
||||||
(simulate-line (make-line-editor) (keys [(typed "!") [enter]]) #:initial "hi")
|
(simulate-line (make-line-editor) (keys [(typed "!") [enter]]) #:initial "hi")
|
||||||
(Tuple (Some (LineText "hi!")) "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.
|
;; The history file.
|
||||||
(def path (path-combine (temp-dir) "bjoterm-history-test.txt"))
|
(def path (path-combine (temp-dir) "bjoterm-history-test.txt"))
|
||||||
(def saved (make-line-editor))
|
(def saved (make-line-editor))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue