bjoterm/tests/async.bjo
Linus Björnstam ce778f9b1e Async line-read, and the decoder and text handling in Bjolang
- read-event, line-read and the editor loop work in both colours. The
  leaf is read-bytes, a defbjouble: a bjoroutine awaits the glue's read,
  other code blocks. with-raw-mode takes a body of either colour.
- Cancelling the scope of a waiting line-read ends it and restores the
  terminal modes. The glue runs reads one at a time.
- The key decoder is (bjoterm keys), and widths, prompt escapes, search
  and common prefixes are (bjoterm text). Clusters and categories come
  from (std unicode grapheme) and (std unicode chars).
- Keys.cs and Text.cs are removed. The glue is termios, signals, poll
  and read.
- New tests: tests/async.bjo and tests/text.bjo.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 21:05:59 +01:00

104 lines
3.8 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` in a bjoroutine: it suspends while it waits, other fibers
;; run, and a deadline cancels it with the modes restored.
;;
;; bjo test tests/async.bjo
;;
;; The test runs itself again under `script`, as tests/terminal.bjo does.
(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 -------------------------------------------------------------
(: (-> string))
(defun (modes)
(match (run/output '(sh "-c" "stty -a </dev/tty"))
((Ok (Tuple _ text)) (if (string-contains? text "-icanon") "raw" "cooked"))
((Err e) (run-error-message e))))
;; Counts ticks of 50 ms until `stop` holds #t.
(: (-> (Box bool) int))
(defbjo (ticks stop)
(let go ((n 0))
(if (box-ref stop)
n
(begin
(sync (timeout (milliseconds 50)))
(go (+ n 1))))))
(: (-> LineEditor Terminal string))
(defbjo (read-with-ticks ed term)
(def stop (make-box #f))
(def counter (bjo (ticks stop)))
(def result (line-read ed term "> "))
(box-set! stop #t)
(def n (match (sync (promise-join counter))
((Ok n) n)
((Err _) -1)))
(str (match result
((LineText text) (str "read " (written text)))
(LineEof "eof")
(LineCancelled "cancelled"))
(if (> n 5) ", ticks ran" ", no ticks")))
(: (-> LineEditor Terminal string))
(defbjo (read-with-deadline ed term)
(match (try (with-deadline (milliseconds 400) (line-read ed term "> "))
#:catch (System.Exception))
((Ok _) "not cancelled")
((Err _) "cancelled")))
(: (-> (Promise string) string))
(defun (joined p)
(match (sync/blocking (promise-join p))
((Ok text) text)
((Err e) (str "raised " (.-Message e)))))
(: (-> void))
(defun (child)
(match (terminal-open)
((Err e) (println (str "open: " (terminal-error-message e))))
((Ok term)
(def ed (make-line-editor))
(println (str "first: " (joined (bjo (read-with-ticks ed term)))))
(println (str "deadline: " (joined (bjo (read-with-deadline ed term)))))
(println (str "after deadline: " (modes) ", raw? " (if (terminal-raw? term) "yes" "no")))
(println (str "next: " (joined (bjo (read-with-ticks ed term))))))))
;; ---- the parent ------------------------------------------------------------
(: (-> void))
(defun (parent)
(def self (str "dotnet " (array-ref (command-line) 0)))
;; The first line comes after 1.5 s of ticks. Nothing comes during the
;; deadline. The last line comes after it.
(def keys "sleep 2.5; printf 'hello\\r'; sleep 2.5; printf 'again\\r'; sleep 1")
(def command (str "(" keys ") | script -qec '" 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))))
(expect-true "the terminal opens" (not (string-contains? out "open:")))
(expect-true "line-read in a bjoroutine reads, and another fiber runs meanwhile"
(string-contains? out "first: read \"hello\", ticks ran"))
(expect-true "a deadline cancels line-read" (string-contains? out "deadline: cancelled"))
(expect-true "and the modes are restored" (string-contains? out "after deadline: cooked, raw? no"))
(expect-true "the next line-read works" (string-contains? out "next: read \"again\", ticks ran"))
(unless (string-contains? out "next: read")
(println out)))
(defun (main args)
(cond
((= args ["child"]) (child))
((file-exists? "/usr/bin/script") (parent))
(else (println "skipped: no script command")))
0)