Skip to content

A modern terminal with a built-in multiplexer: close it and your sessions keep running. Run shells and tons of AI agents across your machines, then pick them up from desktop, browser, TUI and more. Written in Rust with no UI framework: interface drawn straight on the GPU. No Electron, GPUI or webview.

perf(parser): emit printable ASCII runs to terminal consumers

RoversXec9feae145d2
diff --git a/mux/src/lib.rs b/mux/src/lib.rs
index a82b4114..f32ff489 100644
--- a/mux/src/lib.rs
+++ b/mux/src/lib.rs
@@ -669,7 +669,7 @@ fn parse_buffered_data(
Ok(size) => {
unapplied += size;
let work_started = Instant::now();
- parser.parse(&buf[0..size], |action| {
+ parser.parse_print_runs(&buf[0..size], |action| {
let mut flush = false;
match &action {
Action::CSI(CSI::Mode(Mode::SetDecPrivateMode(DecPrivateMode::Code(
diff --git a/term/src/terminal.rs b/term/src/terminal.rs
index 4edaa7c5..e10ee766 100644
--- a/term/src/terminal.rs
+++ b/term/src/terminal.rs
@@ -168,7 +168,8 @@ impl Terminal {
let mut performer = Performer::new(&mut self.state);
- self.parser.parse(bytes, |action| performer.perform(action));
+ self.parser
+ .parse_print_runs(bytes, |action| performer.perform(action));
}
self.trigger_unseen_output_notif();
}
diff --git a/term/src/terminalstate/performer.rs b/term/src/terminalstate/performer.rs
index 02aa525f..0a033216 100644
--- a/term/src/terminalstate/performer.rs
+++ b/term/src/terminalstate/performer.rs
@@ -1299,7 +1299,9 @@ mod ascii_tests {
let input = format!("{setup}abc #q~0123456789az next\rOVERWRITE\nmore\te\u{301}1\u{fe0f}\u{20e3}界\x1b[0m\x1bkASCII title界\x1b\\end");
for chunk in input.as_bytes().chunks(chunk_size) {
let mut actions = Vec::new();
- parser.parse(chunk, |action| action.append_to(&mut actions));
+ parser.parse_print_runs(chunk, |action| {
+ action.append_to(&mut actions)
+ });
actual.increment_seqno();
expected.increment_seqno();
{
diff --git a/vtparse/src/lib.rs b/vtparse/src/lib.rs
index f629b1a1..b262a366 100644
--- a/vtparse/src/lib.rs
+++ b/vtparse/src/lib.rs
@@ -110,6 +110,14 @@ pub trait VTActor {
/// character.
fn print(&mut self, b: char);
+ /// Print a non-empty run of bytes in the range 0x20..=0x7e.
+ /// The default preserves the per-character callback for existing actors.
+ fn print_ascii(&mut self, text: &str) {
+ for byte in text.bytes() {
+ self.print(byte as char);
+ }
+ }
+
/// The C0 or C1 control function should be executed, which may have any one of a variety of
/// effects, including changing the cursor position, suspending or resuming communications or
/// changing the shift states in effect.
@@ -770,6 +778,11 @@ impl VTParser {
return;
}
+ self.parse_non_utf8_byte(byte, actor);
+ }
+
+ #[inline(always)]
+ fn parse_non_utf8_byte(&mut self, byte: u8, actor: &mut dyn VTActor) {
let (action, state) = lookup(self.state, byte);
if state != self.state {
@@ -788,9 +801,46 @@ impl VTParser {
/// Parse a sequence of bytes. The sequence need not be complete.
/// This may result in some number of calls to the methods on the
/// provided `actor`.
- pub fn parse(&mut self, bytes: &[u8], actor: &mut dyn VTActor) {
- for b in bytes {
- self.parse_byte(*b, actor);
+ pub fn parse(&mut self, mut bytes: &[u8], actor: &mut dyn VTActor) {
+ while !bytes.is_empty() {
+ // Continuation bytes cannot begin a printable run. Keep their
+ // original decoder path free of ASCII and payload-state checks.
+ if self.state == State::Utf8Sequence {
+ self.next_utf8(actor, bytes[0]);
+ bytes = &bytes[1..];
+ continue;
+ }
+ // Only Ground can bypass the state machine. In particular, ASCII
+ // following an incomplete UTF-8 sequence must reach next_utf8.
+ if self.is_ground() && (b' '..=b'~').contains(&bytes[0]) {
+ let end = bytes
+ .iter()
+ .position(|b| !(b' '..=b'~').contains(b))
+ .unwrap_or(bytes.len());
+ actor.print_ascii(core::str::from_utf8(&bytes[..end]).unwrap());
+ bytes = &bytes[end..];
+ } else {
+ self.parse_non_utf8_byte(bytes[0], actor);
+ bytes = &bytes[1..];
+ if matches!(
+ self.state,
+ State::ApcString
+ | State::OscString
+ | State::DcsPassthrough
+ | State::DcsIgnore
+ | State::SosPmString
+ ) {
+ // String payloads (notably images) need the byte parser,
+ // so avoid an extra run-detection branch on every byte.
+ // Finish this input chunk with the original loop. Any text
+ // after the terminator still emits ordinary print actions;
+ // batching can resume on the next input chunk.
+ for &byte in bytes {
+ self.parse_byte(byte, actor);
+ }
+ return;
+ }
+ }
}
}
}
@@ -807,6 +857,48 @@ mod test {
actor.into_vec()
}
+ #[test]
+ fn ascii_runs_match_byte_parser_in_every_streaming_state() {
+ let prefixes: &[&[u8]] = &[
+ b"",
+ b"\x1b",
+ b"\x1b[",
+ b"\x1b[12;",
+ b"\x1b]0;",
+ b"\x1bP1;2q",
+ b"\x1b_",
+ b"\x1b^",
+ b"\xc3",
+ b"\xf0\x9f",
+ ];
+ for prefix in prefixes {
+ for byte in 0..=255 {
+ let mut input = prefix.to_vec();
+ input.push(byte);
+ input.extend_from_slice(b"abc ~\x7f\x00\x1b\\e\xcc\x81\r\nend");
+ let mut scalar = VTParser::new();
+ let mut expected = CollectingVTActor::default();
+ for &b in &input {
+ scalar.parse_byte(b, &mut expected);
+ }
+ let expected = expected.into_vec();
+ for size in [1, 2, 7, 64] {
+ let mut batched = VTParser::new();
+ let mut actual = CollectingVTActor::default();
+ for chunk in input.chunks(size) {
+ batched.parse(chunk, &mut actual);
+ }
+ assert_eq!(
+ actual.into_vec(),
+ expected,
+ "prefix={prefix:?}, byte={byte}, chunk={size}"
+ );
+ assert_eq!(batched.is_ground(), scalar.is_ground());
+ }
+ }
+ }
+ }
+
#[test]
fn test_mixed() {
assert_eq!(
diff --git a/wezterm-escape-parser/src/lib.rs b/wezterm-escape-parser/src/lib.rs
index 2b35b0e0..3d3652f0 100644
--- a/wezterm-escape-parser/src/lib.rs
+++ b/wezterm-escape-parser/src/lib.rs
@@ -63,7 +63,7 @@ pub enum Action {
impl Action {
/// Append this `Action` to a `Vec<Action>`.
- /// If this `Action` is `Print` and the last element is `Print` or
+ /// If this `Action` is `Print` or `PrintString` and the last element is `Print` or
/// `PrintString` then the elements are combined into `PrintString`
/// to reduce heap utilization.
pub fn append_to(self, dest: &mut Vec<Self>) {
@@ -83,6 +83,22 @@ impl Action {
_ => {}
}
}
+ if let Action::PrintString(text) = &self {
+ match dest.last_mut() {
+ Some(Action::PrintString(prior)) => {
+ prior.push_str(text);
+ return;
+ }
+ Some(Action::Print(prior)) => {
+ let mut combined = String::with_capacity(prior.len_utf8() + text.len());
+ combined.push(*prior);
+ combined.push_str(text);
+ *dest.last_mut().unwrap() = Action::PrintString(combined);
+ return;
+ }
+ _ => {}
+ }
+ }
dest.push(self);
}
}
diff --git a/wezterm-escape-parser/src/parser/mod.rs b/wezterm-escape-parser/src/parser/mod.rs
index 349ce21d..9c9c79d4 100644
--- a/wezterm-escape-parser/src/parser/mod.rs
+++ b/wezterm-escape-parser/src/parser/mod.rs
@@ -89,7 +89,18 @@ impl Parser {
return tmux_parser.advance_bytes(bytes);
}
- pub fn parse<F: FnMut(Action)>(&mut self, bytes: &[u8], mut callback: F) {
+ pub fn parse<F: FnMut(Action)>(&mut self, bytes: &[u8], callback: F) {
+ self.parse_impl(bytes, callback, false);
+ }
+
+ /// Like `parse`, but emits contiguous printable ASCII as `PrintString`.
+ /// Run boundaries have no semantic meaning: consumers must still combine
+ /// adjacent print actions before Unicode grapheme segmentation.
+ pub fn parse_print_runs<F: FnMut(Action)>(&mut self, bytes: &[u8], callback: F) {
+ self.parse_impl(bytes, callback, true);
+ }
+
+ fn parse_impl<F: FnMut(Action)>(&mut self, bytes: &[u8], mut callback: F, print_runs: bool) {
#[cfg(feature = "tmux_cc")]
let is_tmux_mode: bool = self.state.borrow().tmux_state.is_some();
#[cfg(feature = "tmux_cc")]
@@ -108,6 +119,7 @@ impl Parser {
let mut perform = Performer {
callback: &mut callback,
state: &mut parser_state,
+ print_runs,
};
self.state_machine
.parse(unparsed_str.as_bytes(), &mut perform);
@@ -119,6 +131,7 @@ impl Parser {
let mut perform = Performer {
callback: &mut callback,
state: &mut self.state.borrow_mut(),
+ print_runs,
};
self.state_machine.parse(bytes, &mut perform);
}
@@ -144,6 +157,7 @@ impl Parser {
*first.borrow_mut() = Some(action);
},
state: &mut self.state.borrow_mut(),
+ print_runs: false,
};
for (idx, b) in bytes.iter().enumerate() {
self.state_machine.parse_byte(*b, &mut perform);
@@ -181,6 +195,7 @@ impl Parser {
&mut Performer {
callback: &mut |action| actions.push(action),
state: &mut self.state.borrow_mut(),
+ print_runs: false,
},
);
if !actions.is_empty() && self.state_machine.is_ground() {
@@ -196,6 +211,7 @@ impl Parser {
struct Performer<'a, F: FnMut(Action) + 'a> {
callback: &'a mut F,
state: &'a mut ParseState,
+ print_runs: bool,
}
fn is_short_dcs(intermediates: &[u8], byte: u8) -> bool {
@@ -212,6 +228,16 @@ impl<'a, F: FnMut(Action)> VTActor for Performer<'a, F> {
(self.callback)(Action::Print(c));
}
+ fn print_ascii(&mut self, text: &str) {
+ if self.print_runs && text.len() > 1 {
+ (self.callback)(Action::PrintString(text.to_string()));
+ } else {
+ for byte in text.bytes() {
+ self.print(byte as char);
+ }
+ }
+ }
+
fn execute_c0_or_c1(&mut self, byte: u8) {
match FromPrimitive::from_u8(byte) {
Some(code) => (self.callback)(Action::Control(code)),
@@ -744,6 +770,38 @@ mod test {
actions
}
+ #[test]
+ fn print_runs_preserve_coalesced_actions_across_chunk_boundaries() {
+ let inputs: &[&[u8]] = &[
+ b"hello world",
+ "e\u{301}abc界1\u{fe0f}\u{20e3}xyz".as_bytes(),
+ b"abc\r\n\x1b[31mred\x1b[0m\x1b[5b\x7f\x00tail",
+ b"\x1b]0;title\x07text\x1bP+q544e\x1b\\next",
+ b"\x1b_Gi=1,s=1,v=1,f=24;YWJj\x1b\\end",
+ b"a\xf0\x9fASCII\xc3\x1b[0mb",
+ b"\x1b(0lqqk\x0eqx\x0f\x1b(Bnormal\x1bktitle\x1b\\end",
+ ];
+ for input in inputs {
+ let mut scalar = Parser::new();
+ let mut expected = vec![];
+ scalar.parse(input, |action| action.append_to(&mut expected));
+ for size in 1..=input.len() {
+ let mut batched = Parser::new();
+ let mut actual = vec![];
+ for chunk in input.chunks(size) {
+ batched.parse_print_runs(chunk, |action| action.append_to(&mut actual));
+ }
+ assert_eq!(actual, expected, "input={input:?}, chunk={size}");
+ }
+ }
+ let mut actions = vec![];
+ Parser::new().parse_print_runs(b"a long ASCII run", |action| actions.push(action));
+ assert_eq!(
+ actions,
+ vec![Action::PrintString("a long ASCII run".into())]
+ );
+ }
+
fn parse_as(s: &str, expected: &str) -> Vec<Action> {
let mut p = Parser::new();
let actions = p.parse_as_vec(s.as_bytes());