par-term 0.38.0

Cross-platform GPU-accelerated terminal emulator with inline graphics support (Sixel, iTerm2, Kitty)
Documentation
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
//! Keybinding action dispatch for WindowState.
//!
//! - `execute_keybinding_action`: dispatches named actions (toggle shaders,
//!   new tab, copy, paste, etc.) through the [`ACTION_HANDLERS`] table.
//!
//! Visual notification helpers (`show_toast`, `show_pane_indices`) and shader
//! toggle helpers (`toggle_background_shader`, `toggle_cursor_shader`) live in
//! `keybinding_helpers`.
//!
//! Display/navigation actions (font size, cursor style, tab index switching,
//! throughput mode, etc.) live in `keybinding_display_actions`.
//!
//! Snippet and custom action execution live in `snippet_actions`.
//!
//! # Why a table and not a `match`
//!
//! Actions arrive as `&str` (from `config.yaml` keybindings, from generated
//! `snippet:`/`action:` names), so a `match` never had an exhaustiveness check
//! to lose. What a `match` on string literals *did* give was
//! `unreachable_patterns`: a duplicated arm was a compile-time warning. A
//! linear-scan table makes a duplicate key silently first-wins instead, so
//! `dispatch_tests::action_table_has_no_duplicate_keys` replaces that
//! guarantee, and `dispatch_tests` additionally asserts the table's key set
//! against a frozen inventory and against the display table's keys — neither
//! of which the `match` form could express at all.

use crate::app::window_state::WindowState;

/// Handler for one named keybinding action.
///
/// Every entry in [`ACTION_HANDLERS`] returns `true`; the `bool` exists so the
/// signature matches `execute_keybinding_action`'s contract, where a name that
/// no handler claims returns `false`.
pub(super) type ActionHandler = fn(&mut WindowState) -> bool;

