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:
Linus Björnstam 2026-11-30 21:35:16 +01:00
parent 95d747cf70
commit 566008e264
5 changed files with 181 additions and 25 deletions

View file

@ -35,6 +35,7 @@ Examples:
- `tests/text.bjo`: clusters and widths. - `tests/text.bjo`: clusters and widths.
- `tests/reflow.bjo`: the redraw after the terminal narrows, with and - `tests/reflow.bjo`: the redraw after the terminal narrows, with and
without reflow, under `script`. without reflow, under `script`.
- `tests/tall.bjo`: input taller than the screen, under `script`.
- `tests/async.bjo`: `line-read` in a bjoroutine, and cancelled by a - `tests/async.bjo`: `line-read` in a bjoroutine, and cancelled by a
deadline. deadline.
- `examples/keys.bjo` prints events; `examples/repl.bjo` is a small REPL. - `examples/keys.bjo` prints events; `examples/repl.bjo` is a small REPL.

View file

@ -51,8 +51,15 @@ is in raw mode, and gives it back as it found it.
of the input, a wrong #t erases output above it. of the input, a wrong #t erases output above it.
- Several resizes before one redraw give the same result as one, as - Several resizes before one redraw give the same result as one, as
the terminal joins its own wraps again. the terminal joins its own wraps again.
- Not done: input taller than the screen after a reflow. The rows above - A reflow that makes the drawn rows taller than the screen pushes the
the top are in the scrollback, and the cursor cannot go up to them. first of them into the scrollback, where the cursor cannot go. They
stay there once, as a copy; the next drawing fits the screen.
- Tall input (2026-11-30): input taller than the screen is drawn in
part, so that the cursor can always go up to its first row.
~input-window~ chooses the rows: the window stays while the cursor is in
it, and moves the least it must. A row at each end with hidden rows
says how many ("↑ 3 more rows"). The accepted input is drawn whole, so
the scrollback has all of it.
- Tested with ~script~, which does not reflow: the test checks the - Tested with ~script~, which does not reflow: the test checks the
escape sequences. Not yet tried on a terminal that reflows. escape sequences. Not yet tried on a terminal that reflows.

View file

