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

View file

@ -27,6 +27,7 @@
(import (std prelude))
(import (std maths))
(import (bjoterm terminal))
(import (bjoterm text))
(export
LineEditor LineResult Completion
@ -34,16 +35,6 @@
line-history line-history-add! line-history-load! line-history-save
simulate-line)
(import/extern
(text-clusters (: Bjoterm.Text.Clusters (-> string (Array string)) #:no-raise))
(prompt-clusters (: Bjoterm.Text.PromptClusters (-> string (Array string)) #:no-raise))
(cluster-width (: Bjoterm.Text.Width (-> string int) #:no-raise))
(cluster-shown (: Bjoterm.Text.Shown (-> string string) #:no-raise))
(text-width (: Bjoterm.Text.TextWidth (-> string int) #:no-raise))
(word-cluster? (: Bjoterm.Text.IsWord (-> string bool) #:no-raise))
(common-prefix (: Bjoterm.Text.CommonPrefix (-> (Array string) string) #:no-raise))
(find-clusters (: Bjoterm.Text.Find (-> string string int) #:no-raise)))
;; ===========================================================================
;; Types
;; ===========================================================================
@ -183,6 +174,9 @@
;; by default, spaces as wide as `prompt`.
;; - Sets raw mode for the time it reads, unless the terminal is in raw
;; mode already.
;; - In a bjoroutine it suspends while it waits for keys; elsewhere it parks
;; the thread. Cancelling the scope around it ends it with the modes
;; restored.
;; - Moving: Left, Right, C-b, C-f; by word M-b, M-f, C-Left, C-Right; to
;; the line's start and end Home, End, C-a, C-e; between lines Up, Down.
;; - Deleting: Backspace, Delete, C-d; to the kill buffer C-k (to the end of
@ -195,14 +189,17 @@
;; - Enter accepts (see `#:input-done?`), M-Enter and C-j start a new line,
;; C-l clears the screen, C-z stops the program, C-c cancels, and C-d on
;; an empty buffer is end of input.
(: (-> LineEditor Terminal string (#:continuation (Option string)) LineResult) #:sync)
(: (-> LineEditor Terminal string (#:continuation (Option string)) LineResult))
(defun (line-read ed term prompt #:continuation None)
(def cont (match continuation
((Some c) c)
(None (string-pad-left "" (text-width prompt)))))
(if (terminal-raw? term)
(edit-loop ed term prompt cont)
(with-raw-mode term (fun () (edit-loop ed term prompt cont)))))
;; A `fun` is ordinary code, so the suspending copy passes a
;; bjoroutine.
(by-colour (#:sync (with-raw-mode term (fun () (edit-loop ed term prompt cont))))
(#:bjo (with-raw-mode term (bjoroutine () (edit-loop ed term prompt cont)))))))
(: (-> LineEditor State))
(defun (start-state ed)
@ -210,7 +207,7 @@
(hist-at (vec-length (record-ref ed history))) (draft [])
(search None) (tabbed? #f)))
(: (-> LineEditor Terminal string string LineResult) #:sync)
(: (-> LineEditor Terminal string string LineResult))
(defun (edit-loop ed term prompt cont)
(def out (parameter-ref current-output-port))
(def/mutable row 0)
@ -310,10 +307,10 @@
((Some s)
(def label (str "(" (if (record-ref s failed?) "failed " "")
"search `" (record-ref s query) "'): "))
(layout (array->vec (prompt-clusters label)) (array->vec (prompt-clusters cont))
(layout (prompt-clusters label) (prompt-clusters cont)
(record-ref st text) (record-ref st cursor) width))
(None
(layout (array->vec (prompt-clusters prompt)) (array->vec (prompt-clusters cont))
(layout (prompt-clusters prompt) (prompt-clusters cont)
(record-ref st text) (record-ref st cursor) width))))
(: (-> (Vec string) (Vec string) (Vec string) int int Layout))
@ -366,7 +363,7 @@
(: (-> string (Vec string)))
(defun (clusters s)
(array->vec (text-clusters s)))
(text-clusters s))
(: (-> State string State))
(defun (insert st s)
@ -521,7 +518,7 @@
((= (vec-length candidates) 0) (Bell st))
((= (vec-length candidates) 1) (Edited (replace-with (vec-ref candidates 0))))
(else
(def common (common-prefix (vec->array candidates)))
(def common (common-prefix candidates))
(cond
((> (vec-length (clusters common)) k) (Edited (replace-with common)))
(tabbed? (ShowList st candidates))