cyberterm 0.2.1

A GPU terminal for developers: splits and sessions, command blocks, searchable history, AI help, inline images and Lua scripting
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
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
// src/input/bindings.rs
//
// Terminal-level keybindings (copy, paste, scrollback, splits, tabs, ...).
// A combo that matches a binding is handled by Cyberterm and never reaches
// the shell; everything else is encoded by `keyboard.rs`.
//
// Two tables:
// - Direct bindings. Split/tab defaults follow Ghostty's, so muscle memory
//   carries over.
// - Leader bindings (`"leader+%" = "split_right"`), active only when
//   `[keyboard] leader` is set: press the leader, then the key -- tmux's
//   prefix model. The defaults mirror tmux's own keys.

use std::collections::BTreeMap;
use winit::keyboard::{Key, ModifiersState, NamedKey};

use crate::layout::Direction;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Action {
    Copy,
    Paste,
    PastePrimary,
    SelectAll,
    ScrollPageUp,
    ScrollPageDown,
    ScrollLineUp,
    ScrollLineDown,
    ScrollToTop,
    ScrollToBottom,
    PreviousPrompt,
    NextPrompt,
    ClearScrollback,
    FontIncrease,
    FontDecrease,
    FontReset,
    ThemeMenu,
    ReloadConfig,
    Split(Direction),
    ClosePane,
    Focus(Direction),
    FocusNext,
    FocusPrevious,
    Resize(Direction),
    EqualizeSplits,
    ToggleZoom,
    ToggleBroadcast,
    NewTab,
    CloseTab,
    NextTab,
    PreviousTab,
    /// 1-based; 9 is reserved for "last tab", as in browsers and Ghostty.
    GotoTab(u8),
    LastTab,
    MoveTabLeft,
    MoveTabRight,
    NewWindow,
    CopyLastOutput,
    ShowLastOutput,
    FindInScrollback,
    HistorySearch,
    AskAi,
    ExplainError,
    ToggleDanger,
    Rewind,
    CommandPalette,
}

use Direction::{Down, Left, Right, Up};

