line-read: draw input taller than the screen in part
- input-window chooses the rows to draw: the window stays while the cursor is in it and moves the least it must. A marker row at each end with hidden rows gives their count. - The cursor can always go up to the first row drawn, so redrawing no longer leaves copies of the input in the scrollback. - The accepted input is drawn whole. - layout returns the text of each row. - New tests: input-window in tests/line.bjo, and tests/tall.bjo. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
95d747cf70
commit
566008e264
5 changed files with 181 additions and 25 deletions
117
src/line.bjo
117
src/line.bjo
|
|
@ -20,6 +20,9 @@
|
|||
;; its width in columns: East Asian wide characters and emoji take two.
|
||||
;; - It wraps lines itself, with a line break where a line is full. The
|
||||
;; whole input is drawn again after each key.
|
||||
;; - Input taller than the screen is drawn in part: the rows around the
|
||||
;; cursor, and a row that says how many rows are hidden above or below.
|
||||
;; The accepted input is drawn whole.
|
||||
;; - When the terminal gets narrower, a terminal that reflows wraps the
|
||||
;; rows of the input again, and the cursor moves down. The editor
|
||||
;; computes where the input starts then; see `#:reflow?`.
|
||||
|
|
@ -36,7 +39,7 @@
|
|||
LineEditor LineResult Completion
|
||||
make-line-editor line-read
|
||||
line-history line-history-add! line-history-load! line-history-save
|
||||
simulate-line cursor-row-after-reflow)
|
||||
simulate-line cursor-row-after-reflow input-window)
|
||||
|
||||
;; ===========================================================================
|
||||
;; Types
|
||||
|
|
@ -224,8 +227,11 @@
|
|||
(None (terminal-reflows? term))))
|
||||
;; The input on the screen, and the width it was drawn at. None when the
|
||||
;; cursor is at the start of a row with no input after it.
|
||||
(: (Option Drawn))
|
||||
(def/mutable drawn None)
|
||||
(def/mutable drawn-width 0)
|
||||
;; The first row of the input in the last window drawn.
|
||||
(def/mutable top 0)
|
||||
;; The rows from the start of the input to the cursor.
|
||||
(defun (rows-up width)
|
||||
(match drawn
|
||||
|
|
@ -235,22 +241,28 @@
|
|||
(cursor-row-after-reflow (record-ref l rows) (record-ref l cursor-row)
|
||||
(record-ref l cursor-col) width)
|
||||
(record-ref l cursor-row)))))
|
||||
(defun (draw st)
|
||||
(def width (fst (terminal-size term)))
|
||||
;; Draws `st`: the rows that fit on the screen, or all when `whole?`.
|
||||
(defun (draw st whole?)
|
||||
(def (Tuple width height) (terminal-size term))
|
||||
(def lay (layout-state st prompt cont width))
|
||||
(def shown (window-of lay width (if whole? None (Some height)) top))
|
||||
(write-string out (str "\r" (cursor-up (rows-up width)) clear-below
|
||||
(record-ref lay shown)
|
||||
(cursor-up (- (record-ref lay end-row) (record-ref lay cursor-row)))
|
||||
"\r" (cursor-forward (record-ref lay cursor-col))))
|
||||
(set! drawn (Some lay))
|
||||
(record-ref shown text)
|
||||
(cursor-up (- (last-row shown) (record-ref shown cursor-row)))
|
||||
"\r" (cursor-forward (record-ref shown cursor-col))))
|
||||
(set! top (record-ref shown top))
|
||||
(set! drawn (Some shown))
|
||||
(set! drawn-width width)
|
||||
lay)
|
||||
shown)
|
||||
(: (-> Drawn int))
|
||||
(defun (last-row shown) (- (vec-length (record-ref shown rows)) 1))
|
||||
;; Moves below the input, for output that follows it.
|
||||
(defun (leave lay)
|
||||
(write-string out (str (cursor-down (- (record-ref lay end-row) (record-ref lay cursor-row))) "\n"))
|
||||
(: (-> Drawn void))
|
||||
(defun (leave shown)
|
||||
(write-string out (str (cursor-down (- (last-row shown) (record-ref shown cursor-row))) "\n"))
|
||||
(set! drawn None))
|
||||
(let go ((st (start-state ed)))
|
||||
(def lay (draw st))
|
||||
(def lay (draw st #f))
|
||||
(match (line-step ed st (read-event term))
|
||||
((Edited next) (go next))
|
||||
((Bell next) (write-string out "\u{7}") (go next))
|
||||
|
|
@ -267,7 +279,7 @@
|
|||
(write-string out (columns items (fst (terminal-size term))))
|
||||
(go next))
|
||||
((Finished last result)
|
||||
(def shown (draw (record-set last (search None) (cursor (vec-length (record-ref last text))))))
|
||||
(def shown (draw (record-set last (search None) (cursor (vec-length (record-ref last text)))) #t))
|
||||
(when (= result LineCancelled) (write-string out "^C"))
|
||||
(leave shown)
|
||||
(flush-port out)
|
||||
|
|
@ -320,15 +332,16 @@
|
|||
;; Layout
|
||||
;; ===========================================================================
|
||||
|
||||
;; The input as written to the terminal from the start of the prompt.
|
||||
;; - `shown`: the text, with a line break where a line is full and after a
|
||||
;; line that ends exactly at the edge.
|
||||
;; - `end-row`: the row where `shown` leaves the cursor, from 0.
|
||||
;; - `cursor-row` and `cursor-col`: where the editor's cursor is drawn.
|
||||
;; The input in rows, from the start of the prompt.
|
||||
;; - `texts`: the text of each row. A row ends where a line is full, and
|
||||
;; after a line that ends exactly at the edge there is an empty row.
|
||||
;; - `rows`: the widths of the items on each row, for
|
||||
;; `cursor-row-after-reflow`.
|
||||
(type (: Layout (Record (: shown string) (: end-row int) (: cursor-row int) (: cursor-col int)
|
||||
(: rows (Vec (Vec int))))))
|
||||
;; - `cursor-row` and `cursor-col`: where the editor's cursor is.
|
||||
;; `Drawn` is the same for the rows written to the screen, which can be
|
||||
;; fewer, with marker rows.
|
||||
(type (: Layout (Record (: texts (Vec string)) (: rows (Vec (Vec int)))
|
||||
(: cursor-row int) (: cursor-col int))))
|
||||
|
||||
(: (-> State string string int Layout))
|
||||
(defun (layout-state st prompt cont width)
|
||||
|
|
@ -345,6 +358,7 @@
|
|||
(: (-> (Vec string) (Vec string) (Vec string) int int Layout))
|
||||
(defun (layout prompt cont text cursor width)
|
||||
(def out (stringbuilder-empty))
|
||||
(def/mutable texts [])
|
||||
(def/mutable row 0)
|
||||
(def/mutable col 0)
|
||||
(def/mutable cursor-row 0)
|
||||
|
|
@ -354,7 +368,8 @@
|
|||
(defun (break!)
|
||||
(set! rows (vec-add rows this-row))
|
||||
(set! this-row [])
|
||||
(stringbuilder-add-string! out "\n")
|
||||
(set! texts (vec-add texts (stringbuilder->string out)))
|
||||
(stringbuilder-clear! out)
|
||||
(set! row (+ row 1))
|
||||
(set! col 0))
|
||||
;; Writes `shown`, `w` columns wide, on this row if it fits.
|
||||
|
|
@ -384,9 +399,65 @@
|
|||
(when (= cursor (vec-length text))
|
||||
(set! cursor-row row)
|
||||
(set! cursor-col col))
|
||||
(Layout (shown (stringbuilder->string out)) (end-row row)
|
||||
(cursor-row cursor-row) (cursor-col cursor-col)
|
||||
(rows (vec-add rows this-row))))
|
||||
(Layout (texts (vec-add texts (stringbuilder->string out)))
|
||||
(rows (vec-add rows this-row))
|
||||
(cursor-row cursor-row) (cursor-col cursor-col)))
|
||||
|
||||
;; Which rows of an input `total` rows tall to draw on a screen `height`
|
||||
;; rows tall. Returns the first row and the number of rows.
|
||||
;; - All rows when they fit.
|
||||
;; - Else the window keeps `prev-top`, the first row of the last drawing,
|
||||
;; when the cursor row is in it, and moves the least it must otherwise.
|
||||
;; - With 3 or more rows, a row each is left for the markers of hidden
|
||||
;; rows above and below: one marker at the start and at the end of the
|
||||
;; input, two between.
|
||||
(: (-> int int int int (Tuple int int)))
|
||||
(defun (input-window total height cursor-row prev-top)
|
||||
(defun (clamp x lo hi) (max lo (min hi x)))
|
||||
(cond
|
||||
((<= total height) (Tuple 0 total))
|
||||
((< height 3)
|
||||
(def h (max height 1))
|
||||
(Tuple (clamp prev-top (- cursor-row (- h 1)) cursor-row) h))
|
||||
(else
|
||||
(def top (clamp prev-top (- cursor-row (- height 3)) cursor-row))
|
||||
(cond
|
||||
((<= top 0) (Tuple 0 (- height 1)))
|
||||
((>= (+ top (- height 2)) total) (Tuple (- total (- height 1)) (- height 1)))
|
||||
(else (Tuple top (- height 2)))))))
|
||||
|
||||
;; What is on the screen after a drawing.
|
||||
;; - `text`: what to write from the start of the first row.
|
||||
;; - `rows`: the widths of the items on each row written.
|
||||
;; - `cursor-row` and `cursor-col`: the cursor, from the first row written.
|
||||
;; - `top`: the first row of the input that is written.
|
||||
(type (: Drawn (Record (: text string) (: rows (Vec (Vec int)))
|
||||
(: cursor-row int) (: cursor-col int) (: top int))))
|
||||
|
||||
;; The rows of `lay` to write: those `input-window` chooses with `height`,
|
||||
;; or all of them when `height` is None.
|
||||
(: (-> Layout int (Option int) int Drawn))
|
||||
(defun (window-of lay width height prev-top)
|
||||
(def total (vec-length (record-ref lay rows)))
|
||||
(def (Tuple top count)
|
||||
(match height
|
||||
((Some h) (input-window total h (record-ref lay cursor-row) prev-top))
|
||||
(None (Tuple 0 total))))
|
||||
(def hidden-below (- total top count))
|
||||
(def markers? (and (> total count) (match height ((Some h) (>= h 3)) (None #f))))
|
||||
;; A marker row that fits in `width`.
|
||||
(defun (marker arrow n)
|
||||
(def long (str arrow " " (int->string n) (if (= n 1) " more row" " more rows")))
|
||||
(def text (if (<= (text-width long) width) long arrow))
|
||||
(Tuple text (vec-map (fun (_) 1) (text-clusters text))))
|
||||
(def above (if (and markers? (> top 0)) [(marker "\u{2191}" top)] []))
|
||||
(def below (if (and markers? (> hidden-below 0)) [(marker "\u{2193}" hidden-below)] []))
|
||||
(defun (around items middle) (vec-append (vec-append (vec-map items above) middle) (vec-map items below)))
|
||||
(Drawn (text (string-join (around fst (vec-slice (record-ref lay texts) top count)) "\n"))
|
||||
(rows (around snd (vec-slice (record-ref lay rows) top count)))
|
||||
(cursor-row (+ (vec-length above) (- (record-ref lay cursor-row) top)))
|
||||
(cursor-col (record-ref lay cursor-col))
|
||||
(top top)))
|
||||
|
||||
;; The row of the cursor, counted from the start of the input, after a
|
||||
;; terminal that reflows has got narrower.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue