Expand description
Zero-allocation, panic-free DEC VT500 terminal parser with UTF-8 decoding
and no_std support.
§Usage
ⓘ
use vtty::{Parser, Perform};
struct Screen;
impl Perform for Screen {
fn print_bytes(&mut self, s: &[u8]) { /* printable ASCII run */ }
fn print(&mut self, c: char) { /* non-ASCII code point */ }
fn execute(&mut self, b: u8) { /* \n \t \x1b … */ }
fn csi_dispatch(&mut self, params: &[i32], sep: u16, intermediates: &[u8], final_byte: u8) {
/* CSI Ps ; Ps final */
}
fn osc_dispatch(&mut self, params: &[&[u8]], bell: bool) { /* OSC */ }
}
let mut parser = Parser::new();
parser.advance(&mut screen, b"\x1b[1;31mhello\x1b[0m");
// With the `simd-transcode` feature enabled, opt into SIMD scanning,
// validation and UTF-8→UTF-32 transcode:
let mut parser = Parser::with_simd();
parser.advance(&mut screen, b"\x1b[1;31mhello\x1b[0m");Also available: Event/Recording (owned enum API), State (byte-cursor
sink with line/cursor geometry), Reply (query reply builder), and
ColorId, SgrFlags, TermFlags, KittyFlags, MouseButton, Charset, Cap.
§Design
- Panic-free —
#![forbid(unsafe_code)]; malformed PTY input never crashes. - Zero-allocation hot path — one
print_bytesper ASCII run, oneprint_codepointsper non-ASCII run; CSI/OSC/DCS state is inline. no_stdby default — optionalsimd(scanning + validation) andsimd-transcode(UTF-8→UTF-32) features, enabled per parser.- W3C-correct UTF-8 — maximal-subpart substitution; 8-bit clean (DEC “anywhere”).
§Test
cargo test -p vtty
cargo test -p vtty --test vt_differential -- --nocapture # parity vs vteStructs§
- Cap
- Namespace for terminfo capability names, aliases, and parsing.
- Charsets
- The four ISO 2022 designated character sets (G0, G1, G2, G3), selected by
the
ESC ( / ) / * / +designation sequences and the SO/SI/SS2/SS3 shift controls. A terminal carries these as one unit: DECSC/DECRC save and restore all four together. - ColorId
- A palette index: 0..=255 are the dynamic 256-color table, 256/257 the
default fg/bg, 258+ RGB truecolors inserted on demand.
#[repr(transparent)]overu16gives the exact u16 ABI. - Kitty
Flags - Kitty keyboard protocol flags, reported by
CSI > flags uand pushed byCSI < count u. Each bit enables a progressive feature tier. - Mouse
Tracking - Active mouse-tracking configuration, derived from the DEC mouse flags —
the screen-independent core: every screen maps its flags into this and
delegates to
MouseButton::encode, so X10/SGR selection and motion gating live in one place. - Parser
- The byte-input VT parser: UTF-8 decoding plus per-codepoint dispatch.
- Recording
- Records parser events into a
Vec<Event>(the enum-based, trait-free API). - Reply
- Accumulates VT query replies in dispatch order, so a burst of queries
(
XTVERSION+OSC 10/11+OSC 4+DA) is answered in order instead of dropping all but the last. - SgrFlags
- SGR attribute state: bold/faint/italic/underline/strikethrough/ overline/inverse/invisible/blink.
- State
- Minimal byte-cursor state (the O(bytes) floor).
- Style
Flags - The five SGR decorations a screen stores per span/cell (underline /
strikethrough / overline / bold / italic). The bit layout is the low
five bits of
SgrFlags, soSgrFlags::decorationsextracts them with one truncating cast. - Term
Flags - Settable terminal modes tracked by a screen, as a single flag set.
Enums§
- CapValue
- The reply value for a capability.
- Charset
- A designated charset for G0/G1 (
ESC ( 0= DEC Special Graphics). - Cursor
Style - DECSCUSR cursor shape (
CSI Ps SP q). - Event
- A parsed escape-sequence event — the dispatch as data instead of a
crate::Performcallback. Mirrorsvtparse’sVTAction; useful for differential testing. Params/intermediates use inlineSmallVecs (≤6 params, ≤2 intermediates allocates nothing); only OSC bodies are heapVecs. - Mouse
Button - Mouse button/event codes for X10 and SGR encoding.
- Underline
Style - SGR underline style (
4:0..5/ SGR 21).
Constants§
- DEFAULT_
BG_ ID - Reserved palette index for the default background (SGR 49).
- DEFAULT_
FG_ ID - Reserved palette index for the default foreground (SGR 39).
- MOUSE_
MASK - Mouse-tracking + mouse-protocol modes. Entering the alternate screen
resets these so a pager (
less/man) starts clean and text selection works; mouse-first apps (nvim/herdr) re-enable them right after?1049h.
Traits§
- Perform
- Sink for the byte parser’s decoded output.
crate::Parser::advancecalls these as it decodes; every method has a no-op default, so a screen overrides only what it cares about.crate::Eventis the owned, trait-free alternative.
Functions§
- dec_
special_ graphics - DEC Special Graphics (line drawing) translation for the 0x5F..0x7E range.
Used when G0/G1 is designated to charset
0(ESC ( 0/ESC ) 0). - hex_
decode - Decode a hex-encoded byte string (
DCS + q <hex> STDECRQSS-style query payloads). Stops at the first non-hex byte pair. - hex_
encode - Encode
bytesas lowercase hex pairs.