/// Every action with its config name and description, in the order
/// `+list-termkeys` shows them.
pub const ACTIONS: &[(Action, &str, &str)] = &[
    (Action::Copy, "copy", "Copy the selection to the clipboard"),
    (Action::Paste, "paste", "Paste from the clipboard"),
    (
        Action::PastePrimary,
        "paste_primary",
        "Paste the primary selection",
    ),
    (
        Action::SelectAll,
        "select_all",
        "Select the whole scrollback",
    ),
    (
        Action::ScrollPageUp,
        "scroll_page_up",
        "Scroll back one page",
    ),
    (
        Action::ScrollPageDown,
        "scroll_page_down",
        "Scroll forward one page",
    ),
    (
        Action::ScrollLineUp,
        "scroll_line_up",
        "Scroll back one line",
    ),
    (
        Action::ScrollLineDown,
        "scroll_line_down",
        "Scroll forward one line",
    ),
    (
        Action::ScrollToTop,
        "scroll_to_top",
        "Jump to the top of the scrollback",
    ),
    (
        Action::ScrollToBottom,
        "scroll_to_bottom",
        "Jump back to the live screen",
    ),
    (
        Action::PreviousPrompt,
        "previous_prompt",
        "Jump to the previous shell prompt",
    ),
    (
        Action::NextPrompt,
        "next_prompt",
        "Jump to the next shell prompt",
    ),
    (
        Action::ClearScrollback,
        "clear_scrollback",
        "Erase the scrollback history",
    ),
    (
        Action::FontIncrease,
        "font_increase",
        "Make the font bigger",
    ),
    (
        Action::FontDecrease,
        "font_decrease",
        "Make the font smaller",
    ),
    (Action::FontReset, "font_reset", "Reset the font size"),
    (Action::ThemeMenu, "theme_menu", "Open the theme picker"),
    (
        Action::ReloadConfig,
        "reload_config",
        "Re-read the config file now",
    ),
    (
        Action::Split(Right),
        "split_right",
        "Split: new pane to the right",
    ),
    (Action::Split(Down), "split_down", "Split: new pane below"),
    (
        Action::Split(Left),
        "split_left",
        "Split: new pane to the left",
    ),
    (Action::Split(Up), "split_up", "Split: new pane above"),
    (Action::ClosePane, "close_pane", "Close the focused pane"),
    (
        Action::Focus(Left),
        "focus_left",
        "Focus the pane to the left",
    ),
    (
        Action::Focus(Right),
        "focus_right",
        "Focus the pane to the right",
    ),
    (Action::Focus(Up), "focus_up", "Focus the pane above"),
    (Action::Focus(Down), "focus_down", "Focus the pane below"),
    (Action::FocusNext, "focus_next", "Focus the next pane"),
    (
        Action::FocusPrevious,
        "focus_previous",
        "Focus the previous pane",
    ),
    (
        Action::Resize(Left),
        "resize_left",
        "Move the nearest vertical divider left",
    ),
    (
        Action::Resize(Right),
        "resize_right",
        "Move the nearest vertical divider right",
    ),
    (
        Action::Resize(Up),
        "resize_up",
        "Move the nearest horizontal divider up",
    ),
    (
        Action::Resize(Down),
        "resize_down",
        "Move the nearest horizontal divider down",
    ),
    (
        Action::EqualizeSplits,
        "equalize_splits",
        "Give every pane in the tab equal space",
    ),
    (
        Action::ToggleZoom,
        "toggle_zoom",
        "Zoom the focused pane to the full tab (toggle)",
    ),
    (
        Action::ToggleBroadcast,
        "toggle_broadcast",
        "Type into every pane in the tab (toggle)",
    ),
    (Action::NewTab, "new_tab", "Open a new tab"),
    (Action::CloseTab, "close_tab", "Close the current tab"),
    (Action::NextTab, "next_tab", "Switch to the next tab"),
    (
        Action::PreviousTab,
        "previous_tab",
        "Switch to the previous tab",
    ),
    (Action::GotoTab(1), "goto_tab_1", "Switch to tab 1"),
    (Action::GotoTab(2), "goto_tab_2", "Switch to tab 2"),
    (Action::GotoTab(3), "goto_tab_3", "Switch to tab 3"),
    (Action::GotoTab(4), "goto_tab_4", "Switch to tab 4"),
    (Action::GotoTab(5), "goto_tab_5", "Switch to tab 5"),
    (Action::GotoTab(6), "goto_tab_6", "Switch to tab 6"),
    (Action::GotoTab(7), "goto_tab_7", "Switch to tab 7"),
    (Action::GotoTab(8), "goto_tab_8", "Switch to tab 8"),
    (Action::LastTab, "last_tab", "Switch to the last tab"),
    (
        Action::MoveTabLeft,
        "move_tab_left",
        "Move the current tab left",
    ),
    (
        Action::MoveTabRight,
        "move_tab_right",
        "Move the current tab right",
    ),
    (
        Action::NewWindow,
        "new_window",
        "Open a new Cyberterm window",
    ),
    (
        Action::CopyLastOutput,
        "copy_last_output",
        "Copy the last command's output",
    ),
    (
        Action::ShowLastOutput,
        "show_last_output",
        "Open the last command's output in a pager",
    ),
    (
        Action::FindInScrollback,
        "find",
        "Search the scrollback (Enter older, Shift+Enter newer)",
    ),
    (
        Action::HistorySearch,
        "history_search",
        "Search saved commands and their output",
    ),
    (
        Action::AskAi,
        "ask_ai",
        "Ask AI for a command in plain English",
    ),
    (
        Action::ExplainError,
        "explain_error",
        "Ask AI to explain the last failed command",
    ),
    (
        Action::ToggleDanger,
        "toggle_danger",
        "Mark or unmark the pane as dangerous (red, confirms risky commands)",
    ),
    (
        Action::Rewind,
        "rewind",
        "Step back through what the pane's screen showed",
    ),
    (
        Action::CommandPalette,
        "command_palette",
        "Search every action, pane, theme and recent command",
    ),
];

