par-term 0.49.0

Cross-platform GPU-accelerated terminal emulator with inline graphics support (Sixel, iTerm2, Kitty)
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
//! Tab and window navigation actions (UX.md P4: A10–A14, A21).
//!
//! Split out of `keybinding_actions` to keep that table under the line
//! gate. Every function here is an `ActionHandler`: it returns `true`
//! because the name is claimed even when the action has nothing to do
//! (a single tab, no previous tab).

use crate::app::window_state::WindowState;
use crate::tab::TabId;

/// A10 `last_tab`: toggle to the previously active tab (tmux
/// `last-window`). A no-op with a toast when there is none.
pub(super) fn last_tab(s: &mut WindowState) -> bool {
    match s.tab_manager.previous_tab_id() {
        Some(id) => s.switch_to_tab_id(id),
        None => s.show_toast("No previous tab"),
    }
    true
}

/// A11 `go_to_last_tab`: the rightmost visible tab.
pub(super) fn go_to_last_tab(s: &mut WindowState) -> bool {
    if let Some(id) = s.tab_manager.last_visible_tab_id() {
        s.switch_to_tab_id(id);
    }
    true
}

/// A12 `rename_tab`: open the inline rename field on the active tab. The
/// submit goes through `TabBarAction::RenameTab`, so a par-mux tab's
/// rename still reaches the daemon (`rename-window`).
pub(super) fn rename_tab(s: &mut WindowState) -> bool {
    let Some((id, title)) = s.tab_manager.active_tab().map(|t| (t.id, t.title.clone())) else {
        return true;
    };
    let frame = s
        .egui
        .ctx
        .as_ref()
        .map(|ctx| ctx.cumulative_frame_nr())
        .unwrap_or(0);
    let pos = s.tab_bar_ui.rename_anchor(id);
    s.tab_bar_ui.begin_rename(id, &title, pos, frame);
    s.focus_state.needs_redraw = true;
    s.request_redraw();
    true
}

/// A13 `next_window` / `prev_window` and `switch_to_window_1..9`: these
/// need every window, so they queue on the menu bridge the `WindowManager`
/// drains (the same route `new_window` takes).
pub(super) fn next_window(_s: &mut WindowState) -> bool {
    crate::menu::dispatch(crate::menu::MenuAction::CycleWindow(1));
    true
}

pub(super) fn prev_window(_s: &mut WindowState) -> bool {
    crate::menu::dispatch(crate::menu::MenuAction::CycleWindow(-1));
    true
}

/// A13 `switch_to_window_N`: the window holding number N (iTerm2's
/// Cmd+Opt+N, unbound here; see the actions table).
fn focus_window_number(n: usize) -> bool {
    crate::menu::dispatch(crate::menu::MenuAction::FocusWindowNumber(n));
    true
}

pub(super) fn switch_to_window_1(_s: &mut WindowState) -> bool {
    focus_window_number(1)
}
pub(super) fn switch_to_window_2(_s: &mut WindowState) -> bool {
    focus_window_number(2)
}
pub(super) fn switch_to_window_3(_s: &mut WindowState) -> bool {
    focus_window_number(3)
}
pub(super) fn switch_to_window_4(_s: &mut WindowState) -> bool {
    focus_window_number(4)
}
pub(super) fn switch_to_window_5(_s: &mut WindowState) -> bool {
    focus_window_number(5)
}
pub(super) fn switch_to_window_6(_s: &mut WindowState) -> bool {
    focus_window_number(6)
}
pub(super) fn switch_to_window_7(_s: &mut WindowState) -> bool {
    focus_window_number(7)
}
pub(super) fn switch_to_window_8(_s: &mut WindowState) -> bool {
    focus_window_number(8)
}
pub(super) fn switch_to_window_9(_s: &mut WindowState) -> bool {
    focus_window_number(9)
}

/// A14 `close_window` (real): close the whole window with all its tabs,
/// through the same confirmation the title-bar close runs (D6).
pub(super) fn close_window(_s: &mut WindowState) -> bool {
    crate::menu::dispatch(crate::menu::MenuAction::CloseWholeWindow);
    true
}

/// A14 `close_tab_or_window`: the old smart close under its new id — the
/// active tab when the window holds several, else the window.
pub(super) fn close_tab_or_window(_s: &mut WindowState) -> bool {
    crate::menu::dispatch(crate::menu::MenuAction::CloseWindow);
    true
}

