- (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>
5.1 KiB
5.1 KiB
bjoterm: the terminal for Bjolang programs
Goal
A package for programs that use the terminal directly: a line editor for a shell or a REPL, and full-screen programs. It owns the terminal while it is in raw mode, and gives it back as it found it.
Phase 1 implemented (2026-11-30). Where the code differs from the text below:
- Ctrl-J (byte 10) is
(KeyChar #\j)withctrl?, notKeyEnter: a REPL can bind it to a new line. Byte 8 isKeyBackspacewithctrl?. - Bracketed paste is the program's to turn on and off: leaving raw mode writes nothing.
- SIGTERM, SIGHUP and SIGQUIT end the process with no
finallyand noProcessExit(seeMainExit), so the glue restores the modes in a handler of its own, and does not cancel the signal. - SIGTSTP in raw mode with
#:signals? #trestores the modes and stops the process with SIGSTOP. decode-inputis exported: the decoder on bytes, for tests and recorded input.read-eventis#:sync. Each read polls on a thread of its own (LongRunning). An async form for fibers is for phase 2.- Under
script, a child that stops makesscriptstop itself; the test of stop and continue sends SIGCONT to both.
Decisions
- Linux only, for now. The termios layout,
TIOCGWINSZand the flag values are those of Linux (x86-64 and arm64 have the same ones). macOS has another layout; it is a later phase. On another systemterminal-openreturns(Err TerminalUnsupported). - stdin and stdout must both be terminals. Modes are set on fd 0, keys are read from fd 0, and the size is read from fd 1.
- Keys are read from fd 0 directly, with
pollandread, not through the standard input port: the port cannot wait with a timeout, and a lone ESC is told from the start of a sequence by a timeout (50 ms). Input that the port has already buffered is not seen. - .NET's
Consoleis not used.Console.ReadKeyandConsole.Inmake .NET set its own terminal modes, and set them again at SIGCONT and when a child process ends. The runtime's standard ports never touchConsole.In, so as long as a program does not, the modes are ours. - Raw mode is scoped.
(with-raw-mode term thunk)saves the modes, sets raw mode, runs the thunk, and restores the saved modes in afinally: also on an exception, on Ctrl-C or SIGTERM throughMainExit, and at process exit (ProcessExit). Nested calls are an error. -
What raw mode changes:
- off:
ECHO,ICANON,IEXTEN,ISIG,IXON,ICRNL,BRKINT,INPCK,ISTRIP;CS8on;VMIN1,VTIME0. OPOSTstays on, soprintlnstill starts the next line at column 0.#:signals? #tkeepsISIG: Ctrl-C, Ctrl-Z and Ctrl-\ then send their signals, as in cooked mode.
- off:
- SIGCONT while raw: the raw modes are set again, and the next event is
Resized, so the program draws its screen again. The registration cancels .NET's own SIGCONT handling, which would set .NET's modes. - Ctrl-Z in raw mode is a key.
terminal-suspendrestores the saved modes, sends SIGTSTP to our process group, and sets raw mode again when the process continues. -
Events are one union:
(TermKey key mods):modsis aModsrecord ofshift?,alt?,ctrl?.(TermPaste text): bracketed paste, when it is on.(TermResized cols rows): SIGWINCH, and after SIGCONT.TermEnd: end of input (the terminal hung up).
Keys are
(KeyChar c),KeyEnter,KeyTab,KeyBackspace,KeyEscape, the arrows,KeyHome,KeyEnd,KeyPageUp,KeyPageDown,KeyInsert,KeyDelete,(KeyF n), and(KeyUnknown text)for a sequence the decoder does not know. Names carry a prefix, so that a program can import the module without a prefix of its own. - Ctrl and a letter is
(KeyChar #\a)withctrl?. Tab, Enter (13 and 10), Backspace (127 and 8) and Escape are their own keys. ESC before a key is Alt. - The decoder is C# glue, as the read loop is. It returns a flat
record (kind, code, mods, text), and Bjolang maps it to the union, as
(std run)mapsJobStatus. Tests feed it bytes directly. - Output is strings. The module returns escape sequences as strings,
and the program writes them to stdout.
terminal-flushflushes the standard output port. Nothing is written behind the program's back, except that leaving raw mode turns off what the module turned on (bracketed paste).
Phases
- 1. The terminal. Package, glue,
terminal-open,with-raw-mode,terminal-size,read-eventwith and without a timeout, the decoder (CSI and SS3 keys with modifiers, Alt, UTF-8, bracketed paste),terminal-suspend, SIGWINCH and SIGCONT, escape sequences for the cursor, clearing, the alternate screen and styles. Tests: the decoder from bytes; raw mode, a key, a resize and restore underscript. - 2. The line editor. A prompt, editing keys (Emacs set), a history, completion through a function the program gives, a multi-line buffer for a REPL, and wide characters (East Asian width, combining marks).
- 3. Later. macOS, mouse events, the kitty keyboard protocol, a screen buffer that writes only what changed.