pub const DEFAULT_BINDINGS: &[(&str, &str)] = &[
    ("ctrl+shift+c", "copy"),
    ("ctrl+shift+v", "paste"),
    // Ctrl+Insert / Shift+Insert are also what Omarchy's universal
    // Super+C / Super+V send to the focused window.
    ("ctrl+insert", "copy"),
    ("shift+insert", "paste"),
    ("ctrl+shift+a", "select_all"),
    ("shift+page_up", "scroll_page_up"),
    ("shift+page_down", "scroll_page_down"),
    ("ctrl+shift+up", "scroll_line_up"),
    ("ctrl+shift+down", "scroll_line_down"),
    ("shift+home", "scroll_to_top"),
    ("shift+end", "scroll_to_bottom"),
    ("ctrl+shift+z", "previous_prompt"),
    ("ctrl+shift+x", "next_prompt"),
    ("ctrl+shift+k", "clear_scrollback"),
    ("ctrl+equal", "font_increase"),
    ("ctrl+plus", "font_increase"),
    ("ctrl+minus", "font_decrease"),
    ("ctrl+0", "font_reset"),
    ("ctrl+shift+comma", "theme_menu"),
    ("ctrl+shift+r", "reload_config"),
    // Splits and tabs: Ghostty's defaults.
    ("ctrl+shift+o", "split_right"),
    ("ctrl+shift+e", "split_down"),
    ("ctrl+shift+w", "close_pane"),
    ("ctrl+alt+left", "focus_left"),
    ("ctrl+alt+right", "focus_right"),
    ("ctrl+alt+up", "focus_up"),
    ("ctrl+alt+down", "focus_down"),
    ("ctrl+super+]", "focus_next"),
    ("ctrl+super+[", "focus_previous"),
    ("ctrl+super+shift+left", "resize_left"),
    ("ctrl+super+shift+right", "resize_right"),
    ("ctrl+super+shift+up", "resize_up"),
    ("ctrl+super+shift+down", "resize_down"),
    // Omarchy's Ghostty config resizes with Alt added.
    ("ctrl+super+shift+alt+left", "resize_left"),
    ("ctrl+super+shift+alt+right", "resize_right"),
    ("ctrl+super+shift+alt+up", "resize_up"),
    ("ctrl+super+shift+alt+down", "resize_down"),
    ("ctrl+super+shift+equal", "equalize_splits"),
    ("ctrl+shift+enter", "toggle_zoom"),
    ("ctrl+shift+b", "toggle_broadcast"),
    ("ctrl+shift+t", "new_tab"),
    ("ctrl+tab", "next_tab"),
    ("ctrl+shift+tab", "previous_tab"),
    ("ctrl+page_down", "next_tab"),
    ("ctrl+page_up", "previous_tab"),
    ("ctrl+shift+page_up", "move_tab_left"),
    ("ctrl+shift+page_down", "move_tab_right"),
    ("alt+1", "goto_tab_1"),
    ("alt+2", "goto_tab_2"),
    ("alt+3", "goto_tab_3"),
    ("alt+4", "goto_tab_4"),
    ("alt+5", "goto_tab_5"),
    ("alt+6", "goto_tab_6"),
    ("alt+7", "goto_tab_7"),
    ("alt+8", "goto_tab_8"),
    ("alt+9", "last_tab"),
    ("ctrl+shift+n", "new_window"),
    ("ctrl+shift+g", "show_last_output"),
    ("ctrl+shift+y", "copy_last_output"),
    ("ctrl+shift+f", "find"),
    ("ctrl+shift+h", "history_search"),
    ("ctrl+shift+i", "ask_ai"),
    ("ctrl+shift+j", "explain_error"),
    ("ctrl+shift+d", "toggle_danger"),
    ("ctrl+shift+u", "rewind"),
    ("ctrl+shift+p", "command_palette"),
];

/// Keys after the leader, when one is configured: tmux's own defaults.
pub const DEFAULT_LEADER_BINDINGS: &[(&str, &str)] = &[
    ("%", "split_right"),
    ("\"", "split_down"),
    ("x", "close_pane"),
    ("z", "toggle_zoom"),
    ("o", "focus_next"),
    (";", "focus_previous"),
    ("left", "focus_left"),
    ("right", "focus_right"),
    ("up", "focus_up"),
    ("down", "focus_down"),
    ("ctrl+left", "resize_left"),
    ("ctrl+right", "resize_right"),
    ("ctrl+up", "resize_up"),
    ("ctrl+down", "resize_down"),
    ("space", "equalize_splits"),
    ("c", "new_tab"),
    ("&", "close_tab"),
    ("n", "next_tab"),
    ("p", "previous_tab"),
    ("1", "goto_tab_1"),
    ("2", "goto_tab_2"),
    ("3", "goto_tab_3"),
    ("4", "goto_tab_4"),
    ("5", "goto_tab_5"),
    ("6", "goto_tab_6"),
    ("7", "goto_tab_7"),
    ("8", "goto_tab_8"),
    ("9", "last_tab"),
    ("[", "scroll_page_up"),
];

