line-read: redraw correctly after a terminal reflows

- Each row of the input ends with a line break, so only a narrowing
  terminal that reflows moves the cursor. The layout keeps the widths of
  the items on each row, and cursor-row-after-reflow computes the
  cursor's row after the terminal wraps each row again.
- terminal-reflows? guesses from the environment. An unknown terminal
  counts as not reflowing. #:reflow? of make-line-editor overrides it.
- New tests: the model in tests/line.bjo, and tests/reflow.bjo, which
  narrows the terminal with stty under script.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Linus Björnstam 2026-11-30 21:24:16 +01:00
parent ce778f9b1e
commit 95d747cf70
6 changed files with 226 additions and 13 deletions

View file

@ -216,4 +216,16 @@
(expect "a history file that does not exist is empty"
(line-history-load! (make-line-editor) (path-combine (temp-dir) "bjoterm-no-such-history.txt"))
(Ok unit))
;; Reflow: where the cursor is after a terminal that reflows narrows.
(expect "rows that still fit move nothing" (cursor-row-after-reflow [[1 1 1] [1 1]] 1 2 10) 1)
(expect "a row above that wraps moves the cursor down"
(cursor-row-after-reflow [[1 1 1 1 1 1] [1 1]] 1 1 4) 2)
(expect "every row above counts" (cursor-row-after-reflow [[1 1 1 1 1] [1 1 1 1 1] [1]] 2 0 2) 6)
(expect "the cursor's own row wraps before it" (cursor-row-after-reflow [[1 1 1 1 1 1]] 0 5 4) 1)
(expect "but not after it" (cursor-row-after-reflow [[1 1 1 1 1 1]] 0 2 4) 0)
(expect "a cursor after a full row is on the next" (cursor-row-after-reflow [[1 1 1 1]] 0 4 4) 1)
(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)
(expect "an empty row is one row" (cursor-row-after-reflow [[] [] [1]] 2 1 4) 2)
(println "line ok"))

96
tests/reflow.bjo Normal file
View file

@ -0,0 +1,96 @@
;; 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` when the terminal gets narrower: how far up it goes to draw
;; the input again.
;;
;; bjo test tests/reflow.bjo
;;
;; The test runs itself again under `script`. The child makes its terminal
;; narrower with `stty cols` while `line-read` waits in another
;; bjoroutine. `script` does not reflow, so the test checks the escape
;; sequences the editor writes, not the screen.
(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)))
;; ---- the child -------------------------------------------------------------
(: (-> void))
(defbjo (narrow-later)
(sync (timeout (milliseconds 3000)))
(ignore (sync (blocking (fun () (run/output '(sh "-c" "stty cols 20 </dev/tty")))))))
(: (-> LineEditor Terminal string))
(defbjo (read-and-narrow ed term)
(def narrowing (bjo (narrow-later)))
(def result (line-read ed term "> "))
(ignore (sync (promise-join narrowing)))
(match result
((LineText text) (str "read " (int->string (string-length text))))
(LineEof "eof")
(LineCancelled "cancelled")))
(: (-> bool void))
(defun (child reflow?)
(match (terminal-open)
((Err e) (println (str "open: " (terminal-error-message e))))
((Ok term)
(def ed (make-line-editor #:reflow? (Some reflow?)))
(match (sync/blocking (promise-join (bjo (read-and-narrow ed term))))
((Ok text) (println (str "result: " text)))
((Err e) (println (str "raised " (.-Message e))))))))
;; ---- the parent ------------------------------------------------------------
;; The output of the child, with the terminal 40 columns wide at the start.
(: (-> string string))
(defun (run-child mode)
(def self (str "dotnet " (array-ref (command-line) 0)))
;; "> " and 50 characters: a full row of 40 and a row of 12, the cursor
;; at its end. At 20 columns the first row takes 2 rows, so the cursor is
;; 2 rows below the start of the input, not 1.
(def keys (str "sleep 2; printf '" (string-pad-left "" 50 #:with #\x) "'; sleep 2.5; printf '\\r'; sleep 0.5"))
(def command (str "(" keys ") | script -qec 'stty cols 40 rows 20; " self " " mode "' /dev/null"))
(match (run/output '(sh "-c" ,command))
((Ok (Tuple _ text)) (string-replace text "\r" ""))
((Err e) (run-error-message e))))
;; Whether the first drawing at 20 columns comes right after going up `up`
;; rows and clearing.
(: (-> string int bool))
(defun (first-narrow-draw-after? out up)
;; Only a drawing at 20 columns has a row of "> " and 18 x.
(def row (str "> " (string-pad-left "" 18 #:with #\x) "\n"))
(match (string-split-once out row)
((Some (Tuple before _))
(string-ends-with? before (str "\u{1B}[" (int->string up) "A\u{1B}[J")))
(None #f)))
(: (-> void))
(defun (parent)
(def reflowing (run-child "reflow"))
(expect-true "the input is read" (string-contains? reflowing "result: read 50"))
(expect-true "after a reflow it goes up the 2 rows of the wrapped input"
(first-narrow-draw-after? reflowing 2))
(def fixed (run-child "fixed"))
(expect-true "the input is read" (string-contains? fixed "result: read 50"))
(expect-true "without a reflow it goes up the 1 row as drawn"
(first-narrow-draw-after? fixed 1))
(unless (string-contains? reflowing "result: read")
(println reflowing)))
(defun (main args)
(cond
((= args ["reflow"]) (child #t))
((= args ["fixed"]) (child #f))
((file-exists? "/usr/bin/script") (parent))
(else (println "skipped: no script command")))
0)