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:
Linus Björnstam 2026-11-30 21:05:59 +01:00
parent 6863503fa7
commit ce778f9b1e
11 changed files with 970 additions and 733 deletions

234
src/text.bjo Normal file
View file

@ -0,0 +1,234 @@
;; 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/.
;; (bjoterm text): text on a terminal, as grapheme clusters and columns.
;;
;; - Clusters are extended grapheme clusters (UAX #29), from
;; (std unicode grapheme): what a reader sees as one character, such as
;; "e" with an accent, or an emoji with a skin tone.
;; - Widths follow `wcwidth`: 2 for East Asian wide and fullwidth
;; characters and for emoji shown as emoji, 0 for combining marks and
;; format characters, 1 otherwise. A terminal's own table can differ for
;; new emoji.
;; - A C0 control or DEL is shown as "^X", 2 columns. A C1 control
;; (U+0080 to U+009F) is shown as U+FFFD.
;; - A prompt can hold escape sequences (CSI, and OSC ended by BEL or ST).
;; `prompt-clusters` keeps each as one item of width 0.
(import (std prelude))
(import (std maths))
(import (std unicode grapheme))
(export
text-clusters prompt-clusters cluster-width cluster-shown text-width
word-cluster? find-clusters common-prefix)
;; The grapheme clusters of `s`. "\r\n" is one.
(: (-> string (Vec string)))
(defun (text-clusters s)
(string->graphemes s))
;; Whether code point `c` takes no columns: a non-spacing or enclosing
;; mark, or a format character other than the soft hyphen.
(: (-> int bool))
(defun (zero-width? c)
(and (not (= c 0xad))
(let ((cat (char-general-category (int->char c))))
(or (= cat 'Mn) (= cat 'Me) (= cat 'Cf)))))
;; Whether code point `c` is a letter or a decimal digit.
(: (-> int bool))
(defun (letter-or-digit? c)
(let ((cat (char-general-category (int->char c))))
(or (= cat 'Lu) (= cat 'Ll) (= cat 'Lt) (= cat 'Lm) (= cat 'Lo) (= cat 'Nd))))
(: (-> string int))
(defun (first-code s)
(char->int (string-cursor-ref s (string-cursor-start s))))
(: (-> string bool))
(defun (escape-sequence? c)
(and (> (string-length c) 1) (= (first-code c) 27)))
;; The columns `c` takes. 0 for an escape sequence from `prompt-clusters`.
(: (-> string int))
(defun (cluster-width c)
(cond
((string-empty? c) 0)
((escape-sequence? c) 0)
(else
(def first (first-code c))
(def width (code-width first))
(cond
((or (< first 32) (= first 127)) 2)
((and (>= first 128) (< first 160)) 1)
((= width 2) 2)
;; Emoji presentation, or a flag of two regional indicators.
((string-contains? c "\u{FE0F}") 2)
((and (>= first 0x1f1e6) (<= first 0x1f1ff) (> (string-length c) 1)) 2)
;; A cluster that starts with a mark: the widest of its parts.
((= width 0)
(let ((chars (string->vec c)))
(let go ((i 0) (w 0))
(if (= i (vec-length chars))
w
(go (+ i 1) (max w (code-width (char->int (vec-ref chars i)))))))))
(else width)))))
;; What is written for `c`: "^X" for a C0 control or DEL, U+FFFD for a C1
;; control, else `c`.
(: (-> string string))
(defun (cluster-shown c)
(cond
((string-empty? c) c)
((escape-sequence? c) c)
(else
(def first (first-code c))
(cond
((< first 32) (str "^" (char->string (int->char (+ first 64)))))
((= first 127) "^?")
((and (>= first 128) (< first 160)) "\u{FFFD}")
(else c)))))
;; The columns of `s`, escape sequences as 0.
(: (-> string int))
(defun (text-width s)
(loop (:for c (in-vec (prompt-clusters s)))
(:acc w (summing (cluster-width c)))))
;; Whether `c` is part of a word: it starts with a letter, a digit or "_".
(: (-> string bool))
(defun (word-cluster? c)
(and (not (string-empty? c))
(let ((first (first-code c)))
(or (= first 95) (letter-or-digit? first)))))
;; The width of one code point, as `wcwidth`.
(: (-> int int))
(defun (code-width c)
(cond
((= c 0) 0)
((or (= c 0x200b) (and (>= c 0x1160) (<= c 0x11ff))) 0)
((zero-width? c) 0)
((wide? c) 2)
(else 1)))
;; Ranges of East Asian Wide and Fullwidth code points, and of emoji with
;; emoji presentation by default (Unicode 15). Sorted pairs, inclusive.
(: (Vec int))
(def wide-ranges
[0x1100 0x115f 0x231a 0x231b 0x2329 0x232a 0x23e9 0x23ec 0x23f0 0x23f0
0x23f3 0x23f3 0x25fd 0x25fe 0x2614 0x2615 0x2648 0x2653 0x267f 0x267f
0x2693 0x2693 0x26a1 0x26a1 0x26aa 0x26ab 0x26bd 0x26be 0x26c4 0x26c5
0x26ce 0x26ce 0x26d4 0x26d4 0x26ea 0x26ea 0x26f2 0x26f3 0x26f5 0x26f5
0x26fa 0x26fa 0x26fd 0x26fd 0x2705 0x2705 0x270a 0x270b 0x2728 0x2728
0x274c 0x274c 0x274e 0x274e 0x2753 0x2755 0x2757 0x2757 0x2795 0x2797
0x27b0 0x27b0 0x27bf 0x27bf 0x2b1b 0x2b1c 0x2b50 0x2b50 0x2b55 0x2b55
0x2e80 0x303e 0x3041 0x33ff 0x3400 0x4dbf 0x4e00 0x9fff 0xa000 0xa4cf
0xa960 0xa97f 0xac00 0xd7a3 0xf900 0xfaff 0xfe10 0xfe19 0xfe30 0xfe6f
0xff00 0xff60 0xffe0 0xffe6 0x16fe0 0x16fe4 0x17000 0x18aff 0x1b000 0x1b2ff
0x1f004 0x1f004 0x1f0cf 0x1f0cf 0x1f18e 0x1f18e 0x1f191 0x1f19a 0x1f200 0x1f202
0x1f210 0x1f23b 0x1f240 0x1f248 0x1f250 0x1f251 0x1f260 0x1f265 0x1f300 0x1f320
0x1f32d 0x1f335 0x1f337 0x1f37c 0x1f37e 0x1f393 0x1f3a0 0x1f3ca 0x1f3cf 0x1f3d3
0x1f3e0 0x1f3f0 0x1f3f4 0x1f3f4 0x1f3f8 0x1f43e 0x1f440 0x1f440 0x1f442 0x1f4fc
0x1f4ff 0x1f53d 0x1f54b 0x1f54e 0x1f550 0x1f567 0x1f57a 0x1f57a 0x1f595 0x1f596
0x1f5a4 0x1f5a4 0x1f5fb 0x1f64f 0x1f680 0x1f6c5 0x1f6cc 0x1f6cc 0x1f6d0 0x1f6d2
0x1f6d5 0x1f6d7 0x1f6dc 0x1f6df 0x1f6eb 0x1f6ec 0x1f6f4 0x1f6fc 0x1f7e0 0x1f7eb
0x1f7f0 0x1f7f0 0x1f90c 0x1f93a 0x1f93c 0x1f945 0x1f947 0x1f9ff 0x1fa70 0x1faff
0x20000 0x2fffd 0x30000 0x3fffd])
(: (-> int bool))
(defun (wide? c)
(and (>= c 0x1100)
(let go ((lo 0) (hi (- (quotient (vec-length wide-ranges) 2) 1)))
(if (> lo hi)
#f
(let ((mid (quotient (+ lo hi) 2)))
(cond
((< c (vec-ref wide-ranges (* mid 2))) (go lo (- mid 1)))
((> c (vec-ref wide-ranges (+ (* mid 2) 1))) (go (+ mid 1) hi))
(else #t)))))))
;; The clusters of a prompt: as `text-clusters`, except that an escape
;; sequence is one item, of width 0.
(: (-> string (Vec string)))
(defun (prompt-clusters s)
(def chars (string->vec s))
(def n (vec-length chars))
(defun (text-of from to)
(string-join (vec-map char->string (vec-slice chars from (- to from))) ""))
(let go ((i 0) (plain 0) (out []))
(cond
((= i n)
(if (> n plain) (vec-append out (text-clusters (text-of plain n))) out))
(else
(def length (escape-length chars i))
(if (= length 0)
(go (+ i 1) plain out)
(let ((before (if (> i plain) (vec-append out (text-clusters (text-of plain i))) out)))
(go (+ i length) (+ i length) (vec-add before (text-of i (+ i length))))))))))
;; The length in characters of an escape sequence at `i`, or 0.
(: (-> (Vec char) int int))
(defun (escape-length chars i)
(def n (vec-length chars))
(defun (code j) (char->int (vec-ref chars j)))
(cond
((or (not (= (code i) 27)) (>= (+ i 1) n)) 0)
((= (code (+ i 1)) 91)
;; CSI: parameters and intermediates, then a final byte.
(let go ((j (+ i 2)))
(cond
((= j n) 0)
((and (>= (code j) 0x20) (<= (code j) 0x3f)) (go (+ j 1)))
((and (>= (code j) 0x40) (<= (code j) 0x7e)) (+ 1 (- j i)))
(else 0))))
((= (code (+ i 1)) 93)
;; OSC: ended by BEL, or by ST ("ESC \").
(let go ((j (+ i 2)))
(cond
((= j n) 0)
((= (code j) 7) (+ 1 (- j i)))
((and (= (code j) 27) (< (+ j 1) n) (= (code (+ j 1)) 92)) (+ 2 (- j i)))
(else (go (+ j 1))))))
(else 0)))
;; The index of the first cluster of `text` where the clusters of `query`
;; follow, or -1. Whole clusters only, case matters.
(: (-> string string int))
(defun (find-clusters text query)
(def hay (text-clusters text))
(def needle (text-clusters query))
(def last (- (vec-length hay) (vec-length needle)))
(defun (matches-at? i)
(let go ((j 0))
(cond
((= j (vec-length needle)) #t)
((= (vec-ref hay (+ i j)) (vec-ref needle j)) (go (+ j 1)))
(else #f))))
(let go ((i 0))
(cond
((> i last) -1)
((matches-at? i) i)
(else (go (+ i 1))))))
;; The longest common prefix of `texts`, whole clusters only. Empty for no
;; texts.
(: (-> (Vec string) string))
(defun (common-prefix texts)
(if (vec-empty? texts)
""
(let ((first (text-clusters (vec-ref texts 0))))
(defun (shared other)
(let go ((k 0))
(if (and (< k (vec-length first)) (< k (vec-length other))
(= (vec-ref first k) (vec-ref other k)))
(go (+ k 1))
k)))
(def length
(let go ((i 0) (n (vec-length first)))
(if (= i (vec-length texts))
n
(go (+ i 1) (min n (shared (text-clusters (vec-ref texts i))))))))
(string-join (vec-slice first 0 length) ""))))