1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
//! Focused helper methods for individual phases of `gather_render_data`.
//!
//! Each function here is a thin `impl WindowState` method that accepts the
//! shared local values that `gather_render_data` has already computed, reducing
//! the size of that function without requiring a `GatherDataContext` struct
//! (which the borrow checker fights when `tab.prettifier` and `tab.pane_manager`
//! are simultaneously borrowed).
use std::sync::Arc;
use std::time::Instant;
use par_term_config::ScrollbackMark;
use par_term_terminal::TerminalManager;
use winit::dpi::PhysicalSize;
use crate::app::window_state::WindowState;
use crate::cell_renderer::Cell;
impl WindowState {
/// Collect scrollback length, terminal title, and drain shell lifecycle events
/// from the active terminal. Updates command history from scrollback marks and
/// the core library.
///
/// Returns `(scrollback_len, terminal_title, shell_lifecycle_events)`.
/// Falls back to cached values when the terminal is locked.
pub(super) fn collect_scrollback_state(
&mut self,
terminal: &Arc<tokio::sync::RwLock<TerminalManager>>,
current_cursor_pos: Option<(usize, usize)>,
cached_scrollback_len: usize,
cached_terminal_title: &str,
) -> (usize, String, Vec<par_term_terminal::ShellLifecycleEvent>) {
if let Ok(mut term) = terminal.try_write() {
let cursor_row = current_cursor_pos.map(|(_, row)| row).unwrap_or(0);
let sb_len = term.scrollback_len();
term.update_scrollback_metadata(sb_len, cursor_row);
let shell_events = term.drain_shell_lifecycle_events();
// Feed newly completed commands into persistent history from two sources:
// 1. Scrollback marks (populated via set_mark_command_at from grid text extraction)
// 2. Core library command history (populated by the terminal emulator core)
//
// synced_commands gates the first-time add. For subsequent frames, if the
// exit code was stored as None (command finished before the D-marker was
// processed), we allow an update to Some(n) from either source.
for mark in term.scrollback_marks() {
if let Some(ref cmd) = mark.command
&& !cmd.is_empty()
{
if self.overlay_ui.synced_commands.insert(cmd.clone()) {
self.overlay_ui.command_history.add(
cmd.clone(),
mark.exit_code,
mark.duration_ms,
);
} else if mark.exit_code.is_some() {
self.overlay_ui.command_history.update_exit_code_if_unknown(
cmd,
mark.exit_code,
mark.duration_ms,
);
}
}
}
for (cmd, exit_code, duration_ms) in term.core_command_history() {
if !cmd.is_empty() {
if self.overlay_ui.synced_commands.insert(cmd.clone()) {
self.overlay_ui
.command_history
.add(cmd, exit_code, duration_ms);
} else if exit_code.is_some() {
self.overlay_ui.command_history.update_exit_code_if_unknown(
&cmd,
exit_code,
duration_ms,
);
}
}
}
(sb_len, term.get_title(), shell_events)
} else {
(
cached_scrollback_len,
cached_terminal_title.to_string(),
Vec::new(),
)
}
}
/// Collect scrollback marks from the terminal and append trigger-generated marks.
///
/// Returns `(scrollback_marks, override_show_scrollbar)`. When marks are
/// present, `override_show_scrollbar` is `true`, which forces the scrollbar
/// visible regardless of the configured threshold.
pub(super) fn collect_scrollback_marks(
&self,
terminal: &Arc<tokio::sync::RwLock<TerminalManager>>,
) -> (Vec<ScrollbackMark>, bool) {
let need_marks =
self.config.scrollbar_command_marks || self.config.command_separator_enabled;
let mut scrollback_marks: Vec<ScrollbackMark> = if need_marks {
if let Ok(term) = terminal.try_write() {
term.scrollback_marks()
} else {
Vec::new()
}
} else {
Vec::new()
};
// Append trigger-generated marks
self.with_active_tab(|tab| {
scrollback_marks.extend(tab.scripting.trigger_marks.iter().cloned())
});
let override_show = !scrollback_marks.is_empty();
(scrollback_marks, override_show)
}
/// Update the OS window title when the terminal has set one via OSC sequences.
///
/// Only fires when `allow_title_change` is configured, no URL tooltip is
/// being shown, and the terminal-provided title has changed since last frame.
pub(super) fn update_window_title_if_changed(
&mut self,
terminal_title: &str,
cached_terminal_title: &str,
hovered_url: &Option<String>,
) {
if self.config.allow_title_change
&& hovered_url.is_none()
&& terminal_title != cached_terminal_title
{
let owned = terminal_title.to_string();
self.with_active_tab_mut(|tab| tab.active_cache_mut().terminal_title = owned.clone());
if let Some(window) = &self.window {
if terminal_title.is_empty() {
window.set_title(&self.format_title(&self.config.window_title));
} else {
window.set_title(&self.format_title(terminal_title));
}
}
}
}
/// Run URL detection and search index updates for the current frame.
///
/// Re-detects URLs only on cache misses; search match positions are refreshed
/// every frame since cells may be regenerated even on cache hits.
///
/// Cell modifications (underlines, highlight colors) are NOT applied here —
/// they are applied to pane cells in `gpu_submit.rs` after
/// `gather_pane_render_data()`, which is the only path visible to the renderer.
///
/// Returns the elapsed time spent on URL detection (zero on cache hit).
pub(super) fn apply_url_and_search_highlights(
&mut self,
_cells: &mut [Cell],
_renderer_size: &PhysicalSize<u32>,
_grid_cols: usize,
_scroll_offset: usize,
_scrollback_len: usize,
visible_lines: usize,
) -> std::time::Duration {
let url_detect_start = Instant::now();
let debug_url_detect_time = if !self.debug.cache_hit {
self.detect_urls();
url_detect_start.elapsed()
} else {
std::time::Duration::ZERO
};
if self.overlay_ui.search_ui.visible {
if let Some(tab) = self.tab_manager.active_tab()
&& let Ok(term) = tab.terminal.try_write()
{
let lines_iter =
crate::app::window_state::search_highlight::get_all_searchable_lines(
&term,
visible_lines,
);
self.overlay_ui.search_ui.update_search(lines_iter);
}
// Force GPU cell update when search is visible: highlights are applied to
// pane cells every frame, but renderer.update_cells() is skipped on cache
// hits, causing the highlighted cells to never reach the GPU.
self.debug.cache_hit = false;
// Also mark renderer dirty to ensure a full render pass runs (not just the
// egui fast-path), so the updated cell buffer is actually drawn to screen.
if let Some(renderer) = &mut self.renderer {
renderer.mark_dirty();
}
}
debug_url_detect_time
}
/// Sync the prettifier pipeline state for the active tab.
///
/// Handles alt-screen transitions, keeps cell dimensions up-to-date, and
/// triggers the debounce check. Called once per frame before the main
/// prettifier feed.
pub(super) fn sync_prettifier_state(&mut self, is_alt_screen: bool) {
let prettifier_cell_dims = self
.renderer
.as_ref()
.map(|r| (r.cell_width(), r.cell_height()));
let renderer_cols = self.renderer.as_ref().map(|r| r.grid_size().0);
if let Some(tab) = self.tab_manager.active_tab_mut() {
if is_alt_screen != tab.was_alt_screen {
if let Some(ref mut pipeline) = tab.prettifier {
pipeline.on_alt_screen_change(is_alt_screen);
}
tab.was_alt_screen = is_alt_screen;
}
if let Some(ref mut pipeline) = tab.prettifier {
if let Some((cw, ch)) = prettifier_cell_dims {
pipeline.update_cell_dims(cw, ch);
}
if let Some(cols) = renderer_cols {
pipeline.set_terminal_width(cols);
}
pipeline.check_debounce();
}
}
}
/// Flush the regenerated cell snapshot into the active tab's render cache.
///
/// Skipped when `cache_hit` is set (no new content) to avoid redundant
/// `Arc` allocations every frame.
pub(super) fn flush_cell_cache(
&mut self,
cells: &[Cell],
current_cursor_pos: Option<(usize, usize)>,
) {
if self.debug.cache_hit {
return;
}
if let Some(tab) = self.tab_manager.active_tab_mut() {
let new_gen = if let Ok(term) = tab.terminal.try_write() {
Some(term.update_generation())
} else {
None
};
if let Some(new_gen) = new_gen {
let current_scroll_offset = tab.active_scroll_state().offset;
let current_selection = tab.selection_mouse().selection;
tab.active_cache_mut().cells = Some(Arc::new(cells.to_vec()));
tab.active_cache_mut().generation = new_gen;
tab.active_cache_mut().scroll_offset = current_scroll_offset;
tab.active_cache_mut().cursor_pos = current_cursor_pos;
tab.active_cache_mut().selection = current_selection;
}
}
}
}