const LEADER_PREFIX: &str = "leader+";

fn action_by_name(name: &str) -> Option<Action> {
    ACTIONS
        .iter()
        .find(|(_, n, _)| *n == name)
        .map(|(a, _, _)| *a)
}

fn description(action: Action) -> &'static str {
    ACTIONS
        .iter()
        .find(|(a, _, _)| *a == action)
        .map(|(_, _, d)| *d)
        .unwrap_or("")
}

#[derive(Clone, Debug, PartialEq, Eq)]
enum KeyName {
    Char(String),
    Named(NamedKey),
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Combo {
    mods: ModifiersState,
    key: KeyName,
}

impl Combo {
    /// Parses `ctrl+shift+c`, `shift+page_up`, `ctrl+equal`, `alt+f5`, ...
    pub fn parse(text: &str) -> Result<Self, String> {
        let mut mods = ModifiersState::empty();
        let parts: Vec<&str> = text.split('+').map(str::trim).collect();
        // `ctrl++` means ctrl+plus.
        let (key_part, mod_parts) = match parts.as_slice() {
            [rest @ .., "", ""] => ("+", rest),
            [rest @ .., last] => (*last, rest),
            [] => return Err("empty keybinding".into()),
        };
        for m in mod_parts {
            match m.to_ascii_lowercase().as_str() {
                "ctrl" | "control" => mods |= ModifiersState::CONTROL,
                "shift" => mods |= ModifiersState::SHIFT,
                "alt" | "opt" | "option" => mods |= ModifiersState::ALT,
                "super" | "cmd" | "logo" | "meta" => mods |= ModifiersState::SUPER,
                other => return Err(format!("unknown modifier `{other}` in `{text}`")),
            }
        }
        let lower = key_part.to_ascii_lowercase();
        let key = match lower.as_str() {
            "page_up" | "pageup" => KeyName::Named(NamedKey::PageUp),
            "page_down" | "pagedown" => KeyName::Named(NamedKey::PageDown),
            "home" => KeyName::Named(NamedKey::Home),
            "end" => KeyName::Named(NamedKey::End),
            "insert" => KeyName::Named(NamedKey::Insert),
            "delete" => KeyName::Named(NamedKey::Delete),
            "up" => KeyName::Named(NamedKey::ArrowUp),
            "down" => KeyName::Named(NamedKey::ArrowDown),
            "left" => KeyName::Named(NamedKey::ArrowLeft),
            "right" => KeyName::Named(NamedKey::ArrowRight),
            "enter" | "return" => KeyName::Named(NamedKey::Enter),
            "tab" => KeyName::Named(NamedKey::Tab),
            "space" => KeyName::Named(NamedKey::Space),
            "escape" | "esc" => KeyName::Named(NamedKey::Escape),
            "backspace" => KeyName::Named(NamedKey::Backspace),
            "equal" => KeyName::Char("=".into()),
            "plus" => KeyName::Char("+".into()),
            "minus" => KeyName::Char("-".into()),
            "comma" => KeyName::Char(",".into()),
            "period" => KeyName::Char(".".into()),
            f if f.len() > 1 && f.starts_with('f') && f[1..].parse::<u8>().is_ok() => {
                let n: u8 = f[1..].parse().unwrap_or(0);
                KeyName::Named(function_key(n).ok_or_else(|| format!("no key `{f}`"))?)
            }
            c if c.chars().count() == 1 => KeyName::Char(c.to_string()),
            other => return Err(format!("unknown key `{other}` in `{text}`")),
        };
        Ok(Self { mods, key })
    }

