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>
This commit is contained in:
parent
6863503fa7
commit
ce778f9b1e
11 changed files with 970 additions and 733 deletions
104
tests/async.bjo
Normal file
104
tests/async.bjo
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
;; 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)
|
||||
32
tests/text.bjo
Normal file
32
tests/text.bjo
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
;; 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/.
|
||||
|
||||
;; Clusters and widths of (bjoterm text).
|
||||
;;
|
||||
;; bjo test tests/text.bjo
|
||||
|
||||
(import (std prelude))
|
||||
(import (std simpletest))
|
||||
(import (bjoterm text))
|
||||
|
||||
(defun (main args)
|
||||
(expect "e and a combining accent is one cluster" (vec-length (text-clusters "e\u{301}x")) 2)
|
||||
(expect "an emoji with a skin tone is one cluster" (vec-length (text-clusters "👍🏽")) 1)
|
||||
(expect "CR LF is one cluster" (vec-length (text-clusters "\r\n")) 1)
|
||||
(expect "ASCII is 1 column" (text-width "abc") 3)
|
||||
(expect "CJK is 2 columns" (text-width "中文") 4)
|
||||
(expect "a combining accent is 0 columns" (text-width "e\u{301}") 1)
|
||||
(expect "a zero-width joiner is 0 columns" (cluster-width "\u{200D}") 0)
|
||||
(expect "the soft hyphen is 1 column" (cluster-width "\u{AD}") 1)
|
||||
(expect "an emoji is 2 columns" (text-width "👍🏽") 2)
|
||||
(expect "a flag is 2 columns" (text-width "🇸🇪") 2)
|
||||
(expect "a control is ^X, 2 columns" (cluster-width "\u{1}") 2)
|
||||
(expect "escape sequences in a prompt are 0 columns" (text-width "\u{1B}[1mλ>\u{1B}[0m ") 3)
|
||||
(expect-true "a letter is part of a word" (word-cluster? "ä"))
|
||||
(expect-true "a digit is part of a word" (word-cluster? "7"))
|
||||
(expect-true "_ is part of a word" (word-cluster? "_"))
|
||||
(expect-true "a bracket is not" (not (word-cluster? "(")))
|
||||
(expect "find-clusters does not match inside a cluster" (find-clusters "e\u{301}" "e") -1)
|
||||
(expect "common-prefix keeps whole clusters" (common-prefix ["e\u{301}a" "e\u{301}b"]) "e\u{301}")
|
||||
0)
|
||||
Loading…
Add table
Add a link
Reference in a new issue