diff --git a/manifest.bjodat b/manifest.bjodat index 5d0fa9e..9d9d78b 100644 --- a/manifest.bjodat +++ b/manifest.bjodat @@ -1,6 +1,6 @@ (package (name (bjoterm)) - (version "0.1.2") + (version "0.2.0") (description "The terminal for Bjolang programs: raw mode, key events and escape sequences. Linux.") (license "MPL-2.0") diff --git a/src/line.bjo b/src/line.bjo index 3919663..53840bc 100644 --- a/src/line.bjo +++ b/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. diff --git a/tests/line.bjo b/tests/line.bjo index af883a4..2296a06 100644 --- a/tests/line.bjo +++ b/tests/line.bjo @@ -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))