diff --git a/crates/terminal/src/alacritty.rs b/crates/terminal/src/alacritty.rs index b2e2107da9a..5f47be765c4 100644 --- a/crates/terminal/src/alacritty.rs +++ b/crates/terminal/src/alacritty.rs @@ -31,7 +31,7 @@ use alacritty_terminal::{ use anyhow::{Context as _, Result}; use futures::channel::mpsc::UnboundedSender; use util::paths::PathStyle; -use vte::ansi::Handler; +use vte::ansi::{Handler, Processor, Rgb, StdSyncHandler}; #[cfg(target_os = "windows")] use windows::Win32::{Foundation::HANDLE, System::Threading::GetProcessId}; @@ -244,6 +244,40 @@ pub(super) fn display_offset(term: &AlacrittyTerm) -> usize { term.grid().display_offset() } +pub(super) fn history_size(term: &AlacrittyTerm) -> usize { + term.history_size() +} + +pub(super) fn cursor_line(term: &AlacrittyTerm) -> i32 { + term.grid().cursor.point.line.0 +} + +pub(super) fn cursor_blinking(term: &AlacrittyTerm) -> bool { + term.cursor_style().blinking +} + +/// A color set by the running program (OSC 4/10/11), if any. +pub(super) fn palette_color(term: &AlacrittyTerm, index: usize) -> Option { + term.colors()[index] +} + +pub(super) fn renderable_cells(term: &AlacrittyTerm) -> RenderableCells<'_> { + RenderableCells::new(term.renderable_content().display_iter) +} + +/// Feeds output that doesn't come through the PTY event loop into a terminal. +pub(super) struct OutputParser(Processor); + +impl OutputParser { + pub(super) fn new() -> Self { + Self(Processor::new()) + } + + pub(super) fn advance(&mut self, term: &mut AlacrittyTerm, bytes: &[u8]) { + self.0.advance(term, bytes); + } +} + pub(super) fn scroll_display(term: &mut AlacrittyTerm, scroll: Scroll) { term.scroll_display(scroll.to_alacritty()); } diff --git a/crates/terminal/src/terminal.rs b/crates/terminal/src/terminal.rs index 8f6550235c1..ca6e4f838ab 100644 --- a/crates/terminal/src/terminal.rs +++ b/crates/terminal/src/terminal.rs @@ -15,7 +15,6 @@ use futures::{ channel::mpsc::{UnboundedReceiver, unbounded}, }; -use alacritty_terminal::grid::Dimensions as _; use itertools::Itertools as _; use mappings::mouse::{ alt_scroll, grid_point, grid_point_and_side, mouse_button_report, mouse_moved_report, @@ -63,10 +62,11 @@ use gpui::{ use crate::alacritty::current_child_signal_mask; use crate::alacritty::{ AlacrittyCell, AlacrittyGridIterator, AlacrittyHyperlink, AlacrittySearch, AlacrittyTerm, - AlacrittyTermConfig, AlacrittyTermLock, HyperlinkMatch, PtySender, RegexSearches, - append_text_to_term, apply_config, clear_saved_screen, content_text, display_offset, - display_only_term_config, find_from_terminal_point, full_content_range, last_non_empty_lines, - make_content, new_term, open_pty, pty_options, pty_term_config, resize, screen_lines, + AlacrittyTermConfig, AlacrittyTermLock, HyperlinkMatch, OutputParser, PtySender, RegexSearches, + append_text_to_term, apply_config, clear_saved_screen, content_text, cursor_blinking, + cursor_line, display_offset, display_only_term_config, find_from_terminal_point, + full_content_range, history_size, last_non_empty_lines, make_content, new_term, open_pty, + palette_color, pty_options, pty_term_config, renderable_cells, resize, screen_lines, scroll_display, scroll_to_point, search_matches, selection_text, set_default_cursor_style, set_selection as set_term_selection, shrink_to_used, spawn_event_loop, toggle_vi_mode as toggle_term_vi_mode, total_lines, update_selection as update_term_selection, @@ -1527,7 +1527,7 @@ pub struct Terminal { completion_tx: Option>>, term: Arc, term_config: AlacrittyTermConfig, - output_processor: Option>, + output_processor: Option, output_events_tx: Option>, output_generation: u64, events: VecDeque, @@ -1687,8 +1687,7 @@ impl Terminal { self.write_to_pty(format(self.last_content.terminal_bounds).into_bytes()) } TerminalBackendEvent::CursorBlinkingChange => { - let terminal = self.term.lock(); - let blinking = terminal.cursor_style().blinking; + let blinking = cursor_blinking(&self.term.lock()); cx.emit(Event::BlinkChanged(blinking)); } TerminalBackendEvent::Bell => { @@ -1716,7 +1715,7 @@ impl Terminal { // we might respond with out of date value if a "set color" sequence is immediately // followed by a color request sequence. - let color = self.term.lock().colors()[index] + let color = palette_color(&self.term.lock(), index) .unwrap_or_else(|| to_vte_rgb(get_color_at_index(index, cx.theme().as_ref()))); self.write_to_pty(format(color).into_bytes()); } @@ -1869,7 +1868,7 @@ impl Terminal { self.path_style, ) { Some(hyperlink) => { - let history_size = term.history_size(); + let history_size = history_size(term); self.process_hyperlink(hyperlink, *open, history_size, cx); } None => { @@ -1880,7 +1879,7 @@ impl Terminal { InternalEvent::ProcessHyperlink(hyperlink, open) => { // history_size must be read here since process_hyperlink cannot lock term // (sync() already holds the lock when dispatching events) - let history_size = term.history_size(); + let history_size = history_size(term); self.process_hyperlink(hyperlink.clone(), *open, history_size, cx); } } @@ -1996,8 +1995,8 @@ impl Terminal { pub fn write_raw_output(&mut self, bytes: &[u8], cx: &mut Context) { let mut term = self.term.lock(); self.output_processor - .get_or_insert_with(Processor::::new) - .advance(&mut *term, bytes); + .get_or_insert_with(OutputParser::new) + .advance(&mut term, bytes); drop(term); self.detect_init_command_startup_marker(); cx.emit(Event::Wakeup); @@ -2049,9 +2048,7 @@ impl Terminal { self.cwd_history.clear(); self.pending_cwd_boundary = None; self.write_raw_output(bytes, cx); - cx.emit(Event::BlinkChanged( - self.term.lock().cursor_style().blinking, - )); + cx.emit(Event::BlinkChanged(cursor_blinking(&self.term.lock()))); cx.emit(Event::BreadcrumbsChanged); cx.emit(Event::NewNavigationTarget(None)); cx.emit(Event::OutputReplaced); @@ -2325,8 +2322,8 @@ impl Terminal { if !self.is_remote_terminal && input.contains(&b'\r') { let term = self.term.lock_unfair(); self.pending_cwd_boundary = Some(Self::scrollback_position( - term.grid().cursor.point.line.0, - term.history_size(), + cursor_line(&term), + history_size(&term), )); } @@ -2518,8 +2515,7 @@ impl Terminal { pub fn with_renderable_cells(&self, f: impl for<'a> FnOnce(RenderableCells<'a>) -> R) -> R { let term = self.term.lock_unfair(); - let content = term.renderable_content(); - f(RenderableCells::new(content.display_iter)) + f(renderable_cells(&term)) } pub fn get_content(&self) -> String { @@ -3011,7 +3007,7 @@ impl Terminal { let scrollback_position = self.pending_cwd_boundary.take().unwrap_or_else(|| { let term = self.term.lock_unfair(); - Self::scrollback_position(term.grid().cursor.point.line.0, term.history_size()) + Self::scrollback_position(cursor_line(&term), history_size(&term)) }); self.cwd_history.push(CwdHistoryEntry { scrollback_position, @@ -3414,7 +3410,7 @@ fn spawn_task_subprocess( let events_tx = events_tx.clone(); async move { let Some(mut reader) = reader else { return }; - let mut processor = Processor::::new(); + let mut processor = OutputParser::new(); let mut buffer = [0u8; 8192]; let mut previous_byte_was_cr = false; loop { @@ -3429,7 +3425,7 @@ fn spawn_task_subprocess( convert_lf_to_crlf(&buffer[..count], &mut previous_byte_was_cr); { let mut term = term.lock(); - processor.advance(&mut *term, &converted); + processor.advance(&mut term, &converted); } events_tx .unbounded_send(PtyEvent::Event(