bjoterm: raw mode, key events and escape sequences

- (bjoterm terminal): terminal-open, with-raw-mode, terminal-size,
  terminal-suspend, read-event and read-event/timeout, and escape
  sequences for the cursor, clearing, the alternate screen, bracketed
  paste and styles. Linux only.
- glue/Tty.cs: termios, poll on fd 0, SIGWINCH, SIGCONT, SIGTSTP, and
  restoring the modes at SIGTERM, SIGHUP, SIGQUIT and process exit.
- glue/Keys.cs: the decoder of CSI and SS3 keys with modifiers, Alt,
  UTF-8 and bracketed paste.
- Tests: the decoder from bytes, and raw mode on a pseudo-terminal under
  script. plan.org has the decisions and the phases.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Linus Björnstam 2026-11-30 20:31:29 +01:00
commit fe42387a4c
12 changed files with 1830 additions and 0 deletions

95
tests/keys.bjo Normal file
View file

@ -0,0 +1,95 @@
;; 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/.
;; The key decoder: bytes of terminal input in, events out.
;;
;; bjo test tests/keys.bjo
(import (std prelude))
(import (std simpletest))
(import (bjoterm terminal))
(: (-> (Vec int) (Array byte)))
(defun (bytes-of xs)
(def (: out (Array byte)) (make-array (vec-length xs)))
(loop (:for i (range 0 (vec-length xs)))
(:do (array-set! out i (cast byte (vec-ref xs i)))))
out)
(: (-> string (Array byte)))
(defun (utf8 s)
(bytestring->bytes (string->bytestring s)))
(: (-> (Array byte) string))
(defun (decoded bytes)
(string-join (vec-map event->string (decode-input bytes)) ", "))
(: (-> string string string void))
(defun (expect-keys name input expected)
(expect name (decoded (utf8 input)) expected))
(: string)
(def esc "\u{1B}")
(defun (main args)
;; Plain keys and control bytes.
(expect-keys "a letter" "a" "key a")
(expect-keys "Ctrl and a letter" "\u{1}" "key C-a")
(expect-keys "CR is Enter" "\r" "key <enter>")
(expect-keys "LF is Ctrl-J" "\n" "key C-j")
(expect-keys "Tab" "\t" "key <tab>")
(expect-keys "DEL is Backspace" "\u{7F}" "key <backspace>")
(expect-keys "BS is Ctrl-Backspace" "\u{8}" "key C-<backspace>")
(expect "NUL is Ctrl-Space" (decoded (bytes-of [0])) "key C-<space>")
(expect-keys "Ctrl-\\" "\u{1C}" "key C-\\")
(expect-keys "several keys" "ab" "key a, key b")
;; Escape and Alt.
(expect-keys "a lone ESC is Escape" esc "key <escape>")
(expect-keys "ESC and a key is Alt" (str esc "x") "key M-x")
(expect-keys "ESC and Ctrl-A" (str esc "\u{1}") "key C-M-a")
(expect-keys "ESC ESC is Alt-Escape" (str esc esc) "key M-<escape>")
(expect-keys "ESC [ alone is Alt-[" (str esc "[") "key M-[")
;; CSI keys.
(expect-keys "arrows" (str esc "[A" esc "[B" esc "[C" esc "[D")
"key <up>, key <down>, key <right>, key <left>")
(expect-keys "Home and End" (str esc "[H" esc "[F") "key <home>, key <end>")
(expect-keys "Ctrl-Up" (str esc "[1;5A") "key C-<up>")
(expect-keys "Alt-Left" (str esc "[1;3D") "key M-<left>")
(expect-keys "Shift-Right" (str esc "[1;2C") "key S-<right>")
(expect-keys "Ctrl-Shift-Alt-Up" (str esc "[1;8A") "key C-M-S-<up>")
(expect-keys "the ~ keys" (str esc "[2~" esc "[3~" esc "[5~" esc "[6~" esc "[1~" esc "[4~")
"key <insert>, key <delete>, key <pageup>, key <pagedown>, key <home>, key <end>")
(expect-keys "Ctrl-PageUp" (str esc "[5;5~") "key C-<pageup>")
(expect-keys "F5 to F12" (str esc "[15~" esc "[17~" esc "[21~" esc "[23~" esc "[24~")
"key <f5>, key <f6>, key <f10>, key <f11>, key <f12>")
(expect-keys "Shift-Tab" (str esc "[Z") "key S-<tab>")
(expect-keys "Alt-Up as ESC ESC [ A" (str esc esc "[A") "key M-<up>")
(expect-keys "Shift-F1 as CSI" (str esc "[1;2P") "key S-<f1>")
(expect-keys "an unknown sequence" (str esc "[99x") "key <unknown ^[[99x>")
(expect-keys "a cut sequence at the end" (str esc "[1;5") "key <unknown ^[[1;5>")
;; SS3 keys.
(expect-keys "F1 to F4" (str esc "OP" esc "OQ" esc "OR" esc "OS")
"key <f1>, key <f2>, key <f3>, key <f4>")
(expect-keys "application cursor keys" (str esc "OA" esc "OD") "key <up>, key <left>")
(expect-keys "ESC O alone is Alt-O" (str esc "O") "key M-O")
;; UTF-8.
(expect-keys "two bytes" "é" "key é")
(expect-keys "four bytes" "😀" "key 😀")
(expect-keys "letters between keys" (str "å" esc "[A" "ö") "key å, key <up>, key ö")
(expect "a cut character at the end" (decoded (bytes-of [195])) "key \u{FFFD}")
(expect "a byte that starts nothing" (decoded (bytes-of [255 97])) "key \u{FFFD}, key a")
(expect "a cut character before ASCII" (decoded (bytes-of [226 130 97])) "key \u{FFFD}, key a")
;; Bracketed paste.
(expect-keys "a paste" (str esc "[200~hello" esc "[201~") "paste \"hello\"")
(expect-keys "a paste makes line ends LF" (str esc "[200~a\r\nb\rc\nd" esc "[201~")
"paste \"a\\nb\\nc\\nd\"")
(expect-keys "keys in a paste are text" (str esc "[200~" esc "[A\u{3}" esc "[201~x")
(str "paste " (written (str esc "[A\u{3}")) ", key x"))
(expect-keys "a paste that does not end" (str esc "[200~abc") "")
(println "keys ok"))