sugarcraft / candy-vt
In-memory virtual terminal emulator — parses ANSI byte stream into a cell grid.
dev-master
2026-05-18 02:36 UTC
Requires
- php: ^8.3
- sugarcraft/candy-core: dev-master
- sugarcraft/candy-sprinkles: dev-master
Requires (Dev)
- phpunit/phpunit: ^10.5
This package is auto-updated.
Last update: 2026-05-18 02:39:58 UTC
README
CandyVt
In-memory virtual terminal emulator — parses an ANSI byte stream into a cell grid with cursor, mode, SGR style, and hyperlink state.
Mirrors charmbracelet/x/vt.
Install
composer require sugarcraft/candy-vt
Quickstart
use SugarCraft\Vt\Terminal\Terminal; $vt = Terminal::create(cols: 80, rows: 24); // Feed raw ANSI bytes from a terminal program. $vt->feed("\x1b[1;1H\x1b[31mmhello\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";
Architecture
| Layer | Class | Role |
|---|---|---|
| Facade | Terminal\Terminal |
Owns a Parser + ScreenHandler; feed() drives bytes in |
| Parser | Parser\Parser |
VT500 state machine — Paul-Williams algorithm, handles partial input |
| Handler | Handler\ScreenHandler |
Dispatches parser actions to Buffer / Cursor / Sgr / Mode |
| Screen | Screen\Screen |
Immutable snapshot — read current grid after feeding bytes |
| Buffer | Buffer\Buffer |
Cell grid — rows × cols of styled grapheme cells |
| Cursor | Cursor\Cursor |
Position + visibility + origin mode tracking |
| SGR | Sgr\Sgr |
Current graphics rendition: foreground / background / attributes |
| Mode | Mode\Mode |
Dec private mode (DECSET/DECRST) flags |
| Hyperlink | Hyperlink\Hyperlink |
OSC 8 URL + id tracker |
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/USCSgrHandler— SGR sequences (colour + attributes)EraseHandler— ED, EL, ECH, DECSCAScrollHandler— SU, SD, DECSTBMModeHandler— DECSET/DECRST/DECMODESET/DECMODERSTTabHandler— TBC, HTSOscHandler— OSC window title, hyperlink, colour paletteScreenHandler— orchestrates all of the above; owns the Buffer
Diff API
After feeding bytes, snapshot the screen and compare:
$before = $vt->screen(); $vt->feed($moreBytes); $after = $vt->screen(); foreach ($before->diff($after) as $change) { [$row, $col, $prev, $next] = $change; echo "{$row},{$col}: '{$prev->grapheme}' → '{$next->grapheme}'\n"; }
Test
cd candy-vt && composer install && vendor/bin/phpunit
Related
- SugarCraft monorepo
- Upstream: charmbracelet/x/vt