/// A21 `close_other_tabs`: close every visible tab except the active one.
pub(super) fn close_other_tabs(s: &mut WindowState) -> bool {
    let Some(keep) = s.tab_manager.active_tab_id() else {
        return true;
    };
    let targets: Vec<TabId> = s
        .tab_manager
        .visible_tabs()
        .iter()
        .map(|t| t.id)
        .filter(|id| *id != keep)
        .collect();
    s.close_tabs_keeping(&targets, keep);
    true
}

/// A21 `close_tabs_to_right`: close every visible tab right of the active
/// one.
pub(super) fn close_tabs_to_right(s: &mut WindowState) -> bool {
    let Some(keep) = s.tab_manager.active_tab_id() else {
        return true;
    };
    let visible: Vec<TabId> = s.tab_manager.visible_tabs().iter().map(|t| t.id).collect();
    let targets: Vec<TabId> = visible
        .iter()
        .skip_while(|id| **id != keep)
        .skip(1)
        .copied()
        .collect();
    s.close_tabs_keeping(&targets, keep);
    true
}

/// A18 `move_tab_to_window_picker`: open the palette on the Move Tab
/// rows — one per other window plus New Window (the keyboard version of
/// the tab context submenu).
pub(super) fn move_tab_to_window_picker(s: &mut WindowState) -> bool {
    if let Some(id) = s.tab_manager.active_tab_id()
        && s.mux_window_for_tab(id).is_some()
    {
        s.show_toast(
            "par-mux: tabs attached to a par-mux session can't move between windows — \
             detach first",
        );
        return true;
    }
    let rows = s.move_tab_palette_rows();
    s.overlay_ui
        .command_palette
        .open(rows, &s.keybinding_registry);
    s.overlay_ui.command_palette.set_query("Move Tab to");
    s.focus_state.needs_redraw = true;
    s.request_redraw();
    true
}

/// `move_tab_to_window:<n>`: the runtime row id for "move the active tab
/// to window N".
pub(crate) fn move_tab_row_id(window_number: usize) -> String {
    format!("move_tab_to_window:{window_number}")
}

impl WindowState {
    /// A18 palette rows: one per other window, labelled with its number and
    /// active tab (`move_tab_candidates`, refreshed every frame by the
    /// manager). Empty for a par-mux tab, which cannot move.
    pub(crate) fn move_tab_palette_rows(
        &self,
    ) -> Vec<crate::command_palette::catalog::PaletteEntry> {
        let movable = self
            .tab_manager
            .active_tab_id()
            .is_some_and(|id| self.mux_window_for_tab(id).is_none());
        if !movable {
            return Vec::new();
        }
        self.overlay_ui
            .move_tab_candidates
            .iter()
            .filter_map(|(_, label)| {
                let number: usize = label
                    .strip_prefix("Window ")?
                    .split(|c: char| !c.is_ascii_digit())
                    .next()?
                    .parse()
                    .ok()?;
                Some(crate::command_palette::catalog::PaletteEntry {
                    action_id: move_tab_row_id(number),
                    label: format!("Move Tab to {label}"),
                    chord: None,
                    priority: 0,
                })
            })
            .collect()
    }

    /// Dispatch a `move_tab_to_window:<n>` row: queue the move of the
    /// active tab to the window holding number N.
    pub(crate) fn dispatch_move_tab_to_window(&mut self, action: &str) -> bool {
        let Some(number) = action
            .strip_prefix("move_tab_to_window:")
            .and_then(|n| n.parse::<usize>().ok())
        else {
            return false;
        };
        let Some(tab_id) = self.tab_manager.active_tab_id() else {
            return false;
        };
        if self.mux_window_for_tab(tab_id).is_some() {
            self.show_toast(
                "par-mux: tabs attached to a par-mux session can't move between windows — \
                 detach first",
            );
            return true;
        }
        let dest = self
            .overlay_ui
            .move_tab_candidates
            .iter()
            .find(|(_, label)| {
                label
                    .strip_prefix("Window ")
                    .and_then(|rest| rest.split(|c: char| !c.is_ascii_digit()).next())
                    .and_then(|n| n.parse::<usize>().ok())
                    == Some(number)
            })
            .map(|(id, _)| *id);
        match dest {
            Some(dest) => {
                self.overlay_ui.pending_move_tab_request =
                    Some(crate::app::window_manager::MoveTabRequest {
                        tab_id,
                        destination: crate::app::window_manager::MoveDestination::ExistingWindow(
                            dest,
                        ),
                    });
                true
            }
            None => {
                self.show_toast(format!("No window {number}"));
                false
            }
        }
    }

    /// Switch to `id` through the user tab-switch tail (copy mode exit,
    /// render invalidation, `select-window` for an attached tab).
    pub(crate) fn switch_to_tab_id(&mut self, id: TabId) {
        self.copy_mode.exit();
        self.tab_manager.switch_to(id);
        self.clear_and_invalidate();
        self.after_user_tab_switch();
    }

