line-read: a prompt may contain line breaks

- A line break in the prompt ends its row, as one in the input does. Each
  drawing goes up to the prompt's first row and draws it again; before,
  each key wrote the prompt's first line once more.
- The default continuation prompt is as wide as the prompt's last line.
- tests/prompt.bjo, under script.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Linus Björnstam 2026-12-01 11:47:24 +01:00
parent 5a8a441cbc
commit dc8a3325b8
2 changed files with 80 additions and 4 deletions

View file

@ -187,8 +187,9 @@
;; Reads one input from the terminal, with editing.
;; - `prompt` may contain escape sequences, such as colors; they take no
;; columns. `#:continuation` is the prompt of the lines after the first;
;; by default, spaces as wide as `prompt`.
;; columns. It may contain line breaks: the input starts after its last
;; line. `#:continuation` is the prompt of the lines after the first;
;; by default, spaces as wide as the last line of `prompt`.
;; - Sets raw mode for the time it reads, unless the terminal is in raw
;; mode already.
;; - In a bjoroutine it suspends while it waits for keys; elsewhere it parks
@ -215,7 +216,7 @@
(defun (line-read ed term prompt #:continuation None #:notices None)
(def cont (match continuation
((Some c) c)
(None (string-pad-left "" (text-width prompt)))))
(None (string-pad-left "" (text-width (last-line prompt))))))
(if (terminal-raw? term)
(edit-loop ed term prompt cont notices)
;; A `fun` is ordinary code, so the suspending copy passes a
@ -227,6 +228,14 @@
(: notice-poll-ms int)
(def notice-poll-ms 200)
;; The text after the last line break of `s`, or all of it.
(: (-> string string))
(defun (last-line s)
(let go ((rest s))
(match (string-split-once rest "\n")
((Some (Tuple _ after)) (go after))
(None rest))))
(: (-> LineEditor State))
(defun (start-state ed)
(State (text []) (cursor 0) (kill "") (killing? #f)
@ -412,9 +421,12 @@
(set! this-row (vec-add this-row w))
(set! col (+ col w))
(when (>= col width) (break!)))
;; A line break in a prompt ends its row, as one in the text does.
(defun (put-prompt! items)
(loop (:for item (in-vec items))
(:do (put! (cluster-shown item) (cluster-width item)))))
(:do (if (or (= item "\n") (= item "\r\n"))
(break!)
(put! (cluster-shown item) (cluster-width item))))))
(put-prompt! prompt)
(loop (:for c (in-vec text))
(:for i (range 0 (vec-length text)))