diff options
Diffstat (limited to 'src/pane')
| -rw-r--r-- | src/pane/terminal.rs | 136 | ||||
| -rw-r--r-- | src/pane/terminal/windows_recent_fallback.rs | 50 |
2 files changed, 131 insertions, 55 deletions
diff --git a/src/pane/terminal.rs b/src/pane/terminal.rs index 81c57ef9..d5070b9b 100644 --- a/src/pane/terminal.rs +++ b/src/pane/terminal.rs @@ -141,6 +141,12 @@ pub(crate) struct ProcessBytesResult { pub terminal_responses: Vec<Bytes>, } +#[derive(Debug, Clone, PartialEq, Eq, Default)] +pub(crate) struct TerminalReadSnapshot { + pub text: String, + pub truncated: bool, +} + pub(crate) struct GhosttyPaneTerminal { pub core: Mutex<GhosttyPaneCore>, key_encoder: Mutex<crate::ghostty::KeyEncoder>, @@ -387,18 +393,26 @@ impl PaneTerminal { self.ghostty.recent_text(lines) } - pub fn recent_ansi(&self, lines: usize) -> String { - self.ghostty.recent_ansi(lines) + pub(crate) fn recent_text_snapshot(&self, lines: usize) -> TerminalReadSnapshot { + self.ghostty.recent_text_snapshot(lines) } - pub fn recent_unwrapped_text(&self, lines: usize) -> String { - self.ghostty.recent_unwrapped_text(lines) + pub(crate) fn recent_ansi_snapshot(&self, lines: usize) -> TerminalReadSnapshot { + self.ghostty.recent_ansi_snapshot(lines) + } + + pub(crate) fn recent_unwrapped_text_snapshot(&self, lines: usize) -> TerminalReadSnapshot { + self.ghostty.recent_unwrapped_text_snapshot(lines) } pub fn recent_unwrapped_ansi(&self, lines: usize) -> String { self.ghostty.recent_unwrapped_ansi(lines) } + pub(crate) fn recent_unwrapped_ansi_snapshot(&self, lines: usize) -> TerminalReadSnapshot { + self.ghostty.recent_unwrapped_ansi_snapshot(lines) + } + pub fn extract_selection(&self, selection: &crate::selection::Selection) -> Option<String> { self.ghostty.extract_selection(selection) } @@ -1708,34 +1722,52 @@ impl GhosttyPaneTerminal { } pub fn recent_text(&self, lines: usize) -> String { + self.recent_text_snapshot(lines).text + } + + pub(crate) fn recent_text_snapshot(&self, lines: usize) -> TerminalReadSnapshot { self.core .lock() .ok() - .and_then(|core| ghostty_recent_text(&core, lines).ok()) + .and_then(|core| ghostty_recent_text_snapshot(&core, lines).ok()) .unwrap_or_default() } + #[cfg(test)] pub fn recent_ansi(&self, lines: usize) -> String { + self.recent_ansi_snapshot(lines).text + } + + pub(crate) fn recent_ansi_snapshot(&self, lines: usize) -> TerminalReadSnapshot { self.core .lock() .ok() - .and_then(|core| ghostty_recent_ansi(&core, lines, false).ok()) + .and_then(|core| ghostty_recent_ansi_snapshot(&core, lines, false).ok()) .unwrap_or_default() } + #[cfg(test)] pub fn recent_unwrapped_text(&self, lines: usize) -> String { + self.recent_unwrapped_text_snapshot(lines).text + } + + pub(crate) fn recent_unwrapped_text_snapshot(&self, lines: usize) -> TerminalReadSnapshot { self.core .lock() .ok() - .and_then(|core| ghostty_recent_text_unwrapped(&core, lines).ok()) + .and_then(|core| ghostty_recent_text_unwrapped_snapshot(&core, lines).ok()) .unwrap_or_default() } pub fn recent_unwrapped_ansi(&self, lines: usize) -> String { + self.recent_unwrapped_ansi_snapshot(lines).text + } + + pub(crate) fn recent_unwrapped_ansi_snapshot(&self, lines: usize) -> TerminalReadSnapshot { self.core .lock() .ok() - .and_then(|core| ghostty_recent_ansi(&core, lines, true).ok()) + .and_then(|core| ghostty_recent_ansi_snapshot(&core, lines, true).ok()) .unwrap_or_default() } @@ -2289,34 +2321,23 @@ fn ghostty_recent_text( core: &GhosttyPaneCore, lines: usize, ) -> Result<String, crate::ghostty::Error> { + ghostty_recent_text_snapshot(core, lines).map(|snapshot| snapshot.text) +} + +fn ghostty_recent_text_snapshot( + core: &GhosttyPaneCore, + lines: usize, +) -> Result<TerminalReadSnapshot, crate::ghostty::Error> { let text = ghostty_recent_text_for_terminal(&core.terminal, lines)?; - #[cfg(windows)] - { - if text.trim().is_empty() { - let fallback = windows_recent_fallback::recent_text(core, lines, false); - if !fallback.trim().is_empty() { - return Ok(fallback); - } - } - } - Ok(text) + Ok(finish_recent_snapshot(core, text, lines, false)) } -fn ghostty_recent_text_unwrapped( +fn ghostty_recent_text_unwrapped_snapshot( core: &GhosttyPaneCore, lines: usize, -) -> Result<String, crate::ghostty::Error> { +) -> Result<TerminalReadSnapshot, crate::ghostty::Error> { let text = ghostty_recent_text_unwrapped_for_terminal(&core.terminal, lines)?; - #[cfg(windows)] - { - if text.trim().is_empty() { - let fallback = windows_recent_fallback::recent_text(core, lines, true); - if !fallback.trim().is_empty() { - return Ok(fallback); - } - } - } - Ok(text) + Ok(finish_recent_snapshot(core, text, lines, true)) } fn ghostty_recent_ansi( @@ -2324,17 +2345,42 @@ fn ghostty_recent_ansi( lines: usize, unwrap: bool, ) -> Result<String, crate::ghostty::Error> { - let ansi = ghostty_recent_ansi_for_terminal(&core.terminal, lines, unwrap)?; + ghostty_recent_ansi_snapshot(core, lines, unwrap).map(|snapshot| snapshot.text) +} + +fn ghostty_recent_ansi_snapshot( + core: &GhosttyPaneCore, + lines: usize, + unwrap: bool, +) -> Result<TerminalReadSnapshot, crate::ghostty::Error> { + let text = ghostty_recent_ansi_for_terminal(&core.terminal, lines, unwrap)?; + Ok(finish_recent_snapshot(core, text, lines, unwrap)) +} + +fn finish_recent_snapshot( + core: &GhosttyPaneCore, + text: String, + lines: usize, + unwrap: bool, +) -> TerminalReadSnapshot { + #[cfg(not(windows))] + let _ = unwrap; #[cfg(windows)] - { - if ansi.trim().is_empty() { - let fallback = windows_recent_fallback::recent_text(core, lines, unwrap); - if !fallback.trim().is_empty() { - return Ok(fallback); - } + if text.trim().is_empty() { + let fallback = windows_recent_fallback::recent_text(core, lines, unwrap); + if !fallback.text.trim().is_empty() { + return fallback; } } - Ok(ansi) + + // Recent read limits are measured in rendered rows, including blank or styled rows. + TerminalReadSnapshot { + text, + truncated: core + .terminal + .total_rows() + .is_ok_and(|total_rows| total_rows > lines), + } } fn ghostty_recent_text_for_terminal( @@ -4342,6 +4388,20 @@ mod tests { } #[test] + fn recent_snapshots_report_omitted_rendered_rows() { + let (tx, _rx) = mpsc::channel(4); + let mut terminal = crate::ghostty::Terminal::new(20, 3, 100).unwrap(); + terminal.write(b"one\r\ntwo\r\nthree\r\nfour"); + let pane = GhosttyPaneTerminal::new(terminal, tx).unwrap(); + + assert!(pane.recent_text_snapshot(2).truncated); + assert!(pane.recent_ansi_snapshot(2).truncated); + assert!(pane.recent_unwrapped_text_snapshot(2).truncated); + assert!(pane.recent_unwrapped_ansi_snapshot(2).truncated); + assert!(!pane.recent_text_snapshot(100).truncated); + } + + #[test] fn plain_text_reads_skip_wide_character_spacer_cells() { let (tx, _rx) = mpsc::channel(4); let mut terminal = crate::ghostty::Terminal::new(40, 3, 100).unwrap(); diff --git a/src/pane/terminal/windows_recent_fallback.rs b/src/pane/terminal/windows_recent_fallback.rs index c9a02f67..caa3b9f7 100644 --- a/src/pane/terminal/windows_recent_fallback.rs +++ b/src/pane/terminal/windows_recent_fallback.rs @@ -1,4 +1,4 @@ -use super::{ghostty_line_from_cells, GhosttyPaneCore}; +use super::{ghostty_line_from_cells, GhosttyPaneCore, TerminalReadSnapshot}; const CACHE_LINES: usize = 2000; @@ -44,9 +44,13 @@ pub(super) fn update(core: &mut GhosttyPaneCore) { core.recent_fallback.usable = true; } -pub(super) fn recent_text(core: &GhosttyPaneCore, lines: usize, unwrap: bool) -> String { +pub(super) fn recent_text( + core: &GhosttyPaneCore, + lines: usize, + unwrap: bool, +) -> TerminalReadSnapshot { if !primary_screen_active(core) { - return String::new(); + return TerminalReadSnapshot::default(); } if unwrap { unwrapped_text(core, lines) @@ -62,9 +66,9 @@ fn primary_screen_active(core: &GhosttyPaneCore) -> bool { ) } -fn wrapped_text(core: &GhosttyPaneCore, lines: usize) -> String { +fn wrapped_text(core: &GhosttyPaneCore, lines: usize) -> TerminalReadSnapshot { if !core.recent_fallback.usable { - return String::new(); + return TerminalReadSnapshot::default(); } let rows: Vec<&str> = core .recent_fallback @@ -72,16 +76,24 @@ fn wrapped_text(core: &GhosttyPaneCore, lines: usize) -> String { .iter() .map(|line| line.text.as_str()) .collect(); - cache_text(&rows, lines) + TerminalReadSnapshot { + text: cache_text(&rows, lines), + truncated: rows.len() > lines, + } } -fn unwrapped_text(core: &GhosttyPaneCore, lines: usize) -> String { +fn unwrapped_text(core: &GhosttyPaneCore, lines: usize) -> TerminalReadSnapshot { if !core.recent_fallback.usable { - return String::new(); + return TerminalReadSnapshot::default(); } - let unwrapped = unwrap_render_lines(&core.recent_fallback.rows); + let rendered_rows = &core.recent_fallback.rows; + let start = rendered_rows.len().saturating_sub(lines); + let unwrapped = unwrap_render_lines(&rendered_rows[start..]); let rows: Vec<&str> = unwrapped.iter().map(String::as_str).collect(); - cache_text(&rows, lines) + TerminalReadSnapshot { + text: cache_text(&rows, rows.len()), + truncated: rendered_rows.len() > lines, + } } fn viewport_is_at_bottom(core: &GhosttyPaneCore) -> bool { @@ -298,8 +310,8 @@ mod tests { pane.process_pty_bytes(pane_id, 0, b"new output\r\n", &tx); let core = pane.core.lock().unwrap(); - assert_eq!(recent_text(&core, 3, false), ""); - assert_eq!(recent_text(&core, 3, true), ""); + assert_eq!(recent_text(&core, 3, false).text, ""); + assert_eq!(recent_text(&core, 3, true).text, ""); } #[test] @@ -311,12 +323,12 @@ mod tests { pane.seed_history_ansi("seeded history\r\n"); let core = pane.core.lock().unwrap(); - assert!(recent_text(&core, 3, false).contains("seeded history")); - assert!(recent_text(&core, 3, true).contains("seeded history")); + assert!(recent_text(&core, 3, false).text.contains("seeded history")); + assert!(recent_text(&core, 3, true).text.contains("seeded history")); } #[test] - fn recent_ansi_unwrapped_uses_unwrapped_render_cache() { + fn recent_ansi_unwrapped_limits_rendered_rows_before_unwrapping() { let (tx, _rx) = mpsc::channel(4); let terminal = crate::ghostty::Terminal::new(40, 3, 1024).unwrap(); let pane = GhosttyPaneTerminal::new(terminal, tx).unwrap(); @@ -324,13 +336,17 @@ mod tests { { let mut core = pane.core.lock().unwrap(); core.recent_fallback.rows = vec![ + rendered_line("older", false, false), rendered_line("wrapped", true, false), rendered_line("rows", false, true), + rendered_line("last", false, false), ]; core.recent_fallback.usable = true; } - assert_eq!(pane.recent_ansi(2), "wrapped\nrows\n"); - assert_eq!(pane.recent_unwrapped_ansi(2), "wrappedrows\n"); + let snapshot = pane.recent_unwrapped_ansi_snapshot(3); + assert_eq!(snapshot.text, "wrappedrows\nlast\n"); + assert!(snapshot.truncated); + assert_eq!(pane.recent_ansi(3), "wrapped\nrows\nlast\n"); } } |
