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:
parent
5a8a441cbc
commit
9ef9951c84
2 changed files with 80 additions and 4 deletions
20
src/line.bjo
20
src/line.bjo
|
|
@ -187,8 +187,9 @@
|
||||||
|
|
||||||
;; Reads one input from the terminal, with editing.
|
;; Reads one input from the terminal, with editing.
|
||||||
;; - `prompt` may contain escape sequences, such as colors; they take no
|
;; - `prompt` may contain escape sequences, such as colors; they take no
|
||||||
;; columns. `#:continuation` is the prompt of the lines after the first;
|
;; columns. It may contain line breaks: the input starts after its last
|
||||||
;; by default, spaces as wide as `prompt`.
|
;; 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
|
;; - Sets raw mode for the time it reads, unless the terminal is in raw
|
||||||
;; mode already.
|
;; mode already.
|
||||||
;; - In a bjoroutine it suspends while it waits for keys; elsewhere it parks
|
;; - 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)
|
(defun (line-read ed term prompt #:continuation None #:notices None)
|
||||||
(def cont (match continuation
|
(def cont (match continuation
|
||||||
((Some c) c)
|
((Some c) c)
|
||||||
(None (string-pad-left "" (text-width prompt)))))
|
(None (string-pad-left "" (text-width (last-line prompt))))))
|
||||||
(if (terminal-raw? term)
|
(if (terminal-raw? term)
|
||||||
(edit-loop ed term prompt cont notices)
|
(edit-loop ed term prompt cont notices)
|
||||||
;; A `fun` is ordinary code, so the suspending copy passes a
|
;; A `fun` is ordinary code, so the suspending copy passes a
|
||||||
|
|
@ -227,6 +228,14 @@
|
||||||
(: notice-poll-ms int)
|
(: notice-poll-ms int)
|
||||||
(def notice-poll-ms 200)
|
(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))
|
(: (-> LineEditor State))
|
||||||
(defun (start-state ed)
|
(defun (start-state ed)
|
||||||
(State (text []) (cursor 0) (kill "") (killing? #f)
|
(State (text []) (cursor 0) (kill "") (killing? #f)
|
||||||
|
|
@ -412,9 +421,12 @@
|
||||||
(set! this-row (vec-add this-row w))
|
(set! this-row (vec-add this-row w))
|
||||||
(set! col (+ col w))
|
(set! col (+ col w))
|
||||||
(when (>= col width) (break!)))
|
(when (>= col width) (break!)))
|
||||||
|
;; A line break in a prompt ends its row, as one in the text does.
|
||||||
(defun (put-prompt! items)
|
(defun (put-prompt! items)
|
||||||
(loop (:for item (in-vec 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)
|
(put-prompt! prompt)
|
||||||
(loop (:for c (in-vec text))
|
(loop (:for c (in-vec text))
|
||||||
(:for i (range 0 (vec-length text)))
|
(:for i (range 0 (vec-length text)))
|
||||||
|
|
|
||||||
64
tests/prompt.bjo
Normal file
64
tests/prompt.bjo
Normal file
|
|
@ -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)
|
||||||
Loading…
Add table
Add a link
Reference in a new issue