/// Exact-match dispatch table for named keybinding actions.
///
/// Entry order is irrelevant to behavior — every key is a distinct literal and
/// lookup is by equality — but keys must stay unique, which
/// `dispatch_tests::action_table_has_no_duplicate_keys` enforces.
///
/// Names not found here fall through to `execute_display_keybinding_action`
/// and then to the `snippet:` / `action:` / `restore_arrangement:` prefix
/// forms; see `execute_keybinding_action`.
pub(super) static ACTION_HANDLERS: &[(&str, ActionHandler)] = &[
    ("toggle_background_shader", |s: &mut WindowState| {
        s.toggle_background_shader();
        true
    }),
    ("toggle_cursor_shader", |s: &mut WindowState| {
        s.toggle_cursor_shader();
        true
    }),
    ("cycle_background_shader", |s: &mut WindowState| {
        s.cycle_background_shader();
        true
    }),
    ("toggle_shader_animation", |s: &mut WindowState| {
        s.toggle_shader_animation();
        true
    }),
    ("toggle_shader_readability_mode", |s: &mut WindowState| {
        s.toggle_shader_readability_mode();
        true
    }),
    ("reload_config", |s: &mut WindowState| {
        s.reload_config();
        true
    }),
    ("open_settings", |s: &mut WindowState| {
        s.overlay_state.open_settings_window_requested = true;
        s.request_redraw();
        log::info!("Settings window requested via keybinding");
        true
    }),
    ("toggle_fullscreen", toggle_fullscreen),
    ("maximize_vertically", maximize_vertically),
    ("toggle_help", |s: &mut WindowState| {
        s.overlay_ui.help_ui.toggle();
        s.request_redraw();
        log::info!(
            "Help UI toggled via keybinding: {}",
            if s.overlay_ui.help_ui.visible {
                "visible"
            } else {
                "hidden"
            }
        );
        true
    }),
    ("toggle_fps_overlay", |s: &mut WindowState| {
        s.debug.show_fps_overlay = !s.debug.show_fps_overlay;
        s.request_redraw();
        log::info!(
            "FPS overlay toggled via keybinding: {}",
            if s.debug.show_fps_overlay {
                "visible"
            } else {
                "hidden"
            }
        );
        true
    }),
    ("toggle_search", toggle_search),
    ("toggle_ai_inspector", toggle_ai_inspector),
    ("new_tab", |s: &mut WindowState| {
        s.new_tab_or_show_profiles();
        true
    }),
    ("close_tab", |s: &mut WindowState| {
        if s.has_multiple_tabs() {
            s.close_current_tab();
            log::info!("Tab closed via keybinding");
        }
        true
    }),
    ("duplicate_tab", |s: &mut WindowState| {
        s.duplicate_tab();
        log::info!("Tab duplicated via keybinding");
        true
    }),
    ("move_tab_to_new_window", move_tab_to_new_window),
    ("next_tab", |s: &mut WindowState| {
        s.next_tab();
        log::debug!("Switched to next tab via keybinding");
        true
    }),
    ("prev_tab", |s: &mut WindowState| {
        s.prev_tab();
        log::debug!("Switched to previous tab via keybinding");
        true
    }),
    ("paste_special", paste_special),
    ("toggle_session_logging", toggle_session_logging),
    ("split_horizontal", |s: &mut WindowState| {
        s.split_pane_horizontal();
        true
    }),
    ("split_vertical", |s: &mut WindowState| {
        s.split_pane_vertical();
        true
    }),
    ("close_pane", |s: &mut WindowState| {
        s.close_focused_pane();
        true
    }),
    ("navigate_pane_left", |s: &mut WindowState| {
        s.navigate_pane(crate::pane::NavigationDirection::Left);
        true
    }),
    ("navigate_pane_right", |s: &mut WindowState| {
        s.navigate_pane(crate::pane::NavigationDirection::Right);
        true
    }),
    ("navigate_pane_up", |s: &mut WindowState| {
        s.navigate_pane(crate::pane::NavigationDirection::Up);
        true
    }),
    ("navigate_pane_down", |s: &mut WindowState| {
        s.navigate_pane(crate::pane::NavigationDirection::Down);
        true
    }),
    ("resize_pane_left", |s: &mut WindowState| {
        s.resize_pane(crate::pane::NavigationDirection::Left);
        true
    }),
    ("resize_pane_right", |s: &mut WindowState| {
        s.resize_pane(crate::pane::NavigationDirection::Right);
        true
    }),
    ("resize_pane_up", |s: &mut WindowState| {
        s.resize_pane(crate::pane::NavigationDirection::Up);
        true
    }),
    ("resize_pane_down", |s: &mut WindowState| {
        s.resize_pane(crate::pane::NavigationDirection::Down);
        true
    }),
    ("toggle_tmux_session_picker", |s: &mut WindowState| {
        s.overlay_ui.tmux_session_picker_ui.toggle();
        s.request_redraw();
        log::info!(
            "tmux session picker toggled via keybinding: {}",
            if s.overlay_ui.tmux_session_picker_ui.visible {
                "visible"
            } else {
                "hidden"
            }
        );
        true
    }),
    // Deliberate alias pair: `toggle_copy_mode` and `enter_copy_mode` are two
    // public action names for one behavior (the handler toggles either way).
    // Do not "deduplicate" these into one entry — both names are documented and
    // bindable, and dropping either silently breaks existing user configs.
    ("toggle_copy_mode", toggle_copy_mode),
    ("enter_copy_mode", toggle_copy_mode),
    ("toggle_broadcast_input", |s: &mut WindowState| {
        s.broadcast_input = !s.broadcast_input;
        let message = if s.broadcast_input {
            "Broadcast Input: ON"
        } else {
            "Broadcast Input: OFF"
        };
        s.show_toast(message);
        log::info!(
            "Broadcast input mode {}",
            if s.broadcast_input {
                "enabled"
            } else {
                "disabled"
            }
        );
        true
    }),
    ("promote_pane_to_tab", |s: &mut WindowState| {
        s.promote_pane_to_tab();
        true
    }),
    ("demote_tab_to_pane", |s: &mut WindowState| {
        s.start_demote_tab();
        true
    }),
    ("toggle_profile_drawer", |s: &mut WindowState| {
        s.toggle_profile_drawer();
        log::info!(
            "Profile drawer toggled via keybinding: {}",
            if s.overlay_ui.profile_drawer_ui.expanded {
                "expanded"
            } else {
                "collapsed"
            }
        );
        true
    }),
    ("toggle_clipboard_history", |s: &mut WindowState| {
        s.toggle_clipboard_history();
        log::info!(
            "Clipboard history toggled via keybinding: {}",
            if s.overlay_ui.clipboard_history_ui.visible {
                "visible"
            } else {
                "hidden"
            }
        );
        true
    }),
    ("toggle_command_history", |s: &mut WindowState| {
        s.toggle_command_history();
        log::info!(
            "Command history toggled via keybinding: {}",
            if s.overlay_ui.command_history_ui.visible {
                "visible"
            } else {
                "hidden"
            }
        );
        true
    }),
    ("clear_scrollback", clear_scrollback),
    // Menu-parity actions. These need the `WindowManager` — the event loop and
    // every window — not just a `WindowState`, so they go through the same queue
    // the in-app menu uses. Routing both through it means a keybinding and its
    // menu item cannot drift apart.
    ("new_window", |_s: &mut WindowState| {
        crate::menu::dispatch(crate::menu::MenuAction::NewWindow);
        true
    }),
    ("close_window", |_s: &mut WindowState| {
        crate::menu::dispatch(crate::menu::MenuAction::CloseWindow);
        true
    }),
    ("quit", |_s: &mut WindowState| {
        crate::menu::dispatch(crate::menu::MenuAction::Quit);
        true
    }),
    ("select_all", |_s: &mut WindowState| {
        crate::menu::dispatch(crate::menu::MenuAction::SelectAll);
        true
    }),
    ("toggle_menu", |s: &mut WindowState| {
        crate::menu::request_toggle();
        s.request_redraw();
        true
    }),
];

