bjoterm/tests/line-terminal.bjo
Linus Björnstam 5a8a441cbc line-read: #:notices writes lines above the input
line-read calls the function every 200 ms while it waits for a key. The
lines it returns are written above the input, which is drawn again below
them. For a shell's "[1] Done" lines.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 09:50:09 +01:00

92 lines
4.3 KiB
Text

;; 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` on a real terminal: what it returns, and what it leaves on
;; the screen.
;;
;; bjo test tests/line-terminal.bjo
;;
;; The test runs itself again under `script`, and writes keys to it with
;; pauses between them, as tests/terminal.bjo does.
(import (std prelude))
(import (std simpletest))
(import (std run))
(import (bjoterm terminal))
(import (bjoterm line))
(import/extern
(temp-directory (: System.IO.Path.GetTempPath (-> string)))
(process-id (: System.Environment.ProcessId int #:get #:no-raise))
(command-line (: System.Environment.GetCommandLineArgs (-> (Array string)) #:no-raise)))
(: (-> string bool))
(defun (balanced? text)
(<= (loop (:for c (in-vec (string->vec text)))
(:acc d (summing (cond ((= c #\() 1) ((= c #\)) -1) (else 0)))))
0))
;; A notice when the parent has made the file `flag`, once.
(: (-> string (Vec string)))
(defun (notice flag)
(if (file-exists? flag)
(begin (ignore (file-delete flag)) ["notice"])
[]))
(: (-> string void))
(defun (child flag)
(match (terminal-open)
((Err e) (println (str "open: " (terminal-error-message e))))
((Ok term)
(def ed (make-line-editor #:input-done? (Some balanced?)))
(let go ()
(match (line-read ed term "> " #:notices (Some (fun () (notice flag))))
((LineText text) (println (str "read: " (written text))) (go))
(LineCancelled (println "cancelled") (go))
(LineEof (println (str "eof, raw: " (if (terminal-raw? term) "yes" "no")))))))))
(: (-> void))
(defun (parent)
(def self (str "dotnet " (array-ref (command-line) 0)))
(def flag (path-combine (temp-directory) (str "bjoterm-notice-" (int->string process-id))))
;; Each step waits, so that the child reads each key on its own.
(def keys (string-join ["sleep 2" "printf 'one\\r'" "sleep 0.3"
"printf '(a'" "sleep 0.2" "printf '\\r'" "sleep 0.2" "printf 'b)\\r'" "sleep 0.3"
"printf 'gone'" "sleep 0.2" "printf '\\003'" "sleep 0.3"
"printf '\\020'" "sleep 0.2" "printf '\\020'" "sleep 0.2" "printf '!\\r'" "sleep 0.3"
"printf 'par'" "sleep 0.3" (str "touch " flag) "sleep 0.6" "printf 't\\r'" "sleep 0.3"
"printf '\\004'" "sleep 1"]
"; "))
(def command (str "(" keys ") | script -qec 'stty cols 40 rows 20; " self " child " flag "' /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 terminal opens" (not (string-contains? out "open:")))
(expect-true "a line" (string-contains? out "read: \"one\""))
(expect-true "Enter in open brackets starts a new line" (string-contains? out "read: \"(a\\nb)\""))
(expect-true "the continuation prompt is as wide as the prompt" (string-contains? out "\n b)"))
(expect-true "C-c cancels" (string-contains? out "cancelled"))
;; The cursor goes to the end of the input with an escape sequence, so
;; "gone" and "^C" are not next to each other in the output.
(expect-true "and leaves the input with ^C" (string-contains? out "^C\ncancelled"))
;; C-p, not Up: Up in the two lines of the newer entry moves up a line.
(expect-true "C-p twice shows an older entry" (string-contains? out "read: \"one!\""))
(expect-true "a notice is written above the input"
(string-contains? out "notice\n"))
(expect-true "and the input is drawn again below it"
(match (string-split-once out "notice\n")
((Some (Tuple _ after)) (string-contains? after "> par"))
(None #f)))
(expect-true "the input goes on after it" (string-contains? out "read: \"part\""))
(expect-true "C-d on an empty line is end of input, and raw mode is over"
(string-contains? out "eof, raw: no"))
(unless (string-contains? out "eof, raw: no")
(println out)))
(defun (main args)
(cond
((and (= (vec-length args) 2) (= (vec-ref args 0) "child")) (child (vec-ref args 1)))
((file-exists? "/usr/bin/script") (parent))
(else (println "skipped: no script command")))
0)