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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
//! Declarative description of par-term's menu.
//!
//! This is the single source of truth for the menu's contents. Two renderers
//! consume it:
//!
//! - [`super::MenuManager::new`] walks it to build the [`muda::Menu`] that macOS
//!   and Windows attach natively.
//! - [`super::egui_menu::AppMenuUi`] walks the same model to draw the in-app
//!   menu on platforms that cannot attach a native menu bar (Linux/BSD, where
//!   muda needs a `gtk::Window` that winit never creates — see `super::linux`).
//!
//! Neither renderer owns a list of commands, so the two cannot drift apart.

use super::actions::MenuAction;
use muda::accelerator::{Accelerator, Code, Modifiers};

/// Title of the Help section.
///
/// `MenuManager` inserts the macOS-only native Window menu immediately before
/// this section, following the platform convention of Window preceding Help.
pub const HELP_SECTION_TITLE: &str = "Help";

/// A single activatable menu command.
pub struct MenuItemSpec {
    /// Stable muda menu id. Also used as the egui widget id salt.
    pub id: &'static str,
    /// Human-readable label.
    pub label: &'static str,
    /// Keyboard accelerator, if the command has one.
    pub accelerator: Option<Accelerator>,
    /// Action dispatched when the item is activated.
    pub action: MenuAction,
}

/// One entry inside a menu section.
pub enum MenuEntry {
    /// A command.
    Item(MenuItemSpec),
    /// A horizontal rule.
    Separator,
    /// Insertion point for one entry per configured profile.
    ///
    /// The entries are generated at render time from the live
    /// [`ProfileManager`] by [`profile_entries`], so both renderers stay in
    /// sync with profile edits without duplicating the mapping.
    Profiles,
}

/// A top-level menu (File, Tab, Edit, …).
pub struct MenuSection {
    /// Title shown in the menu bar.
    pub title: &'static str,
    /// Entries in display order.
    pub entries: Vec<MenuEntry>,
}

/// Build the menu model for the current platform's native menu.
///
/// macOS carries Quit and Preferences in the separate application menu built by
/// [`super::macos::build_app_menu`], so they are omitted from File/Edit there.
pub fn platform_menu_model() -> Vec<MenuSection> {
    menu_model(cfg!(target_os = "macos"))
}

