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

94
plan.org Normal file
View file

@ -0,0 +1,94 @@
#+TITLE: 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)~ with ~ctrl?~, not ~KeyEnter~: a
REPL can bind it to a new line. Byte 8 is ~KeyBackspace~ with ~ctrl?~.
- 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 ~finally~ and no
~ProcessExit~ (see ~MainExit~), so the glue restores the modes in a
handler of its own, and does not cancel the signal.
- SIGTSTP in raw mode with ~#:signals? #t~ restores the modes and stops
the process with SIGSTOP.
- ~decode-input~ is exported: the decoder on bytes, for tests and recorded
input.
- ~read-event~ is ~#: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 makes ~script~ stop itself; the test
of stop and continue sends SIGCONT to both.
* Decisions
1. *Linux only, for now.* The termios layout, ~TIOCGWINSZ~ and 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 system
~terminal-open~ returns ~(Err TerminalUnsupported)~.
2. *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.
3. *Keys are read from fd 0 directly*, with ~poll~ and ~read~, 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.
4. *.NET's ~Console~ is not used.* ~Console.ReadKey~ and ~Console.In~ make
.NET set its own terminal modes, and set them again at SIGCONT and when a
child process ends. The runtime's standard ports never touch ~Console.In~,
so as long as a program does not, the modes are ours.
5. *Raw mode is scoped.* ~(with-raw-mode term thunk)~ saves the modes, sets
raw mode, runs the thunk, and restores the saved modes in a ~finally~:
also on an exception, on Ctrl-C or SIGTERM through ~MainExit~, and at
process exit (~ProcessExit~). Nested calls are an error.
6. *What raw mode changes*:
- off: ~ECHO~, ~ICANON~, ~IEXTEN~, ~ISIG~, ~IXON~, ~ICRNL~, ~BRKINT~,
~INPCK~, ~ISTRIP~; ~CS8~ on; ~VMIN~ 1, ~VTIME~ 0.
- ~OPOST~ stays on, so ~println~ still starts the next line at column 0.
- ~#:signals? #t~ keeps ~ISIG~: Ctrl-C, Ctrl-Z and Ctrl-\ then send
their signals, as in cooked mode.
7. *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.
8. *Ctrl-Z in raw mode is a key.* ~terminal-suspend~ restores the saved
modes, sends SIGTSTP to our process group, and sets raw mode again when
the process continues.
9. *Events* are one union:
- ~(TermKey key mods)~: ~mods~ is a ~Mods~ record of ~shift?~, ~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.
10. *Ctrl and a letter* is ~(KeyChar #\a)~ with ~ctrl?~. Tab, Enter
(13 and 10), Backspace (127 and 8) and Escape are their own keys.
ESC before a key is Alt.
11. *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)~ maps ~JobStatus~. Tests feed it bytes directly.
12. *Output is strings.* The module returns escape sequences as strings,
and the program writes them to stdout. ~terminal-flush~ flushes 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-event~ with 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 under ~script~.
- *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.