Compare commits
No commits in common. "main" and "v0.1.0" have entirely different histories.
8 changed files with 85 additions and 588 deletions
|
|
@ -38,7 +38,6 @@
|
||||||
(match (line-read ed term (styled "λ> " [(StyleFg (Ansi 2)) StyleBold]) #:continuation (Some ".. "))
|
(match (line-read ed term (styled "λ> " [(StyleFg (Ansi 2)) StyleBold]) #:continuation (Some ".. "))
|
||||||
((LineText text) (println (str "read: " (written text))) (go))
|
((LineText text) (println (str "read: " (written text))) (go))
|
||||||
(LineCancelled (go))
|
(LineCancelled (go))
|
||||||
(LineEof unit)
|
(LineEof unit)))
|
||||||
((LineKey _ _) (go))))
|
|
||||||
(ignore (line-history-save ed history))
|
(ignore (line-history-save ed history))
|
||||||
0)
|
0)
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
(package
|
(package
|
||||||
(name (bjoterm))
|
(name (bjoterm))
|
||||||
(version "0.2.3")
|
(version "0.1.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")
|
||||||
|
|
||||||
|
|
|
||||||
462
src/line.bjo
462
src/line.bjo
|
|
@ -28,12 +28,6 @@
|
||||||
;; computes where the input starts then; see `#:reflow?`.
|
;; computes where the input starts then; see `#:reflow?`.
|
||||||
;; - 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.
|
|
||||||
;; Keys that move right at the end of the input accept it, which makes
|
|
||||||
;; the input the suggested line.
|
|
||||||
;; - `#:highlighter` draws parts of the input in styles of its choosing.
|
|
||||||
;; - `#:command-key?` hands keys to the caller: `line-read` erases the input
|
|
||||||
;; and returns `LineKey`.
|
|
||||||
;; - The design is in plan.org, phase 2.
|
;; - The design is in plan.org, phase 2.
|
||||||
|
|
||||||
(import (std prelude))
|
(import (std prelude))
|
||||||
|
|
@ -42,10 +36,10 @@
|
||||||
(import (bjoterm text))
|
(import (bjoterm text))
|
||||||
|
|
||||||
(export
|
(export
|
||||||
LineEditor LineResult Completion Suggestion suggestion-after Highlight
|
LineEditor LineResult Completion
|
||||||
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 completion-listing menu-lines cluster-styles)
|
simulate-line cursor-row-after-reflow input-window)
|
||||||
|
|
||||||
;; ===========================================================================
|
;; ===========================================================================
|
||||||
;; Types
|
;; Types
|
||||||
|
|
@ -55,45 +49,18 @@
|
||||||
;; - `LineText`: the accepted input. Lines are separated by "\n".
|
;; - `LineText`: the accepted input. Lines are separated by "\n".
|
||||||
;; - `LineEof`: Ctrl-D on an empty buffer, or the terminal hung up.
|
;; - `LineEof`: Ctrl-D on an empty buffer, or the terminal hung up.
|
||||||
;; - `LineCancelled`: Ctrl-C. The input is left on the screen with "^C".
|
;; - `LineCancelled`: Ctrl-C. The input is left on the screen with "^C".
|
||||||
;; - `LineKey`: a key that `#:command-key?` claimed, and the input when it
|
(type (: LineResult (Union (: LineText string) LineEof LineCancelled)))
|
||||||
;; was pressed. The input is erased from the screen, prompt included, so
|
|
||||||
;; the next `line-read` draws in its place.
|
|
||||||
(type (: LineResult (Union (: LineText string) LineEof LineCancelled (: LineKey TermEvent string))))
|
|
||||||
|
|
||||||
;; What a completer returns for the text before the cursor.
|
;; What a completer returns for the text before the cursor.
|
||||||
;; - `prefix`: the end of that text that the candidates replace, such as
|
;; - `prefix`: the end of that text that the candidates replace, such as
|
||||||
;; 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.
|
||||||
;; - `descriptions`: empty, or one per candidate, shown beside it in the
|
(type (: Completion (Record (: prefix string) (: candidates (Vec string)))))
|
||||||
;; 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))))
|
|
||||||
|
|
||||||
;; A part of the input to draw in `style`, from `#:highlighter`.
|
|
||||||
;; - `start` and `end`: character offsets into the input, as `string->vec`
|
|
||||||
;; counts them; `end` is not included.
|
|
||||||
;; - A later highlight over the same characters wins.
|
|
||||||
(type (: Highlight (Record (: start int) (: end int) (: style (Vec Style)))))
|
|
||||||
|
|
||||||
;; 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 Suggestion))))
|
|
||||||
(: highlighter (Option (-> string (Vec Highlight))))
|
|
||||||
(: command-key? (Option (-> TermEvent string bool)))
|
|
||||||
(: input-done? (Option (-> string bool)))
|
(: input-done? (Option (-> string bool)))
|
||||||
(: history-size int)
|
(: history-size int)
|
||||||
(: reflow? (Option bool))
|
(: reflow? (Option bool))
|
||||||
|
|
@ -108,33 +75,24 @@
|
||||||
(type (: Search (Record (: query string) (: at int) (: failed? bool)
|
(type (: Search (Record (: query string) (: at int) (: failed? bool)
|
||||||
(: before-text (Vec string)) (: before-cursor int))))
|
(: before-text (Vec string)) (: before-cursor int))))
|
||||||
|
|
||||||
;; The candidates that Tab shows below the input, to choose from.
|
|
||||||
;; - `items` and `descriptions`: from the `Completion`.
|
|
||||||
;; - `start` and `end`: the clusters that a choice replaces. `end` is the
|
|
||||||
;; end of the chosen candidate, or of the word as typed before the first.
|
|
||||||
;; - `selected`: the chosen item, or -1 before the first.
|
|
||||||
;; - `before-text` and `before-cursor`: the input when the menu opened,
|
|
||||||
;; restored by Escape and C-g.
|
|
||||||
(type (: Menu (Record (: items (Vec string)) (: descriptions (Vec string))
|
|
||||||
(: start int) (: end int) (: selected int)
|
|
||||||
(: before-text (Vec string)) (: before-cursor int))))
|
|
||||||
|
|
||||||
;; The state of one `line-read`.
|
;; The state of one `line-read`.
|
||||||
;; - `text`: grapheme clusters; "\n" separates lines.
|
;; - `text`: grapheme clusters; "\n" separates lines.
|
||||||
;; - `cursor`: an index into `text`, from 0 to its length.
|
;; - `cursor`: an index into `text`, from 0 to its length.
|
||||||
;; - `killing?`: the last key killed text, so the next kill adds to `kill`.
|
;; - `killing?`: the last key killed text, so the next kill adds to `kill`.
|
||||||
;; - `hist-at`: the history entry shown; the history's length for the
|
;; - `hist-at`: the history entry shown; the history's length for the
|
||||||
;; input being written, which is kept in `draft` meanwhile.
|
;; input being written, which is kept in `draft` meanwhile.
|
||||||
;; - `menu`: the candidates of Tab, shown below the input while it is open.
|
;; - `tabbed?`: the last key was Tab and completed nothing more, so the next
|
||||||
|
;; Tab lists the candidates.
|
||||||
(type (: State (Record (: text (Vec string)) (: cursor int)
|
(type (: State (Record (: text (Vec string)) (: cursor int)
|
||||||
(: kill string) (: killing? bool)
|
(: kill string) (: killing? bool)
|
||||||
(: hist-at int) (: draft (Vec string))
|
(: hist-at int) (: draft (Vec string))
|
||||||
(: search (Option Search))
|
(: search (Option Search))
|
||||||
(: menu (Option Menu)))))
|
(: tabbed? bool))))
|
||||||
|
|
||||||
;; 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))
|
||||||
(: ClearScreen State)
|
(: ClearScreen State)
|
||||||
(: SuspendNow State)
|
(: SuspendNow State)
|
||||||
(: Bell State))))
|
(: Bell State))))
|
||||||
|
|
@ -147,13 +105,6 @@
|
||||||
;; - `#:completer`: called by Tab with the text before the cursor.
|
;; - `#:completer`: called by Tab with the text before the cursor.
|
||||||
;; - `#: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
|
|
||||||
;; 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.
|
|
||||||
;; - `#:highlighter`: called with the whole input after each key. Returns
|
|
||||||
;; the parts to draw styled; see `Highlight`. 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.
|
;; - `#:history-size`: the most entries kept; the oldest go first.
|
||||||
;; - `#:suspend?`: whether C-z stops the program. A shell sets #f; C-z
|
;; - `#:suspend?`: whether C-z stops the program. A shell sets #f; C-z
|
||||||
;; then rings the bell.
|
;; then rings the bell.
|
||||||
|
|
@ -162,18 +113,14 @@
|
||||||
;; 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 Suggestion))))
|
|
||||||
(#:highlighter (Option (-> string (Vec Highlight))))
|
|
||||||
(#:command-key? (Option (-> TermEvent string bool)))
|
|
||||||
(#:input-done? (Option (-> string bool)))
|
(#:input-done? (Option (-> string bool)))
|
||||||
(#:history-size int)
|
(#:history-size int)
|
||||||
(#:reflow? (Option bool))
|
(#:reflow? (Option bool))
|
||||||
(#:suspend? bool)
|
(#:suspend? bool)
|
||||||
LineEditor))
|
LineEditor))
|
||||||
(defun (make-line-editor #:completer None #:suggester None #:highlighter None #:command-key? None #:input-done? None
|
(defun (make-line-editor #:completer None #:input-done? None #:history-size 1000 #:reflow? None
|
||||||
#:history-size 1000 #:reflow? None #:suspend? #t)
|
#:suspend? #t)
|
||||||
(LineEditor (completer completer) (suggester suggester) (highlighter highlighter) (command-key? command-key?)
|
(LineEditor (completer completer) (input-done? input-done?)
|
||||||
(input-done? input-done?)
|
|
||||||
(history-size history-size) (reflow? reflow?) (suspend? suspend?) (history [])))
|
(history-size history-size) (reflow? reflow?) (suspend? suspend?) (history [])))
|
||||||
|
|
||||||
;; The history, oldest first.
|
;; The history, oldest first.
|
||||||
|
|
@ -240,9 +187,8 @@
|
||||||
|
|
||||||
;; Reads one input from the terminal, with editing.
|
;; Reads one input from the terminal, with editing.
|
||||||
;; - `prompt` may contain escape sequences, such as colors; they take no
|
;; - `prompt` may contain escape sequences, such as colors; they take no
|
||||||
;; columns. It may contain line breaks: the input starts after its last
|
;; columns. `#:continuation` is the prompt of the lines after the first;
|
||||||
;; line. `#:continuation` is the prompt of the lines after the first;
|
;; by default, spaces as wide as `prompt`.
|
||||||
;; by default, spaces as wide as the last line of `prompt`.
|
|
||||||
;; - Sets raw mode for the time it reads, unless the terminal is in raw
|
;; - Sets raw mode for the time it reads, unless the terminal is in raw
|
||||||
;; mode already.
|
;; mode already.
|
||||||
;; - In a bjoroutine it suspends while it waits for keys; elsewhere it parks
|
;; - In a bjoroutine it suspends while it waits for keys; elsewhere it parks
|
||||||
|
|
@ -255,13 +201,8 @@
|
||||||
;; M-d (the word after); C-y yanks. C-t swaps two characters.
|
;; M-d (the word after); C-y yanks. C-t swaps two characters.
|
||||||
;; - History: Up and Down on the first and last line, C-p, C-n; C-r
|
;; - History: Up and Down on the first and last line, C-p, C-n; C-r
|
||||||
;; searches back, again C-r for an older match, C-g ends the search.
|
;; searches back, again C-r for an older match, C-g ends the search.
|
||||||
;; - Tab completes with `#:completer`: it inserts the candidates' common
|
;; - Tab completes with `#:completer`, and a second Tab lists the
|
||||||
;; prefix, or, when that adds nothing, opens a menu of them below the
|
;; candidates. Without a completer it inserts a tab.
|
||||||
;; input, as fish does. Without a completer it inserts a tab. In the
|
|
||||||
;; menu, Tab and Shift-Tab choose the next and the one before, Escape
|
|
||||||
;; restores the input, and another key closes it (see `menu-step`).
|
|
||||||
;; - A suggestion from `#:suggester`: at the end of the input, Right, C-f,
|
|
||||||
;; End and C-e accept all of it; M-f, C-Right and M-Right one word.
|
|
||||||
;; - Enter accepts (see `#:input-done?`), M-Enter and C-j start a new line,
|
;; - Enter accepts (see `#:input-done?`), M-Enter and C-j start a new line,
|
||||||
;; C-l clears the screen, C-z stops the program (see `#:suspend?`),
|
;; C-l clears the screen, C-z stops the program (see `#:suspend?`),
|
||||||
;; C-c cancels, and C-d on an empty buffer is end of input.
|
;; C-c cancels, and C-d on an empty buffer is end of input.
|
||||||
|
|
@ -269,46 +210,31 @@
|
||||||
;; `notice-poll-ms` while it waits for a key. The lines it returns are
|
;; `notice-poll-ms` while it waits for a key. The lines it returns are
|
||||||
;; written above the input, which is then drawn again below them. For a
|
;; written above the input, which is then drawn again below them. For a
|
||||||
;; shell's "[1] Done" lines. It must not write to the terminal itself.
|
;; shell's "[1] Done" lines. It must not write to the terminal itself.
|
||||||
;; - `#:initial`: the input to start with, the cursor at its end.
|
|
||||||
(: (-> LineEditor Terminal string (#:continuation (Option string)) (#:notices (Option (-> (Vec string))))
|
(: (-> LineEditor Terminal string (#:continuation (Option string)) (#:notices (Option (-> (Vec string))))
|
||||||
(#:initial string)
|
|
||||||
LineResult))
|
LineResult))
|
||||||
(defun (line-read ed term prompt #:continuation None #:notices None #:initial "")
|
(defun (line-read ed term prompt #:continuation None #:notices None)
|
||||||
(def cont (match continuation
|
(def cont (match continuation
|
||||||
((Some c) c)
|
((Some c) c)
|
||||||
(None (string-pad-left "" (text-width (last-line prompt))))))
|
(None (string-pad-left "" (text-width prompt)))))
|
||||||
(if (terminal-raw? term)
|
(if (terminal-raw? term)
|
||||||
(edit-loop ed term prompt cont notices initial)
|
(edit-loop ed term prompt cont notices)
|
||||||
;; A `fun` is ordinary code, so the suspending copy passes a
|
;; A `fun` is ordinary code, so the suspending copy passes a
|
||||||
;; bjoroutine.
|
;; bjoroutine.
|
||||||
(by-colour (#:sync (with-raw-mode term (fun () (edit-loop ed term prompt cont notices initial))))
|
(by-colour (#:sync (with-raw-mode term (fun () (edit-loop ed term prompt cont notices))))
|
||||||
(#:bjo (with-raw-mode term (bjoroutine () (edit-loop ed term prompt cont notices initial)))))))
|
(#:bjo (with-raw-mode term (bjoroutine () (edit-loop ed term prompt cont notices)))))))
|
||||||
|
|
||||||
;; How a suggestion is drawn: bright black, which most themes show as grey.
|
|
||||||
(: (Vec Style))
|
|
||||||
(def ghost-style [(StyleFg (Ansi 8))])
|
|
||||||
|
|
||||||
;; How often `line-read` calls its `#:notices` function, in milliseconds.
|
;; How often `line-read` calls its `#:notices` function, in milliseconds.
|
||||||
(: notice-poll-ms int)
|
(: notice-poll-ms int)
|
||||||
(def notice-poll-ms 200)
|
(def notice-poll-ms 200)
|
||||||
|
|
||||||
;; The text after the last line break of `s`, or all of it.
|
(: (-> LineEditor State))
|
||||||
(: (-> string string))
|
(defun (start-state ed)
|
||||||
(defun (last-line s)
|
(State (text []) (cursor 0) (kill "") (killing? #f)
|
||||||
(let go ((rest s))
|
|
||||||
(match (string-split-once rest "\n")
|
|
||||||
((Some (Tuple _ after)) (go after))
|
|
||||||
(None rest))))
|
|
||||||
|
|
||||||
(: (-> LineEditor string State))
|
|
||||||
(defun (start-state ed initial)
|
|
||||||
(def text (clusters initial))
|
|
||||||
(State (text text) (cursor (vec-length text)) (kill "") (killing? #f)
|
|
||||||
(hist-at (vec-length (record-ref ed history))) (draft [])
|
(hist-at (vec-length (record-ref ed history))) (draft [])
|
||||||
(search None) (menu None)))
|
(search None) (tabbed? #f)))
|
||||||
|
|
||||||
(: (-> LineEditor Terminal string string (Option (-> (Vec string))) string LineResult))
|
(: (-> LineEditor Terminal string string (Option (-> (Vec string))) LineResult))
|
||||||
(defun (edit-loop ed term prompt cont notices initial)
|
(defun (edit-loop ed term prompt cont notices)
|
||||||
(def out (parameter-ref current-output-port))
|
(def out (parameter-ref current-output-port))
|
||||||
(def reflow? (match (record-ref ed reflow?)
|
(def reflow? (match (record-ref ed reflow?)
|
||||||
((Some r) r)
|
((Some r) r)
|
||||||
|
|
@ -330,26 +256,10 @@
|
||||||
(record-ref l cursor-col) width)
|
(record-ref l cursor-col) width)
|
||||||
(record-ref l cursor-row)))))
|
(record-ref l cursor-row)))))
|
||||||
;; Draws `st`: the rows that fit on the screen, or all when `whole?`.
|
;; Draws `st`: the rows that fit on the screen, or all when `whole?`.
|
||||||
;; The suggestion is drawn unless `whole?`.
|
|
||||||
(defun (draw st whole?)
|
(defun (draw st whole?)
|
||||||
(def (Tuple width height) (terminal-size term))
|
(def (Tuple width height) (terminal-size term))
|
||||||
(def ghost (if whole?
|
(def lay (layout-state st prompt cont width))
|
||||||
[]
|
(def shown (window-of lay width (if whole? None (Some height)) top))
|
||||||
(match (suggestion ed st)
|
|
||||||
((Some s) (clusters (record-ref s ghost)))
|
|
||||||
(None []))))
|
|
||||||
(def lay (layout-state st prompt cont width ghost (cluster-styles ed (record-ref st text))))
|
|
||||||
(def input (window-of lay width (if whole? None (Some height)) top))
|
|
||||||
;; The menu, below the input, in the rows the screen has left.
|
|
||||||
(def menu-rows
|
|
||||||
(match (if whole? None (record-ref st menu))
|
|
||||||
((Some m) (menu-lines (record-ref m items) (record-ref m descriptions) (record-ref m selected)
|
|
||||||
width (Some (max 1 (- height (vec-length (record-ref input rows)) 1)))))
|
|
||||||
(None [])))
|
|
||||||
(def shown (record-set input
|
|
||||||
(text (str (record-ref input text)
|
|
||||||
(string-join (vec-map (fun (l) (str "\n" l)) menu-rows) "")))
|
|
||||||
(rows (vec-append (record-ref input rows) (vec-map (fun (_) [1]) menu-rows)))))
|
|
||||||
(write-string out (str "\r" (cursor-up (rows-up width)) clear-below
|
(write-string out (str "\r" (cursor-up (rows-up width)) clear-below
|
||||||
(record-ref shown text)
|
(record-ref shown text)
|
||||||
(cursor-up (- (last-row shown) (record-ref shown cursor-row)))
|
(cursor-up (- (last-row shown) (record-ref shown cursor-row)))
|
||||||
|
|
@ -384,7 +294,7 @@
|
||||||
(set! drawn None)
|
(set! drawn None)
|
||||||
(ignore (draw st #f)))
|
(ignore (draw st #f)))
|
||||||
(poll)))))))
|
(poll)))))))
|
||||||
(let go ((st (start-state ed initial)))
|
(let go ((st (start-state ed)))
|
||||||
(def lay (draw st #f))
|
(def lay (draw st #f))
|
||||||
(match (line-step ed st (next-event st))
|
(match (line-step ed st (next-event st))
|
||||||
((Edited next) (go next))
|
((Edited next) (go next))
|
||||||
|
|
@ -397,17 +307,13 @@
|
||||||
(leave lay)
|
(leave lay)
|
||||||
(terminal-suspend term)
|
(terminal-suspend term)
|
||||||
(go next))
|
(go next))
|
||||||
((Finished _ (LineKey ev text))
|
((ShowList next items)
|
||||||
(def (Tuple width _) (terminal-size term))
|
(leave lay)
|
||||||
(write-string out (str "\r" (cursor-up (rows-up width)) clear-below))
|
(write-string out (columns items (fst (terminal-size term))))
|
||||||
(set! drawn None)
|
(go next))
|
||||||
(flush-port out)
|
|
||||||
(LineKey ev text))
|
|
||||||
((Finished last result)
|
((Finished last result)
|
||||||
(def shown (draw (record-set last (search None) (cursor (vec-length (record-ref last text)))) #t))
|
(def shown (draw (record-set last (search None) (cursor (vec-length (record-ref last text)))) #t))
|
||||||
(match result
|
(when (= result LineCancelled) (write-string out "^C"))
|
||||||
(LineCancelled (write-string out "^C"))
|
|
||||||
(_ unit))
|
|
||||||
(leave shown)
|
(leave shown)
|
||||||
(flush-port out)
|
(flush-port out)
|
||||||
(match result
|
(match result
|
||||||
|
|
@ -415,103 +321,31 @@
|
||||||
(_ unit))
|
(_ unit))
|
||||||
result))))
|
result))))
|
||||||
|
|
||||||
;; The rows of the menu of `items`.
|
;; `items` in columns as wide as the widest, filling rows first, each row
|
||||||
;; - Names are shown without the directory part that all of them have, so
|
;; ended by "\n".
|
||||||
;; that "src/a.bjo" and "src/b.bjo" show as "a.bjo" and "b.bjo".
|
(: (-> (Vec string) int string))
|
||||||
;; - Without descriptions: in columns as wide as the widest, filling rows
|
(defun (columns items width)
|
||||||
;; first. With them: one item a row, its description after it, dimmed
|
(def widest (let go ((i 0) (w 0))
|
||||||
;; and cut to fit `width`, in a column as wide as the widest item, up
|
(if (= i (vec-length items))
|
||||||
;; to half of `width`.
|
w
|
||||||
;; - `selected` is shown in reverse; -1 shows none.
|
(go (+ i 1) (max w (text-width (vec-ref items i)))))))
|
||||||
;; - With `max-rows`, a longer menu shows the rows around the selected
|
(def cell (+ widest 2))
|
||||||
;; item, and a last row that says how many more there are.
|
(def per-row (max 1 (quotient width cell)))
|
||||||
(: (-> (Vec string) (Vec string) int int (Option int) (Vec string)))
|
(def out (stringbuilder-empty))
|
||||||
(defun (menu-lines items descriptions selected width max-rows)
|
(loop (:for item (in-vec items))
|
||||||
(def names (shown-names items))
|
(:for i (range 0 (vec-length items)))
|
||||||
(def n (vec-length names))
|
(:do (stringbuilder-add-string! out item)
|
||||||
(def described? (not (or (vec-empty? descriptions) (all? string-empty? descriptions))))
|
(if (or (= (remainder (+ i 1) per-row) 0) (= (+ i 1) (vec-length items)))
|
||||||
(def widest (loop (:for name (in-vec names))
|
(stringbuilder-add-string! out "\n")
|
||||||
(:acc w (folding 0 (max w (text-width name))))))
|
(stringbuilder-add-string! out (string-pad-left "" (- cell (text-width item)))))))
|
||||||
(def per-row (if described? 1 (max 1 (quotient width (+ widest 2)))))
|
(stringbuilder->string out))
|
||||||
(defun (name-at i)
|
|
||||||
(def name (fit-width (vec-ref names i) (max 1 (- width 1))))
|
|
||||||
(if (= i selected) (styled name [StyleReverse]) name))
|
|
||||||
(def rows
|
|
||||||
(if described?
|
|
||||||
(let ((cell (min (+ widest 2) (max 1 (quotient width 2)))))
|
|
||||||
(loop (:for i (range 0 n))
|
|
||||||
(:acc out (vecing
|
|
||||||
(let* ((d (if (< i (vec-length descriptions)) (vec-ref descriptions i) ""))
|
|
||||||
(w (text-width (vec-ref names i)))
|
|
||||||
(room (- width (max cell (+ w 2)))))
|
|
||||||
(if (or (string-empty? d) (<= room 3))
|
|
||||||
(name-at i)
|
|
||||||
(str (name-at i) (string-pad-left "" (max 2 (- cell w)))
|
|
||||||
(styled (fit-width d room) ghost-style))))))))
|
|
||||||
(loop (:for r (range 0 (quotient (+ n (- per-row 1)) per-row)))
|
|
||||||
(:acc out (vecing
|
|
||||||
(let ((last (min n (* (+ r 1) per-row))))
|
|
||||||
(string-join
|
|
||||||
(loop (:for i (range (* r per-row) last))
|
|
||||||
(:acc cells (vecing
|
|
||||||
(if (= (+ i 1) last)
|
|
||||||
(name-at i)
|
|
||||||
(str (name-at i)
|
|
||||||
(string-pad-left "" (- (+ widest 2) (text-width (vec-ref names i)))))))))
|
|
||||||
"")))))))
|
|
||||||
(def total (vec-length rows))
|
|
||||||
(match max-rows
|
|
||||||
((Some limit)
|
|
||||||
(if (<= total limit)
|
|
||||||
rows
|
|
||||||
(let* ((room (max 1 (- limit 1)))
|
|
||||||
(at (if (< selected 0) 0 (quotient selected per-row)))
|
|
||||||
(top (max 0 (min (- total room) (- at (- room 1))))))
|
|
||||||
(vec-add (vec-slice rows top room)
|
|
||||||
(styled (str "\u{2026} " (int->string (- total room)) " more rows") ghost-style)))))
|
|
||||||
(None rows)))
|
|
||||||
|
|
||||||
;; `items` without the directory part that all of them have, and without
|
|
||||||
;; a space at the end. A name that would be empty keeps its directory.
|
|
||||||
(: (-> (Vec string) (Vec string)))
|
|
||||||
(defun (shown-names items)
|
|
||||||
(def names (vec-map string-trim-right items))
|
|
||||||
(def common (if (vec-empty? names) "" (common-prefix names)))
|
|
||||||
(let cut ((dir (let ((parts (string-split common "/")))
|
|
||||||
(string-drop-right common (string-length (vec-ref parts (- (vec-length parts) 1)))))))
|
|
||||||
(cond
|
|
||||||
((string-empty? dir) names)
|
|
||||||
((any? (fun (name) (= name dir)) names)
|
|
||||||
(def shorter (string-drop-right dir 1))
|
|
||||||
(def parts (string-split shorter "/"))
|
|
||||||
(cut (string-drop-right shorter (string-length (vec-ref parts (- (vec-length parts) 1))))))
|
|
||||||
(else (vec-map (fun (name) (string-drop name (string-length dir))) names)))))
|
|
||||||
|
|
||||||
;; The menu of `items`, each row ended by "\n". See `menu-lines`.
|
|
||||||
(: (-> (Vec string) (Vec string) int string))
|
|
||||||
(defun (completion-listing items descriptions width)
|
|
||||||
(string-join (vec-map (fun (l) (str l "\n")) (menu-lines items descriptions -1 width None)) ""))
|
|
||||||
|
|
||||||
;; `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))))))))
|
|
||||||
|
|
||||||
;; Runs the editor on `events` without a terminal, as `line-read` would.
|
;; Runs the editor on `events` without a terminal, as `line-read` would.
|
||||||
;; Returns the result, or None when the events ran out first, and the
|
;; Returns the result, or None when the events ran out first, and the
|
||||||
;; buffer's text at the end. For tests.
|
;; buffer's text at the end. For tests.
|
||||||
(: (-> LineEditor (Vec TermEvent) (#:initial string) (Tuple (Option LineResult) string)))
|
(: (-> LineEditor (Vec TermEvent) (Tuple (Option LineResult) string)))
|
||||||
(defun (simulate-line ed events #:initial "")
|
(defun (simulate-line ed events)
|
||||||
(let go ((st (start-state ed initial))
|
(let go ((st (start-state ed))
|
||||||
(i 0))
|
(i 0))
|
||||||
(if (= i (vec-length events))
|
(if (= i (vec-length events))
|
||||||
(Tuple None (joined (record-ref st text)))
|
(Tuple None (joined (record-ref st text)))
|
||||||
|
|
@ -524,7 +358,8 @@
|
||||||
((Edited next) (go next (+ i 1)))
|
((Edited next) (go next (+ i 1)))
|
||||||
((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)))))))
|
||||||
|
|
||||||
;; ===========================================================================
|
;; ===========================================================================
|
||||||
;; Layout
|
;; Layout
|
||||||
|
|
@ -541,51 +376,20 @@
|
||||||
(type (: Layout (Record (: texts (Vec string)) (: rows (Vec (Vec int)))
|
(type (: Layout (Record (: texts (Vec string)) (: rows (Vec (Vec int)))
|
||||||
(: cursor-row int) (: cursor-col int))))
|
(: cursor-row int) (: cursor-col int))))
|
||||||
|
|
||||||
(: (-> State string string int (Vec string) (Vec (Vec Style)) Layout))
|
(: (-> State string string int Layout))
|
||||||
(defun (layout-state st prompt cont width ghost styles)
|
(defun (layout-state st prompt cont width)
|
||||||
(match (record-ref st search)
|
(match (record-ref st search)
|
||||||
((Some s)
|
((Some s)
|
||||||
(def label (str "(" (if (record-ref s failed?) "failed " "")
|
(def label (str "(" (if (record-ref s failed?) "failed " "")
|
||||||
"search `" (record-ref s query) "'): "))
|
"search `" (record-ref s query) "'): "))
|
||||||
(layout (prompt-clusters label) (prompt-clusters cont)
|
(layout (prompt-clusters label) (prompt-clusters cont)
|
||||||
(record-ref st text) (record-ref st cursor) width [] []))
|
(record-ref st text) (record-ref st cursor) width))
|
||||||
(None
|
(None
|
||||||
(layout (prompt-clusters prompt) (prompt-clusters cont)
|
(layout (prompt-clusters prompt) (prompt-clusters cont)
|
||||||
(record-ref st text) (record-ref st cursor) width ghost styles))))
|
(record-ref st text) (record-ref st cursor) width))))
|
||||||
|
|
||||||
;; The style of each cluster of `text`, from `#:highlighter`. Empty when
|
(: (-> (Vec string) (Vec string) (Vec string) int int Layout))
|
||||||
;; there is none.
|
(defun (layout prompt cont text cursor width)
|
||||||
(: (-> LineEditor (Vec string) (Vec (Vec Style))))
|
|
||||||
(defun (cluster-styles ed text)
|
|
||||||
(match (record-ref ed highlighter)
|
|
||||||
(None [])
|
|
||||||
((Some f)
|
|
||||||
(def highlights (f (joined text)))
|
|
||||||
(if (vec-empty? highlights)
|
|
||||||
[]
|
|
||||||
(let next ((k 0) (at 0) (out []))
|
|
||||||
(if (= k (vec-length text))
|
|
||||||
out
|
|
||||||
(next (+ k 1)
|
|
||||||
(+ at (vec-length (string->vec (vec-ref text k))))
|
|
||||||
(vec-add out (style-at highlights at)))))))))
|
|
||||||
|
|
||||||
;; The style of the last of `highlights` that covers character `at`.
|
|
||||||
(: (-> (Vec Highlight) int (Vec Style)))
|
|
||||||
(defun (style-at highlights at)
|
|
||||||
(let go ((i (- (vec-length highlights) 1)))
|
|
||||||
(cond
|
|
||||||
((< i 0) [])
|
|
||||||
(else
|
|
||||||
(def h (vec-ref highlights i))
|
|
||||||
(if (and (<= (record-ref h start) at) (< at (record-ref h end)))
|
|
||||||
(record-ref h style)
|
|
||||||
(go (- i 1)))))))
|
|
||||||
|
|
||||||
;; `ghost`: clusters drawn dimmed after `text`, where the cursor does not go.
|
|
||||||
;; `styles`: the style of each cluster of `text`, or empty for none.
|
|
||||||
(: (-> (Vec string) (Vec string) (Vec string) int int (Vec string) (Vec (Vec Style)) Layout))
|
|
||||||
(defun (layout prompt cont text cursor width ghost styles)
|
|
||||||
(def out (stringbuilder-empty))
|
(def out (stringbuilder-empty))
|
||||||
(def/mutable texts [])
|
(def/mutable texts [])
|
||||||
(def/mutable row 0)
|
(def/mutable row 0)
|
||||||
|
|
@ -608,12 +412,9 @@
|
||||||
(set! this-row (vec-add this-row w))
|
(set! this-row (vec-add this-row w))
|
||||||
(set! col (+ col w))
|
(set! col (+ col w))
|
||||||
(when (>= col width) (break!)))
|
(when (>= col width) (break!)))
|
||||||
;; A line break in a prompt ends its row, as one in the text does.
|
|
||||||
(defun (put-prompt! items)
|
(defun (put-prompt! items)
|
||||||
(loop (:for item (in-vec items))
|
(loop (:for item (in-vec items))
|
||||||
(:do (if (or (= item "\n") (= item "\r\n"))
|
(:do (put! (cluster-shown item) (cluster-width item)))))
|
||||||
(break!)
|
|
||||||
(put! (cluster-shown item) (cluster-width item))))))
|
|
||||||
(put-prompt! prompt)
|
(put-prompt! prompt)
|
||||||
(loop (:for c (in-vec text))
|
(loop (:for c (in-vec text))
|
||||||
(:for i (range 0 (vec-length text)))
|
(:for i (range 0 (vec-length text)))
|
||||||
|
|
@ -627,21 +428,10 @@
|
||||||
((= c "\t")
|
((= c "\t")
|
||||||
(def w (- 8 (remainder col 8)))
|
(def w (- 8 (remainder col 8)))
|
||||||
(put! (string-pad-left "" w) w))
|
(put! (string-pad-left "" w) w))
|
||||||
((< i (vec-length styles))
|
|
||||||
(put! (styled (cluster-shown c) (vec-ref styles i)) (cluster-width c)))
|
|
||||||
(else (put! (cluster-shown c) (cluster-width c))))))
|
(else (put! (cluster-shown c) (cluster-width c))))))
|
||||||
(when (= cursor (vec-length text))
|
(when (= cursor (vec-length text))
|
||||||
(set! cursor-row row)
|
(set! cursor-row row)
|
||||||
(set! cursor-col col))
|
(set! cursor-col col))
|
||||||
(loop (:for c (in-vec ghost))
|
|
||||||
(:do (cond
|
|
||||||
((or (= c "\n") (= c "\r\n"))
|
|
||||||
(break!)
|
|
||||||
(put-prompt! cont))
|
|
||||||
((= c "\t")
|
|
||||||
(def w (- 8 (remainder col 8)))
|
|
||||||
(put! (string-pad-left "" w) w))
|
|
||||||
(else (put! (styled (cluster-shown c) ghost-style) (cluster-width c))))))
|
|
||||||
(Layout (texts (vec-add texts (stringbuilder->string out)))
|
(Layout (texts (vec-add texts (stringbuilder->string out)))
|
||||||
(rows (vec-add rows this-row))
|
(rows (vec-add rows this-row))
|
||||||
(cursor-row cursor-row) (cursor-col cursor-col)))
|
(cursor-row cursor-row) (cursor-col cursor-col)))
|
||||||
|
|
@ -733,43 +523,6 @@
|
||||||
;; Editing
|
;; Editing
|
||||||
;; ===========================================================================
|
;; ===========================================================================
|
||||||
|
|
||||||
;; 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 Suggestion)))
|
|
||||||
(defun (suggestion ed st)
|
|
||||||
(def text (record-ref st text))
|
|
||||||
(match (record-ref ed suggester)
|
|
||||||
(None None)
|
|
||||||
((Some f)
|
|
||||||
(if (or (some? (record-ref st search))
|
|
||||||
(some? (record-ref st menu))
|
|
||||||
(vec-empty? text)
|
|
||||||
(not (= (record-ref st cursor) (vec-length text))))
|
|
||||||
None
|
|
||||||
(match (f (joined text) (record-ref ed history))
|
|
||||||
((Some s) (if (string-empty? (record-ref s ghost)) None (Some s)))
|
|
||||||
(None None))))))
|
|
||||||
|
|
||||||
;; 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 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))
|
|
||||||
(if (and (< i n) (not (word-cluster? (vec-ref cs i)))) (go (+ i 1)) i)))
|
|
||||||
(end (let go ((i gap))
|
|
||||||
(if (and (< i n) (word-cluster? (vec-ref cs i))) (go (+ i 1)) i))))
|
|
||||||
(Edited (insert st (joined (vec-slice cs 0 end)))))))))
|
|
||||||
|
|
||||||
(: (-> (Vec string) string))
|
(: (-> (Vec string) string))
|
||||||
(defun (joined text)
|
(defun (joined text)
|
||||||
(string-join text ""))
|
(string-join text ""))
|
||||||
|
|
@ -914,8 +667,8 @@
|
||||||
(Finished st (LineText text))
|
(Finished st (LineText text))
|
||||||
(Edited (insert st "\n"))))
|
(Edited (insert st "\n"))))
|
||||||
|
|
||||||
(: (-> LineEditor State Step))
|
(: (-> LineEditor State bool Step))
|
||||||
(defun (complete ed st)
|
(defun (complete ed st tabbed?)
|
||||||
(match (record-ref ed completer)
|
(match (record-ref ed completer)
|
||||||
(None (Edited (insert st "\t")))
|
(None (Edited (insert st "\t")))
|
||||||
((Some completer)
|
((Some completer)
|
||||||
|
|
@ -934,53 +687,8 @@
|
||||||
(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)))
|
||||||
(else
|
(tabbed? (ShowList st candidates))
|
||||||
(Edited (record-set st (menu (Some (Menu (items candidates) (descriptions (record-ref c descriptions))
|
(else (Bell (record-set st (tabbed? #t))))))))))
|
||||||
(start (- cursor k)) (end cursor) (selected -1)
|
|
||||||
(before-text (record-ref st text))
|
|
||||||
(before-cursor cursor)))))))))))))
|
|
||||||
|
|
||||||
;; What `ev` does while the menu is open.
|
|
||||||
;; - Tab and C-i choose the next item, Shift-Tab the one before.
|
|
||||||
;; - Escape and C-g close the menu and restore the input.
|
|
||||||
;; - Enter after a choice closes the menu and keeps it.
|
|
||||||
;; - Another key closes the menu, and does what it does.
|
|
||||||
(: (-> LineEditor State Menu TermEvent Step))
|
|
||||||
(defun (menu-step ed st m ev)
|
|
||||||
(def closed (record-set st (menu None)))
|
|
||||||
(match ev
|
|
||||||
((TermKey KeyTab (Mods (shift? back?) (alt? #f) (ctrl? #f))) (Edited (choose st m (if back? -1 1))))
|
|
||||||
((TermKey (KeyChar #\i) (Mods (alt? #f) (ctrl? #t))) (Edited (choose st m 1)))
|
|
||||||
((TermKey KeyEscape _) (Edited (restore-menu st m)))
|
|
||||||
((TermKey (KeyChar #\g) (Mods (alt? #f) (ctrl? #t))) (Edited (restore-menu st m)))
|
|
||||||
((TermKey KeyEnter (Mods (alt? #f)))
|
|
||||||
(if (< (record-ref m selected) 0) (editing ed closed ev) (Edited closed)))
|
|
||||||
((TermResized _ _) (Edited st))
|
|
||||||
(_ (editing ed closed ev))))
|
|
||||||
|
|
||||||
;; The input with the item `delta` steps from the chosen one in place of
|
|
||||||
;; the word.
|
|
||||||
(: (-> State Menu int State))
|
|
||||||
(defun (choose st m delta)
|
|
||||||
(def items (record-ref m items))
|
|
||||||
(def n (vec-length items))
|
|
||||||
(def selected (record-ref m selected))
|
|
||||||
(def i (if (< selected 0)
|
|
||||||
(if (> delta 0) 0 (- n 1))
|
|
||||||
(remainder (+ selected delta n) n)))
|
|
||||||
(def chosen (clusters (vec-ref items i)))
|
|
||||||
(def text (record-ref st text))
|
|
||||||
(def start (record-ref m start))
|
|
||||||
(def end (record-ref m end))
|
|
||||||
(def after (+ start (vec-length chosen)))
|
|
||||||
(record-set st (text (vec-append (vec-append (vec-slice text 0 start) chosen)
|
|
||||||
(vec-slice text end (- (vec-length text) end))))
|
|
||||||
(cursor after)
|
|
||||||
(menu (Some (record-set m (selected i) (end after))))))
|
|
||||||
|
|
||||||
(: (-> State Menu State))
|
|
||||||
(defun (restore-menu st m)
|
|
||||||
(record-set st (text (record-ref m before-text)) (cursor (record-ref m before-cursor)) (menu None)))
|
|
||||||
|
|
||||||
;; 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.
|
||||||
(: (-> LineEditor string int (Option (Tuple int int))))
|
(: (-> LineEditor string int (Option (Tuple int int))))
|
||||||
|
|
@ -1029,24 +737,14 @@
|
||||||
;; What `ev` does to `st`.
|
;; What `ev` does to `st`.
|
||||||
(: (-> LineEditor State TermEvent Step))
|
(: (-> LineEditor State TermEvent Step))
|
||||||
(defun (line-step ed prev ev)
|
(defun (line-step ed prev ev)
|
||||||
(def claimed? (and (none? (record-ref prev search))
|
|
||||||
(match (record-ref ed command-key?)
|
|
||||||
((Some f) (f ev (joined (record-ref prev text))))
|
|
||||||
(None #f))))
|
|
||||||
(cond
|
|
||||||
(claimed? (Finished prev (LineKey ev (joined (record-ref prev text)))))
|
|
||||||
(else
|
|
||||||
(match (record-ref prev search)
|
(match (record-ref prev search)
|
||||||
((Some s) (searching ed prev s ev))
|
((Some s) (searching ed prev s ev))
|
||||||
(None
|
(None (editing ed prev ev))))
|
||||||
(match (record-ref prev menu)
|
|
||||||
((Some m) (menu-step ed prev m ev))
|
|
||||||
(None (editing ed prev ev))))))))
|
|
||||||
|
|
||||||
(: (-> LineEditor State TermEvent Step))
|
(: (-> LineEditor State TermEvent Step))
|
||||||
(defun (editing ed prev ev)
|
(defun (editing ed prev ev)
|
||||||
;; The flags hold only for the key after the one that set them.
|
;; The flags hold only for the key after the one that set them.
|
||||||
(def st (record-set prev (killing? #f)))
|
(def st (record-set prev (killing? #f) (tabbed? #f)))
|
||||||
(def killing? (record-ref prev killing?))
|
(def killing? (record-ref prev killing?))
|
||||||
(def cursor (record-ref st cursor))
|
(def cursor (record-ref st cursor))
|
||||||
(def n (vec-length (record-ref st text)))
|
(def n (vec-length (record-ref st text)))
|
||||||
|
|
@ -1070,20 +768,16 @@
|
||||||
;; Moving.
|
;; Moving.
|
||||||
((Tuple KeyLeft (Tuple #f #f)) (Edited (move st (max 0 (- cursor 1)))))
|
((Tuple KeyLeft (Tuple #f #f)) (Edited (move st (max 0 (- cursor 1)))))
|
||||||
((Tuple (KeyChar #\b) (Tuple #t #f)) (Edited (move st (max 0 (- cursor 1)))))
|
((Tuple (KeyChar #\b) (Tuple #t #f)) (Edited (move st (max 0 (- cursor 1)))))
|
||||||
((Tuple KeyRight (Tuple #f #f))
|
((Tuple KeyRight (Tuple #f #f)) (Edited (move st (min n (+ cursor 1)))))
|
||||||
(accept-suggestion ed st #f (fun () (Edited (move st (min n (+ cursor 1)))))))
|
((Tuple (KeyChar #\f) (Tuple #t #f)) (Edited (move st (min n (+ cursor 1)))))
|
||||||
((Tuple (KeyChar #\f) (Tuple #t #f))
|
|
||||||
(accept-suggestion ed st #f (fun () (Edited (move st (min n (+ cursor 1)))))))
|
|
||||||
((Tuple KeyLeft _) (Edited (move st (word-back st))))
|
((Tuple KeyLeft _) (Edited (move st (word-back st))))
|
||||||
((Tuple (KeyChar #\b) (Tuple #f #t)) (Edited (move st (word-back st))))
|
((Tuple (KeyChar #\b) (Tuple #f #t)) (Edited (move st (word-back st))))
|
||||||
((Tuple KeyRight _) (accept-suggestion ed st #t (fun () (Edited (move st (word-forward st))))))
|
((Tuple KeyRight _) (Edited (move st (word-forward st))))
|
||||||
((Tuple (KeyChar #\f) (Tuple #f #t))
|
((Tuple (KeyChar #\f) (Tuple #f #t)) (Edited (move st (word-forward st))))
|
||||||
(accept-suggestion ed st #t (fun () (Edited (move st (word-forward st))))))
|
|
||||||
((Tuple KeyHome _) (Edited (move st (line-start st))))
|
((Tuple KeyHome _) (Edited (move st (line-start st))))
|
||||||
((Tuple (KeyChar #\a) (Tuple #t #f)) (Edited (move st (line-start st))))
|
((Tuple (KeyChar #\a) (Tuple #t #f)) (Edited (move st (line-start st))))
|
||||||
((Tuple KeyEnd _) (accept-suggestion ed st #f (fun () (Edited (move st (line-end st))))))
|
((Tuple KeyEnd _) (Edited (move st (line-end st))))
|
||||||
((Tuple (KeyChar #\e) (Tuple #t #f))
|
((Tuple (KeyChar #\e) (Tuple #t #f)) (Edited (move st (line-end st))))
|
||||||
(accept-suggestion ed st #f (fun () (Edited (move st (line-end st))))))
|
|
||||||
;; Lines, then the history.
|
;; Lines, then the history.
|
||||||
((Tuple KeyUp _)
|
((Tuple KeyUp _)
|
||||||
(match (vertical st #t)
|
(match (vertical st #t)
|
||||||
|
|
@ -1127,8 +821,8 @@
|
||||||
(def text (vec-set (vec-set (record-ref st text) (- i 1) b) i a))
|
(def text (vec-set (vec-set (record-ref st text) (- i 1) b) i a))
|
||||||
(Edited (record-set st (text text) (cursor (+ i 1)))))))
|
(Edited (record-set st (text text) (cursor (+ i 1)))))))
|
||||||
;; Completion, the screen, and the program.
|
;; Completion, the screen, and the program.
|
||||||
((Tuple KeyTab (Tuple #f #f)) (complete ed st))
|
((Tuple KeyTab (Tuple #f #f)) (complete ed st (record-ref prev tabbed?)))
|
||||||
((Tuple (KeyChar #\i) (Tuple #t #f)) (complete ed st))
|
((Tuple (KeyChar #\i) (Tuple #t #f)) (complete ed st (record-ref prev tabbed?)))
|
||||||
((Tuple (KeyChar #\l) (Tuple #t #f)) (ClearScreen st))
|
((Tuple (KeyChar #\l) (Tuple #t #f)) (ClearScreen st))
|
||||||
((Tuple (KeyChar #\z) (Tuple #t #f)) (if (record-ref ed suspend?) (SuspendNow st) (Bell st)))
|
((Tuple (KeyChar #\z) (Tuple #t #f)) (if (record-ref ed suspend?) (SuspendNow st) (Bell st)))
|
||||||
(_ (Bell st))))))
|
(_ (Bell st))))))
|
||||||
|
|
|
||||||
|
|
@ -48,8 +48,7 @@
|
||||||
(str (match result
|
(str (match result
|
||||||
((LineText text) (str "read " (written text)))
|
((LineText text) (str "read " (written text)))
|
||||||
(LineEof "eof")
|
(LineEof "eof")
|
||||||
(LineCancelled "cancelled")
|
(LineCancelled "cancelled"))
|
||||||
((LineKey _ _) "key"))
|
|
||||||
(if (> n 5) ", ticks ran" ", no ticks")))
|
(if (> n 5) ", ticks ran" ", no ticks")))
|
||||||
|
|
||||||
(: (-> LineEditor Terminal string))
|
(: (-> LineEditor Terminal string))
|
||||||
|
|
|
||||||
|
|
@ -44,7 +44,6 @@
|
||||||
(match (line-read ed term "> " #:notices (Some (fun () (notice flag))))
|
(match (line-read ed term "> " #:notices (Some (fun () (notice flag))))
|
||||||
((LineText text) (println (str "read: " (written text))) (go))
|
((LineText text) (println (str "read: " (written text))) (go))
|
||||||
(LineCancelled (println "cancelled") (go))
|
(LineCancelled (println "cancelled") (go))
|
||||||
((LineKey _ _) (go))
|
|
||||||
(LineEof (println (str "eof, raw: " (if (terminal-raw? term) "yes" "no")))))))))
|
(LineEof (println (str "eof, raw: " (if (terminal-raw? term) "yes" "no")))))))))
|
||||||
|
|
||||||
(: (-> void))
|
(: (-> void))
|
||||||
|
|
|
||||||
131
tests/line.bjo
131
tests/line.bjo
|
|
@ -42,19 +42,6 @@
|
||||||
out
|
out
|
||||||
(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.
|
|
||||||
(: (-> 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 (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 ----------------------------------------------------
|
;; ---- running the editor ----------------------------------------------------
|
||||||
|
|
||||||
(: (-> (Option LineResult) string))
|
(: (-> (Option LineResult) string))
|
||||||
|
|
@ -63,7 +50,6 @@
|
||||||
((Some (LineText text)) (str "text " (written text)))
|
((Some (LineText text)) (str "text " (written text)))
|
||||||
((Some LineEof) "eof")
|
((Some LineEof) "eof")
|
||||||
((Some LineCancelled) "cancelled")
|
((Some LineCancelled) "cancelled")
|
||||||
((Some (LineKey ev text)) (str "key " (event->string ev) " " (written text)))
|
|
||||||
(None "not finished")))
|
(None "not finished")))
|
||||||
|
|
||||||
;; The result, or the buffer when the events end first.
|
;; The result, or the buffer when the events end first.
|
||||||
|
|
@ -98,8 +84,7 @@
|
||||||
(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.
|
||||||
|
|
@ -214,123 +199,9 @@
|
||||||
"buffer \"x vec-map \"")
|
"buffer \"x vec-map \"")
|
||||||
(expect "no candidate changes nothing" (run-on completing (keys [(typed "zz") [(key KeyTab) (key KeyTab)]]))
|
(expect "no candidate changes nothing" (run-on completing (keys [(typed "zz") [(key KeyTab) (key KeyTab)]]))
|
||||||
"buffer \"zz\"")
|
"buffer \"zz\"")
|
||||||
(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))])])
|
|
||||||
(expect "Tab without a completer inserts a tab" (run-fresh (keys [(typed "a") [(key KeyTab)] [enter]]))
|
(expect "Tab without a completer inserts a tab" (run-fresh (keys [(typed "a") [(key KeyTab)] [enter]]))
|
||||||
"text \"a\\t\"")
|
"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 "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\"")
|
|
||||||
(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!"))
|
|
||||||
|
|
||||||
;; 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]])
|
|
||||||
|
|
||||||
;; 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))
|
||||||
|
|
|
||||||
|
|
@ -1,64 +0,0 @@
|
||||||
;; 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/.
|
|
||||||
|
|
||||||
;; `line-read` with a prompt of two lines: each drawing goes up to the
|
|
||||||
;; prompt's first row, and the continuation prompt is as wide as its last
|
|
||||||
;; line.
|
|
||||||
;;
|
|
||||||
;; bjo test tests/prompt.bjo
|
|
||||||
;;
|
|
||||||
;; The test runs itself again under `script` on a screen of 40 columns and
|
|
||||||
;; 20 rows. `script` does not keep a screen, so the test checks the escape
|
|
||||||
;; sequences the editor writes.
|
|
||||||
|
|
||||||
(import (std prelude))
|
|
||||||
(import (std simpletest))
|
|
||||||
(import (std run))
|
|
||||||
(import (bjoterm terminal))
|
|
||||||
(import (bjoterm line))
|
|
||||||
|
|
||||||
(import/extern
|
|
||||||
(command-line (: System.Environment.GetCommandLineArgs (-> (Array string)) #:no-raise)))
|
|
||||||
|
|
||||||
(: (-> void))
|
|
||||||
(defun (child)
|
|
||||||
(match (terminal-open)
|
|
||||||
((Err e) (println (str "open: " (terminal-error-message e))))
|
|
||||||
((Ok term)
|
|
||||||
(def ed (make-line-editor))
|
|
||||||
(def prompt "\u{1B}[1mtop line\u{1B}[0m\n> ")
|
|
||||||
(loop (:for _ (range 0 2))
|
|
||||||
(:do (match (line-read ed term prompt)
|
|
||||||
((LineText text) (println (str "result: " (written text))))
|
|
||||||
(_ (println "result: no text"))))))))
|
|
||||||
|
|
||||||
(: (-> void))
|
|
||||||
(defun (parent)
|
|
||||||
(def self (str "dotnet " (array-ref (command-line) 0)))
|
|
||||||
;; "ab" a key at a time, then a second input of two lines: C-j starts the
|
|
||||||
;; second line.
|
|
||||||
(def keys (string-join ["sleep 2" "printf a" "sleep 0.3" "printf b" "sleep 0.3" "printf '\\r'" "sleep 0.5"
|
|
||||||
"printf '(c'" "sleep 0.3" "printf '\\012'" "sleep 0.3" "printf 'd)\\r'" "sleep 0.5"]
|
|
||||||
"; "))
|
|
||||||
(def command (str "(" keys ") | script -qec 'stty cols 40 rows 20; " self " child' /dev/null"))
|
|
||||||
(def out (match (run/output '(sh "-c" ,command))
|
|
||||||
((Ok (Tuple _ text)) (string-replace text "\r" ""))
|
|
||||||
((Err e) (run-error-message e))))
|
|
||||||
(def redraw (str (cursor-up 1) clear-below "\u{1B}[1mtop line\u{1B}[0m\n> "))
|
|
||||||
(expect-true "the input is read" (string-contains? out "result: \"ab\""))
|
|
||||||
(expect-true "a key goes up to the prompt's first row, and draws it again"
|
|
||||||
(string-contains? out (str redraw "a")))
|
|
||||||
(expect-true "and the next key too" (string-contains? out (str redraw "ab")))
|
|
||||||
(expect-true "an input of two lines" (string-contains? out "result: \"(c\\nd)\""))
|
|
||||||
(expect-true "the continuation prompt is as wide as the prompt's last line"
|
|
||||||
(string-contains? out "\n> (c\n d)"))
|
|
||||||
(unless (string-contains? out "result: \"(c")
|
|
||||||
(println out)))
|
|
||||||
|
|
||||||
(defun (main args)
|
|
||||||
(cond
|
|
||||||
((and (= (vec-length args) 1) (= (vec-ref args 0) "child")) (child))
|
|
||||||
((file-exists? "/usr/bin/script") (parent))
|
|
||||||
(else (println "skipped: no script command")))
|
|
||||||
0)
|
|
||||||
|
|
@ -36,8 +36,7 @@
|
||||||
(match result
|
(match result
|
||||||
((LineText text) (str "read " (int->string (string-length text))))
|
((LineText text) (str "read " (int->string (string-length text))))
|
||||||
(LineEof "eof")
|
(LineEof "eof")
|
||||||
(LineCancelled "cancelled")
|
(LineCancelled "cancelled")))
|
||||||
((LineKey _ _) "key")))
|
|
||||||
|
|
||||||
(: (-> bool void))
|
(: (-> bool void))
|
||||||
(defun (child reflow?)
|
(defun (child reflow?)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue