Files
pipelines/internal/termstate/cell.go
T
alan e13cb99ade Terminals: keep screen state on the server; snapshot on reload and gaps
A headless xterm-compatible emulator (internal/termstate) follows each
machine terminal's PTY output under the same lock as the 1 MiB tail. A
terminal_output from before the retained tail (new page, reload, gap)
now gets output_snapshot: an escape stream that resets the client and
restores scrollback (1000 lines), main screen, the alternate screen when
in use (via ?1049h so the program's exit brings the shell back), saved
cursors, tabs, scroll region, cursor incl. pending wrap, modes (mouse
tracking/encoding, bracketed paste, DECCKM, keypad, autowrap, origin,
focus, ...), charsets, pen and title, as of output_end; frames continue
there. Reads inside the tail stay raw.

No Go library was adequate: charmbracelet/x/vt and vito/midterm scroll at
0.2-4 MB/s and hide modes/pen/region; vt10x is unmaintained. termstate
runs at 160-200 MB/s.

The app treats a snapshot reply as a replay without the dropped-output
line, and every replay now starts with RIS so stale modes end.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-26 00:56:49 +00:00

172 lines
4.1 KiB
Go

// Package termstate is a headless terminal emulator that follows one
// pseudo-terminal's output stream and can write a snapshot: an escape
// sequence stream that brings a freshly reset terminal to the same state
// (main screen with bounded scrollback, the alternate screen when it is in
// use, cursor, pen, scroll region, character sets and terminal modes).
//
// It interprets what xterm-compatible programs write (the subset of xterm
// that full-screen programs use, the same one the app's emulator follows);
// it never answers queries, since the clients' emulators do. It is built for
// throughput: a line scrolled off the top is moved, not copied, and blank
// cells past a line's written part are not stored.
package termstate
// Color is a cell colour: 0 is the default colour, colorIndexed|n is
// palette entry n, colorRGB|0xRRGGBB a direct colour.
type Color uint32
const (
colorIndexed Color = 1 << 24
colorRGB Color = 2 << 24
colorKind Color = 3 << 24
)
// Attribute flags of a cell (SGR).
const (
attrBold uint16 = 1 << iota
attrFaint
attrItalic
attrBlink
attrInverse
attrHidden
attrStrike
attrOverline
// The underline style (SGR 4:n, 1 to 5) is held in three bits.
attrUnderlineShift = 13
attrUnderlineMask = 7 << attrUnderlineShift
)
// Attr is the pen a cell was written with.
type Attr struct {
Fg, Bg Color
Flags uint16
}
func (a Attr) underline() int { return int(a.Flags&attrUnderlineMask) >> attrUnderlineShift }
func (a *Attr) setUnderline(style int) {
a.Flags = a.Flags&^attrUnderlineMask | uint16(style&7)<<attrUnderlineShift
}
// Cell kinds: a wide character takes its cell and a tail cell after it.
const (
cellNarrow uint8 = iota
cellWide
cellWideTail
)
// Cell is one character cell. R is 0 for a blank (never written or erased)
// cell, which still carries the background it was erased with.
type Cell struct {
R rune
A Attr
Kind uint8
}
// line is one row. Cells past len(cells) are blank with the default pen.
type line struct {
cells []Cell
// cont: the row was filled and the text went on in the next row (a soft
// wrap), so the two are one logical line.
cont bool
// marks are combining characters drawn on the cell at a column.
marks map[int]string
}
func (l *line) reset() {
l.cells = l.cells[:0]
l.cont = false
l.marks = nil
}
// ensure makes at least n cells addressable.
func (l *line) ensure(n int) {
if n <= len(l.cells) {
return
}
if n <= cap(l.cells) {
old := len(l.cells)
l.cells = l.cells[:n]
clear(l.cells[old:])
return
}
grown := make([]Cell, n, max(n, 2*cap(l.cells)))
copy(grown, l.cells)
l.cells = grown
}
// blank fills [from, to) with blank cells of background bg, truncating
// instead when the default background reaches the end.
func (l *line) blank(from, to int, bg Color) {
if from >= to {
return
}
l.dropMarks(from, to)
if bg == 0 && to >= len(l.cells) {
if from < len(l.cells) {
l.cells = l.cells[:from]
}
return
}
l.ensure(to)
fill := Cell{A: Attr{Bg: bg}}
for i := from; i < to; i++ {
l.cells[i] = fill
}
}
func (l *line) dropMarks(from, to int) {
if l.marks == nil {
return
}
for x := range l.marks {
if x >= from && x < to {
delete(l.marks, x)
}
}
}
// repair blanks half of a wide character whose other half is gone, at x.
func (l *line) repair(x int) {
if x < 0 || x >= len(l.cells) {
return
}
switch l.cells[x].Kind {
case cellWideTail:
if x == 0 || l.cells[x-1].Kind != cellWide {
l.clearCell(x)
}
case cellWide:
if x+1 >= len(l.cells) || l.cells[x+1].Kind != cellWideTail {
l.clearCell(x)
}
}
}
// clearCell blanks the cell at x, keeping its background.
func (l *line) clearCell(x int) {
l.cells[x] = Cell{A: Attr{Bg: l.cells[x].A.Bg}}
if l.marks != nil {
delete(l.marks, x)
}
}
// truncate cuts the row at width cols.
func (l *line) truncate(cols int) {
if len(l.cells) > cols {
l.cells = l.cells[:cols]
l.dropMarks(cols, int(^uint(0)>>1))
l.repair(cols - 1)
}
}
// empty reports whether nothing visible is on the row.
func (l *line) empty() bool {
for i := range l.cells {
if l.cells[i] != (Cell{}) {
return false
}
}
return true
}