ANSI escape-sequence parser and state machine
Parse ANSI escape sequences into structured state transitions โ SGR attributes, cursor movement, erase operations, and DEC private modes โ without coupling to terminal-cell-grid concerns. Used by CandyVt to drive the VT500 state machine.
composer require sugarcraft/candy-ansi
use SugarCraft\Ansi\Parser\Parser;
use SugarCraft\Ansi\Parser\CsiHandler;
use SugarCraft\Ansi\Parser\AnsiState;
$state = new AnsiState();
$handler = new MyCsiHandler(); // implements CsiHandler
$parser = new Parser($state, $handler);
$parser->feed("\x1b[1;31m"); // CSI 1;31m โ bold + red foreground
$parser->feed("Hello");
$parser->feed("\x1b[0m"); // reset
AnsiState holds current SGR parameters, cursor position, erase mode, DEC mode flags, and scroll region bounds โ parseable independently of terminal-cell-grid coupling.| Class | Method | Description |
|---|---|---|
| Parser | feed(bytes) | Feed raw ANSI bytes to parser |
| AnsiState | sgr, cursor, eraseMode, decModes | Current parsed state |
| AnsiState | withSgr(array) | New state with updated SGR params |
| AnsiState | withCursor(int, int) | New state with cursor position |
| CsiHandler | printable(string) | Handle printable ASCII character |
| CsiHandler | cuu/cud/cuf/cub(int) | Cursor movement |
| CsiHandler | cup(int, int) | Cursor position (1-indexed) |
| CsiHandler | sgr(array) | Select Graphic Rendition |
| CsiHandler | ed/el(int) | Erase Display / Line |
| CsiHandler | decset/decrst(int, int) | DEC private mode set/reset |
| CsiHandler | decstbm(int, int) | Set top/bottom scroll margins |
| CsiHandler | tbc/cht/cbt(int) | Tab control |
VHS-recorded GIFs of every example shipped with the library. Regenerated automatically on every push that touches the source.