/// Build the menu model.
///
/// `has_native_app_menu` is true when the platform provides a separate
/// application menu that already carries Quit and Preferences (macOS). When it
/// is false those two commands are folded into File and Edit, which is what
/// Windows and Linux expect — and what the in-app egui menu always needs, since
/// it is the only menu wherever it is drawn.
pub fn menu_model(has_native_app_menu: bool) -> Vec<MenuSection> {
    // Platform-specific modifier keys
    // macOS: Cmd (META) is safe — it's separate from Ctrl used by terminal control codes
    // Windows/Linux: Use Ctrl+Shift to avoid conflicts with terminal control codes
    // (Ctrl+C=SIGINT, Ctrl+D=EOF, Ctrl+W=delete-word, Ctrl+V=literal-next, etc.)
    #[cfg(target_os = "macos")]
    let cmd_or_ctrl = Modifiers::META;
    #[cfg(not(target_os = "macos"))]
    let cmd_or_ctrl = Modifiers::CONTROL | Modifiers::SHIFT;

    // For items that already include Shift (same on all platforms)
    #[cfg(target_os = "macos")]
    let cmd_or_ctrl_shift = Modifiers::META | Modifiers::SHIFT;
    #[cfg(not(target_os = "macos"))]
    let cmd_or_ctrl_shift = Modifiers::CONTROL | Modifiers::SHIFT;

    // Tab number switching: Cmd+N (macOS) / Alt+N (Windows/Linux)
    #[cfg(target_os = "macos")]
    let tab_switch_mod = Modifiers::META;
    #[cfg(not(target_os = "macos"))]
    let tab_switch_mod = Modifiers::ALT;

    let accel = |mods: Modifiers, code: Code| Some(Accelerator::new(Some(mods), code));

    let mut file = vec![
        item(
            "new_window",
            "New Window",
            accel(cmd_or_ctrl, Code::KeyN),
            MenuAction::NewWindow,
        ),
        // Smart close: closes tab if multiple, window if single
        item(
            "close_window",
            "Close",
            accel(cmd_or_ctrl, Code::KeyW),
            MenuAction::CloseWindow,
        ),
        MenuEntry::Separator,
    ];
    if !has_native_app_menu {
        file.push(item(
            "quit",
            "Quit",
            accel(cmd_or_ctrl, Code::KeyQ),
            MenuAction::Quit,
        ));
    }

    let mut tab = vec![
        item(
            "new_tab",
            "New Tab",
            accel(cmd_or_ctrl, Code::KeyT),
            MenuAction::NewTab,
        ),
        // Matches the `duplicate_tab` default in `Config::default().keybindings`,
        // which is what dispatches this on Linux (no native menu there, so the
        // in-app menu only advertises the chord — it does not register it).
        item(
            "duplicate_tab",
            "Duplicate Tab",
            accel(cmd_or_ctrl_shift, Code::KeyJ),
            MenuAction::DuplicateTab,
        ),
        // No accelerator: same as Close in the File menu (smart close)
        item("close_tab", "Close Tab", None, MenuAction::CloseTab),
        MenuEntry::Separator,
        item(
            "next_tab",
            "Next Tab",
            accel(cmd_or_ctrl_shift, Code::BracketRight),
            MenuAction::NextTab,
        ),
        item(
            "prev_tab",
            "Previous Tab",
            accel(cmd_or_ctrl_shift, Code::BracketLeft),
            MenuAction::PreviousTab,
        ),
        // Reordering is otherwise dispatched by the hardcoded
        // Cmd/Ctrl+Shift+Arrow layer in `key_handler::tabs`. The accelerators
        // here name that same chord, and the settings window's
        // `AVAILABLE_ACTIONS` advertises it — `key_handler::chord_tests` checks
        // all three agree.
        item(
            "move_tab_left",
            "Move Tab Left",
            accel(cmd_or_ctrl_shift, Code::ArrowLeft),
            MenuAction::MoveTabLeft,
        ),
        item(
            "move_tab_right",
            "Move Tab Right",
            accel(cmd_or_ctrl_shift, Code::ArrowRight),
            MenuAction::MoveTabRight,
        ),
        MenuEntry::Separator,
    ];
    for (index, (id, label, code)) in TAB_SWITCH_ITEMS.iter().enumerate() {
        tab.push(item(
            id,
            label,
            accel(tab_switch_mod, *code),
            MenuAction::SwitchToTab(index + 1),
        ));
    }

    let mut edit = vec![
        // Copy/Paste/Select All: Cmd+C/V/A (macOS) / Ctrl+Shift+C/V/A (other)
        item(
            "copy",
            "Copy",
            accel(cmd_or_ctrl, Code::KeyC),
            MenuAction::Copy,
        ),
        item(
            "paste",
            "Paste",
            accel(cmd_or_ctrl, Code::KeyV),
            MenuAction::Paste,
        ),
        item(
            "select_all",
            "Select All",
            accel(cmd_or_ctrl, Code::KeyA),
            MenuAction::SelectAll,
        ),
        MenuEntry::Separator,
        item(
            "clear_scrollback",
            "Clear Scrollback",
            accel(cmd_or_ctrl_shift, Code::KeyK),
            MenuAction::ClearScrollback,
        ),
        item(
            "clipboard_history",
            "Clipboard History",
            accel(cmd_or_ctrl_shift, Code::KeyH),
            MenuAction::ClipboardHistory,
        ),
    ];
    if !has_native_app_menu {
        // Preferences belongs in Edit on Windows and Linux.
        edit.push(MenuEntry::Separator);
        edit.push(item(
            "preferences",
            "Preferences...",
            accel(Modifiers::CONTROL | Modifiers::SHIFT, Code::Comma),
            MenuAction::OpenSettings,
        ));
    }

    vec![
        MenuSection {
            title: "File",
            entries: file,
        },
        MenuSection {
            title: "Tab",
            entries: tab,
        },
        MenuSection {
            title: "Profiles",
            entries: vec![
                // `Manage Profiles...` is a configuration dialog, also reachable
                // from Settings, and it has no `AVAILABLE_ACTIONS` row, so a
                // native accelerator on it burned Cmd/Ctrl+Shift+P for a chord
                // no user could rebind.
                item(
                    "manage_profiles",
                    "Manage Profiles...",
                    None,
                    MenuAction::ManageProfiles,
                ),
                // The drawer owns Cmd/Ctrl+Shift+P: the settings table
                // advertises it, `Config::default().keybindings` binds it, and
                // the hardcoded `profile_drawer_toggle` layer dispatches it.
                // While `Manage Profiles...` held the accelerator the native
                // menu bar ate the chord on macOS and Windows, and on Linux —
                // where the in-app menu only *labels* accelerators — the same
                // chord already reached the drawer while the menu named the
                // manager. See `key_handler::chord_tests`.
                item(
                    "toggle_profile_drawer",
                    "Toggle Profile Drawer",
                    accel(cmd_or_ctrl_shift, Code::KeyP),
                    MenuAction::ToggleProfileDrawer,
                ),
                MenuEntry::Separator,
                MenuEntry::Profiles,
            ],
        },
        MenuSection {
            title: "Edit",
            entries: edit,
        },
        MenuSection {
            title: "View",
            entries: vec![
                item(
                    "toggle_fullscreen",
                    "Toggle Fullscreen",
                    Some(Accelerator::new(None, Code::F11)),
                    MenuAction::ToggleFullscreen,
                ),
                item(
                    "maximize_vertically",
                    "Maximize Vertically",
                    accel(Modifiers::SHIFT, Code::F11),
                    MenuAction::MaximizeVertically,
                ),
                MenuEntry::Separator,
                item(
                    "increase_font",
                    "Increase Font Size",
                    accel(cmd_or_ctrl, Code::Equal),
                    MenuAction::IncreaseFontSize,
                ),
                item(
                    "decrease_font",
                    "Decrease Font Size",
                    accel(cmd_or_ctrl, Code::Minus),
                    MenuAction::DecreaseFontSize,
                ),
                item(
                    "reset_font",
                    "Reset Font Size",
                    accel(cmd_or_ctrl, Code::Digit0),
                    MenuAction::ResetFontSize,
                ),
                MenuEntry::Separator,
                item(
                    "fps_overlay",
                    "FPS Overlay",
                    Some(Accelerator::new(None, Code::F3)),
                    MenuAction::ToggleFpsOverlay,
                ),
                item(
                    "settings",
                    "Settings...",
                    Some(Accelerator::new(None, Code::F12)),
                    MenuAction::OpenSettings,
                ),
                MenuEntry::Separator,
                item(
                    "save_arrangement",
                    "Save Window Arrangement...",
                    None,
                    MenuAction::SaveArrangement,
                ),
            ],
        },
        MenuSection {
            title: "Shell",
            entries: vec![item(
                "install_remote_shell_integration",
                "Install Shell Integration on Remote Host...",
                None,
                MenuAction::InstallShellIntegrationRemote,
            )],
        },
        MenuSection {
            title: HELP_SECTION_TITLE,
            entries: vec![
                item(
                    "keyboard_shortcuts",
                    "Keyboard Shortcuts",
                    Some(Accelerator::new(None, Code::F1)),
                    MenuAction::ShowHelp,
                ),
                MenuEntry::Separator,
                item("about", "About par-term", None, MenuAction::About),
            ],
        },
    ]
}

