Skip to main content

Crate vtty

Crate vtty 

Source
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_bytes per ASCII run, one print_codepoints per non-ASCII run; CSI/OSC/DCS state is inline.
  • no_std by default — optional simd (scanning + validation) and simd-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 vte

Structs§

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)] over u16 gives the exact u16 ABI.
KittyFlags
Kitty keyboard protocol flags, reported by CSI > flags u and pushed by CSI < count u. Each bit enables a progressive feature tier.
MouseTracking
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).
StyleFlags
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, so SgrFlags::decorations extracts them with one truncating cast.
TermFlags
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).
CursorStyle
DECSCUSR cursor shape (CSI Ps SP q).
Event
A parsed escape-sequence event — the dispatch as data instead of a crate::Perform callback. Mirrors vtparse’s VTAction; useful for differential testing. Params/intermediates use inline SmallVecs (≤6 params, ≤2 intermediates allocates nothing); only OSC bodies are heap Vecs.
MouseButton
Mouse button/event codes for X10 and SGR encoding.
UnderlineStyle
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::advance calls these as it decodes; every method has a no-op default, so a screen overrides only what it cares about. crate::Event is 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> ST DECRQSS-style query payloads). Stops at the first non-hex byte pair.
hex_encode
Encode bytes as lowercase hex pairs.