line-read: #:highlighter draws parts of the input styled; 0.2.3

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Linus Björnstam 2026-12-01 14:58:26 +01:00
parent 4eef978b6b
commit 125d0fd7c2
3 changed files with 69 additions and 12 deletions

View file

@ -1,6 +1,6 @@
(package
(name (bjoterm))
(version "0.2.2")
(version "0.2.3")
(description "The terminal for Bjolang programs: raw mode, key events and escape sequences. Linux.")
(license "MPL-2.0")

View file

@ -31,6 +31,7 @@
;; - `#: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.
@ -41,10 +42,10 @@
(import (bjoterm text))
(export
LineEditor LineResult Completion Suggestion suggestion-after
LineEditor LineResult Completion Suggestion suggestion-after Highlight
make-line-editor line-read
line-history line-history-add! line-history-load! line-history-save
simulate-line cursor-row-after-reflow input-window completion-listing menu-lines)
simulate-line cursor-row-after-reflow input-window completion-listing menu-lines cluster-styles)
;; ===========================================================================
;; Types
@ -76,6 +77,12 @@
;; 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)
@ -85,6 +92,7 @@
(type (: LineEditor #:opaque
(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)))
(: history-size int)
@ -142,6 +150,8 @@
;; - `#: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.
@ -153,15 +163,16 @@
;; the input above it.
(: (-> (#: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)))
(#:history-size int)
(#:reflow? (Option bool))
(#:suspend? bool)
LineEditor))
(defun (make-line-editor #:completer None #:suggester None #:command-key? None #:input-done? None
(defun (make-line-editor #:completer None #:suggester None #:highlighter None #:command-key? None #:input-done? None
#:history-size 1000 #:reflow? None #:suspend? #t)
(LineEditor (completer completer) (suggester suggester) (command-key? command-key?)
(LineEditor (completer completer) (suggester suggester) (highlighter highlighter) (command-key? command-key?)
(input-done? input-done?)
(history-size history-size) (reflow? reflow?) (suspend? suspend?) (history [])))
@ -327,7 +338,7 @@
(match (suggestion ed st)
((Some s) (clusters (record-ref s ghost)))
(None []))))
(def lay (layout-state st prompt cont width ghost))
(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
@ -530,21 +541,51 @@
(type (: Layout (Record (: texts (Vec string)) (: rows (Vec (Vec int)))
(: cursor-row int) (: cursor-col int))))
(: (-> State string string int (Vec string) Layout))
(defun (layout-state st prompt cont width ghost)
(: (-> State string string int (Vec string) (Vec (Vec Style)) Layout))
(defun (layout-state st prompt cont width ghost styles)
(match (record-ref st search)
((Some s)
(def label (str "(" (if (record-ref s failed?) "failed " "")
"search `" (record-ref s query) "'): "))
(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
(layout (prompt-clusters prompt) (prompt-clusters cont)
(record-ref st text) (record-ref st cursor) width ghost))))
(record-ref st text) (record-ref st cursor) width ghost styles))))
;; 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.
(: (-> (Vec string) (Vec string) (Vec string) int int (Vec string) Layout))
(defun (layout prompt cont text cursor width ghost)
;; `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/mutable texts [])
(def/mutable row 0)
@ -586,6 +627,8 @@
((= c "\t")
(def w (- 8 (remainder col 8)))
(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))))))
(when (= cursor (vec-length text))
(set! cursor-row row)

View file

@ -308,6 +308,20 @@
(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"