/// Menu ids, labels and key codes for the Tab 1-9 switch items.
const TAB_SWITCH_ITEMS: [(&str, &str, Code); 9] = [
    ("tab_1", "Tab 1", Code::Digit1),
    ("tab_2", "Tab 2", Code::Digit2),
    ("tab_3", "Tab 3", Code::Digit3),
    ("tab_4", "Tab 4", Code::Digit4),
    ("tab_5", "Tab 5", Code::Digit5),
    ("tab_6", "Tab 6", Code::Digit6),
    ("tab_7", "Tab 7", Code::Digit7),
    ("tab_8", "Tab 8", Code::Digit8),
    ("tab_9", "Tab 9", Code::Digit9),
];

/// Shorthand for a command entry.
fn item(
    id: &'static str,
    label: &'static str,
    accelerator: Option<Accelerator>,
    action: MenuAction,
) -> MenuEntry {
    MenuEntry::Item(MenuItemSpec {
        id,
        label,
        accelerator,
        action,
    })
}

/// One dynamically generated profile entry.
pub struct ProfileEntry {
    /// Stable muda menu id.
    pub menu_id: String,
    /// Label as shown in the menu.
    pub label: String,
    /// Action dispatched when the entry is activated.
    pub action: MenuAction,
}

/// Expand [`MenuEntry::Profiles`] into one entry per configured profile.
///
/// Shared by the muda and egui renderers so both show the same profiles, with
/// the same labels, in the same order.
pub fn profile_entries<'a>(
    profiles: impl IntoIterator<Item = &'a crate::profile::Profile>,
) -> Vec<ProfileEntry> {
    profiles
        .into_iter()
        .map(|profile| ProfileEntry {
            menu_id: format!("profile_{}", profile.id),
            label: profile.display_label(),
            action: MenuAction::OpenProfile(profile.id),
        })
        .collect()
}