fn toggle_fullscreen(s: &mut WindowState) -> bool {
    if let Some(window) = &s.window {
        s.is_fullscreen = !s.is_fullscreen;
        if s.is_fullscreen {
            window.set_fullscreen(Some(winit::window::Fullscreen::Borderless(None)));
            log::info!("Entering fullscreen mode via keybinding");
        } else {
            window.set_fullscreen(None);
            log::info!("Exiting fullscreen mode via keybinding");
        }
    }
    true
}

fn maximize_vertically(s: &mut WindowState) -> bool {
    if let Some(window) = &s.window {
        // Get current monitor to determine screen height
        if let Some(monitor) = window.current_monitor() {
            let monitor_pos = monitor.position();
            let monitor_size = monitor.size();
            let window_pos = window.outer_position().unwrap_or_default();
            let window_size = window.outer_size();

            // Set window to span full height while keeping current X position and width
            window.set_outer_position(winit::dpi::PhysicalPosition::new(
                window_pos.x,
                monitor_pos.y,
            ));
            let _ = window.request_inner_size(winit::dpi::PhysicalSize::new(
                window_size.width,
                monitor_size.height,
            ));
            log::info!("Window maximized vertically via keybinding");
        }
    }
    true
}

fn toggle_search(s: &mut WindowState) -> bool {
    s.overlay_ui.search_ui.toggle();
    if s.overlay_ui.search_ui.visible {
        s.overlay_ui.search_ui.init_from_config(
            s.config.load().search.search_case_sensitive,
            s.config.load().search.search_regex,
        );
    }
    s.focus_state.needs_redraw = true;
    s.request_redraw();
    log::info!(
        "Search UI toggled via keybinding: {}",
        if s.overlay_ui.search_ui.visible {
            "visible"
        } else {
            "hidden"
        }
    );
    true
}

fn toggle_ai_inspector(s: &mut WindowState) -> bool {
    if s.config.load().ai_inspector.ai_inspector_enabled {
        let just_opened = s.overlay_ui.ai_inspector.toggle();
        s.sync_ai_inspector_width();
        if just_opened {
            if s.config.load().ai_inspector.ai_inspector_input_history_mode
                == par_term_config::AssistantInputHistoryMode::Persist
            {
                s.overlay_ui.ai_inspector.merge_persisted_input_history();
            }
            s.try_auto_connect_agent();
        }
        s.request_redraw();
    }
    true
}