@ -20,6 +20,9 @@
;; its width in columns: East Asian wide characters and emoji take two. ;; 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 ;; - It wraps lines itself, with a line break where a line is full. The
;; whole input is drawn again after each key. ;; 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 ;; - When the terminal gets narrower, a terminal that reflows wraps the
;; rows of the input again, and the cursor moves down. The editor ;; rows of the input again, and the cursor moves down. The editor
;; computes where the input starts then; see `#:reflow?`. ;; computes where the input starts then; see `#:reflow?`.
@ -36,7 +39,7 @@
LineEditor LineResult Completion 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) simulate-line cursor-row-after-reflow input-window)
;; =========================================================================== ;; ===========================================================================
;; Types ;; Types
@ -224,8 +227,11 @@
(None (terminal-reflows? term)))) (None (terminal-reflows? term))))
;; The input on the screen, and the width it was drawn at. None when the ;; 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. ;; cursor is at the start of a row with no input after it.
(: (Option Drawn))
(def/mutable drawn None) (def/mutable drawn None)
(def/mutable drawn-width 0) (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. ;; The rows from the start of the input to the cursor.
(defun (rows-up width) (defun (rows-up width)
(match drawn (match drawn
@ -235,22 +241,28 @@
(cursor-row-after-reflow (record-ref l rows) (record-ref l cursor-row) (cursor-row-after-reflow (record-ref l rows) (record-ref l cursor-row)
(record-ref l cursor-col) width) (record-ref l cursor-col) width)
(record-ref l cursor-row))))) (record-ref l cursor-row)))))
(defun (draw st) ;; Draws `st`: the rows that fit on the screen, or all when `whole?`.
(def width (fst (terminal-size term))) (defun (draw st whole?)
(def (Tuple width height) (terminal-size term))
(def lay (layout-state st prompt cont width)) (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 (write-string out (str "\r" (cursor-up (rows-up width)) clear-below
(record-ref lay shown) (record-ref shown text)
(cursor-up (- (record-ref lay end-row) (record-ref lay cursor-row))) (cursor-up (- (last-row shown) (record-ref shown cursor-row)))
"\r" (cursor-forward (record-ref lay cursor-col)))) "\r" (cursor-forward (record-ref shown cursor-col))))
(set! drawn (Some lay)) (set! top (record-ref shown top))
(set! drawn (Some shown))
(set! drawn-width width) (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. ;; Moves below the input, for output that follows it.
(defun (leave lay) (: (-> Drawn void))
(write-string out (str (cursor-down (- (record-ref lay end-row) (record-ref lay cursor-row))) "\n")) (defun (leave shown)
(write-string out (str (cursor-down (- (last-row shown) (record-ref shown cursor-row))) "\n"))
(set! drawn None)) (set! drawn None))
(let go ((st (start-state ed))) (let go ((st (start-state ed)))
(def lay (draw st)) (def lay (draw st #f))
(match (line-step ed st (read-event term)) (match (line-step ed st (read-event term))
((Edited next) (go next)) ((Edited next) (go next))
((Bell next) (write-string out "\u{7}") (go next)) ((Bell next) (write-string out "\u{7}") (go next))
@ -267,7 +279,7 @@
(write-string out (columns items (fst (terminal-size term)))) (write-string out (columns items (fst (terminal-size term))))
(go next)) (go next))
((Finished last result) ((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")) (when (= result LineCancelled) (write-string out "^C"))
(leave shown) (leave shown)
(flush-port out) (flush-port out)
@ -320,15 +332,16 @@
;; Layout ;; Layout
;; =========================================================================== ;; ===========================================================================
;; The input as written to the terminal from the start of the prompt. ;; The input in rows, from the start of the prompt.
;; - `shown`: the text, with a line break where a line is full and after a ;; - `texts`: the text of each row. A row ends where a line is full, and
;; line that ends exactly at the edge. ;; after a line that ends exactly at the edge there is an empty row.
;; - `end-row`: the row where `shown` leaves the cursor, from 0.
;; - `cursor-row` and `cursor-col`: where the editor's cursor is drawn.
;; - `rows`: the widths of the items on each row, for ;; - `rows`: the widths of the items on each row, for
;; `cursor-row-after-reflow`. ;; `cursor-row-after-reflow`.
(type (: Layout (Record (: shown string) (: end-row int) (: cursor-row int) (: cursor-col int) ;; - `cursor-row` and `cursor-col`: where the editor's cursor is.
(: rows (Vec (Vec int)))))) ;; `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)) (: (-> State string string int Layout))
(defun (layout-state st prompt cont width) (defun (layout-state st prompt cont width)
@ -345,6 +358,7 @@
(: (-> (Vec string) (Vec string) (Vec string) int int Layout)) (: (-> (Vec string) (Vec string) (Vec string) int int Layout))
(defun (layout prompt cont text cursor width) (defun (layout prompt cont text cursor width)
(def out (stringbuilder-empty)) (def out (stringbuilder-empty))
(def/mutable texts [])
(def/mutable row 0) (def/mutable row 0)
(def/mutable col 0) (def/mutable col 0)
(def/mutable cursor-row 0) (def/mutable cursor-row 0)
@ -354,7 +368,8 @@
(defun (break!) (defun (break!)
(set! rows (vec-add rows this-row)) (set! rows (vec-add rows this-row))
(set! 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! row (+ row 1))
(set! col 0)) (set! col 0))
;; Writes `shown`, `w` columns wide, on this row if it fits. ;; Writes `shown`, `w` columns wide, on this row if it fits.
@ -384,9 +399,65 @@
(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))
(Layout (shown (stringbuilder->string out)) (end-row row) (Layout (texts (vec-add texts (stringbuilder->string out)))
(cursor-row cursor-row) (cursor-col cursor-col) (rows (vec-add rows this-row))
(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 ;; The row of the cursor, counted from the start of the input, after a
;; terminal that reflows has got narrower. ;; terminal that reflows has got narrower.

View file

@ -228,4 +228,13 @@
(expect "a wide item that does not fit goes to the next row" (expect "a wide item that does not fit goes to the next row"
(cursor-row-after-reflow [[1 1 1 2] [1]] 1 0 4) 2) (cursor-row-after-reflow [[1 1 1 2] [1]] 1 0 4) 2)
(expect "an empty row is one row" (cursor-row-after-reflow [[] [] [1]] 2 1 4) 2) (expect "an empty row is one row" (cursor-row-after-reflow [[] [] [1]] 2 1 4) 2)
;; The rows drawn of an input taller than the screen.
(expect "input that fits is drawn whole" (input-window 5 10 3 0) (Tuple 0 5))
(expect "at the start, a marker below" (input-window 20 6 2 0) (Tuple 0 5))
(expect "at the end, a marker above" (input-window 20 6 19 0) (Tuple 15 5))
(expect "between, a marker each side" (input-window 20 6 10 0) (Tuple 7 4))
(expect "the window stays while the cursor is in it" (input-window 20 6 9 7) (Tuple 7 4))
(expect "and moves the least it must" (input-window 20 6 5 7) (Tuple 5 4))
(expect "a screen of 2 rows has no markers" (input-window 20 2 10 0) (Tuple 9 2))
(println "line ok")) (println "line ok"))

68
tests/tall.bjo Normal file
View file

@ -0,0 +1,68 @@
;; 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 input taller than the screen: it draws only the rows
;; that fit, and never goes up more rows than the screen has.
;;
;; bjo test tests/tall.bjo
;;
;; The test runs itself again under `script` on a screen of 20 columns and
;; 6 rows. `script` does not keep a screen, so the test checks the escape
;; sequences and the markers 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)
(match (line-read (make-line-editor) term "> ")
((LineText text) (println (str "result: read " (int->string (string-length text)))))
(_ (println "result: no text"))))))
;; The largest `n` of the cursor-up sequences ESC [ n A in `out`.
(: (-> string int))
(defun (most-rows-up out)
(def parts (string-split out "\u{1B}["))
(match (loop (:for part (in-vec parts))
(:acc most (maximizing
(match (string-split-once part "A")
((Some (Tuple digits _))
(match (string->int digits) ((Some n) n) (None 0)))
(None 0)))))
((Some n) n)
(None 0)))
(: (-> void))
(defun (parent)
(def self (str "dotnet " (array-ref (command-line) 0)))
;; "> " and 150 characters take 8 rows of 20. C-a goes to the start of
;; the line, so the window moves to the first rows.
(def keys (str "sleep 2; printf '" (string-pad-left "" 150 #:with #\x) "'; sleep 0.5; "
"printf '\\001'; sleep 0.5; printf '\\r'; sleep 0.5"))
(def command (str "(" keys ") | script -qec 'stty cols 20 rows 6; " 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))))
(expect-true "the input is read" (string-contains? out "result: read 150"))
(expect-true "at the end, a marker for the rows above" (string-contains? out "\u{2191} 3 more rows"))
(expect-true "at the start, a marker for the rows below" (string-contains? out "\u{2193} 3 more rows"))
(expect-true "it never goes up more rows than the screen has" (< (most-rows-up out) 6))
(unless (string-contains? out "result: read")
(println out)))
(defun (main args)
(cond
((= args ["child"]) (child))
((file-exists? "/usr/bin/script") (parent))
(else (println "skipped: no script command")))
0)