sugarcraft / sugar-vterm
In-memory virtual terminal emulator — parses ANSI byte stream into a cell grid.
Requires
- php: ^8.3
- react/promise: ^3.3
- react/stream: ^1.2
- sugarcraft/sugar-ansi-parser: dev-master
- sugarcraft/sugar-async: dev-master
- sugarcraft/sugar-core: dev-master
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.65
- phpstan/phpstan: ^2.1
- phpstan/phpstan-phpunit: ^2.0
- phpunit/phpunit: ^10.5
- sugarcraft/sugar-testing: dev-master
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-11-20 01:04:14 UTC
README
Formerly published as
sugarcraft/candy-vtwith namespaceSugarCraft\Vt(renamed 2026-10).
In-memory virtual terminal emulator — parses an ANSI byte stream into a cell grid with cursor, mode, SGR style, and hyperlink state.
Used as the terminal emulator behind
candy-vcr's render-tape pipeline — every frame produced
for a GIF is a Snapshot::of($terminal, $time) after the renderer feeds
output bytes into a Terminal instance.
Contents
- Text units
- Install
- Quickstart
- Architecture
- Two Terminal classes
- Parser state machine
- CSI coverage table
- OSC coverage
- SGR attributes
- Background Color Erase (BCE)
- Diff API
- Auto-wrap (DECAWM)
- SGR underline styles
- Scrollback buffer
- Origin mode (DECOM)
- Cursor shape (DECSCUSR)
- Focus events (mode 1004)
- Combining characters and wide characters
- Synchronized output (DEC 2026)
- Theme catalog
- Subsystems
- Trust boundary and hostile input
- Development
- Snapshot tests
- Related
- Credits & inspiration
Text units
Text moves through three levels, and this README uses one fixed term for each:
- code point — a single Unicode scalar, the unit the parser decodes from
UTF-8 bytes.
printChar()classifies every incoming code point by width, and charset designation translates only 7-bit code points. - grapheme — a user-perceived character: a base code point plus the
zero-width marks that attach to it. The grid stores the base cluster in
Cell::$char(whose emulator-facing alias isCell::$grapheme), with any attached marks inCell::$combining. - cell — one grid slot. A grapheme occupies one cell, or two when it is wide; the second slot is a continuation cell.
"Rune" and "graphic" are retired as unit names. Two spellings survive on
purpose: SGR keeps its ECMA-48 expansion, "Select Graphic Rendition",
because that names the sequence rather than a text unit, and code
identifiers such as the translate($charset, $rune) parameter keep their
own spelling.
Install
composer require sugarcraft/sugar-vterm
Quickstart
use SugarCraft\Vterm\Terminal\Terminal; $vt = Terminal::new(cols: 80, rows: 24); // Feed raw ANSI bytes from a terminal program. $vt->feed("\x1b[1;1H\x1b[31mhello\x1b[0m"); // Inspect the rendered screen. $screen = $vt->screen(); foreach ($screen->lines() as $row => $line) { echo "$row: $line\n"; } // Current cursor position. $cursor = $vt->cursor(); echo "cursor at {$cursor->row},{$cursor->col}\n";
The same flow, end to end and runnable, is examples/feed-and-screenshot.php
(php examples/feed-and-screenshot.php — feeds a scripted stream and prints
the resulting grid).
lines() echoes the raw grid contents — on bytes from anywhere but your own
program, treat those bytes as hostile and read
Trust boundary and hostile input before
echoing that output anywhere.
Captured-stream gotcha (ONLCR). A real terminal writes line breaks as
\r\nbecause the tty layer (ONLCR) expands each\non output. Feeding the emulator a raw\nreproduces exact VT semantics — the cursor moves down one row without resetting the column — so text captured with bare LFs (files, pipes, hand-written fixtures) stacks diagonally and the cell grid false-diffs against what a human saw on screen. Normalise\nto\r\n(collapsing existing\r\nfirst) beforefeed()when the source is not a tty capture — or callfeedCaptured(), which does this for you.
Architecture
| Layer | Class | Role |
|---|---|---|
| Facade | Terminal\Terminal |
Owns a Parser + ScreenHandler; feed() drives bytes in |
| Parser | SugarCraft\AnsiParser\Parser\Parser |
VT500 state machine (owned by sugar-ansi-parser; consumed via both facades — src/Terminal/Terminal.php and src/Terminal.php) — Paul Williams state machine, handles partial input, parses subparameters (:) |
| Handler | SugarCraft\AnsiParser\Parser\CsiHandler |
CSI sequence handler contract (owned by sugar-ansi-parser) — vterm's Parser\CsiHandlerImpl implements it |
| Handler | SugarCraft\AnsiParser\Parser\OscHandler |
OSC sequence handler contract (owned by sugar-ansi-parser) — vterm's Parser\OscHandlerImpl implements it |
| Handler | Handler\ScreenHandler |
Dispatches parser actions to Buffer / Cursor / Sgr / Mode |
| Screen | Screen\Screen |
Immutable snapshot — grid copy + cloned ring, so later scrolling never retro-mutates a captured screen |
| Buffer | Buffer\Buffer |
Cell grid — rows × cols of styled grapheme cells |
| Cursor | Cursor\Cursor |
Position + visibility + shape (the emulator-path class also carries the saved row/column pair) |
| SGR | Sgr\Sgr |
Current graphics rendition: foreground / background / attributes |
| Rendition | Rendition |
Double-height / double-width line rendition (ESC # 3–ESC # 6), stamped on cells |
| Charset | Charset\Charsets |
SCS character-set tables — G0–G3 designations, SO/SI locking shifts, SS2/SS3 single shifts |
| Mode | Mode\Mode |
DEC private mode flags (DECSET/DECRST) including DECAWM auto-wrap and DECOM origin mode ($originMode — origin is mode state, not cursor state; the emulator keeps a separate saved-origin slot per DECSC); altScreenVariant separates the four-way 47/1047/1048/1049 alt-screen semantics |
| Hyperlink | Hyperlink\Hyperlink |
OSC 8 URL + id tracker |
| Scrollback | Screen\Scrollback |
Ring buffer — stores rows that scroll off the top (default 1000) |
| Msg | Msg\FocusInMsg / FocusOutMsg |
Focus-in / focus-out event records (CSI I and CSI O, mode 1004) |
| Theme | Theme |
256-color palette + default fg/bg + factory methods for named themes |
| Catalog | Themes |
Bundled theme catalog: all() and v1() accessors |
Two Terminal classes
sugar-vterm ships two Terminal entry-points for distinct use cases —
both share the short class name Terminal, so import one per file or alias
the other (use SugarCraft\Vterm\Terminal\Terminal as FullTerminal;).
The collision that actually bites is Cursor: root SugarCraft\Vterm\Cursor
(the renderer value object — the one Snapshot::$cursor holds, a DISTINCT
class) versus SugarCraft\Vterm\Cursor\Cursor (the emulator state class
carrying the saved row/column pair and save()/restore()). Cell versus
Cell\Cell, by contrast, is not a two-class collision — the namespaced name
is a class_alias shim resolving to the canonical root Cell.
| Class | Use case | Methods |
|---|---|---|
SugarCraft\Vterm\Terminal\Terminal |
Full VT500 emulator — parses CSI/OSC/DCS, maintains Sgr/Mode/Hyperlink/Scrollback. | new(cols, rows, ?scrollbackSize, ?buffer, ?cursor, ?mode, ?onFocusEvent) factory — every param after rows is optional and named, so focus-event wiring or a pre-set DEC mode never needs positional fillers, feed(bytes, ?respond), feedAsync(bytes): PromiseInterface<string>, feedStream(ReadableStreamInterface, ?respond, ?cancellation): PromiseInterface<string>, replies(), flush(), screen(), cursor(), cols()/rows() (live grid dimensions read straight from the handler buffer — current after a resize(), no screen() rebuild), mode(), windowTitle() (null until the first title), palette(), clipboardEvents() and focusEvents() (both drain-on-read), resize(cols, rows), isWrapPending(), bells(): int (emulator-only, monotonic across RIS), enableAltScreen()/disableAltScreen()/isAltScreen(), plus with*() builders — withTabStops, withScrollbackSize and withCellPixels are supported API (withCellPixels($widthPx, $heightPx) sizes the advisory XTWINOPS 14t/16t replies), while withBuffer, withCursor, withMode and withWindowTitle carry an @internal mark in src/Terminal/Terminal.php: they are composition seams for tests and the parser path, not supported entry points. Grid intake is bounded by MAX_COLS/MAX_ROWS (4096 per axis) and MAX_SCROLLBACK (1 000 000 rows): new, resize() and withScrollbackSize() reject larger with InvalidArgumentException before allocating anything (see the grid-ceiling bullets under Trust boundary and hostile input). create() is deprecated — use new(). |
SugarCraft\Vterm\Terminal (root) |
Lightweight emulator used by candy-vcr's renderer — produces Snapshot value objects directly. |
new(cols, rows, ?Theme) factory (e.g. Terminal::new(80, 24, $theme) or named theme:), theme(), feed(bytes): self (mutates in place, returns $this), snapshot(float $time = 0.0): Snapshot (deep-copies the grid), revision(): int (counts feed()/flush() calls only — a direct grid() mutation does not bump it), cursor(): Cursor, grid(): Buffer (the LIVE mutable grid — a borrow, not a snapshot: put() through it changes the frame invisibly to revision()), windowTitle(): string (empty until the first title), isWrapPending(): bool, flush(): void (drains the parser so trailing OSC side effects land before the next snapshot()). enableAltScreen()/disableAltScreen() exist for interface parity but always throw \LogicException — alt-screen machinery lives only on the full Terminal\Terminal. |
Grid dimensions read live on both facades; only the shape differs: the root
exposes public readonly properties ($term->cols, $term->rows), the full
emulator exposes accessors ($term->cols(), $term->rows()) that track its
buffer across resize() without rebuilding a screen.
use SugarCraft\Vterm\Terminal; // root — renderer path use SugarCraft\Vterm\Theme; $vt = Terminal::new(cols: 80, rows: 24, theme: Theme::tokyoNight()); $vt = $vt->feed("\x1b[1;1H\x1b[31mhello\x1b[0m"); $snapshot = $vt->snapshot(time: 1.234);
The root Terminal mutates in place — feed() writes into the terminal's
own grid and returns $this purely for chaining (it is not an immutable
fluent builder). Frames are frozen explicitly: snapshot() deep-copies the
grid, so a captured Snapshot never retro-mutates as later feeds land. The
root path uses the HandlerAdapter + CsiHandlerImpl + OscHandlerImpl
triple internally. The full Terminal\Terminal also mutates in place
(feed(): void) — it's optimised for byte-stream ingest rather than
per-frame snapshots.
Constructor-shape detail (the factories above wrap real constructors whose positional signatures differ from the named-arg idiom):
- The emulator factory forwards to
__construct(cols, rows, ?Buffer, ?Cursor, ?Mode, scrollbackSize = 1000, ?onFocusEvent), so a directnew Terminal(80, 24, 5000)TypeErrors on?Buffer, not on?int— positional fillers afterrowsmust name their types, or use the factory. - The root facade forwards to
__construct(cols, rows, Buffer, Cursor, Parser, CsiHandlerImpl, OscHandlerImpl, ?Theme)— five required injected collaborators — so the full-path idiomnew(80, 24, 5000)TypeErrors on?Themehere.Terminal::new(80, 24, $theme)or namedtheme:is the supported shape.
Renderer value objects (vcr path)
The SugarCraft\Vterm root namespace provides the value objects behind the
candy-vcr VHS renderer path. They are not a parallel simplified class set:
the grid and the cell are the one Buffer\Buffer and the one Cell SHARED
by both engines — Cell\Cell is a class_alias shim over the canonical root
Cell (the two names resolve to the same class after autoload; pinned by
tests/CellAliasTest.php), and the emulator-facing payload fields simply
carry their defaults on the renderer path. What IS renderer-path state: the
root Terminal facade, Snapshot, the root Cursor value object, and
Theme:
use SugarCraft\Vterm\Buffer\Buffer; use SugarCraft\Vterm\Cell; use SugarCraft\Vterm\Cursor; // Cell — the one cell class for both engines: char/grapheme + palette fg (0-255) + bg (0-255) // + attrs bitfield; the emulator payload fields (sgr/truecolour/hyperlink/rendition) default here $cell = new Cell(char: 'X', fg: 196, bg: 21, attrs: Cell::ATTR_BOLD); $cell = $cell->withFg(34); // green foreground $cell = $cell->withBg(226); // yellow background $cell = $cell->withAttrs(Cell::ATTR_ITALIC | Cell::ATTR_UNDERLINE); // Buffer — the one 2D cell grid (emulator + renderer), mutable put(), dirty-region tracking $grid = new Buffer(cols: 80, rows: 24); $grid->put(0, 0, new Cell(char: 'H')); // in place, returns void $grid->put(0, 1, new Cell(char: 'i')); echo $grid->cell(0, 0)->char; // 'H' echo implode(',', $grid->dirtyRegion()); // minRow, maxRow, minCol, maxCol // On a never-dirtied grid those four are the sentinels (minRow/minCol = // PHP_INT_MAX, maxRow/maxCol = -1 — Buffer.php), so test emptiness with // maxRow < minRow, not by arithmetic on the corners. $grid = $grid->clear(); // NEW empty grid, same size; dirty box resets $grid = $grid->resize(100, 40); // NEW grid; content within bounds kept, // cuts discarded, edge wide-base erased, // whole viewport marked dirty for repaint // Cursor — row + col + shape + visibility $cursor = new Cursor(row: 0, col: 0, shape: 0, visible: true); $cursor = $cursor->at(5, 10); // move to row 5, col 10 $cursor = $cursor->withShape(2); // shape 2 = steady block $cursor = $cursor->hidden(); // hide cursor $cursor = $cursor->shown(); // show cursor
Parser handlers
Each handler translates parser actions into handler state mutations:
CursorHandler— CUP, HVP, CUU/CUD/CUF/CUB, CR, LF/VT/FF, BS, RI, HOME, DECSC/DECRC (ESC 7/ESC 8, sharing the general slot with the SCOCSI s/CSI ualiases)SgrHandler— SGR sequences (colour + attributes)EraseHandler— ED, EL, ECHScrollHandler— SU, SD, DECSTBMModeHandler— DECSET/DECRST private modes only (?-prefixed; the extended SM/RM mode numbers 10/19/20/29 are not implemented — bare forms stay inert on both engines; includes DECAWM mode 7)TabHandler— TBC, HTSOscHandler— OSC window title, hyperlink, colour paletteScreenHandler— orchestrates all of the above; owns the Buffer
Snapshot
SugarCraft\Vterm\Snapshot is an immutable point-in-time grid + cursor +
feed revision. Captures through Snapshot::of() or Terminal::snapshot()
deep-copy the grid, so a snapshot keeps showing the frame as it was even
after the terminal consumes more bytes:
use SugarCraft\Vterm\Snapshot; use SugarCraft\Vterm\Terminal; $terminal = Terminal::new(cols: 80, rows: 24)->feed("hello\r\n"); $snap = Snapshot::of($terminal, time: 1.234); // deep-copies grid + cursor $snap->grid; // Buffer — frozen copy, unaffected by later feeds $snap->cursor; // Cursor $snap->time; // float — virtual playback time $snap->revision; // int — terminal's feed counter at capture time $terminal->feed("world\r\n"); // live grid moves on... $snap->grid->cell(1, 0)->grapheme; // ' ' — snapshot's row 1 stayed blank $replay = Snapshot::of($terminal, time: 1.234); $snap->equals($replay); // false — the second feed changed the grid $snap->equalsWithTime($replay); // false — grid + cursor + time strict check
Build a Snapshot directly from its constructor only when you already own
the Buffer — the constructor stores what it is given without copying.
Snapshot::equals() is the dedup signal used by candy-vcr's
FrameDedup to collapse visually identical adjacent frames before GIF
encoding. Terminal::revision() gives the cheaper first pass: two captures
with the same revision saw no intervening feed()/flush() — note that a
direct grid() borrow-mutation (put()) changes the frame without bumping
the revision, so the counter tracks parser writes, not every borrow.
Parser state machine
The VT500 state machine is not in this package — it is owned by
sugar-ansi-parser as
SugarCraft\AnsiParser\Parser\Parser, a Paul Williams state machine
(reference) that drives a
AnsiParser\Parser\Handler interface with parsed actions. sugar-vterm
consumes it directly — both Terminal\Terminal and the root Terminal
import SugarCraft\AnsiParser\Parser\Parser by use statement
(src/Terminal/Terminal.php, src/Terminal.php) and feed it the same
byte chunks; what lives here is the screen-side
handler layer that the parser drives.
| Component | Owner | Role |
|---|---|---|
AnsiParser\Parser\State |
sugar-ansi-parser | Enum of the 15 parser states: Ground, Escape, EscapeIntermediate, CsiEntry, CsiParam, CsiIntermediate, DcsEntry, DcsParam, DcsIntermediate, DcsString, OscString, SosString, PmString, ApcString, Utf8. No CsiIgnore state exists — ignored CSI bytes are handled inside the transition table. |
AnsiParser\Parser\Action |
sugar-ansi-parser | Enum of the parser's per-event actions: None, Clear, Collect, Prefix, Dispatch, Execute, Start, Put, Param, Print. |
AnsiParser\Parser\Transitions |
sugar-ansi-parser | Static transition table — (state, byte) → (action, next_state). |
AnsiParser\Parser\Handler |
sugar-ansi-parser | The contract the parser drives. |
AnsiParser\Parser\CsiHandler |
sugar-ansi-parser | CSI dispatch contract — printable, cuu/cud/cuf/cub, cup/hvp, sgr, ed/el, decset/decrst, decstbm, tbc, cht/cbt, cr/lf, su/sd, il/dl, ich/dch, rep, scosc/scorc, gridRows/gridCols — 29 members, matching sugar-ansi-parser/src/Parser/CsiHandler.php. |
AnsiParser\Parser\OscHandler |
sugar-ansi-parser | OSC dispatch contract — title, hyperlink. |
AnsiParser\Parser\HandlerAdapter |
sugar-ansi-parser | Glue that wires CsiHandler + OscHandler into a single Handler for the parser. |
Parser\CsiHandlerImpl |
sugar-vterm | Default CSI handler used by the root Terminal renderer path. |
Parser\OscHandlerImpl |
sugar-vterm | Default OSC handler — tracks last window title. |
Parser\DebugHandler |
sugar-vterm | Local debug sink that dumps every parsed action. |
Parser\RendererHandler |
sugar-vterm | Screen-writing handler for the renderer path. |
Partial input is supported — feed any byte boundary and the parser
state persists across feed() calls. Subparameter colons
(CSI 4:2 m → double underline) are parsed natively.
The full emulator also speaks ReactPHP: feedAsync() parses a chunk and
resolves with the terminal→host reply bytes (DA1/CPR/XTWINOPS …) instead of
requiring the feed($bytes, $respond) callback, and feedStream() pumps a
ReadableStreamInterface (PTY output, socket) through the same parser —
incrementally per data event, flushed at end, rejecting on error or a
close that never signalled end-of-stream. The synchronous feed() surface is
unchanged; these are additive.
CSI coverage table
The default CsiHandlerImpl (used by SugarCraft\Vterm\Terminal) implements
the renderer-relevant subset of CSI dispatches. The full
Handler\ScreenHandler (used by SugarCraft\Vterm\Terminal\Terminal)
implements the superset.
| Final byte | Name | Method | Behaviour |
|---|---|---|---|
@ |
ICH | ich($n=1) |
Insert N blank characters at cursor. |
A |
CUU | cuu($n=1) |
Cursor up N, clamped to the buffer top (no scroll-region confinement — the renderer models no DECOM; the emulator confines the motion to the region only under origin mode). |
B |
CUD | cud($n=1) |
Cursor down N, clamped to the buffer bottom (no scroll-region confinement — the renderer models no DECOM; the emulator confines the motion to the region only under origin mode). |
b |
REP | rep($n=1) |
Repeat the preceding printable N times (renderer only; the emulator CSI dispatcher has no b arm and discards it). |
C |
CUF | cuf($n=1) |
Cursor right N, clamped to right margin. |
D |
CUB | cub($n=1) |
Cursor left N, clamped to column 0. |
E |
CNL | (Handler) | Cursor down N + column 0. |
F |
CPL | (Handler) | Cursor up N + column 0. |
G |
CHA | (Handler) | Cursor to column N. |
H |
CUP | cup($row, $col) |
Move cursor to (row, col) 1-indexed. |
I |
CHT | cht($n=1) |
Cursor forward N tab stops (both engines walk the tab-stop table through the shared TabHandler: defaults every 8 columns from 8, clamped to the last column when no stop remains). Shadowed while ?1004 h is set — see the focus I/O rows under DECSET modes. |
J |
ED | ed($mode=0) |
Erase display: 0=cursor→end, 1=begin→cursor, 2=all, 3=all+scrollback (the emulator drains its scrollback ring and leaves the visible grid alone; the renderer has no ring and arms 0/1/2 only, so mode 3 is a no-op there). |
K |
EL | el($mode=0) |
Erase line: 0=cursor→eol, 1=bol→cursor, 2=full line. |
L |
IL | il($n=1) |
Insert N blank lines at cursor. |
M |
DL | dl($n=1) |
Delete N lines at cursor. |
P |
DCH | dch($n=1) |
Delete N characters at cursor. |
S |
SU | su($n=1) |
Scroll up N lines. |
T |
SD | sd($n=1) |
Scroll down N lines. |
X |
ECH | (Handler) | Erase N characters from cursor (preserves BCE bg). |
Z |
CBT | cbt($n=1) |
Cursor backward N tab stops (both engines walk the tab-stop table through the shared TabHandler, clamped to column 0). |
d |
VPA | (Handler) | Cursor to row N. |
f |
HVP | hvp($row, $col) |
Same as CUP — move cursor to (row, col). |
g |
TBC | tbc($mode=0) |
Tab clear: 0=column, 3=all (both engines share the TabHandler clear semantics; unknown modes no-op). |
h |
DECSET (private, ?-prefixed only) |
decset($mode, $prefix) |
Set mode; bare SM without the ? prefix is inert on both engines (the emulator gate and CsiHandlerImpl::decset refuse it). ?7 h enables DECAWM, ?25 h cursor visible (?12 h accepted and discarded — xterm's blinking-cursor control, not a BCE gate; see Background Color Erase (BCE)), ?47/?1047/?1049 h alt screen, ?1004 h focus events, ?2026 h synchronized output, ?6 h DECOM, ?1000/?1001/?1002/?1003 h mouse tracking, ?1005/?1006/?1015 h mouse encoding, ?1048 h cursor save/restore, ?2004 h bracketed paste (the renderer's decset matches only ?7 and ?25 — every other mode named here is emulator-side state) — full roster below. |
l |
DECRST (private, ?-prefixed only) |
decrst($mode, $prefix) |
Reset mode (mirror of h; bare RM without the prefix is inert). |
m |
SGR | sgr($params) |
Set graphic rendition. Subparam colons (4:1..4:5) parsed. |
r |
DECSTBM | decstbm($top, $bottom) |
Set scroll region rows (1-indexed). |
s |
DECSC | scosc() |
Save cursor + pen; the renderer's snapshot is the position plus its foreground/background/attributes triple (truecolor included), while the emulator additionally saves origin mode, charset designations and GL. |
u |
DECRC | scorc() |
Restore cursor + pen; the renderer restores that position and pen triple when all three saved rendition parts are present, while the emulator additionally restores origin mode, charset designations and GL whenever its general saved-rendition slot is populated. |
<n> SP q |
DECSCUSR | (Handler) | Cursor shape; the 0–6 vocabulary maps to block/underline/bar × blink, out-of-vocabulary values are stored verbatim (unclamped). |
I / O |
Focus | (Handler) | Focus-in / focus-out recorded on the params-less CSI I / CSI O form real terminals emit under ?1004 h. While that mode is set the intercept runs before tab handling, so a parameterised CSI Ps I is consumed as focus-in and CHT is unreachable on the emulator path. |
(Handler) rows live in the full Handler/ScreenHandler dispatcher used
by SugarCraft\Vterm\Terminal\Terminal; the renderer's upstream
HandlerAdapter does not route those final bytes, so the renderer path
silently discards them. Rows with an explicit Method are implemented by
Parser/CsiHandlerImpl — the emulator implements a superset of those, save
the six asymmetries named below (one of them, b REP, is renderer-only).
Six asymmetries are deliberate: b REP (renderer only, the
emulator discards it), X ECH (emulator only), ED mode-3 scrollback
clearing (the emulator drains its ring and leaves the visible grid alone;
the renderer has no ring and arms 0/1/2 only, so mode 3 does nothing
there), DECOM origin mode (emulator only — with ?6 h set the emulator
confines cursor addressing to the scroll region, while the renderer always
clamps to the whole buffer), BCE erase fill (on the DEC model the emulator
carries the current SGR background into erased cells; the renderer's erases
write plain default cells), and the SGR rendition subset (the renderer
paints the five Cell::ATTR_* bits — see "SGR attributes" below). Tab
handling (I/Z/g) was formerly a seventh asymmetry: the renderer now
models tab stops with the same TabHandler arithmetic as the emulator, and
tests/EngineDifferentialTest.php's corpus polices the HT/CHT/CBT/HTS/TBC
streams. The
first four concern grid shape and text and stay out of
tests/EngineDifferentialTest.php's corpus on purpose; the last two differ
only in rendition, which that corpus's text-only cell encoding cannot see.
The renderer honours the same DECAWM/DECTCEM private modes as the emulator,
but cursor addressing clamps to the buffer rather than to any origin-mode
region, and it implements only part of the SGR set — see "SGR attributes"
below.
DECSET/DECRST mode roster (emulator ModeHandler)
The full emulator's Handler\ModeHandler tracks these private modes (all
?-prefixed; bare SM/RM stays inert on both engines). Mouse and paste state
lands on Mode fields the library exposes for consumers to read — decoded,
never encoded:
| Mode | Effect on set (h) |
|---|---|
| 6 | Mode::$originMode — DECOM, cursor rows relative to the scroll region. |
| 7 | Mode::$autoWrap — DECAWM wrap at the right margin. |
| 25 | DECTCEM cursor visibility (setCursorVisible). |
| 47 | Alt-buffer swap without save (enterAltScreenNoSave). |
| 1000 | Mode::$mouseAny — button press/release tracking flag. |
| 1001 | Mode::$mouseHighlights — highlight tracking flag. |
| 1002 | Mode::$mouseCellMotion — cell-motion tracking flag. |
| 1003 | Mode::$mouseExtended — any-event tracking flag. |
| 1004 | Mode::$reportFocusEvents — arms the CSI I/O focus intercept. |
| 1005 | Mode::$mouseUtf8 — UTF-8 coordinate-form flag. |
| 1006 | Mode::$mouseSgr — SGR coordinate-form flag. |
| 1015 | Mode::$mouseUrxvt — urxvt coordinate-form flag. |
| 1047 | Alt-buffer swap without save, same arm as 47. |
| 1048 | Cursor save/restore only (enterAltScreenCursorOnly) — no buffer swap. |
| 1049 | Full alt screen (enterAltScreen): save cursor, switch, clear. |
| 2004 | Mode::$bracketedPaste — flags that paste arrives bracketed. |
| 2026 | Mode::$syncUpdate — synchronized output queue (16384-pending-mutations budget, see Synchronized output (DEC 2026)). |
Mode 1 (DECCKM — application cursor keys) is intentionally unmodelled: it
is absent from the roster above, and ?1 h/?1 l are accepted and
discarded by both engines (no consumer in this repository reads a keyboard
transmit state).
Modes 1005/1006/1015 are independent flags (xterm switches each one on its
own): the reporting dialect a consumer should use is
Mode::effectiveMouseEncoding(), which resolves the set flags by xterm's
SGR > urxvt > UTF-8 > X10 priority, so clearing the winning mode falls to the
next flag still on rather than to X10. DECRQM answers each of the three
modes from its own flag. None of them change which tracking modes (1000–1003)
are armed. The library emits nothing for any mouse, focus or paste mode;
consumers read the Mode fields and build report bytes themselves.
The printable and execute paths also route through the same
adapter — printable bytes call CsiHandlerImpl::printable() (writes
the cell and defers the wrap at the right margin, exactly like the
emulator's ScreenHandler), and C0 controls (\b \t \n \r) call the corresponding cursor handler.
Line rendition and character sets (ESC forms)
CSI sequences are only half the picture; two ESC families change how
glyphs are drawn and which glyphs a byte means:
| Sequence | Name | Effect |
|---|---|---|
ESC # 3 |
DECDHL top | Line renders as the top half of a double-height line. |
ESC # 4 |
DECDHL bottom | Line renders as the bottom half of a double-height line. |
ESC # 5 |
DECSWL | Line returns to single width/height (rendition None). |
ESC # 6 |
DECDWL | Line renders double-width (a continuation cell follows each printed grapheme, when there is room). |
ESC ( <b> / ESC ) <b> / ESC * <b> / ESC + <b> |
SCS | Designate charset <b> into slot G0/G1/G2/G3. |
SO (0x0E) / SI (0x0F) |
LS1 / LS0 | Lock G1 (or back to G0) into the GL position for subsequent graphemes. |
SS2 / SS3 (C1 0x8E/0x8F) |
Single shift | Use G2/G3 for exactly the next grapheme. |
Line rendition is not a pen state — it is stamped on the cells
(Cell::$rendition), so it scrolls with the content and rides every
diff payload. Both engines implement ESC # (the emulator in
Handler/ScreenHandler, the renderer path via RendererHandler into
CsiHandlerImpl::escLineRendition); the designation and shift
machinery is emulator-side only — ScreenHandler holds the four
designatable slots ($charsets) plus the GL selection and pending
SS2/SS3, and restores all four slots to ASCII on RIS, DECSTR, and
DECALN. Only the GL-reachable designation translates 7-bit code points;
multi-byte UTF-8 always passes through untranslated.
Charset\Charsets carries the tables (ASCII B, DEC Special Graphics
0, UK A, No-Break Space U) and the translate() entry point.
Mode::$altScreenVariant (see Subsystems) is another
previously-undocumented surface: DECSET 47/1047 swap buffers
without saving, 1048 saves only the cursor, and 1049 saves buffer +
cursor + SGR with a full restore.
OSC coverage
The default OscHandlerImpl (renderer path) implements:
| Sequence | Method | Behaviour |
|---|---|---|
OSC 0;<title> BEL/ST |
title($title) |
Set window title. |
OSC 1;<name> BEL/ST |
title($name) |
Set icon name — folded onto the same title channel; both engines route 0/1/2 into one slot and store no separate icon name. |
OSC 2;<title> BEL/ST |
title($title) |
Set window title. |
OSC 8;<id>;<uri> BEL/ST |
hyperlink($uri, $id) |
Begin/end hyperlink (OSC 8). Empty URI ends the link. |
Both handler contracts also expose a lastTitle() accessor — the most recent
title set during the stream (not a wire sequence, hence listed here rather
than in the table).
The full Handler\OscHandler (used by SugarCraft\Vterm\Terminal\Terminal)
also handles palette set (OSC 4 — writes only, no query reply;
consumers that need OSC 4/10/11/104 replies maintain their own
palette above the library) and
clipboard get/set requests (OSC 52). The OSC 52 log is a bounded ring:
a remote program can arm OSC 52 itself, so past 1024 entries the oldest
event is dropped. Drain it with Terminal::clipboardEvents(), which
returns and clears the backlog (like replies()). A read request is
recorded, never answered: the library owns no output channel, so emitting
the reply (OSC 52 ; p ; <base64> ST) is the consumer's duty on the host
it mirrors. OSC 10/OSC 11
(default foreground/background query and set) are discarded — the
emulator keeps no host-window or default-colour state. Focus and
mode-query events are CSI-driven, not OSC: see the focus-reporting and
DEC 2026 sections below.
SGR attributes
Sgr\Sgr tracks the current pen state. Supported attributes (the emulator
pen carries the full roster, but the renderer's CsiHandlerImpl lights
only the five Cell::ATTR_* bits: 2/5/6/8 have no renderer arm
and are accepted as parity no-ops, and every underline style collapses
onto the single underline bit. Consumers that paint from the renderer's
grid — candy-vcr's TapeToGif included — therefore draw faint, blinking
and hidden text at full brightness). This subset is a deliberate
modeling choice, not an oversight: extend the bitfield only when a real
consumer needs faint/blink/hidden in rendered snapshots (sugar-vterm-091
revisit note — recorded, no action):
| SGR param | Effect |
|---|---|
0 |
Reset all attributes. |
1 |
Bold. |
2 |
Faint (pen bit only — no renderer arm). |
3 |
Italic. |
4 (or 4:1) |
Single underline. |
4:2 / 4:3 / 4:4 / 4:5 |
Double / Curly / Dotted / Dashed underline. |
5 / 6 |
Blink (slow and rapid fold onto one pen bit; no renderer arm). |
7 |
Inverse (swap fg/bg). |
8 |
Hidden (conceal) — tracked on the pen, no renderer arm. Screen::lines() is a TEXT channel: concealed text still appears in extracted content, so spoiler/secrets hygiene is the consuming host's job. |
9 |
Strikethrough. |
21 |
Accepted and ignored — the SGR handler has no arm for it. |
22 |
Reset bold + faint. |
23 / 24 / 25 / 27 / 28 / 29 |
Reset italic / underline / blink / inverse / hidden / strikethrough. |
30..37 / 90..97 |
Set 8 + 8 bright ANSI foreground. |
38;5;<n> / 38;2;<r>;<g>;<b> |
256-color / truecolor foreground. |
40..47 / 100..107 |
Set 8 + 8 bright ANSI background. |
48;5;<n> / 48;2;<r>;<g>;<b> |
256-color / truecolor background. |
39 / 49 |
Reset fg / bg to default. |
58;5/58;2 / 59 |
Underline-colour specifiers consumed and discarded (no per-style colour state). |
Attribute bitfield exposed as Cell::ATTR_* constants:
ATTR_BOLD, ATTR_ITALIC, ATTR_UNDERLINE, ATTR_INVERSE,
ATTR_STRIKETHROUGH.
Accessibility (blink and motion)
SGR 5 and 6 (slow/rapid blink) fold onto a single Sgr::$blink bit, and
only the emulator tracks it — the renderer path's cell attribute model
carries no blink bit at all. DECSCUSR (CSI <n> SP q) records the
blinking-versus-steady cursor shapes verbatim in the CursorShape
enum. Nothing in vterm animates; it only records. A host that lights a
blink or a blinking cursor from that record should honour the user's
no-blink preference and prefers-reduced-motion (WCAG 2.2.2 Pause,
Stop, Hide): suppress the animation and render the steady form. The
sugarcraft site's reduced-motion chrome covers page transitions only —
the VHS demo GIF loops regardless of viewer settings.
Background Color Erase (BCE)
On the DEC model, erase operations (CSI K, CSI J, CSI X) fill cleared
cells with the current SGR background color instead of a plain blank cell —
BCE is always on, there is no gate. DEC private mode 12 (CSI ?12 h) is
xterm's blinking-text-cursor control, not a BCE switch, and neither engine
models it: it is accepted and discarded. BCE prevents "ghosting" in
terminals with coloured backgrounds — the erased area retains its visual
background rather than going black. Scrolled-in rows are the documented
exception: the blanks IL/DL/SU/SD insert stay default cells (see
ScrollHandler).
use SugarCraft\Vterm\Color\Color; // color value object use SugarCraft\Vterm\Terminal\Terminal; $vt = Terminal::new(cols: 80, rows: 24); $vt->feed("\x1b[48;5;196m"); // red background — BCE needs no gate $vt->feed("\x1b[2J"); // erase screen — cells carry red bg $bg = $vt->screen()->cell(0, 0)->background(); // ?Color on the erased cell echo $bg->value, "\n"; // 196 — palette slot ($bg->kind is 2 = indexed-256) echo Color::truecolor(255, 0, 0)->red(), "\n"; // 255 — truecolor splits via red()/green()/blue()
There is no separate BCE flag to track: the erased fill reads the Sgr
object's background colour directly, so the pen must be set before the
erase sequence for the background to carry through.
Diff API
After feeding bytes, snapshot the screen and compare:
use SugarCraft\Vterm\Terminal\Terminal; $vt = Terminal::new(cols: 80, rows: 24); $vt->feed("before\r\n"); $before = $vt->screen(); $vt->feed("after\r\n"); $after = $vt->screen(); foreach ($before->diff($after) as $change) { // Each change is ['row' => int, 'col' => int, 'prev' => Cell, 'next' => Cell]. echo "{$change['row']},{$change['col']}: '{$change['prev']->grapheme}' → '{$change['next']->grapheme}'\n"; }
Auto-wrap (DECAWM)
DECAWM (CSI ? 7 h / CSI ? 7 l) controls whether the cursor
automatically wraps to the next line when a character is printed at the
rightmost column. The Mode object exposes this as:
use SugarCraft\Vterm\Terminal\Terminal; $vt = Terminal::new(cols: 80, rows: 24); // Query the current state. $mode = $vt->mode(); var_dump($mode->autoWrap); // bool // Build a new mode with auto-wrap forced on/off. $wrapped = $mode->withAutoWrap(true);
When auto-wrap is on (the default, matching xterm/VT/ANSI.SYS), a grapheme landing in the
rightmost column parks the cursor there, arming a deferred (phantom)
wrap; the next grapheme consumes it, moving to column 0 of the next row
before writing — and if that row is within a scroll region the region
scrolls up by one line, matching VT100 behaviour. When off, the
cursor clamps at the rightmost column and subsequent graphemes overwrite
it (the phantom flag is set by geometry alone and consulted with the mode at
the next write, so a mid-stream ?7l / ?7h toggle changes the pending
wrap's outcome exactly as xterm's does — never at arming time).
Scroll regions (DECSTBM, CSI r) and auto-wrap interact correctly:
wrapping at the bottom of a scroll region triggers a scroll within that
region, not on the whole buffer.
SGR underline styles
SGR CSI 4:N controls the underline style. The Sgr object exposes
this as an UnderlineStyle enum and a fluent setter:
use SugarCraft\Vterm\Sgr\Sgr; use SugarCraft\Vterm\Sgr\UnderlineStyle; // Build an Sgr state with a specific underline style. $sgr = Sgr::empty()->withUnderlineStyle(UnderlineStyle::Double); echo $sgr->underline; // true echo $sgr->underlineStyle->value; // 2 (Double)
UnderlineStyle values map to CSI sequences as follows:
| Enum value | CSI sequence | Description |
|---|---|---|
None |
CSI 4:0 m |
No underline |
Single |
CSI 4:1 m |
Single underline |
Double |
CSI 4:2 m |
Double underline |
Curly |
CSI 4:3 m |
Curly underline |
Dotted |
CSI 4:4 m |
Dotted underline |
Dashed |
CSI 4:5 m |
Dashed underline |
CSI 24 m clears underline (any style). Plain CSI 4 m (no subparam)
is equivalent to 4:1 (single).
Scrollback buffer
When the screen scrolls (LF at the bottom, DECSTBM scroll region, or
auto-wrap at the bottom of a region), rows pushed off the top are stored
in a ring-buffer Scrollback. Access it via:
use SugarCraft\Vterm\Terminal\Terminal; $vt = Terminal::new(cols: 80, rows: 4); foreach (range(1, 6) as $n) { $vt->feed("line $n\r\n"); // each LF at the bottom row scrolls one off: lines 1-3 land in the ring } $screen = $vt->screen(); $scrollback = $screen->scrollback(); foreach ($scrollback->all() as $rowIndex => $row) { // $row is array<int, Cell> echo "scrollback row $rowIndex\n"; }
The default capacity is 1000 rows. Configure it at construction or retrospectively:
use SugarCraft\Vterm\Terminal\Terminal; // Set at construction. $vt = Terminal::new(cols: 80, rows: 24, scrollbackSize: 5000); // Retrospective change — the ring is replaced with a fresh one: // All accumulated history is dropped, on grow and on shrink alike. $vt = $vt->withScrollbackSize(5000);
Available Scrollback accessors:
use SugarCraft\Vterm\Terminal\Terminal; $scrollback = Terminal::new(cols: 80, rows: 24)->screen()->scrollback(); $scrollback->count(); // int — rows currently stored $scrollback->maxSize(); // int — configured capacity $scrollback->at(0); // array<int, Cell>|null — row offset N from oldest $scrollback->all(); // array<int, array<int, Cell>> — all rows oldest→newest $scrollback->clear(); // void — drop every stored row (ED mode 3, `CSI 3 J`)
The scrollback is consumed by ScreenHandler's scroll-up and scroll-down
operations (SU/SD/IND/RI/NEL). Erase-of-scrollback (CSI 3 J) clears the
ring buffer.
Origin mode (DECOM)
DECOM (CSI ?6 h / CSI ?6 l) controls whether cursor addressing is
relative to the absolute screen origin or to the scroll region defined by
DECSTBM. The Mode object exposes this as:
use SugarCraft\Vterm\Terminal\Terminal; $vt = Terminal::new(cols: 80, rows: 24); $mode = $vt->mode(); var_dump($mode->originMode); // bool $origin = $mode->withOriginMode(true);
When origin mode is on, the cursor is constrained to the scroll region
and cursor-addressing commands (CSI row;col H, CSI row;col f) treat
(1,1) as the top-left of the DECSTBM region. When off (the default),
(1,1) is the absolute top-left of the screen. Leaving origin mode
(CSI ?6 l) flips only the mode: the DECSTBM region stays armed and is
re-adopted when origin mode is re-enabled.
Cursor shape (DECSCUSR)
DECSCUSR (CSI Ps SP q) sets the terminal cursor shape. The handler records it
on two fields — Cursor::$shape, the copy a snapshot host reads, and
Mode::$cursorShape, the copy the mode state carries — which the handler writes
together and keeps equal across every rebuild of the cursor; the parser-side
renderer never reaches this sequence (SP q is discarded there — see the
renderer-silences roster under the CSI table). Mode exposes the
value as an integer:
use SugarCraft\Vterm\CursorShape; use SugarCraft\Vterm\Terminal\Terminal; $vt = Terminal::new(cols: 80, rows: 24); $mode = $vt->mode(); var_dump($mode->cursorShape); // int — 0–6 for well-formed input, stored verbatim otherwise // Build a new mode with a specific cursor shape. $bar = $mode->withCursorShape(CursorShape::SteadyBar->toInt());
The DECSCUSR parameter (CSI <n> SP q) maps to CursorShape cases. In VT
vocabulary 0 means "default appearance" and is a blinking block, and
CursorShape::fromInt() folds both 0 and 1 onto BlinkingBlock:
| Enum value | CSI param | Sequence | Description |
|---|---|---|---|
BlinkingBlock |
0 or 1 |
CSI 0/1 SP q |
Blinking block |
SteadyBlock |
2 |
CSI 2 SP q |
Steady block |
BlinkingUnderline |
3 |
CSI 3 SP q |
Blinking underline |
SteadyUnderline |
4 |
CSI 4 SP q |
Steady underline |
BlinkingBar |
5 |
CSI 5 SP q |
Blinking vertical bar |
SteadyBar |
6 |
CSI 6 SP q |
Steady vertical bar |
The 0–6 vocabulary is not enforced at parse time: the handler stores the
raw CSI Ps parameter verbatim on both fields, so CSI 9 SP q records the
integer 9. Only readers that convert through CursorShape::fromInt() see an
enum, and its default arm silently folds any unknown value onto
BlinkingBlock — an adversarial shape parameter round-trips as a rewritten
block rather than an error.
Across the reset and screen-swap paths the shape follows xterm-411: RIS
(ESC c) and DECSTR (CSI ! p) reset it to 0 on both fields, while DECALN
(ESC # 8) and the alt-screen swaps (DECSET 1048/1049) preserve it. Injecting
both a Cursor and a Mode into ScreenHandler is the one documented case
where the pair is taken exactly as supplied — nothing reconciles a disagreeing
pair there, which is why the two fields are guaranteed equal only for
terminals the facade built itself. DECSTR
resets it because xterm's soft reset shares RIS's cursor block, and RIS
re-invokes InitCursorShape() — xterm's internal that re-derives the cursor
style from the cursorStyle resource (the patch #411 cursor-style machinery)
— so a DECSCUSR setting
does not outlive a soft reset.
Focus events (mode 1004)
When focus event reporting is enabled (CSI ?1004 h / CSI ?1004 l),
the terminal records focus-in (CSI I) and focus-out (CSI O) events.
The Mode object tracks the reporting flag:
use SugarCraft\Vterm\Terminal\Terminal; $vt = Terminal::new(cols: 80, rows: 24); $vt->feed("\x1b[?1004h"); // start reporting focus in/out $mode = $vt->mode(); var_dump($mode->reportFocusEvents); // bool — true under mode 1004 $tracking = $mode->withReportFocusEvents(true);
Events land in a bounded focus ring inside the handler (drop-oldest past
1024 entries — a hostile stream can arm mode 1004 and flood CSI I/O),
as FocusInMsg / FocusOutMsg value objects. The emulator facade drains
it: Terminal::focusEvents() returns the backlog oldest-first and clears
it, so poll after each feed. For live notification instead of draining,
pass an onFocusEvent callback to the constructor.
Combining characters and wide characters
ScreenHandler::printChar() categorises every incoming code point by width:
- Wide (width ≥ 2): CJK and most emoji occupy 2 cells. A
Cell::continuation()marker is written to the second cell and inherits SGR + hyperlink from the base cell. The cursor advances by the full width. - Normal (width 1): Written to a single cell.
- Combining mark (width 0): Zero-width code points are not silently discarded.
Attaching is driven by the sugar-core
Width::stringclassification, not by a fixed code point range: every zero-width code point (U+0300–U+036F plus the full U+1AB0–U+1AFF, U+1DC0–U+1DFF, U+20D0–U+20FF, U+FE00–U+FE0F, U+FE20–U+FE2F ranges, variation selectors, ZWJ/ZWNJ — anythingWidth::stringscores 0) is attached to the preceding cell viaCell::withCombining()so the base character and its marks render as a single composed grapheme in one column:
use SugarCraft\Vterm\Terminal\Terminal; $vt = Terminal::new(cols: 80, rows: 24); $vt->feed("\x1b[31me\xcc\x81"); // 'e' + combining acute accent → 'é' $cell = $vt->screen()->cell(0, 0); echo $cell->grapheme, "\n"; // 'e' echo $cell->combining, "\n"; // "\xcc\x81" — attach to grapheme for full cluster
Cell::$combining is a plain string; $cell->withCombining($mark) appends
to it. The $combining field is compared in Cell::equals() so snapshots
that include composed characters compare correctly.
Combining marks that arrive at column 0 or immediately after a wide-char continuation cell are silently dropped — nothing in the preceding column to attach to.
Synchronized output (DEC 2026)
DEC 2026 (CSI ?2026 h / CSI ?2026 l) enables synchronized output
mode. While active, all buffer mutations (character writes, combining
attachments, erase fills) are held in a queue on ScreenHandler instead
of being applied immediately. When the mode is disabled (CSI ?2026 l),
all queued mutations are replayed atomically. This prevents mid-sequence
screen updates from being visible to the user, matching xterm's batched
update behaviour.
use SugarCraft\Vterm\Terminal\Terminal; $vt = Terminal::new(cols: 80, rows: 24); $mode = $vt->mode(); var_dump($mode->syncUpdate); // bool — false until synchronized output is set $vt->feed("\x1b[?2026h"); // enter synchronized output mode $vt->feed("\x1b[31mA\x1b[0m"); // queued, not yet visible $vt->feed("\x1b[?2026l"); // flush → both cells appear together var_dump($vt->screen()->cell(0, 0)->grapheme); // 'A'
Mutations are queued on the handler's internal synchronized-output queue and replayed atomically when the window closes. BCE erase and combining-char attachments are both eligible for queued writes. The queue is bounded: past 16384 pending mutations a partial flush lands: memory stays bounded, the price is a torn frame, not lost bytes — all-or-nothing is surrendered only under hostile pressure.
Theme catalog
Theme provides a 256-color palette with factory methods for named themes.
Themes is the catalog listing all available themes:
use SugarCraft\Vterm\Theme; use SugarCraft\Vterm\Themes; // Tokyo Night — the standard of the monorepo's VHS demos $theme = Theme::tokyoNight(); // Index into the 256-color palette $red = $theme->color(1); // 0xf7768e // Resolve RGB components from a 256-color index $rgb = Theme::rgb(196); // [255, 0, 0] // Map ANSI 0-15 slot to its 256-color index $idx = Theme::fgIndex(1); // 1 (Ansi1 → index 1) // V1-ready themes — every bundled theme carries a complete palette, // so v1() currently lists the same five themes as all() foreach (Themes::v1() as $name => $theme) { echo "$name\n"; } // TokyoNight, TokyoNightLight, TokyoNightStorm, Dracula, SolarizedDark // All themes in the catalog foreach (Themes::all() as $name => $theme) { echo "$name\n"; } // Same five names as above: every bundled theme is v1-ready
Available themes:
| Theme | Status | Description |
|---|---|---|
TokyoNight |
✅ v1 | Dark theme — the standard of the monorepo's VHS demos |
TokyoNightLight |
✅ v1 | Light variant of TokyoNight |
TokyoNightStorm |
✅ v1 | Storm variant of TokyoNight |
Dracula |
✅ v1 | Full Dracula palette |
SolarizedDark |
✅ v1 | Full Solarized Dark palette |
Attribute constants on Theme match Cell::ATTR_* for SGR bitfield construction:
Theme::ATTR_BOLD, Theme::ATTR_ITALIC, Theme::ATTR_UNDERLINE, Theme::ATTR_INVERSE, Theme::ATTR_STRIKETHROUGH.
Theme accessors
| Method | Returns |
|---|---|
$theme->color($index) (instance) |
int — packed 24-bit RGB for palette slot 0..255. When a slot is absent, falls back through Theme::rgb() for any index — the standard xterm mapping (cube 16..231 and grayscale 232..255), not only the grayscale ramp. |
Theme::rgb($index) |
array{int,int,int} — [R,G,B] resolution for any 256-color index, including the 6x6x6 color cube (16..231) and the 24-step grayscale ramp (232..255). |
Theme::fgIndex($slot) |
int — map an ANSI 0..15 slot to its 256-color index (currently a no-op identity, kept for spec parity). |
Theme::bgIndex($slot) |
int — same for background slot. |
Theme::defaultPalette() |
array<int,int> — Bootstrap palette (xterm defaults); the Theme constructor falls back to it when no palette is passed (Themes::ansi() does not exist). |
Theme::ANSI_OFFSET / Theme::CUBE_OFFSET / Theme::GRAYSCALE_OFFSET |
int constants 0 / 16 / 232 — palette region boundaries. |
Subsystems
Each subdirectory of src/ owns a small piece of VT state:
| Namespace | Purpose | Public surface |
|---|---|---|
Buffer\Buffer |
Cell grid storage (rows × cols array of Cell). |
cell($row, $col) (either coordinate out of bounds → Cell::empty()), put($row, $col, $cell) (out-of-grid writes silently ignored), insertRows($count, $from, ?$top, ?$bottom), deleteRows($count, $from, ?$top, ?$bottom), dirtyRegion(), each(): Generator, copy(): array, clear(): self (a NEW blank grid of the same size — the receiver is untouched and its dirty box resets; contrast resize($cols, $rows): self, which keeps in-bounds content), equals(self $other): bool (grid-for-grid comparison), resize($cols, $rows): self (refuses dims < 1, repairs wide pairs at the cut, marks the whole viewport dirty). Writes are split by intent: put() and the range operations mutate the receiver, while resize() and clear() return new grids. The silent out-of-bounds behaviour (cell() → Cell::empty(), out-of-grid put() ignored) is load-bearing, not an oversight: the emulator's scroll-region arithmetic reads and clips through these bounds-checked accesses and relies on them never throwing (the cross-lib throw-vs-silent note in docs/MATCHUPS.md resolves on the vterm side as documented behaviour). |
Screen\Screen |
Immutable snapshot of Buffer + Scrollback — fromBuffer() copies the grid AND clones the ring, so captured screens keep the history they were born with; cell-level diff API. |
fromBuffer($buf, ?$scrollback), lines() (Generator: full-width rows, combining marks re-appended), linesWithHistory() (Generator: scrolled-off rows oldest-first, then visible), cell($row, $col) (out-of-bounds in EITHER coordinate returns Cell::empty()), scrollback() (?Scrollback — null when built via fromBuffer($buf) without a ring; the full Terminal\Terminal::screen() path always wires one), diff($other). |
Screen\Scrollback |
Ring buffer (default 1000 rows) for rows scrolled off the top. | push($row) (record a scrolled-off line, evicting the oldest at capacity), count(), maxSize(), at($n), all(), clear() (drop every saved row — what ED 3 calls). |
Cell\Cell |
class_alias shim over the canonical root SugarCraft\Vterm\Cell — the one cell class for both engines (char + grapheme aliases, palette fg/bg, attrs bitfield, optional Sgr/truecolour/hyperlink/Rendition/combining payload). |
Static empty(), continuation($prev); withFg($i), withBg($i), withAttrs($flags), withFgTruecolor(?int), withBgTruecolor(?int), withRendition(Rendition), withCombining($s); sgr(), foreground()/background() (?Color), fgRgb()/bgRgb() (?array), colorSgr(), equals($other). |
Cursor\Cursor |
Position + visibility + shape + saved-state. | withRow($r), withCol($c), withVisible($v), withShape($s), save(), restore(), equals(). |
Sgr\Sgr |
Pen state (fg, bg, attrs bitfield, underline style). | empty(), withBold/Italic/Underline/Strikethrough/Blink/Reverse/Dim/Hidden($v), withUnderlineStyle($style), withForeground(?$color), withBackground(?$color), equals(). |
Sgr\UnderlineStyle |
Enum: None/Single/Double/Curly/Dotted/Dashed. |
Backed int enum (None=0 … Dashed=5); ->value is the CSI code (no fromInt()). |
Rendition (root enum) |
Double-height/width line rendition (ESC # 3–ESC # 6), stamped on every cell of the line so it scrolls with content and rides each diff payload. |
None (0) / DoubleTop (1) / DoubleBottom (2) / DoubleWidth (3), isDoubleHeight(). |
Charset\Charsets |
SCS character-set tables — G0–G3 designations (ESC ( ) * +), locking SO/SI, single-shift SS2/SS3; emulator-side only, 7-bit code points translate through the GL-reachable set, UTF-8 never. |
ASCII ('B'), DEC_SPECIAL ('0'), UK ('A'), NO_BREAK_SPACE ('U'), translate($charset, $rune). |
Mode\Mode |
DEC private modes. altScreenVariant (consts ALT_NONE/ALT_NO_SAVE/ALT_CURSOR_ONLY/ALT_FULL) records which of the four alt-screen meanings (47/1047 swap only, 1048 cursor save only, 1049 full save+restore) a switch came from; withAltScreen(bool) maps to ALT_FULL/ALT_NONE. |
withAltScreen/AltScreenVariant/CursorVisible/MouseEncoding/MouseHighlights/MouseAny/MouseCellMotion/MouseExtended/BracketedPaste/SyncUpdate/AutoWrap/OriginMode/CursorShape/ReportFocusEvents, isAltScreen(), isMouseReporting(), equals(). |
Hyperlink\Hyperlink |
OSC 8 URI + id state. | fromRaw($id, $uri), equals(). |
Msg\FocusInMsg / FocusOutMsg |
Focus event records — land in the emulator's bounded focus ring (DEC mode 1004) and drain via Terminal::focusEvents(). |
Plain DTOs. |
Color\Color |
Color value object — palette index or truecolor RGB. | default(), indexed16($i), indexed256($i), truecolor($r,$g,$b), fromInt($kind,$v), red(), green(), blue(), equals(), equalsOrBothNull(?self $a, ?self $b): bool (static null-safe comparison: two nulls are equal, one null never is). |
CursorShape (root enum) |
BlinkingBlock (0/1) / SteadyBlock (2) / BlinkingUnderline (3) / SteadyUnderline (4) / BlinkingBar (5) / SteadyBar (6). |
fromInt(), toInt(). |
Trust boundary and hostile input
Both facades exist to parse bytes emitted by UNTRUSTED programs, so every unbounded channel is capped and nothing leaves the grid's text unsanitised (stored side-channels — attacker-controlled strings kept verbatim, such as window titles and OSC 8 URIs riding on grid cells — are called out below):
- Grid ceilings — the full facade bounds dimension intake at
Terminal::MAX_COLS/MAX_ROWS(4096 per axis) andMAX_SCROLLBACK(1 000 000 rows): terminals sized from external input (XTWINOPS reports, window-size handshakes) reject larger withInvalidArgumentExceptionbefore any grid or ring is allocated, sonew Terminal(100000, 100000)throws in constant memory.BufferandScrollbackkeep their own>= 1low-level guards unchanged, and a caller-supplied pre-builtBufferbypasses the ceiling because its bytes were paid for above the library. - Parser string buffer —
Terminal::MAX_STRING_BUFFER(64 KiB, shared by both facade parsers). OSC payloads past the cap are truncated, so no per-stream allocation grows with attacker-chosen length; DCS bytes never ride this path — their payloads are discarded outright (see Discarded without parsing below). One model per byte class. - Replies — the emulator's query-reply queue is a 1024-entry drop-oldest
ring; under a DA/DSR flood an awaited reply can be evicted, so drain
replies()(or pass the?respondcallback) promptly. - Clipboard / focus events — OSC 52 and DEC 1004 are self-armed by the
remote program (no capability negotiation), so both logs are 1024-entry
drop-oldest rings.
clipboardEvents()andfocusEvents()drain-on-read. - Synchronized output (DEC 2026) queue — at most 16384 pending mutations; past that the queue partially replays to the grid. Atomicity is surrendered only under adversarial pressure, never silently dropped.
- Per-cell combining marks — a cell accumulates at most
Cell::MAX_COMBINING_PER_CELL(32) zero-width marks, the same bound in both engines (the emulator'sCell::withCombining()truncates the burst to the remaining budget; the renderer's mark append drops whole marks once full). The first marks retained are always kept; a bare-mark flood cannot grow any cell. - Scrollback — bounded ring (default 1000 rows);
withScrollbackSize()swaps in a fresh ring on any size change, growing or shrinking — history accumulated before the call is dropped, not trimmed (pinned byScrollbackTest::testWithScrollbackSizeReplacesRing). The renderer path keeps no scrollback. - Stored strings are attacker text — window titles, OSC 8 hyperlinks and
OSC 52 payloads are kept VERBATIM (each bounded by the 64 KiB
MAX_STRING_BUFFERcap above); consumers must sanitize before display or onward propagation (Sanitize::untrusted()upstream) — or use the built-in sanitized accessor:windowTitleSanitized()on both facades routes the stored title throughOscHandler::sanitizeControl(), which strips C0, DEL and C1 code points (the parser already swallows ESC, NUL, TAB and CR/LF on the way in; DEL and C1 — whose raw byte 0x9D is the 8-bit OSC introducer — survive verbatim) and can only ever shorten the string. - Discarded without parsing — DCS, SOS, PM and APC payloads; malformed UTF-8 bytes (the upstream tokenizer emits no print event for them, so the grid only ever holds decoded graphemes).
- BEL — counted via
bells()on the emulator path only; the renderer discards BEL upstream in the adapter before any sink exists. - One-way doors — mouse, focus and bracketed-paste sequences are DECODED
only; the DECSET/DECRST roster above names every mode bit the emulator
tracks and none of them produce outbound bytes.
Mode\MouseEncodingdescribes the decoder slot; the library ships no encoders, so programs emitting those sequences build their own bytes. - No I/O —
src/never writes streams, spawns processes, or touches the filesystem;feedStream()merely accepts one. - Renderer silences — the CSI finals the parser adapter does not route (E F G d X c n t p q, SP q, and OSC 4/10/11/52) are discarded on the renderer path; the CSI/OSC coverage tables above are the per-sequence ledger for which engine implements what.
Development
# from the monorepo root: wire siblings to the local tree (CI injects the same path-repo closure) php scripts/refresh-deps.php --mode=linked --libs=sugar-vterm cd sugar-vterm vendor/bin/phpunit # test suite vendor/bin/phpstan analyze # static analysis (level: max) vendor/bin/php-cs-fixer fix --config=../.php-cs-fixer.dist.php # lint + auto-fix style
Snapshot tests
Render output is covered by golden-file snapshot tests. The fixture set is
exactly two files in tests/fixtures/ — terminal-cleared.golden and
terminal-parsed-text.golden — both compared against actual ANSI byte
output by tests/GoldenRenderTest.php via
SugarCraft\Testing\Snapshot\Assertions::assertGoldenAnsi(); no other
fixture files are read. To re-record a fixture after an intentional output
change, delete it and re-run the test once with UPDATE_GOLDENS=1 (or its
more explicit spelling, SUGAR_TESTING_UPDATE_GOLDENS=1):
rm tests/fixtures/<name>.golden UPDATE_GOLDENS=1 vendor/bin/phpunit --filter <TestName>
The flag arms creation only — an existing golden is never rewritten, and a missing golden without the flag is a hard failure, so a CI run can never silently re-baseline itself.
Code style is enforced by php-cs-fixer via the root .php-cs-fixer.dist.php (PSR-12 + declare_strict_types + strict_param + short array syntax). Append --dry-run --diff to preview without writing.
Related
- candy-vcr — VHS-compatible cassette recorder /
GIF renderer. Uses this lib's root
Terminal+Snapshotto drive every rendered frame. - SugarCraft monorepo
Credits & inspiration
Originally inspired by the Go Charm ecosystem; SugarCraft is developed as a native PHP project.