Files
ghostty-rs/crates/ghostty-vt/src/device_status.rs
T
ddidderr bf84326c2f 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

77 lines
1.9 KiB
Rust

//! Device status requests (ZIG/src/terminal/device_status.zig).
/// The available device status requests. Each is identified by a numeric
/// value plus whether it uses the "?" prefix.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Request {
OperatingStatus,
CursorPosition,
ColorScheme,
}
impl Request {
/// The numeric value of the request.
pub fn value(self) -> u16 {
ENTRIES
.iter()
.find(|e| e.request == self)
.map_or(0, |e| e.value)
}
/// Whether the request uses the "?" prefix.
pub fn question(self) -> bool {
ENTRIES
.iter()
.find(|e| e.request == self)
.is_some_and(|e| e.question)
}
}
struct Entry {
request: Request,
value: u16,
question: bool,
}
/// The full list of device status request entries.
const ENTRIES: &[Entry] = &[
Entry {
request: Request::OperatingStatus,
value: 5,
question: false,
},
Entry {
request: Request::CursorPosition,
value: 6,
question: false,
},
Entry {
request: Request::ColorScheme,
value: 996,
question: true,
},
];
pub fn req_from_int(v: u16, question: bool) -> Option<Request> {
ENTRIES
.iter()
.find(|e| e.value == v && e.question == question)
.map(|e| e.request)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn req_from_int_lookup() {
assert_eq!(req_from_int(5, false), Some(Request::OperatingStatus));
assert_eq!(req_from_int(6, false), Some(Request::CursorPosition));
assert_eq!(req_from_int(996, true), Some(Request::ColorScheme));
assert_eq!(req_from_int(996, false), None);
assert_eq!(req_from_int(5, true), None);
assert_eq!(Request::ColorScheme.value(), 996);
assert!(Request::ColorScheme.question());
}
}