fn move_tab_to_new_window(s: &mut WindowState) -> bool {
    if let Some(tab_id) = s.tab_manager.active_tab_id()
        && !s.is_gateway_active()
        && s.has_multiple_tabs()
    {
        s.overlay_ui.pending_move_tab_request = Some(crate::app::window_manager::MoveTabRequest {
            tab_id,
            destination: crate::app::window_manager::MoveDestination::NewWindow,
        });
        log::info!("Move Tab to New Window triggered via keybinding");
    }
    true
}

fn paste_special(s: &mut WindowState) -> bool {
    // Get clipboard content and open paste special UI
    if let Some(text) = s.input_handler.paste_from_clipboard() {
        s.overlay_ui.paste_special_ui.open(text);
        s.focus_state.needs_redraw = true;
        s.request_redraw();
        log::info!("Paste special UI opened");
    } else {
        log::debug!("Paste special: no clipboard content");
    }
    true
}

fn toggle_session_logging(s: &mut WindowState) -> bool {
    if let Some(tab) = s.tab_manager.active_tab_mut() {
        match tab.toggle_session_logging(&s.config.load()) {
            Ok(is_active) => {
                let message = if is_active {
                    "⏺ Recording Started"
                } else {
                    "⏹ Recording Stopped"
                };
                log::info!(
                    "Session logging toggled: {}",
                    if is_active { "started" } else { "stopped" }
                );
                // Show toast after releasing tab borrow
                s.show_toast(message);
            }
            Err(e) => {
                log::error!("Failed to toggle session logging: {}", e);
                s.show_toast(format!("Recording Error: {}", e));
            }
        }
    }
    true
}

fn toggle_copy_mode(s: &mut WindowState) -> bool {
    if s.is_copy_mode_active() {
        s.exit_copy_mode();
    } else {
        s.enter_copy_mode();
    }
    true
}

fn clear_scrollback(s: &mut WindowState) -> bool {
    let cleared = if let Some(tab) = s.tab_manager.active_tab_mut() {
        // try_lock: intentional — keybinding action in sync event loop.
        // On miss: scrollback not cleared this invocation. User can retry.
        let did_clear = if let Ok(mut term) = tab.terminal.try_write() {
            term.clear_scrollback();
            term.clear_scrollback_metadata();
            true
        } else {
            false
        };
        if did_clear {
            tab.active_cache_mut().scrollback_len = 0;
            tab.scripting.trigger_marks.clear();
            let tab_terminal = std::sync::Arc::clone(&tab.terminal);
            if let Some(pm) = tab.pane_manager_mut() {
                for pane in pm.all_panes_mut() {
                    if std::sync::Arc::ptr_eq(&pane.terminal, &tab_terminal) {
                        pane.cache.invalidate_pane_cells();
                    }
                }
            }
        }
        did_clear
    } else {
        false
    };
    if cleared {
        s.set_scroll_target(0);
        log::info!("Cleared scrollback buffer via keybinding");
    }
    true
}

impl WindowState {
    /// Execute a keybinding action by name.
    ///
    /// Returns true if the action was handled, false if unknown.
    pub(crate) fn execute_keybinding_action(&mut self, action: &str) -> bool {
        if let Some((_, handler)) = ACTION_HANDLERS.iter().find(|(name, _)| *name == action) {
            return handler(self);
        }

        // Miss path — order is load-bearing and must not be rearranged.
        // Delegate display/navigation actions to the companion handler
        if let Some(result) = self.execute_display_keybinding_action(action) {
            return result;
        }
        // Check for snippet or action keybindings
        if let Some(snippet_id) = action.strip_prefix("snippet:") {
            self.execute_snippet(snippet_id)
        } else if let Some(action_id) = action.strip_prefix("action:") {
            self.execute_custom_action(action_id)
        } else if let Some(arrangement_name) = action.strip_prefix("restore_arrangement:") {
            // Restore arrangement by name - handled by WindowManager
            self.overlay_state.pending_arrangement_restore = Some(arrangement_name.to_string());
            self.request_redraw();
            log::info!(
                "Arrangement restore requested via keybinding: {}",
                arrangement_name
            );
            true
        } else {
            log::warn!("Unknown keybinding action: {}", action);
            false
        }
    }
}