From dc8a3325b88999134a06dca9bd8e576a309a75a86d638550cea2dcc4a7e3cb42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linus=20Bj=C3=B6rnstam?= Date: Tue, 1 Dec 2026 11:47:24 +0100 Subject: [PATCH] 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 --- src/line.bjo | 20 ++++++++++++--- tests/prompt.bjo | 64 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 tests/prompt.bjo diff --git a/src/line.bjo b/src/line.bjo index a457b51..11eefbc 100644 --- a/src/line.bjo +++ b/src/line.bjo @@ -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))) diff --git a/tests/prompt.bjo b/tests/prompt.bjo new file mode 100644 index 0000000..1792643 --- /dev/null +++ b/tests/prompt.bjo @@ -0,0 +1,64 @@ +;; 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 a prompt of two lines: each drawing goes up to the +;; prompt's first row, and the continuation prompt is as wide as its last +;; line. +;; +;; bjo test tests/prompt.bjo +;; +;; The test runs itself again under `script` on a screen of 40 columns and +;; 20 rows. `script` does not keep a screen, so the test checks the escape +;; sequences 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) + (def ed (make-line-editor)) + (def prompt "\u{1B}[1mtop line\u{1B}[0m\n> ") + (loop (:for _ (range 0 2)) + (:do (match (line-read ed term prompt) + ((LineText text) (println (str "result: " (written text)))) + (_ (println "result: no text")))))))) + +(: (-> void)) +(defun (parent) + (def self (str "dotnet " (array-ref (command-line) 0))) + ;; "ab" a key at a time, then a second input of two lines: C-j starts the + ;; second line. + (def keys (string-join ["sleep 2" "printf a" "sleep 0.3" "printf b" "sleep 0.3" "printf '\\r'" "sleep 0.5" + "printf '(c'" "sleep 0.3" "printf '\\012'" "sleep 0.3" "printf 'd)\\r'" "sleep 0.5"] + "; ")) + (def command (str "(" keys ") | script -qec 'stty cols 40 rows 20; " 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)))) + (def redraw (str (cursor-up 1) clear-below "\u{1B}[1mtop line\u{1B}[0m\n> ")) + (expect-true "the input is read" (string-contains? out "result: \"ab\"")) + (expect-true "a key goes up to the prompt's first row, and draws it again" + (string-contains? out (str redraw "a"))) + (expect-true "and the next key too" (string-contains? out (str redraw "ab"))) + (expect-true "an input of two lines" (string-contains? out "result: \"(c\\nd)\"")) + (expect-true "the continuation prompt is as wide as the prompt's last line" + (string-contains? out "\n> (c\n d)")) + (unless (string-contains? out "result: \"(c") + (println out))) + +(defun (main args) + (cond + ((and (= (vec-length args) 1) (= (vec-ref args 0) "child")) (child)) + ((file-exists? "/usr/bin/script") (parent)) + (else (println "skipped: no script command"))) + 0)