* key model for releases: KeyAction, SetKeyReport, focus and key report events, hold pairing A key event now carries its Action: KeyPress, KeyRepeat or KeyRelease. Every terminal still reports presses only, so Action stays KeyPress and every event is what v0.4.3 sent, until a game asks with SetKeyReport. The ask names the sources releases may come from (KeySourceConsole, KeySourceProtocol); an EventKeyReport answers each ask with the level reached and its source. No source exists yet, so every ask answers presses and nothing is written. KeyHolds pairs a release with its press by physical key, so a release carries what its press carried, and turns a source's lost reports into Lost releases. It is exported for hosts that read native key events (KeySourceHost). The reader sends keys through the level in force: at presses a repeat is a press and a release is dropped; at releases a key with no physical key behind it is a tap. Focus reports end every hold, Lost, before EventFocus; they are read only once a source turns them on. Mouse events gain a Touch bit for hosts. Terminal gains SetKeyReport, so this is v0.5.0. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01B3zCVjspRDuEuMrDTumVhp * protocol source: releases from the terminal's key event protocol, Alacritty's SetKeyReport with KeySourceProtocol asks the terminal whether it has the key event protocol (CSI ? u, fenced by a DA1 request), pushes its flags (all five, CSI > 31 u) and asks what they came to before reporting releases. A terminal must confirm event types, every key as an escape code and the text keys type; one that answers DA1 alone, takes the push without them, or answers nothing within two seconds stays at presses, and is not asked again. Detection goes by these answers only, never by TERM or the terminal's name. While the flags are pushed, key reports are decoded into the events a legacy terminal sends for the same keys, so a game can keep the protocol on for its whole run: Shift goes into the character, Ctrl with a character into its control key, Alt stays a modifier; Super, the locks, lone modifiers, media keys and F13 and up report nothing. Ctrl with a character that has no control key keeps ModCtrl. Releases pair with their presses through KeyHolds; a key the terminal names none for (text from an input method) is a tap. Focus reports come on with the protocol and end every hold, Lost, as focus leaves. Going back to presses releases what is held, pops the flags and stops focus reports, decoding until the terminal confirms the pop. Fini pops the flags before the alternate screen goes, and EmergencyReset when they are pushed. Fini now stops the input reader without holding the terminal's lock, since the reader writes its requests through it. Checked against Alacritty 0.13.2 on Xvfb with xdotool: 58 key combinations read identically with the protocol on and off (Ctrl+1 keeps ModCtrl), every press got its release, Shift released first still released 'A', focus loss and a release swallowed by Alacritty's Vi mode ended holds Lost. Inside tmux 3.4 the ask came to presses. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01B3zCVjspRDuEuMrDTumVhp --------- Co-authored-by: Claude <noreply@anthropic.com>
55 lines
2.2 KiB
Go
55 lines
2.2 KiB
Go
// Package terminal provides direct ANSI terminal control with zero-alloc rendering.
|
|
//
|
|
// # Features
|
|
//
|
|
// - True color (24-bit), 256-color, ANSI 16-color and colorless output, and
|
|
// the terminal's own default colors
|
|
// - Double-buffered output with cell-level diffing
|
|
// - Raw input parsing with escape sequence handling and bracketed paste
|
|
// - Key releases on request, through the terminal's key event protocol
|
|
// (Alacritty's), and presses on terminals without it
|
|
// - Drawing on /dev/tty when stdin or stdout is redirected (Unix)
|
|
// - Resize detection (SIGWINCH on Unix, callback on WASM)
|
|
// - Clean terminal restoration on exit/panic
|
|
//
|
|
// # Platform Support
|
|
//
|
|
// The package uses build tags to separate platform-specific code:
|
|
//
|
|
// - unix: Native terminal via termios, unix.Poll, SIGWINCH
|
|
// - wasm: Browser terminal via xterm.js JavaScript bridge
|
|
//
|
|
// # Architecture
|
|
//
|
|
// The Backend interface abstracts platform-specific operations:
|
|
//
|
|
// Backend (interface)
|
|
// ├── unixBackend (//go:build unix) - termios, raw I/O, signals
|
|
// └── wasmBackend (//go:build wasm) - syscall/js, callbacks
|
|
//
|
|
// Shared code (no build tags): Terminal interface, cell diffing, ANSI generation,
|
|
// escape sequence parsing.
|
|
//
|
|
// # WASM Integration
|
|
//
|
|
// WASM builds require JavaScript glue exposing these globals:
|
|
//
|
|
// goTerminalWrite(Uint8Array) // Go → JS: terminal output
|
|
// goTerminalInput(Uint8Array) // JS → Go: keyboard input
|
|
// goTerminalResize(cols, rows) // JS → Go: terminal resize
|
|
// xterm.cols, xterm.rows // Initial size query
|
|
//
|
|
// # Performance
|
|
//
|
|
// Output uses cell-level diffing. Only changed cells generate ANSI sequences.
|
|
// Style attributes are coalesced to minimize SGR calls. One goroutine writes
|
|
// what is rendered, so a terminal that stops reading never holds the caller:
|
|
// Flush keeps the newest frame until the write in flight returns, and Blocked
|
|
// reports how long it has waited.
|
|
// Input parsing is zero-allocation for common cases.
|
|
//
|
|
// This package bypasses terminfo/termcap entirely, emitting direct ANSI sequences.
|
|
// Target environments: Linux, macOS, BSDs with xterm-compatible terminals, and
|
|
// modern browsers with xterm.js.
|
|
package terminal
|