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
2b7749013d
3 changed files with 104 additions and 27 deletions
103
src/line.bjo
103
src/line.bjo
|
|
@ -29,7 +29,8 @@
|
|||
;; - The history belongs to the editor. Accepted lines that are not empty
|
||||
;; and not the same as the last one are added to it.
|
||||
;; - `#: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
|
||||
;; and returns `LineKey`.
|
||||
;; - The design is in plan.org, phase 2.
|
||||
|
|
@ -40,10 +41,10 @@
|
|||
(import (bjoterm text))
|
||||
|
||||
(export
|
||||
LineEditor LineResult Completion
|
||||
LineEditor LineResult Completion Suggestion suggestion-after
|
||||
make-line-editor line-read
|
||||
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
|
||||
|
|
@ -63,12 +64,27 @@
|
|||
;; the word being written. It must be a suffix of the text.
|
||||
;; - `candidates`: each a whole replacement for `prefix`. A candidate can end
|
||||
;; 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.
|
||||
(type (: LineEditor #:opaque
|
||||
(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)))
|
||||
(: input-done? (Option (-> string bool)))
|
||||
(: history-size int)
|
||||
|
|
@ -101,7 +117,7 @@
|
|||
;; What one key does.
|
||||
(type (: Step (Union (: Edited State)
|
||||
(: Finished State LineResult)
|
||||
(: ShowList State (Vec string))
|
||||
(: ShowList State (Vec string) (Vec string))
|
||||
(: ClearScreen State)
|
||||
(: SuspendNow State)
|
||||
(: Bell State))))
|
||||
|
|
@ -115,8 +131,8 @@
|
|||
;; - `#:input-done?`: called by Enter with the whole input. #f makes Enter
|
||||
;; start a new line; a REPL returns #f while brackets are open.
|
||||
;; - `#: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
|
||||
;; show after the input, or None. Called after each key, so it must be fast.
|
||||
;; first, while the cursor is at the end of the input. Returns a
|
||||
;; `Suggestion`, or None. Called after each key, so it must be fast.
|
||||
;; - `#:command-key?`: called with each key and the whole input. #t makes
|
||||
;; `line-read` return `(LineKey key input)`.
|
||||
;; - `#: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
|
||||
;; the input above it.
|
||||
(: (-> (#:completer (Option (-> string Completion)))
|
||||
(#:suggester (Option (-> string (Vec string) (Option string))))
|
||||
(#:suggester (Option (-> string (Vec string) (Option Suggestion))))
|
||||
(#:command-key? (Option (-> TermEvent string bool)))
|
||||
(#:input-done? (Option (-> string bool)))
|
||||
(#:history-size int)
|
||||
|
|
@ -297,7 +313,7 @@
|
|||
(def ghost (if whole?
|
||||
[]
|
||||
(match (suggestion ed st)
|
||||
((Some s) (clusters s))
|
||||
((Some s) (clusters (record-ref s ghost)))
|
||||
(None []))))
|
||||
(def lay (layout-state st prompt cont width ghost))
|
||||
(def shown (window-of lay width (if whole? None (Some height)) top))
|
||||
|
|
@ -348,9 +364,9 @@
|
|||
(leave lay)
|
||||
(terminal-suspend term)
|
||||
(go next))
|
||||
((ShowList next items)
|
||||
((ShowList next items descriptions)
|
||||
(leave lay)
|
||||
(write-string out (columns items (fst (terminal-size term))))
|
||||
(write-string out (completion-listing items descriptions (fst (terminal-size term))))
|
||||
(go next))
|
||||
((Finished _ (LineKey ev text))
|
||||
(def (Tuple width _) (terminal-size term))
|
||||
|
|
@ -370,6 +386,46 @@
|
|||
(_ unit))
|
||||
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
|
||||
;; ended by "\n".
|
||||
(: (-> (Vec string) int string))
|
||||
|
|
@ -408,7 +464,7 @@
|
|||
((Bell next) (go next (+ i 1)))
|
||||
((ClearScreen next) (go next (+ i 1)))
|
||||
((SuspendNow next) (go next (+ i 1)))
|
||||
((ShowList next _) (go next (+ i 1)))))))
|
||||
((ShowList next _ _) (go next (+ i 1)))))))
|
||||
|
||||
;; ===========================================================================
|
||||
;; Layout
|
||||
|
|
@ -585,9 +641,9 @@
|
|||
;; 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.
|
||||
(: (-> LineEditor State (Option string)))
|
||||
(: (-> LineEditor State (Option Suggestion)))
|
||||
(defun (suggestion ed st)
|
||||
(def text (record-ref st text))
|
||||
(match (record-ref ed suggester)
|
||||
|
|
@ -598,18 +654,21 @@
|
|||
(not (= (record-ref st cursor) (vec-length text))))
|
||||
None
|
||||
(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))))))
|
||||
|
||||
;; At the end of the input with a suggestion, adds all of it, or with
|
||||
;; `word?` up to the end of its first word. Else `otherwise`.
|
||||
;; At the end of the input with a suggestion, accepts all of it, or with
|
||||
;; `word?` up to the end of the ghost's first word. Else `otherwise`.
|
||||
(: (-> LineEditor State bool (-> Step) Step))
|
||||
(defun (accept-suggestion ed st word? otherwise)
|
||||
(match (suggestion ed st)
|
||||
(None (otherwise))
|
||||
((Some s)
|
||||
(if (not word?)
|
||||
(Edited (insert st s))
|
||||
((Some sg)
|
||||
(def s (record-ref sg ghost))
|
||||
(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))
|
||||
(n (vec-length cs))
|
||||
(gap (let go ((i 0))
|
||||
|
|
@ -782,7 +841,7 @@
|
|||
(def common (common-prefix candidates))
|
||||
(cond
|
||||
((> (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))))))))))
|
||||
|
||||
;; The next older match of `query` from history index `from` down, or None.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue