Files
ghostty-rs/crates/ghostty-vt/src/ansi.rs
T
ddidderr b2b248e003 feat(vt): port Ghostty's VT parsing layer
Ports the parsing half of ZIG/src/terminal into the ghostty-vt crate:
- Parser state machine with a compile-time generated parse table and
  the UTF-8 decoder
- OSC parser incl. all sub-parsers (titles, colors, clipboard, hyperlinks,
  semantic prompts/OSC 133, OSC 9 / ConEmu, iTerm2, kitty clipboard,
  color and text-sizing protocols, context signals)
- CSI, SGR, modes table with save/restore, charsets, ansi, device status,
  mouse shapes, APC, DCS (incl. XTGETTCAP), tmux control-mode parser
- color palette / X11 color names, kitty color and graphics command parser

Deviations from Zig (documented in docs/rewrite/notes/ghostty-vt-W*.md):
parser actions own their payloads instead of borrowing parser buffers;
OSC strings are Vec<u8>; non-exhaustive Zig enums gain an Other(raw)
variant; tmux regexes replaced by equivalent hand-written matchers.

Known follow-up: semantic_prompt.rs carries local copies of the printf-q
and percent decoders that belong in ghostty-os.

Test Plan:
- cargo test -p ghostty-vt: 395 passed, 1 ignored (skipped in Zig too)
- cargo clippy -p ghostty-vt --all-targets -- -D warnings: clean
2026-10-11 21:52:41 +02:00

157 lines
3.4 KiB
Rust

//! ANSI types (ZIG/src/terminal/ansi.zig).
/// C0 (7-bit) control characters from ANSI.
///
/// This is not complete, control characters are only added to this
/// as the terminal emulator handles them.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum C0 {
/// Null
Nul = 0x00,
/// Start of heading
Soh = 0x01,
/// Start of text
Stx = 0x02,
/// Enquiry
Enq = 0x05,
/// Bell
Bel = 0x07,
/// Backspace
Bs = 0x08,
/// Horizontal tab
Ht = 0x09,
/// Line feed
Lf = 0x0A,
/// Vertical Tab
Vt = 0x0B,
/// Form feed
Ff = 0x0C,
/// Carriage return
Cr = 0x0D,
/// Shift out
So = 0x0E,
/// Shift in
Si = 0x0F,
}
impl C0 {
/// Convert from a raw byte; unknown values return `None` (the Zig enum
/// is non-exhaustive).
pub fn from_int(v: u8) -> Option<Self> {
Some(match v {
0x00 => Self::Nul,
0x01 => Self::Soh,
0x02 => Self::Stx,
0x05 => Self::Enq,
0x07 => Self::Bel,
0x08 => Self::Bs,
0x09 => Self::Ht,
0x0A => Self::Lf,
0x0B => Self::Vt,
0x0C => Self::Ff,
0x0D => Self::Cr,
0x0E => Self::So,
0x0F => Self::Si,
_ => return None,
})
}
}
/// The SGR rendition aspects that can be set, sometimes known as attributes.
/// The value corresponds to the parameter value for the SGR command (ESC [ m).
/// Non-exhaustive in Zig; `Other` carries unknown values.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RenditionAspect {
Default,
Bold,
DefaultFg,
DefaultBg,
Other(u16),
}
impl RenditionAspect {
pub fn from_int(v: u16) -> Self {
match v {
0 => Self::Default,
1 => Self::Bold,
39 => Self::DefaultFg,
49 => Self::DefaultBg,
v => Self::Other(v),
}
}
}
/// The device attribute request type (ESC [ c).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DeviceAttributeReq {
/// Blank
Primary,
/// >
Secondary,
/// =
Tertiary,
}
/// Possible cursor styles (ESC [ q)
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CursorStyle {
Default,
BlinkingBlock,
SteadyBlock,
BlinkingUnderline,
SteadyUnderline,
BlinkingBar,
SteadyBar,
}
/// The status line type for DECSSDT. Non-exhaustive in Zig.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StatusLineType {
None,
Indicator,
HostWritable,
Other(u16),
}
impl StatusLineType {
pub fn from_int(v: u16) -> Self {
match v {
0 => Self::None,
1 => Self::Indicator,
2 => Self::HostWritable,
v => Self::Other(v),
}
}
}
/// The display to target for status updates (DECSASD).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StatusDisplay {
Main,
StatusLine,
}
/// The possible modify key formats to ESC[>{a};{b}m
/// Note: this is not complete, we should add more as we support more
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ModifyKeyFormat {
Legacy,
CursorKeys,
FunctionKeys,
OtherKeysNone,
OtherKeysNumericExcept,
OtherKeysNumeric,
}
/// The protection modes that can be set for the terminal. See DECSCA and
/// ESC V, W.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ProtectedMode {
Off,
/// ESC V, W
Iso,
/// CSI Ps " q
Dec,
}