A virtual terminal emulator (VTE) processes raw byte streams containing ANSI/VT100 escape sequences and maintains the resulting terminal state: a visible Grid.t, cursor position, style, scrollback history, and mode flags.
The emulator maintains two screen buffers. The primary screen has optional scrollback history; the alternate screen is a temporary buffer used by full-screen applications like vi or less and never accumulates scrollback. The VTE automatically switches between buffers in response to DECSET/DECRST sequences.
Dirty tracking. Grid, cursor, and style mutations flip a dirty flag (see is_dirty and dirty_rows). Metadata changes such as title updates or mode toggles do not mark the VTE dirty; track those separately if you display them.
Thread safety. Values of type t are mutable and not thread-safe.
Invariants
Cursor row satisfies 0 <= row < rows t. Column satisfies 0 <= col <= cols t; col = cols t encodes the pending-wrap column required by DECAWM.
Scroll region is always within [0; {!rows} - 1].
The active grid is either primary or alternate, never both.
Scrollback only accumulates on the primary screen.
create ~rows ~cols () is a VTE with the given dimensions and:
scrollback is the maximum number of scrollback lines stored in a ring buffer. Defaults to 10000. Use 0 to disable. Only applies to the primary screen.
glyph_pool is a shared grapheme pool for multi-width character storage. If omitted a fresh pool is created and shared between both grids and the scrollback.
width_method is the grapheme width computation method. Defaults to `Unicode. See Glyph.width_method.
respect_alpha enables alpha blending for semi-transparent colours. Defaults to false.
default_fg is the opaque foreground colour used when the current style has no explicit foreground. Defaults to Ansi.Color.white.
default_bg is the opaque background colour used when the current style has no explicit background, and for clearing grids on resize or reset. Defaults to Ansi.Color.black.
rows and cols are clamped to a minimum of 1. The returned VTE has both screens cleared with default_bg, cursor at (0, 0) and visible, auto-wrap on, all other modes off, and the scroll region spanning the full screen.
resize t ~rows ~cols resizes both grids to the given dimensions. Both grids are cleared after the resize. Cursor and scroll region are clamped to the new bounds. Scrollback content is preserved; compressed lines adapt to the new width during decompression.
rows and cols are clamped to a minimum of 1. Marks all rows dirty.
feed t buf ofs len processes len bytes starting at offset ofs in buf as terminal input.
Accepts partial and interleaved text/escape sequences. Invalid or unrecognised sequences are silently ignored. Updates the active grid, cursor, title, style, and mode flags.
ofs and len must satisfy 0 <= ofs and ofs + len <= Bytes.length buf. Out-of-bounds access is undefined behaviour.
cursor_pos t is the cursor position (row, col). Satisfies 0 <= row < rows t and 0 <= col <= cols t. col can equal cols t in pending-wrap state when auto-wrap is enabled.
scrollback_size t is the current number of lines in the scrollback buffer. 0 if scrollback is disabled, empty, or the alternate screen is active. Satisfies 0 <= scrollback_size t <= scrollback_capacity t.
scrollback_lines t is the scrollback content as plain-text strings ordered oldest to newest. Style information is discarded. Trailing spaces are trimmed during compression. Empty if scrollback is disabled, empty, or the alternate screen is active. O(scrollback_size).
Sourceval render_with_scrollback : t->offset:int ->Grid.t-> unit
render_with_scrollback t ~offset dst renders a snapshot combining scrollback history and the live screen into dst.
The top rows of dst are filled with scrollback lines (oldest at the top) and the remaining rows with the current screen. offset is the number of lines above the live screen the snapshot extends: 0 shows only the live terminal. offset is clamped to [0, scrollback_size].
dst should have the same width as t for correct glyph placement; smaller widths clip decompressed graphemes. On the alternate screen (no scrollback) this is equivalent to Grid.blit ~src:(grid t) ~dst.
auto_wrap_mode t is true iff auto-wrap (DECAWM) is active. When enabled, writing past the right margin wraps to the next line. Controlled via CSI ?7h/l. Defaults to true.
insert_mode t is true iff insert mode (IRM) is active. Characters push existing content right instead of replacing it. Controlled via CSI 4h/l. Defaults to false.
cursor_key_mode t is true iff cursor keys use application mode (DECCKM). Application mode sends ESC O A instead of ESC [ A for arrow keys. Controlled via CSI ?1h/l. Defaults to false.
bracketed_paste_mode t is true iff bracketed paste is active. Pasted text is wrapped with ESC [ 200 ~ / ESC [ 201 ~ markers. Controlled via CSI ?2004h/l. Defaults to false.
scroll_up t n scrolls the current scroll region up by n lines. Lines leaving the top enter scrollback only when the primary screen is active, scrollback is enabled, and the region starts at row 0. Newly exposed rows are cleared with the default transparent background. No effect if n <= 0.
scroll_down t n scrolls the current scroll region down by n lines. Blank rows are inserted at the top; lines leaving the bottom are discarded (never saved to scrollback). No effect if n <= 0.
reset t resets t to its initial state. Clears both grids and scrollback, moves cursor to (0, 0) and makes it visible, resets style to default, sets auto-wrap on and all other modes off, resets scroll region to full screen, and switches to the primary screen. Dimensions are unchanged. Equivalent to RIS (ESC c).
to_string t is the visible screen content as a multi-line string. One line per row, separated by newlines. Trailing spaces are preserved. Wide graphemes appear once. Style information is not included. The result does not end with a trailing newline.