    /// `logical` is the key as typed (Shift applied), `base` the same key
    /// without modifiers. A binding on a shifted symbol like `ctrl+plus`
    /// matches the logical key and ignores the Shift it took to type it.
    pub(crate) fn matches(&self, logical: &Key, base: &Key, mods: ModifiersState) -> bool {
        let relevant = ModifiersState::CONTROL
            | ModifiersState::SHIFT
            | ModifiersState::ALT
            | ModifiersState::SUPER;
        let mods = mods & relevant;
        match &self.key {
            KeyName::Named(named) => {
                matches!(logical, Key::Named(n) if n == named) && mods == self.mods
            }
            KeyName::Char(c) => {
                let base_hit = matches!(base, Key::Character(b) if b.to_lowercase() == *c);
                let logical_hit = matches!(logical, Key::Character(l) if l.to_lowercase() == *c);
                (base_hit && mods == self.mods)
                    || (logical_hit && mods - ModifiersState::SHIFT == self.mods)
            }
        }
    }
}

fn function_key(n: u8) -> Option<NamedKey> {
    use NamedKey::*;
    [F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12]
        .get((n as usize).checked_sub(1)?)
        .copied()
}

/// `ctrl+shift+c` -> `Ctrl+Shift+C`, for display.
fn pretty(combo: &str) -> String {
    combo
        .split('+')
        .map(|part| {
            let mut chars = part.chars();
            match chars.next() {
                Some(first) => first.to_uppercase().chain(chars).collect(),
                None => "+".to_string(),
            }
        })
        .collect::<Vec<_>>()
        .join("+")
        .replace("++", "+")
        .replace("Comma", ",")
        .replace("Period", ".")
        .replace("Equal", "=")
        .replace("Minus", "-")
        .replace("Plus", "+")
}

type Entry = (Combo, Action, String);

pub struct Bindings {
    /// Combo, action, and the combo as written (for menus and help).
    entries: Vec<Entry>,
    leader: Option<(Combo, String)>,
    leader_entries: Vec<Entry>,
}

impl Bindings {
    /// Built-in bindings with the user's `[keybindings]` table applied on
    /// top, plus the leader table when `leader` is set. Returns the
    /// problems found so they can be reported instead of silently ignored.
    pub fn new(overrides: &BTreeMap<String, String>, leader: Option<&str>) -> (Self, Vec<String>) {
        let mut errors = Vec::new();
        let defaults = |table: &[(&str, &str)]| -> Vec<Entry> {
            table
                .iter()
                .filter_map(|(combo, action)| {
                    Some((
                        Combo::parse(combo).ok()?,
                        action_by_name(action)?,
                        combo.to_string(),
                    ))
                })
                .collect()
        };
        let mut entries = defaults(DEFAULT_BINDINGS);
        let mut leader_entries = defaults(DEFAULT_LEADER_BINDINGS);

        let leader = match leader.map(str::trim).filter(|l| !l.is_empty()) {
            Some(text) => match Combo::parse(text) {
                Ok(combo) => {
                    // The leader itself must never also trigger a binding.
                    entries.retain(|(c, _, _)| *c != combo);
                    Some((combo, text.to_string()))
                }
                Err(e) => {
                    errors.push(format!("leader: {e}"));
                    None
                }
            },
            None => None,
        };

        for (combo_text, action_name) in overrides {
            let (table, key_text) = match combo_text.strip_prefix(LEADER_PREFIX) {
                Some(rest) => (&mut leader_entries, rest),
                None => (&mut entries, combo_text.as_str()),
            };
            let combo = match Combo::parse(key_text) {
                Ok(c) => c,
                Err(e) => {
                    errors.push(e);
                    continue;
                }
            };
            table.retain(|(c, _, _)| *c != combo);
            if action_name == "none" {
                continue;
            }
            match action_by_name(action_name) {
                Some(action) => table.push((combo, action, key_text.to_string())),
                None => errors.push(format!("unknown action `{action_name}` for `{combo_text}`")),
            }
        }
        (
            Self {
                entries,
                leader,
                leader_entries,
            },
            errors,
        )
    }

    pub fn lookup(&self, logical: &Key, base: &Key, mods: ModifiersState) -> Option<Action> {
        find(&self.entries, logical, base, mods)
    }

    /// Whether this key is the configured leader.
    pub fn is_leader(&self, logical: &Key, base: &Key, mods: ModifiersState) -> bool {
        self.leader
            .as_ref()
            .is_some_and(|(combo, _)| combo.matches(logical, base, mods))
    }

    pub fn lookup_leader(&self, logical: &Key, base: &Key, mods: ModifiersState) -> Option<Action> {
        find(&self.leader_entries, logical, base, mods)
    }

