Compare commits

..

No commits in common. "main" and "v0.2.2" have entirely different histories.
main ... v0.2.2

3 changed files with 12 additions and 69 deletions

View file

@ -1,6 +1,6 @@
(package (package
(name (bjoterm)) (name (bjoterm))
(version "0.2.3") (version "0.2.2")
(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")

View file

@ -31,7 +31,6 @@
;; - `#: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, which makes ;; Keys that move right at the end of the input accept it, which makes
;; the input the suggested line. ;; 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 ;; - `#: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.
@ -42,10 +41,10 @@
(import (bjoterm text)) (import (bjoterm text))
(export (export
LineEditor LineResult Completion Suggestion suggestion-after Highlight 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 completion-listing menu-lines cluster-styles) simulate-line cursor-row-after-reflow input-window completion-listing menu-lines)
;; =========================================================================== ;; ===========================================================================
;; Types ;; Types
@ -77,12 +76,6 @@
;; input in case, or be another line that `ghost` describes. ;; input in case, or be another line that `ghost` describes.
(type (: Suggestion (Record (: ghost string) (: line string)))) (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`. ;; The suggestion of `rest` after `text`.
(: (-> string string Suggestion)) (: (-> string string Suggestion))
(defun (suggestion-after text rest) (defun (suggestion-after text rest)
@ -92,7 +85,6 @@
(type (: LineEditor #:opaque (type (: LineEditor #:opaque
(Record (: completer (Option (-> string Completion))) (Record (: completer (Option (-> string Completion)))
(: suggester (Option (-> string (Vec string) (Option Suggestion)))) (: suggester (Option (-> string (Vec string) (Option Suggestion))))
(: highlighter (Option (-> string (Vec Highlight))))
(: 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)
@ -150,8 +142,6 @@
;; - `#: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 a ;; 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. ;; `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 ;; - `#: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.
@ -163,16 +153,15 @@
;; the input above it. ;; the input above it.
(: (-> (#:completer (Option (-> string Completion))) (: (-> (#:completer (Option (-> string Completion)))
(#:suggester (Option (-> string (Vec string) (Option Suggestion)))) (#:suggester (Option (-> string (Vec string) (Option Suggestion))))
(#:highlighter (Option (-> string (Vec Highlight))))
(#: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)
(#: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 #:suggester None #:command-key? None #:input-done? None
#:history-size 1000 #:reflow? None #:suspend? #t) #:history-size 1000 #:reflow? None #:suspend? #t)
(LineEditor (completer completer) (suggester suggester) (highlighter highlighter) (command-key? command-key?) (LineEditor (completer completer) (suggester suggester) (command-key? command-key?)
(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 [])))
@ -338,7 +327,7 @@
(match (suggestion ed st) (match (suggestion ed st)
((Some s) (clusters (record-ref s ghost))) ((Some s) (clusters (record-ref s ghost)))
(None [])))) (None []))))
(def lay (layout-state st prompt cont width ghost (cluster-styles ed (record-ref st text)))) (def lay (layout-state st prompt cont width ghost))
(def input (window-of lay width (if whole? None (Some height)) top)) (def input (window-of lay width (if whole? None (Some height)) top))
;; The menu, below the input, in the rows the screen has left. ;; The menu, below the input, in the rows the screen has left.
(def menu-rows (def menu-rows
@ -541,51 +530,21 @@
(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 (Vec string) Layout))
(defun (layout-state st prompt cont width ghost styles) (defun (layout-state st prompt cont width ghost)
(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 ghost))))
;; The style of each cluster of `text`, from `#:highlighter`. Empty when
;; there is none.
(: (-> 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. ;; `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) Layout))
(: (-> (Vec string) (Vec string) (Vec string) int int (Vec string) (Vec (Vec Style)) Layout)) (defun (layout prompt cont text cursor width ghost)
(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)
@ -627,8 +586,6 @@
((= 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)

View file

@ -308,20 +308,6 @@
(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!"))
;; 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. ;; The list of candidates.
(expect "without descriptions, in columns" (completion-listing ["ab" "cd" "ef"] [] 20) "ab cd ef\n") (expect "without descriptions, in columns" (completion-listing ["ab" "cd" "ef"] [] 20) "ab cd ef\n")
(expect "with them, one a row" (expect "with them, one a row"