/// Render an accelerator the way a menu displays it, e.g. `⌘N` or `Ctrl+Shift+N`.
///
/// Derived from the same [`Accelerator`] the native menu registers, so the
/// in-app menu cannot advertise a shortcut the native menu does not have.
pub fn accelerator_label(accelerator: &Accelerator) -> String {
    let mut label = String::new();
    // `Accelerator::new` normalises META to SUPER, so only SUPER is ever set.
    // macOS renders modifiers as adjacent symbols; everywhere else they are
    // spelled out and joined with '+'.
    let named: [(Modifiers, &str, &str); 4] = [
        (Modifiers::CONTROL, "", "Ctrl"),
        (Modifiers::ALT, "", "Alt"),
        (Modifiers::SHIFT, "", "Shift"),
        (Modifiers::SUPER, "", "Super"),
    ];
    let mods = accelerator.modifiers();
    for (flag, symbol, word) in named {
        if mods.contains(flag) {
            if cfg!(target_os = "macos") {
                label.push_str(symbol);
            } else {
                label.push_str(word);
                label.push('+');
            }
        }
    }
    label.push_str(&code_label(accelerator.key()));
    label
}

/// Human-readable name for a key code (`KeyN` → `N`, `BracketLeft` → `[`).
fn code_label(code: Code) -> String {
    let raw = format!("{code:?}");
    match raw.as_str() {
        "Comma" => ",".to_string(),
        "Period" => ".".to_string(),
        "Equal" => "+".to_string(),
        "Minus" => "-".to_string(),
        "BracketLeft" => "[".to_string(),
        "BracketRight" => "]".to_string(),
        "Space" => "Space".to_string(),
        // Without these the in-app menu would advertise "ArrowLeft"; "Left" is
        // also what the settings window's keybinding table prints.
        "ArrowLeft" => "Left".to_string(),
        "ArrowRight" => "Right".to_string(),
        other => other
            .strip_prefix("Key")
            .or_else(|| other.strip_prefix("Digit"))
            .unwrap_or(other)
            .to_string(),
    }
}

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

    fn items(model: &[MenuSection]) -> Vec<&MenuItemSpec> {
        model
            .iter()
            .flat_map(|section| &section.entries)
            .filter_map(|entry| match entry {
                MenuEntry::Item(spec) => Some(spec),
                _ => None,
            })
            .collect()
    }

    #[test]
    fn menu_ids_are_unique() {
        for has_native_app_menu in [false, true] {
            let model = menu_model(has_native_app_menu);
            let mut seen = HashSet::new();
            for spec in items(&model) {
                assert!(
                    seen.insert(spec.id),
                    "duplicate menu id {:?} (has_native_app_menu={has_native_app_menu})",
                    spec.id
                );
            }
        }
    }

    /// Without a native application menu the model must carry Quit and
    /// Preferences itself — this is exactly what Linux was missing.
    #[test]
    fn quit_and_preferences_present_without_native_app_menu() {
        let model = menu_model(false);
        let actions: Vec<MenuAction> = items(&model).iter().map(|spec| spec.action).collect();
        assert!(actions.contains(&MenuAction::Quit));
        assert!(actions.contains(&MenuAction::OpenSettings));
        assert!(actions.contains(&MenuAction::NewWindow));
        assert!(actions.contains(&MenuAction::CloseWindow));
        assert!(actions.contains(&MenuAction::SelectAll));
        assert!(actions.contains(&MenuAction::MaximizeVertically));
    }

    /// `MenuAction::DuplicateTab` was declared and handled but emitted by no
    /// menu item, which left `duplicate_tab` with a handler nothing could reach.
    #[test]
    fn duplicate_tab_is_reachable_from_the_menu() {
        for has_native_app_menu in [false, true] {
            let model = menu_model(has_native_app_menu);
            let actions: Vec<MenuAction> = items(&model).iter().map(|spec| spec.action).collect();
            assert!(
                actions.contains(&MenuAction::DuplicateTab),
                "no menu item emits DuplicateTab (has_native_app_menu={has_native_app_menu})"
            );
        }
    }

    /// `MoveTabLeft`/`MoveTabRight` were declared and handled but emitted by no
    /// menu item, so tab reordering existed only as an unadvertised chord.
    #[test]
    fn tab_reordering_is_reachable_from_the_menu() {
        for has_native_app_menu in [false, true] {
            let model = menu_model(has_native_app_menu);
            let actions: Vec<MenuAction> = items(&model).iter().map(|spec| spec.action).collect();
            for expected in [MenuAction::MoveTabLeft, MenuAction::MoveTabRight] {
                assert!(
                    actions.contains(&expected),
                    "no menu item emits {expected:?} (has_native_app_menu={has_native_app_menu})"
                );
            }
        }
    }

    /// macOS keeps Quit in the application menu, so File must not duplicate it.
    #[test]
    fn quit_absent_when_native_app_menu_owns_it() {
        let model = menu_model(true);
        let actions: Vec<MenuAction> = items(&model).iter().map(|spec| spec.action).collect();
        assert!(!actions.contains(&MenuAction::Quit));
    }

    /// The two variants must offer the same commands apart from the ones the
    /// native application menu owns.
    #[test]
    fn variants_differ_only_by_app_menu_items() {
        let with_app_menu: HashSet<&str> = items(&menu_model(true))
            .iter()
            .map(|spec| spec.id)
            .collect();
        let without: HashSet<&str> = items(&menu_model(false))
            .iter()
            .map(|spec| spec.id)
            .collect();
        let extra: Vec<&&str> = without.difference(&with_app_menu).collect();
        assert_eq!(extra.len(), 2, "unexpected difference: {extra:?}");
        assert!(with_app_menu.difference(&without).next().is_none());
    }

    #[test]
    fn every_section_has_entries() {
        for section in menu_model(false) {
            assert!(
                !section.entries.is_empty(),
                "section {:?} is empty",
                section.title
            );
        }
    }

    /// The Profiles insertion point must exist exactly once.
    #[test]
    fn profiles_placeholder_appears_once() {
        let count = menu_model(false)
            .iter()
            .flat_map(|section| &section.entries)
            .filter(|entry| matches!(entry, MenuEntry::Profiles))
            .count();
        assert_eq!(count, 1);
    }

    /// Flatten the model to `Section/entry` lines, in order.
    fn outline(model: &[MenuSection]) -> Vec<String> {
        model
            .iter()
            .flat_map(|section| {
                section.entries.iter().map(move |entry| match entry {
                    MenuEntry::Item(spec) => {
                        format!("{}/{} = {:?}", section.title, spec.id, spec.label)
                    }
                    MenuEntry::Separator => format!("{}/---", section.title),
                    MenuEntry::Profiles => format!("{}/<profiles>", section.title),
                })
            })
            .collect()
    }

    /// The order-sensitive snapshot.
    ///
    /// The macOS and Windows menus are built by walking this model, and neither
    /// can be exercised from the other's CI. A reordered section, a dropped
    /// separator or a renamed label is invisible to every other test here, so
    /// this freezes the structure that shipped before the model existed. Update
    /// it deliberately when the menu changes.
    #[test]
    fn model_matches_the_shipped_menu_structure() {
        let expected_common = [
            "File/new_window = \"New Window\"",
            "File/close_window = \"Close\"",
            "File/---",
            "Tab/new_tab = \"New Tab\"",
            "Tab/duplicate_tab = \"Duplicate Tab\"",
            "Tab/close_tab = \"Close Tab\"",
            "Tab/---",
            "Tab/next_tab = \"Next Tab\"",
            "Tab/prev_tab = \"Previous Tab\"",
            "Tab/move_tab_left = \"Move Tab Left\"",
            "Tab/move_tab_right = \"Move Tab Right\"",
            "Tab/---",
            "Tab/tab_1 = \"Tab 1\"",
            "Tab/tab_2 = \"Tab 2\"",
            "Tab/tab_3 = \"Tab 3\"",
            "Tab/tab_4 = \"Tab 4\"",
            "Tab/tab_5 = \"Tab 5\"",
            "Tab/tab_6 = \"Tab 6\"",
            "Tab/tab_7 = \"Tab 7\"",
            "Tab/tab_8 = \"Tab 8\"",
            "Tab/tab_9 = \"Tab 9\"",
            "Profiles/manage_profiles = \"Manage Profiles...\"",
            "Profiles/toggle_profile_drawer = \"Toggle Profile Drawer\"",
            "Profiles/---",
            "Profiles/<profiles>",
            "Edit/copy = \"Copy\"",
            "Edit/paste = \"Paste\"",
            "Edit/select_all = \"Select All\"",
            "Edit/---",
            "Edit/clear_scrollback = \"Clear Scrollback\"",
            "Edit/clipboard_history = \"Clipboard History\"",
        ];
        let expected_tail = [
            "View/toggle_fullscreen = \"Toggle Fullscreen\"",
            "View/maximize_vertically = \"Maximize Vertically\"",
            "View/---",
            "View/increase_font = \"Increase Font Size\"",
            "View/decrease_font = \"Decrease Font Size\"",
            "View/reset_font = \"Reset Font Size\"",
            "View/---",
            "View/fps_overlay = \"FPS Overlay\"",
            "View/settings = \"Settings...\"",
            "View/---",
            "View/save_arrangement = \"Save Window Arrangement...\"",
            "Shell/install_remote_shell_integration = \
             \"Install Shell Integration on Remote Host...\"",
            "Help/keyboard_shortcuts = \"Keyboard Shortcuts\"",
            "Help/---",
            "Help/about = \"About par-term\"",
        ];

        // macOS: Quit and Preferences belong to the native application menu.
        let mut with_app_menu: Vec<&str> = expected_common.to_vec();
        with_app_menu.extend(expected_tail);
        assert_eq!(outline(&menu_model(true)), with_app_menu);

        // Everywhere else they are folded into File and Edit.
        let mut without: Vec<&str> = expected_common.to_vec();
        without.insert(3, "File/quit = \"Quit\"");
        without.push("Edit/---");
        without.push("Edit/preferences = \"Preferences...\"");
        without.extend(expected_tail);
        assert_eq!(outline(&menu_model(false)), without);
    }

    /// Which commands carry a keyboard accelerator is part of the contract the
    /// in-app menu advertises, and is platform-independent even though the
    /// modifiers are not.
    #[test]
    fn the_same_commands_carry_accelerators() {
        let accelerated: Vec<&str> = items(&menu_model(false))
            .iter()
            .filter(|spec| spec.accelerator.is_some())
            .map(|spec| spec.id)
            .collect();
        assert_eq!(
            accelerated,
            vec![
                "new_window",
                "close_window",
                "quit",
                "new_tab",
                "duplicate_tab",
                "next_tab",
                "prev_tab",
                "move_tab_left",
                "move_tab_right",
                "tab_1",
                "tab_2",
                "tab_3",
                "tab_4",
                "tab_5",
                "tab_6",
                "tab_7",
                "tab_8",
                "tab_9",
                "toggle_profile_drawer",
                "copy",
                "paste",
                "select_all",
                "clear_scrollback",
                "clipboard_history",
                "preferences",
                "toggle_fullscreen",
                "maximize_vertically",
                "increase_font",
                "decrease_font",
                "reset_font",
                "fps_overlay",
                "settings",
                "keyboard_shortcuts",
            ]
        );
    }

    #[test]
    fn accelerator_labels_are_readable() {
        let plain = Accelerator::new(None, Code::F11);
        assert_eq!(accelerator_label(&plain), "F11");

        let bracket = Accelerator::new(Some(Modifiers::SHIFT), Code::BracketRight);
        let label = accelerator_label(&bracket);
        assert!(label.ends_with(']'), "unexpected label {label:?}");

        let digit = Accelerator::new(Some(Modifiers::ALT), Code::Digit1);
        assert!(accelerator_label(&digit).ends_with('1'));

        // The arrow keys reach the label through `code_label`'s fallback unless
        // they are named, which would print "ArrowLeft" in the in-app menu.
        let arrow = Accelerator::new(Some(Modifiers::SHIFT), Code::ArrowLeft);
        assert!(
            accelerator_label(&arrow).ends_with("Left"),
            "unexpected label {:?}",
            accelerator_label(&arrow)
        );
        assert!(!accelerator_label(&arrow).contains("Arrow"));
    }
}