    /// The first combo bound to `action`, formatted for display.
    pub fn hint(&self, action: Action) -> String {
        if let Some((_, _, text)) = self.entries.iter().find(|(_, a, _)| *a == action) {
            return pretty(text);
        }
        match (
            &self.leader,
            self.leader_entries.iter().find(|(_, a, _)| *a == action),
        ) {
            (Some((_, leader)), Some((_, _, text))) => {
                format!("{} {}", pretty(leader), pretty(text))
            }
            _ => String::new(),
        }
    }

    /// The effective binding list, for `cyberterm +list-termkeys`.
    pub fn describe(&self) -> Vec<(String, &'static str)> {
        let mut out: Vec<(String, &'static str)> = self
            .entries
            .iter()
            .map(|(_, action, text)| (text.clone(), description(*action)))
            .collect();
        if let Some((_, leader)) = &self.leader {
            out.extend(
                self.leader_entries
                    .iter()
                    .map(|(_, action, text)| (format!("{leader}, {text}"), description(*action))),
            );
        }
        out
    }
}

fn find(entries: &[Entry], logical: &Key, base: &Key, mods: ModifiersState) -> Option<Action> {
    entries
        .iter()
        .find(|(combo, _, _)| combo.matches(logical, base, mods))
        .map(|(_, a, _)| *a)
}

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

    fn ch(s: &str) -> Key {
        Key::Character(s.into())
    }

    fn none() -> BTreeMap<String, String> {
        BTreeMap::new()
    }

    #[test]
    fn defaults_match_shifted_letters_by_base_key() {
        let (b, errors) = Bindings::new(&none(), None);
        assert!(errors.is_empty());
        let mods = ModifiersState::CONTROL | ModifiersState::SHIFT;
        assert_eq!(b.lookup(&ch("C"), &ch("c"), mods), Some(Action::Copy));
        assert_eq!(b.lookup(&ch("V"), &ch("v"), mods), Some(Action::Paste));
        // Plain Ctrl+C is the shell's interrupt, never a copy.
        assert_eq!(b.lookup(&ch("c"), &ch("c"), ModifiersState::CONTROL), None);
    }

    #[test]
    fn shifted_symbols_match_their_logical_key() {
        let (b, _) = Bindings::new(&none(), None);
        let mods = ModifiersState::CONTROL | ModifiersState::SHIFT;
        assert_eq!(
            b.lookup(&ch("+"), &ch("="), mods),
            Some(Action::FontIncrease)
        );
        assert_eq!(
            b.lookup(&ch("="), &ch("="), ModifiersState::CONTROL),
            Some(Action::FontIncrease)
        );
        let page_up = Key::Named(NamedKey::PageUp);
        assert_eq!(
            b.lookup(&page_up, &page_up, ModifiersState::SHIFT),
            Some(Action::ScrollPageUp)
        );
    }

    #[test]
    fn ghostty_split_and_tab_defaults() {
        let (b, _) = Bindings::new(&none(), None);
        let cs = ModifiersState::CONTROL | ModifiersState::SHIFT;
        assert_eq!(b.lookup(&ch("O"), &ch("o"), cs), Some(Action::Split(Right)));
        assert_eq!(b.lookup(&ch("E"), &ch("e"), cs), Some(Action::Split(Down)));
        assert_eq!(b.lookup(&ch("T"), &ch("t"), cs), Some(Action::NewTab));
        let tab = Key::Named(NamedKey::Tab);
        assert_eq!(
            b.lookup(&tab, &tab, ModifiersState::CONTROL),
            Some(Action::NextTab)
        );
        assert_eq!(b.lookup(&tab, &tab, cs), Some(Action::PreviousTab));
        assert_eq!(
            b.lookup(&ch("3"), &ch("3"), ModifiersState::ALT),
            Some(Action::GotoTab(3))
        );
        let left = Key::Named(NamedKey::ArrowLeft);
        assert_eq!(
            b.lookup(&left, &left, ModifiersState::CONTROL | ModifiersState::ALT),
            Some(Action::Focus(Left))
        );
    }

    #[test]
    fn omarchy_universal_copy_paste_keys_work() {
        let (b, _) = Bindings::new(&none(), None);
        let insert = Key::Named(NamedKey::Insert);
        assert_eq!(
            b.lookup(&insert, &insert, ModifiersState::CONTROL),
            Some(Action::Copy)
        );
        assert_eq!(
            b.lookup(&insert, &insert, ModifiersState::SHIFT),
            Some(Action::Paste)
        );
    }

