Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

terminal: Separate raw byte ingestion from text normalization (#64586)

Ben Brandt05c2c9aba0fd
diff --git a/crates/terminal/src/terminal.rs b/crates/terminal/src/terminal.rs
index ed12f8eb447..6bbd5faddac 100644
--- a/crates/terminal/src/terminal.rs
+++ b/crates/terminal/src/terminal.rs
@@ -1964,16 +1964,19 @@ impl Terminal {
apply_config(&self.term, &self.term_config);
}
+ /// Normalizes line endings so text captured outside a PTY starts each line at column zero.
pub fn write_output(&mut self, bytes: &[u8], cx: &mut Context<Self>) {
- // Inject bytes directly into the terminal emulator and refresh the UI.
- // This bypasses the PTY/event loop for display-only terminals.
let mut previous_byte_was_cr = false;
let converted = convert_lf_to_crlf(bytes, &mut previous_byte_was_cr);
+ self.write_raw_output(&converted, cx);
+ }
+ /// Terminal byte streams already contain their control sequences and must not be normalized.
+ fn write_raw_output(&mut self, bytes: &[u8], cx: &mut Context<Self>) {
let mut term = self.term.lock();
self.output_processor
.get_or_insert_with(Processor::<StdSyncHandler>::new)
- .advance(&mut *term, &converted);
+ .advance(&mut *term, bytes);
drop(term);
self.detect_init_command_startup_marker();
cx.emit(Event::Wakeup);
@@ -4875,6 +4878,133 @@ mod tests {
);
}
+ fn display_only_terminal(cx: &mut TestAppContext) -> Entity<Terminal> {
+ cx.new(|cx| {
+ TerminalBuilder::new_display_only(
+ SettingsCursorShape::default(),
+ AlternateScroll::On,
+ None,
+ 0,
+ cx.background_executor(),
+ PathStyle::local(),
+ )
+ .subscribe(cx)
+ })
+ }
+
+ fn raw_content(chunks: &[&[u8]], cx: &mut TestAppContext) -> Content {
+ let terminal = display_only_terminal(cx);
+ for chunk in chunks {
+ terminal.update(cx, |terminal, cx| terminal.write_raw_output(chunk, cx));
+ }
+ terminal.read_with(cx, |terminal, _| {
+ let term = terminal.term.lock_unfair();
+ make_content(&term, &terminal.last_content)
+ })
+ }
+
+ #[gpui::test]
+ async fn test_write_raw_output_preserves_line_control_semantics(cx: &mut TestAppContext) {
+ let bytes = b"a\nb\rc\r\nd";
+ let raw = raw_content(&[bytes], cx);
+
+ let normalized_terminal = display_only_terminal(cx);
+ normalized_terminal.update(cx, |terminal, cx| terminal.write_output(bytes, cx));
+ let normalized = normalized_terminal.read_with(cx, |terminal, _| {
+ let term = terminal.term.lock_unfair();
+ make_content(&term, &terminal.last_content)
+ });
+
+ let raw_cells = raw
+ .cells
+ .iter()
+ .map(|cell| (cell.point.line, cell.point.column, cell.character()))
+ .collect::<Vec<_>>();
+ let normalized_cells = normalized
+ .cells
+ .iter()
+ .map(|cell| (cell.point.line, cell.point.column, cell.character()))
+ .collect::<Vec<_>>();
+
+ assert!(raw_cells.contains(&(0, 0, 'a')));
+ assert!(raw_cells.contains(&(1, 0, 'c')));
+ assert!(raw_cells.contains(&(1, 1, 'b')));
+ assert!(raw_cells.contains(&(2, 0, 'd')));
+ assert!(!normalized_cells.contains(&(1, 1, 'b')));
+ assert!(normalized_cells.contains(&(1, 0, 'c')));
+ assert!(normalized_cells.contains(&(2, 0, 'd')));
+ assert_eq!(raw.cursor.point, Point::new(2, 1));
+ assert_eq!(normalized.cursor.point, Point::new(2, 1));
+ }
+
+ #[gpui::test]
+ async fn test_write_raw_output_is_independent_of_chunk_boundaries(cx: &mut TestAppContext) {
+ let bytes = b"A\nB\rC\r\n\x1b[31mR\xc3\xa9\x1b[0m\x1b[2;6HX";
+ let whole = raw_content(&[bytes], cx);
+ let cells = |content: &Content| {
+ content
+ .cells
+ .iter()
+ .map(|cell| (cell.point, cell.cell.clone()))
+ .collect::<Vec<_>>()
+ };
+ let whole_cells = cells(&whole);
+ let whole_cursor = whole.cursor.point;
+
+ let red_r = whole
+ .cells
+ .iter()
+ .find(|cell| cell.character() == 'R')
+ .expect("raw output should contain the styled R");
+ assert_eq!(red_r.point, Point::new(2, 0));
+ assert_eq!(red_r.foreground(), Color::Named(NamedColor::Red));
+ let accented = whole
+ .cells
+ .iter()
+ .find(|cell| cell.character() == 'é')
+ .expect("raw output should contain the complete UTF-8 character");
+ assert_eq!(accented.point, Point::new(2, 1));
+ assert_eq!(accented.foreground(), Color::Named(NamedColor::Red));
+ let positioned = whole
+ .cells
+ .iter()
+ .find(|cell| cell.point == Point::new(1, 5))
+ .expect("cursor movement should position X");
+ assert_eq!(positioned.character(), 'X');
+ assert_eq!(
+ positioned.foreground(),
+ Color::Named(NamedColor::Foreground)
+ );
+ assert_eq!(
+ positioned.background(),
+ Color::Named(NamedColor::Background)
+ );
+ assert_eq!(whole_cursor, Point::new(1, 6));
+
+ for split in 0..=bytes.len() {
+ let (first, second) = bytes.split_at(split);
+ let split_content = raw_content(&[b"", first, b"", second, b""], cx);
+ assert_eq!(
+ cells(&split_content),
+ whole_cells,
+ "two-part split at byte {split}"
+ );
+ assert_eq!(
+ split_content.cursor.point, whole_cursor,
+ "two-part cursor at byte {split}"
+ );
+ }
+
+ let byte_chunks = bytes
+ .iter()
+ .map(std::slice::from_ref)
+ .chain(std::iter::once(&[][..]))
+ .collect::<Vec<_>>();
+ let bytewise = raw_content(&byte_chunks, cx);
+ assert_eq!(cells(&bytewise), whole_cells);
+ assert_eq!(bytewise.cursor.point, whole_cursor);
+ }
+
#[gpui::test]
async fn test_display_only_write_output_ignores_osc52(cx: &mut TestAppContext) {
cx.update(|cx| {
@@ -4895,13 +5025,16 @@ mod tests {
.subscribe(cx)
});
- terminal.update(cx, |terminal, cx| {
- terminal.write_output(b"\x1b]52;c;b3ZlcndyaXR0ZW4=\x07", cx);
- });
- cx.run_until_parked();
+ for write_output in [Terminal::write_output, Terminal::write_raw_output] {
+ terminal.update(cx, |terminal, cx| {
+ write_output(terminal, b"\x1b]52;c;b3ZlcndyaXR0ZW4=\x07", cx);
+ });
+ cx.run_until_parked();
- let clipboard_text = cx.update(|cx| cx.read_from_clipboard().and_then(|item| item.text()));
- assert_eq!(clipboard_text.as_deref(), Some("original"));
+ let clipboard_text =
+ cx.update(|cx| cx.read_from_clipboard().and_then(|item| item.text()));
+ assert_eq!(clipboard_text.as_deref(), Some("original"));
+ }
}
mod hyperlinks {
@@ -5852,6 +5985,7 @@ mod tests {
terminal.update(cx, |terminal, cx| {
terminal.write_output(b"injected_before_release\n", cx);
+ terminal.write_raw_output(b"\x1b[", cx);
});
assert!(
terminal.read_with(cx, |terminal, _| terminal.output_processor.is_some()),