Expand description
ANSI escape sequence generation helpers.
This module provides pure byte-generation functions for ANSI/VT control sequences. It handles the encoding details so the Presenter can focus on state tracking and diffing.
§Design Principles
- Pure functions: No state tracking, just byte generation
- Zero allocation: Use stack buffers for common sequences
- Explicit: Readable helpers over clever formatting
§Sequence Reference
| Category | Sequence | Description |
|---|---|---|
| CSI | ESC [ n m | SGR (Select Graphic Rendition) |
| CSI | ESC [ row ; col H | CUP (Cursor Position, 1-indexed) |
| CSI | ESC [ n K | EL (Erase Line) |
| CSI | ESC [ n J | ED (Erase Display) |
| CSI | ESC [ top ; bottom r | DECSTBM (Set Scroll Region) |
| CSI | ESC [ ? 2026 h/l | Synchronized Output (DEC) |
| OSC | ESC ] 8 ; ; url ST | Hyperlink (OSC 8) |
| DEC | ESC 7 / ESC 8 | Cursor save/restore (DECSC/DECRC) |
Structs§
- SgrCodes
- SGR attribute codes for style flags.
Enums§
- Erase
Display Mode - ED (Erase Display) mode.
- Erase
Line Mode - EL (Erase Line) mode.
Constants§
- ALT_
SCREEN_ ENTER - Enable alternate screen:
CSI ? 1049 h - ALT_
SCREEN_ LEAVE - Disable alternate screen:
CSI ? 1049 l - BRACKETED_
PASTE_ DISABLE - Disable bracketed paste:
CSI ? 2004 l - BRACKETED_
PASTE_ ENABLE - Enable bracketed paste:
CSI ? 2004 h - CURSOR_
HIDE - Hide cursor:
CSI ? 25 l - CURSOR_
RESTORE - DEC cursor restore:
ESC 8(DECRC) - CURSOR_
SAVE - DEC cursor save:
ESC 7(DECSC) - CURSOR_
SHOW - Show cursor:
CSI ? 25 h - FLAG_
TABLE - Ordered table of (flag, on/off codes) for iteration.
- FOCUS_
DISABLE - Disable focus reporting:
CSI ? 1004 l - FOCUS_
ENABLE - Enable focus reporting:
CSI ? 1004 h - MOUSE_
DISABLE - Disable mouse reporting:
CSI ? 1000;1002;1006 l - MOUSE_
ENABLE - Enable SGR mouse reporting:
CSI ? 1000;1002;1006 h - RESET_
SCROLL_ REGION - Reset scroll region to full screen:
CSI r - SGR_
BLINK - SGR codes for blink (on=5, off=25).
- SGR_
BOLD - SGR codes for bold (on=1, off=22).
- SGR_DIM
- SGR codes for dim (on=2, off=22).
- SGR_
HIDDEN - SGR codes for hidden text (on=8, off=28).
- SGR_
ITALIC - SGR codes for italic (on=3, off=23).
- SGR_
RESET - SGR reset:
CSI 0 m - SGR_
REVERSE - SGR codes for reverse video (on=7, off=27).
- SGR_
STRIKETHROUGH - SGR codes for strikethrough (on=9, off=29).
- SGR_
UNDERLINE - SGR codes for underline (on=4, off=24).
- SYNC_
BEGIN - Begin synchronized output:
CSI ? 2026 h - SYNC_
END - End synchronized output:
CSI ? 2026 l
Functions§
- alt_
screen_ enter - Write alternate screen enter.
- alt_
screen_ leave - Write alternate screen leave.
- bracketed_
paste_ disable - Write bracketed paste disable.
- bracketed_
paste_ enable - Write bracketed paste enable.
- cha
- CUP to column only:
CSI col G(1-indexed) - cr
- Move cursor to start of line:
\r(CR) - cub
- Move cursor back (left):
CSI n D - cud
- Move cursor down:
CSI n B - cuf
- Move cursor forward (right):
CSI n C - cup
- CUP (Cursor Position):
CSI row ; col H(1-indexed) - cursor_
hide - Write hide cursor.
- cursor_
restore - Write cursor restore (DECRC).
- cursor_
save - Write cursor save (DECSC).
- cursor_
show - Write show cursor.
- cuu
- Move cursor up:
CSI n A - erase_
display - ED (Erase Display):
CSI n J - erase_
line - EL (Erase Line):
CSI n K - focus_
disable - Write focus disable.
- focus_
enable - Write focus enable.
- hyperlink_
end - Close an OSC 8 hyperlink.
- hyperlink_
start - Open an OSC 8 hyperlink.
- hyperlink_
start_ with_ id - Open an OSC 8 hyperlink with an ID parameter.
- lf
- Move cursor down one line:
\n(LF) - mouse_
disable - Write mouse disable.
- mouse_
enable - Write mouse enable.
- reset_
scroll_ region - Write reset scroll region.
- set_
scroll_ region - DECSTBM (Set Top and Bottom Margins):
CSI top ; bottom r - sgr_
bg_ 16 - Write SGR sequence for 16-color background.
- sgr_
bg_ 256 - Write SGR sequence for 256-color background:
CSI 48;5;n m - sgr_
bg_ default - Write SGR default background:
CSI 49 m - sgr_
bg_ packed - Write SGR for a PackedRgba color as background (true color).
- sgr_
bg_ rgb - Write SGR sequence for true color background:
CSI 48;2;r;g;b m - sgr_
codes_ for_ flag - Get SGR codes for a style flag.
- sgr_
fg_ 16 - Write SGR sequence for 16-color foreground.
- sgr_
fg_ 256 - Write SGR sequence for 256-color foreground:
CSI 38;5;n m - sgr_
fg_ default - Write SGR default foreground:
CSI 39 m - sgr_
fg_ packed - Write SGR for a PackedRgba color as foreground (true color).
- sgr_
fg_ rgb - Write SGR sequence for true color foreground:
CSI 38;2;r;g;b m - sgr_
flags - Write SGR sequence for style flags (all set flags).
- sgr_
flags_ off - Write SGR sequence to turn off specific style flags.
- sgr_
reset - Write SGR reset sequence.
- sync_
begin - Write synchronized output begin.
- sync_
end - Write synchronized output end.