    #[test]
    fn overrides_rebind_and_unbind() {
        let mut o = none();
        o.insert("ctrl+shift+c".to_string(), "none".to_string());
        o.insert("ctrl+alt+c".to_string(), "copy".to_string());
        o.insert("ctrl+q".to_string(), "bogus".to_string());
        o.insert("hyper+q".to_string(), "copy".to_string());
        let (b, errors) = Bindings::new(&o, None);
        assert_eq!(errors.len(), 2);
        let cs = ModifiersState::CONTROL | ModifiersState::SHIFT;
        assert_eq!(b.lookup(&ch("C"), &ch("c"), cs), None);
        let ca = ModifiersState::CONTROL | ModifiersState::ALT;
        assert_eq!(b.lookup(&ch("c"), &ch("c"), ca), Some(Action::Copy));
    }

    #[test]
    fn leader_bindings_follow_tmux() {
        let mut o = none();
        o.insert("leader+v".to_string(), "split_right".to_string());
        o.insert("leader+c".to_string(), "none".to_string());
        let (b, errors) = Bindings::new(&o, Some("ctrl+b"));
        assert!(errors.is_empty(), "{errors:?}");
        assert!(b.is_leader(&ch("b"), &ch("b"), ModifiersState::CONTROL));
        assert!(!b.is_leader(&ch("b"), &ch("b"), ModifiersState::empty()));
        // `%` is Shift+5.
        assert_eq!(
            b.lookup_leader(&ch("%"), &ch("5"), ModifiersState::SHIFT),
            Some(Action::Split(Right))
        );
        assert_eq!(
            b.lookup_leader(&ch("\""), &ch("'"), ModifiersState::SHIFT),
            Some(Action::Split(Down))
        );
        assert_eq!(
            b.lookup_leader(&ch("v"), &ch("v"), ModifiersState::empty()),
            Some(Action::Split(Right))
        );
        assert_eq!(
            b.lookup_leader(&ch("c"), &ch("c"), ModifiersState::empty()),
            None
        );
        assert_eq!(b.hint(Action::Split(Right)), "Ctrl+Shift+O");
        assert_eq!(b.hint(Action::Focus(Left)), "Ctrl+Alt+Left");
        assert!(b.describe().iter().any(|(c, _)| c == "ctrl+b, %"));
    }

    #[test]
    fn no_leader_means_no_leader_key() {
        let (b, _) = Bindings::new(&none(), None);
        assert!(!b.is_leader(&ch("b"), &ch("b"), ModifiersState::CONTROL));
        let (_, errors) = Bindings::new(&none(), Some("ctrl+nonsense"));
        assert_eq!(errors.len(), 1);
    }

    #[test]
    fn hints_are_readable() {
        let (b, _) = Bindings::new(&none(), None);
        assert_eq!(b.hint(Action::Copy), "Ctrl+Shift+C");
        assert_eq!(b.hint(Action::PastePrimary), "");
        assert_eq!(pretty("ctrl++"), "Ctrl++");
        assert_eq!(pretty("ctrl+shift+comma"), "Ctrl+Shift+,");
        assert_eq!(pretty("ctrl+super+shift+equal"), "Ctrl+Super+Shift+=");
    }

    #[test]
    fn parses_special_spellings() {
        assert_eq!(
            Combo::parse("ctrl++").unwrap(),
            Combo::parse("ctrl+plus").unwrap()
        );
        assert!(Combo::parse("alt+f5").is_ok());
        assert!(Combo::parse("ctrl+shift+comma").is_ok());
        assert!(Combo::parse("ctrl+f13").is_err());
        assert!(Combo::parse("ctrl+nonsense").is_err());
    }

    #[test]
    fn every_default_names_a_real_action() {
        for (combo, action) in DEFAULT_BINDINGS.iter().chain(DEFAULT_LEADER_BINDINGS) {
            assert!(Combo::parse(combo).is_ok(), "{combo}");
            assert!(action_by_name(action).is_some(), "{action}");
        }
    }

    #[test]
    fn default_combos_are_unique() {
        let mut seen = Vec::new();
        for (combo, _) in DEFAULT_BINDINGS {
            let parsed = Combo::parse(combo).unwrap();
            assert!(!seen.contains(&parsed), "{combo} bound twice");
            seen.push(parsed);
        }
    }
}