    /// Close `targets` one by one through the ordinary tab close — each
    /// keeps its running-job confirmation, par-mux `kill-window`, and undo
    /// entry — then return to `keep`. A close that stops for a
    /// confirmation dialog ends the batch there: one dialog at a time, and
    /// the tabs after it stay open.
    pub(crate) fn close_tabs_keeping(&mut self, targets: &[TabId], keep: TabId) {
        let mut closed = 0usize;
        for &id in targets {
            if self.tab_manager.get_tab(id).is_none() {
                continue;
            }
            self.tab_manager.switch_to(id);
            let before = self.tab_manager.tab_count();
            self.close_current_tab();
            if self.overlay_ui.close_confirmation_ui.is_visible()
                || self.overlay_ui.mux_last_tab_ui.is_visible()
            {
                break;
            }
            if self.tab_manager.tab_count() < before || self.mux_window_for_tab(id).is_some() {
                closed += 1;
            }
        }
        if self.tab_manager.get_tab(keep).is_some()
            && !self.overlay_ui.close_confirmation_ui.is_visible()
        {
            self.tab_manager.switch_to(keep);
        }
        self.clear_and_invalidate();
        if closed > 1 {
            // An action toast (UX.md OV7): the button reopens the last
            // closed tab; the hint names the live chord, never a raw
            // config string.
            let hint = self
                .live_chord_hint("reopen_closed_tab")
                .map(|chord| format!(" ({chord} reopens)"))
                .unwrap_or_default();
            self.post_toast(
                crate::app::overlay::toast::ToastKind::Info,
                format!("Closed {closed} tabs{hint}"),
                Some(crate::app::overlay::toast::ToastAction {
                    label: "Reopen".to_string(),
                    action_id: "reopen_closed_tab".to_string(),
                }),
            );
        }
        self.focus_state.needs_redraw = true;
        self.request_redraw();
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn window_with_tabs(n: usize) -> WindowState {
        let runtime = std::sync::Arc::new(
            tokio::runtime::Builder::new_current_thread()
                .enable_all()
                .build()
                .expect("runtime"),
        );
        let mut s = WindowState::new(crate::config::Config::default(), runtime);
        for i in 0..n {
            s.tab_manager
                .push_tab_for_test(crate::tab::Tab::new_stub(i as TabId + 1, i + 1));
        }
        assert_eq!(s.tab_manager.tab_count(), n);
        s
    }

    fn active_index(s: &WindowState) -> usize {
        s.tab_manager.active_tab_index().expect("active tab")
    }

    /// A10: last_tab toggles between the two most recent tabs, like
    /// tmux `last-window`.
    #[test]
    fn last_tab_toggles_between_the_two_most_recent_tabs() {
        let mut s = window_with_tabs(4);
        s.switch_to_tab_index(1);
        s.switch_to_tab_index(3);
        assert!(last_tab(&mut s));
        assert_eq!(active_index(&s), 0, "back to tab 1");
        assert!(last_tab(&mut s));
        assert_eq!(active_index(&s), 2, "and forward to tab 3 again");
    }

    /// A10: a previous tab that has since closed is not a target.
    #[test]
    fn last_tab_ignores_a_closed_previous_tab() {
        let mut s = window_with_tabs(3);
        s.switch_to_tab_index(1);
        let first = s.tab_manager.active_tab_id().unwrap();
        s.switch_to_tab_index(3);
        s.tab_manager.close_tab(first);
        assert_eq!(s.tab_manager.previous_tab_id(), None);
        let before = s.tab_manager.active_tab_id();
        last_tab(&mut s);
        assert_eq!(s.tab_manager.active_tab_id(), before);
    }

    /// A11: go_to_last_tab lands on the rightmost visible tab.
    #[test]
    fn go_to_last_tab_lands_on_the_rightmost_tab() {
        let mut s = window_with_tabs(5);
        s.switch_to_tab_index(2);
        go_to_last_tab(&mut s);
        assert_eq!(active_index(&s), 4);
    }

    /// A11: a hidden rightmost tab is skipped.
    #[test]
    fn go_to_last_tab_skips_a_hidden_tab() {
        let mut s = window_with_tabs(3);
        let last = s.tab_manager.tabs().last().unwrap().id;
        s.tab_manager.get_tab_mut(last).unwrap().is_hidden = true;
        s.switch_to_tab_index(1);
        go_to_last_tab(&mut s);
        assert_eq!(active_index(&s), 1);
    }

    /// A12: rename_tab opens the inline rename on the active tab.
    #[test]
    fn rename_tab_opens_the_inline_rename_on_the_active_tab() {
        let mut s = window_with_tabs(2);
        s.switch_to_tab_index(2);
        assert!(!s.tab_bar_ui.is_renaming());
        rename_tab(&mut s);
        assert!(s.tab_bar_ui.is_renaming());
        assert_eq!(
            s.tab_bar_ui.context_menu_tab_id(),
            s.tab_manager.active_tab_id()
        );
    }

    /// A18: one Move Tab row per other window; a row queues the move of
    /// the active tab to that window.
    #[test]
    fn move_tab_rows_list_other_windows_and_queue_the_move() {
        let mut s = window_with_tabs(2);
        let other = winit::window::WindowId::from(42u64);
        s.overlay_ui.move_tab_candidates = vec![(other, "Window 3 - logs".to_string())];
        let rows = s.move_tab_palette_rows();
        assert_eq!(rows.len(), 1);
        assert_eq!(rows[0].action_id, "move_tab_to_window:3");
        assert_eq!(rows[0].label, "Move Tab to Window 3 - logs");

        assert!(s.execute_keybinding_action("move_tab_to_window:3"));
        let req = s.overlay_ui.pending_move_tab_request.expect("queued");
        assert_eq!(Some(req.tab_id), s.tab_manager.active_tab_id());
        assert!(matches!(
            req.destination,
            crate::app::window_manager::MoveDestination::ExistingWindow(id) if id == other
        ));
        assert!(
            !s.execute_keybinding_action("move_tab_to_window:9"),
            "no window 9"
        );
    }

    /// A21: close_other_tabs keeps only the active tab.
    #[test]
    fn close_other_tabs_keeps_only_the_active_tab() {
        let mut s = window_with_tabs(4);
        s.switch_to_tab_index(2);
        let keep = s.tab_manager.active_tab_id().unwrap();
        close_other_tabs(&mut s);
        assert_eq!(s.tab_manager.tab_count(), 1);
        assert_eq!(s.tab_manager.active_tab_id(), Some(keep));
    }

    /// UX.md OV7: closing a tab posts an Undo toast whose hint is the
    /// live registry chord, never the raw config string (`CmdOrCtrl+Z`),
    /// and whose button runs reopen_closed_tab.
    #[test]
    fn the_tab_close_toast_names_the_live_chord_and_offers_undo() {
        let mut s = window_with_tabs(2);
        s.keybinding_registry =
            par_term_keybindings::KeybindingRegistry::from_config(&[par_term_config::KeyBinding {
                key: "F9".to_string(),
                action: "reopen_closed_tab".to_string(),
            }]);
        s.close_current_tab_immediately();
        let toast = s
            .overlay_state
            .toasts
            .newest()
            .expect("close posts a toast");
        assert!(
            toast.message.starts_with("Tab closed") && toast.message.contains("(F9)"),
            "live chord in the hint: {:?}",
            toast.message
        );
        assert!(
            !toast.message.contains("CmdOrCtrl"),
            "no raw config string: {:?}",
            toast.message
        );
        assert_eq!(
            toast.action,
            Some(crate::app::overlay::toast::ToastAction {
                label: "Undo".to_string(),
                action_id: "reopen_closed_tab".to_string(),
            })
        );
    }

    /// With reopen_closed_tab unbound the hint is dropped rather than
    /// naming a key that does nothing; the Undo button still works.
    #[test]
    fn the_tab_close_toast_drops_the_hint_when_undo_is_unbound() {
        let mut s = window_with_tabs(2);
        s.keybinding_registry = par_term_keybindings::KeybindingRegistry::new();
        s.close_current_tab_immediately();
        let toast = s
            .overlay_state
            .toasts
            .newest()
            .expect("close posts a toast");
        let timeout = s.config.load().session_restore.session_undo_timeout_secs;
        assert_eq!(
            toast.message,
            format!("Tab closed — undo within {timeout}s"),
            "no chord hint (and no 'Press keybinding') when unbound"
        );
        assert!(toast.action.is_some(), "the Undo button stays");
    }

    /// A21: close_tabs_to_right keeps the active tab and everything left
    /// of it.
    #[test]
    fn close_tabs_to_right_keeps_the_left_side() {
        let mut s = window_with_tabs(5);
        s.switch_to_tab_index(2);
        let keep = s.tab_manager.active_tab_id().unwrap();
        close_tabs_to_right(&mut s);
        assert_eq!(s.tab_manager.tab_count(), 2);
        assert_eq!(s.tab_manager.active_tab_id(), Some(keep));
        assert_eq!(active_index(&s), 1);
    }
}