pinto-cli 0.1.0

A lightweight, local-first, Git-friendly Scrum backlog and Kanban board for the CLI and TUI
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
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
//! Configurable Kanban key names and platform-neutral key matching.

use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::fmt;

/// Actions that can be assigned one or more keys in `[tui.key_bindings]`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum KeyAction {
    /// Leave Kanban mode.
    Quit,
    /// Leave Kanban mode and hand control to the shell.
    Shell,
    /// Select the column to the left.
    SelectLeft,
    /// Select the column to the right.
    SelectRight,
    /// Select the row above.
    SelectUp,
    /// Select the row below.
    SelectDown,
    /// Move the selected item to the column on the left.
    MoveLeft,
    /// Move the selected item to the column on the right.
    MoveRight,
    /// Reorder the selected item upwards.
    ReorderUp,
    /// Reorder the selected item downwards.
    ReorderDown,
    /// Expand or collapse the selected parent item.
    ToggleExpand,
    /// Open the form for adding a new PBI.
    Add,
    /// Open the form for adding a dependency to the selected PBI.
    DependencyAdd,
    /// Open the form for removing a dependency from the selected PBI.
    DependencyRemove,
    /// Open the form for setting or clearing the selected PBI's parent.
    Parent,
    /// Edit the selected item.
    Edit,
    /// Reload the board.
    Reload,
    /// Toggle maximized-column mode.
    Maximize,
    /// Open substring search.
    Search,
    /// Open regular-expression search.
    RegexSearch,
    /// Open or close the selected item's details.
    Details,
    /// Open or close the Kanban help window.
    Help,
    /// Clear an active search filter.
    ClearFilter,
    /// Confirm leaving from the quit dialog.
    ConfirmQuit,
    /// Cancel leaving from the quit dialog.
    CancelQuit,
    /// Close the details popup.
    PopupClose,
    /// Scroll the details popup upwards.
    PopupScrollUp,
    /// Scroll the details popup downwards.
    PopupScrollDown,
    /// Select the row above while the details popup is open.
    PopupSelectUp,
    /// Select the row below while the details popup is open.
    PopupSelectDown,
    /// Select the column to the left while the details popup is open.
    PopupSelectLeft,
    /// Select the column to the right while the details popup is open.
    PopupSelectRight,
}

impl KeyAction {
    /// Every supported action, in the order used by the configuration documentation.
    pub const ALL: &'static [Self] = &[
        Self::Quit,
        Self::Shell,
        Self::SelectLeft,
        Self::SelectRight,
        Self::SelectUp,
        Self::SelectDown,
        Self::MoveLeft,
        Self::MoveRight,
        Self::ReorderUp,
        Self::ReorderDown,
        Self::ToggleExpand,
        Self::Add,
        Self::DependencyAdd,
        Self::DependencyRemove,
        Self::Parent,
        Self::Edit,
        Self::Reload,
        Self::Maximize,
        Self::Search,
        Self::RegexSearch,
        Self::Details,
        Self::Help,
        Self::ClearFilter,
        Self::ConfirmQuit,
        Self::CancelQuit,
        Self::PopupClose,
        Self::PopupScrollUp,
        Self::PopupScrollDown,
        Self::PopupSelectUp,
        Self::PopupSelectDown,
        Self::PopupSelectLeft,
        Self::PopupSelectRight,
    ];

    /// The TOML key used for this action.
    #[must_use]
    pub const fn name(self) -> &'static str {
        match self {
            Self::Quit => "quit",
            Self::Shell => "shell",
            Self::SelectLeft => "select_left",
            Self::SelectRight => "select_right",
            Self::SelectUp => "select_up",
            Self::SelectDown => "select_down",
            Self::MoveLeft => "move_left",
            Self::MoveRight => "move_right",
            Self::ReorderUp => "reorder_up",
            Self::ReorderDown => "reorder_down",
            Self::ToggleExpand => "toggle_expand",
            Self::Add => "add",
            Self::DependencyAdd => "dependency_add",
            Self::DependencyRemove => "dependency_remove",
            Self::Parent => "parent",
            Self::Edit => "edit",
            Self::Reload => "reload",
            Self::Maximize => "maximize",
            Self::Search => "search",
            Self::RegexSearch => "regex_search",
            Self::Details => "details",
            Self::Help => "help",
            Self::ClearFilter => "clear_filter",
            Self::ConfirmQuit => "confirm_quit",
            Self::CancelQuit => "cancel_quit",
            Self::PopupClose => "popup_close",
            Self::PopupScrollUp => "popup_scroll_up",
            Self::PopupScrollDown => "popup_scroll_down",
            Self::PopupSelectUp => "popup_select_up",
            Self::PopupSelectDown => "popup_select_down",
            Self::PopupSelectLeft => "popup_select_left",
            Self::PopupSelectRight => "popup_select_right",
        }
    }

    fn from_name(name: &str) -> Option<Self> {
        Self::ALL
            .iter()
            .copied()
            .find(|action| action.name() == name)
    }
}

/// Platform-neutral key codes used by the configuration parser and TUI adapter.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum KeyCode {
    /// A printable character.
    Char(char),
    /// Enter/Return.
    Enter,
    /// Escape.
    Esc,
    /// Tab.
    Tab,
    /// Backspace.
    Backspace,
    /// Delete.
    Delete,
    /// Insert.
    Insert,
    /// Left arrow.
    Left,
    /// Right arrow.
    Right,
    /// Up arrow.
    Up,
    /// Down arrow.
    Down,
    /// Home.
    Home,
    /// End.
    End,
    /// Page up.
    PageUp,
    /// Page down.
    PageDown,
    /// Function key.
    F(u8),
}

/// Modifier flags independent of the terminal backend.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct Modifiers(u8);

impl Modifiers {
    /// No modifiers.
    pub const NONE: Self = Self(0);
    /// Shift modifier for named/non-printable keys.
    pub const SHIFT: Self = Self(1 << 0);
    /// Control modifier.
    pub const CONTROL: Self = Self(1 << 1);
    /// Alt/Option modifier.
    pub const ALT: Self = Self(1 << 2);
    /// Super/Command/Windows modifier.
    pub const SUPER: Self = Self(1 << 3);
    /// Hyper modifier.
    pub const HYPER: Self = Self(1 << 4);
    /// Meta modifier.
    pub const META: Self = Self(1 << 5);

    /// Return whether all bits in `other` are present.
    #[must_use]
    pub const fn contains(self, other: Self) -> bool {
        self.0 & other.0 == other.0
    }

    const fn insert(self, other: Self) -> Self {
        Self(self.0 | other.0)
    }
}

impl std::ops::BitOr for Modifiers {
    type Output = Self;

    fn bitor(self, rhs: Self) -> Self::Output {
        self.insert(rhs)
    }
}

impl std::ops::BitOrAssign for Modifiers {
    fn bitor_assign(&mut self, rhs: Self) {
        *self = self.insert(rhs);
    }
}

/// A parsed key stroke.
#[derive(Debug, Clone)]
pub struct KeyStroke {
    code: KeyCode,
    modifiers: Modifiers,
    /// The spelling used for the on-screen guide.
    display: String,
}

impl PartialEq for KeyStroke {
    fn eq(&self, other: &Self) -> bool {
        self.code == other.code && self.modifiers == other.modifiers
    }
}

impl Eq for KeyStroke {}

impl KeyStroke {
    /// Parse a key expression such as `q`, `Esc`, or `Ctrl+a`.
    pub fn parse(input: &str) -> Result<Self, KeySpecError> {
        let input = input.trim();
        if input.is_empty() {
            return Err(KeySpecError::new(input, "the key name must not be empty"));
        }

        let parts: Vec<&str> = input.split('+').collect();
        if parts.iter().any(|part| part.trim().is_empty()) {
            return Err(KeySpecError::new(
                input,
                "use `Plus` for the plus key and separate modifiers with `+`",
            ));
        }

        let key_name = parts.last().copied().unwrap_or(input).trim();
        let code = parse_key_code(key_name).map_err(|reason| KeySpecError::new(input, reason))?;
        let mut modifiers = Modifiers::NONE;
        for modifier_name in &parts[..parts.len().saturating_sub(1)] {
            let modifier_name = modifier_name.trim();
            let modifier = parse_modifier(modifier_name).ok_or_else(|| {
                KeySpecError::new(
                    input,
                    format!(
                        "unknown modifier {modifier_name:?}; use Ctrl, Alt, Shift, Cmd, Meta, or Hyper"
                    ),
                )
            })?;
            modifiers |= modifier;
        }

        if modifiers.contains(Modifiers::SHIFT) && matches!(code, KeyCode::Char(_)) {
            return Err(KeySpecError::new(
                input,
                "write a Shift-modified printable key as its resulting character (use `A` instead of `Shift+a`, or `<` instead of `Shift+,`)",
            ));
        }

        let display = display_key_stroke(code, modifiers);

        Ok(Self {
            code,
            modifiers,
            display,
        })
    }

    /// Return the display spelling used in the footer key guide.
    #[must_use]
    pub fn display(&self) -> &str {
        &self.display
    }

    /// Compare an input event with this configured stroke.
    #[must_use]
    pub fn matches(&self, code: KeyCode, modifiers: Modifiers) -> bool {
        self.code == code && self.modifiers == modifiers
    }
}

/// A malformed key expression from the configuration file.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct KeySpecError {
    input: String,
    reason: String,
}

impl KeySpecError {
    fn new(input: &str, reason: impl Into<String>) -> Self {
        Self {
            input: input.to_string(),
            reason: reason.into(),
        }
    }
}

impl fmt::Display for KeySpecError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "invalid key {:?}: {}; supported key names include Esc, Enter, Space, arrows, and single characters",
            self.input, self.reason
        )
    }
}

impl std::error::Error for KeySpecError {}

/// Key assignments stored under `[tui.key_bindings]`.
///
/// Each action accepts one or more key expressions. Missing action entries are filled with the
/// defaults so older configuration files and partial overrides remain usable.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(transparent)]
pub struct KeyBindings(BTreeMap<String, Vec<String>>);

impl Default for KeyBindings {
    fn default() -> Self {
        let mut bindings = BTreeMap::new();
        for action in KeyAction::ALL {
            bindings.insert(
                action.name().to_string(),
                default_keys(*action)
                    .iter()
                    .map(|key| (*key).to_string())
                    .collect(),
            );
        }
        Self(bindings)
    }
}

impl KeyBindings {
    /// Return the configured key expressions for an action.
    pub fn keys(&self, action: KeyAction) -> &[String] {
        self.0.get(action.name()).map(Vec::as_slice).unwrap_or(&[])
    }

    /// Override one action. Validation is performed when the board configuration is loaded.
    pub fn set(&mut self, action: KeyAction, keys: Vec<String>) {
        self.0.insert(action.name().to_string(), keys);
    }

    /// Fill omitted actions with their existing defaults.
    #[must_use]
    pub fn with_defaults(mut self) -> Self {
        for action in KeyAction::ALL {
            self.0.entry(action.name().to_string()).or_insert_with(|| {
                default_keys(*action)
                    .iter()
                    .map(|key| (*key).to_string())
                    .collect()
            });
        }
        self
    }

    /// Validate action names, non-empty assignments, and every key expression.
    pub fn validate(&self) -> Result<(), KeyBindingError> {
        for (name, keys) in &self.0 {
            let Some(action) = KeyAction::from_name(name) else {
                return Err(KeyBindingError::UnknownAction {
                    action: name.clone(),
                });
            };
            if keys.is_empty() {
                return Err(KeyBindingError::Empty {
                    action: action.name().to_string(),
                });
            }
            for (index, key) in keys.iter().enumerate() {
                KeyStroke::parse(key).map_err(|source| KeyBindingError::InvalidKey {
                    action: action.name().to_string(),
                    index,
                    source,
                })?;
            }
        }
        Ok(())
    }
}

/// A malformed `[tui.key_bindings]` entry.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum KeyBindingError {
    /// The operation name is not supported.
    UnknownAction { action: String },
    /// An operation has no keys assigned.
    Empty { action: String },
    /// One key expression cannot be parsed.
    InvalidKey {
        action: String,
        index: usize,
        source: KeySpecError,
    },
}

impl fmt::Display for KeyBindingError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::UnknownAction { action } => write!(
                f,
                "unknown operation {action:?}; use one of: {}",
                KeyAction::ALL
                    .iter()
                    .map(|action| action.name())
                    .collect::<Vec<_>>()
                    .join(", ")
            ),
            Self::Empty { action } => write!(
                f,
                "operation {action:?} must have at least one key (remove the override to use its default)"
            ),
            Self::InvalidKey {
                action,
                index,
                source,
            } => write!(f, "operation {action:?} key #{index}: {source}"),
        }
    }
}

impl std::error::Error for KeyBindingError {}

fn default_keys(action: KeyAction) -> &'static [&'static str] {
    match action {
        KeyAction::Quit => &["q", "Esc"],
        KeyAction::Shell => &["Q"],
        KeyAction::SelectLeft => &["h", "Left"],
        KeyAction::SelectRight => &["l", "Right"],
        KeyAction::SelectUp => &["k", "Up"],
        KeyAction::SelectDown => &["j", "Down"],
        KeyAction::MoveLeft => &["H", "Shift+Left"],
        KeyAction::MoveRight => &["L", "Shift+Right"],
        KeyAction::ReorderUp => &["K", "Shift+Up"],
        KeyAction::ReorderDown => &["J", "Shift+Down"],
        KeyAction::ToggleExpand => &["Space", "Enter"],
        KeyAction::Add => &["a"],
        KeyAction::DependencyAdd => &["d"],
        KeyAction::DependencyRemove => &["D"],
        KeyAction::Parent => &["p"],
        KeyAction::Edit => &["e"],
        KeyAction::Reload => &["r"],
        KeyAction::Maximize => &["m"],
        KeyAction::Search => &["/"],
        KeyAction::RegexSearch => &["Ctrl+?"],
        KeyAction::Details => &["v"],
        KeyAction::Help => &["?"],
        KeyAction::ClearFilter => &["Esc"],
        KeyAction::ConfirmQuit => &["y", "Y", "q", "Q", "Enter", "Esc"],
        KeyAction::CancelQuit => &["n", "N"],
        KeyAction::PopupClose => &["v", "Esc", "q"],
        KeyAction::PopupScrollUp => &["k", "Up"],
        KeyAction::PopupScrollDown => &["j", "Down"],
        KeyAction::PopupSelectUp => &["K", "Shift+Up"],
        KeyAction::PopupSelectDown => &["J", "Shift+Down"],
        KeyAction::PopupSelectLeft => &["H", "Shift+Left"],
        KeyAction::PopupSelectRight => &["L", "Shift+Right"],
    }
}

fn parse_modifier(input: &str) -> Option<Modifiers> {
    let modifier = match input.to_ascii_lowercase().as_str() {
        "shift" => Modifiers::SHIFT,
        "ctrl" | "control" => Modifiers::CONTROL,
        "alt" | "option" => Modifiers::ALT,
        "cmd" | "command" | "super" | "windows" | "win" => Modifiers::SUPER,
        "meta" => Modifiers::META,
        "hyper" => Modifiers::HYPER,
        _ => return None,
    };
    Some(modifier)
}

fn parse_key_code(input: &str) -> Result<KeyCode, String> {
    let normalized = input.to_ascii_lowercase();
    let code = match normalized.as_str() {
        "esc" | "escape" => KeyCode::Esc,
        "enter" | "return" => KeyCode::Enter,
        "space" => KeyCode::Char(' '),
        "tab" => KeyCode::Tab,
        "backspace" | "back" => KeyCode::Backspace,
        "delete" | "del" => KeyCode::Delete,
        "insert" | "ins" => KeyCode::Insert,
        "left" => KeyCode::Left,
        "right" => KeyCode::Right,
        "up" => KeyCode::Up,
        "down" => KeyCode::Down,
        "home" => KeyCode::Home,
        "end" => KeyCode::End,
        "pageup" | "page_up" | "pgup" => KeyCode::PageUp,
        "pagedown" | "page_down" | "pgdn" => KeyCode::PageDown,
        "plus" => KeyCode::Char('+'),
        _ if normalized.starts_with('f') && normalized[1..].parse::<u8>().is_ok() => {
            let number = normalized[1..]
                .parse::<u8>()
                .map_err(|_| "function key number is invalid".to_string())?;
            if (1..=24).contains(&number) {
                KeyCode::F(number)
            } else {
                return Err("function keys must be between F1 and F24".to_string());
            }
        }
        _ => {
            let mut chars = input.chars();
            let Some(character) = chars.next() else {
                return Err("the key name must not be empty".to_string());
            };
            if chars.next().is_some() {
                return Err(format!(
                    "unknown key {input:?}; use a named key, a single character, or F1-F24"
                ));
            }
            KeyCode::Char(character)
        }
    };
    Ok(code)
}

fn display_key_stroke(code: KeyCode, modifiers: Modifiers) -> String {
    let mut names = Vec::with_capacity(6);
    if modifiers.contains(Modifiers::CONTROL) {
        names.push("Ctrl");
    }
    if modifiers.contains(Modifiers::ALT) {
        names.push("Alt");
    }
    if modifiers.contains(Modifiers::SUPER) {
        names.push("Cmd");
    }
    if modifiers.contains(Modifiers::META) {
        names.push("Meta");
    }
    if modifiers.contains(Modifiers::HYPER) {
        names.push("Hyper");
    }
    if modifiers.contains(Modifiers::SHIFT) {
        names.push("Shift");
    }
    let key = display_key_code(code);
    if names.is_empty() {
        key
    } else {
        format!("{}+{key}", names.join("+"))
    }
}

fn display_key_code(code: KeyCode) -> String {
    match code {
        KeyCode::Char(' ') => "Space".to_string(),
        KeyCode::Char('+') => "Plus".to_string(),
        KeyCode::Char(character) => character.to_string(),
        KeyCode::Enter => "Enter".to_string(),
        KeyCode::Esc => "Esc".to_string(),
        KeyCode::Tab => "Tab".to_string(),
        KeyCode::Backspace => "Backspace".to_string(),
        KeyCode::Delete => "Delete".to_string(),
        KeyCode::Insert => "Insert".to_string(),
        KeyCode::Left => "Left".to_string(),
        KeyCode::Right => "Right".to_string(),
        KeyCode::Up => "Up".to_string(),
        KeyCode::Down => "Down".to_string(),
        KeyCode::Home => "Home".to_string(),
        KeyCode::End => "End".to_string(),
        KeyCode::PageUp => "PageUp".to_string(),
        KeyCode::PageDown => "PageDown".to_string(),
        KeyCode::F(number) => format!("F{number}"),
    }
}

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

    #[test]
    fn control_and_platform_modifiers_are_parsed_and_displayed() {
        let control = KeyStroke::parse("Ctrl+a").expect("control key is valid");
        assert!(control.matches(KeyCode::Char('a'), Modifiers::CONTROL));
        assert_eq!(control.display(), "Ctrl+a");

        let command = KeyStroke::parse("Cmd+q").expect("command key is valid");
        assert!(command.matches(KeyCode::Char('q'), Modifiers::SUPER));
        assert_eq!(command.display(), "Cmd+q");
    }

    #[test]
    fn default_bindings_keep_multiple_existing_keys() {
        let bindings = KeyBindings::default();

        assert_eq!(
            bindings.keys(KeyAction::Quit),
            ["q".to_string(), "Esc".to_string()]
        );
        assert_eq!(
            bindings.keys(KeyAction::SelectLeft),
            ["h".to_string(), "Left".to_string()]
        );
        assert!(
            KeyAction::ALL
                .iter()
                .all(|action| !bindings.keys(*action).is_empty())
        );
    }

    #[test]
    fn invalid_key_spec_explains_the_supported_syntax() {
        let error = KeyStroke::parse("Controlled+a")
            .expect_err("unknown modifier must be rejected")
            .to_string();

        assert!(error.contains("Ctrl"));
        assert!(error.contains("Alt"));
        assert!(error.contains("Cmd"));
    }

    #[test]
    fn parses_named_keys_aliases_and_function_keys() {
        let cases = [
            ("Esc", KeyCode::Esc),
            ("Escape", KeyCode::Esc),
            ("Enter", KeyCode::Enter),
            ("Return", KeyCode::Enter),
            ("Space", KeyCode::Char(' ')),
            ("Tab", KeyCode::Tab),
            ("Backspace", KeyCode::Backspace),
            ("Back", KeyCode::Backspace),
            ("Delete", KeyCode::Delete),
            ("Del", KeyCode::Delete),
            ("Insert", KeyCode::Insert),
            ("Ins", KeyCode::Insert),
            ("Left", KeyCode::Left),
            ("Right", KeyCode::Right),
            ("Up", KeyCode::Up),
            ("Down", KeyCode::Down),
            ("Home", KeyCode::Home),
            ("End", KeyCode::End),
            ("PageUp", KeyCode::PageUp),
            ("page_up", KeyCode::PageUp),
            ("PgUp", KeyCode::PageUp),
            ("PageDown", KeyCode::PageDown),
            ("page_down", KeyCode::PageDown),
            ("PgDn", KeyCode::PageDown),
            ("Plus", KeyCode::Char('+')),
            ("F1", KeyCode::F(1)),
            ("F24", KeyCode::F(24)),
        ];

        for (input, expected) in cases {
            let stroke = KeyStroke::parse(input).expect("named key is valid");
            assert!(stroke.matches(expected, Modifiers::NONE), "input: {input}");
        }
        assert_eq!(
            KeyStroke::parse("Space").expect("space key").display(),
            "Space"
        );
        assert_eq!(
            KeyStroke::parse("Plus").expect("plus key").display(),
            "Plus"
        );
        assert_eq!(KeyStroke::parse(".").expect("character key").display(), ".");
        assert_eq!(
            KeyStroke::parse("F1").expect("function key").display(),
            "F1"
        );
    }

    #[test]
    fn parses_modifier_aliases_and_displays_combined_modifiers() {
        let aliases = [
            ("Shift+Left", Modifiers::SHIFT),
            ("Ctrl+Left", Modifiers::CONTROL),
            ("Control+Left", Modifiers::CONTROL),
            ("Alt+Left", Modifiers::ALT),
            ("Option+Left", Modifiers::ALT),
            ("Cmd+Left", Modifiers::SUPER),
            ("Command+Left", Modifiers::SUPER),
            ("Super+Left", Modifiers::SUPER),
            ("Windows+Left", Modifiers::SUPER),
            ("Win+Left", Modifiers::SUPER),
            ("Meta+Left", Modifiers::META),
            ("Hyper+Left", Modifiers::HYPER),
        ];

        for (input, expected) in aliases {
            let stroke = KeyStroke::parse(input).expect("modifier alias is valid");
            assert!(stroke.matches(KeyCode::Left, expected), "input: {input}");
        }

        let combined = KeyStroke::parse("Ctrl+Alt+Cmd+Meta+Hyper+Shift+PageDown")
            .expect("combined modifiers are valid");
        assert_eq!(combined.display(), "Ctrl+Alt+Cmd+Meta+Hyper+Shift+PageDown");
        assert!(combined.matches(
            KeyCode::PageDown,
            Modifiers::CONTROL
                | Modifiers::ALT
                | Modifiers::SUPER
                | Modifiers::META
                | Modifiers::HYPER
                | Modifiers::SHIFT
        ));
    }

    #[test]
    fn rejects_malformed_key_expressions() {
        for input in [
            "",
            "+",
            "Ctrl+",
            "Unknown+a",
            "Shift+a",
            "F0",
            "F25",
            "Fabc",
            "left right",
        ] {
            assert!(KeyStroke::parse(input).is_err(), "input: {input:?}");
        }
        assert!(
            KeyStroke::parse("Ctrl+")
                .expect_err("missing key")
                .to_string()
                .contains("Plus")
        );
        assert!(
            KeyStroke::parse("F25")
                .expect_err("function key range")
                .to_string()
                .contains("F1 and F24")
        );
    }

    #[test]
    fn key_bindings_fill_defaults_and_report_validation_errors() {
        let empty = KeyBindings(BTreeMap::new());
        assert!(empty.keys(KeyAction::Quit).is_empty());
        let completed = empty.with_defaults();
        assert!(completed.validate().is_ok());
        assert_eq!(completed.keys(KeyAction::Quit), ["q", "Esc"]);

        let mut unknown = KeyBindings(BTreeMap::new());
        unknown
            .0
            .insert("unknown".to_string(), vec!["q".to_string()]);
        let error = unknown.validate().expect_err("unknown action must fail");
        assert!(matches!(error, KeyBindingError::UnknownAction { .. }));
        assert!(error.to_string().contains("unknown operation"));

        let mut empty_action = KeyBindings(BTreeMap::new());
        empty_action.0.insert("quit".to_string(), Vec::new());
        let error = empty_action
            .validate()
            .expect_err("empty assignment must fail");
        assert!(matches!(error, KeyBindingError::Empty { .. }));
        assert!(error.to_string().contains("at least one key"));

        let mut invalid = KeyBindings(BTreeMap::new());
        invalid
            .0
            .insert("quit".to_string(), vec!["not a key".to_string()]);
        let error = invalid.validate().expect_err("invalid key must fail");
        assert!(matches!(
            error,
            KeyBindingError::InvalidKey { index: 0, .. }
        ));
        assert!(error.to_string().contains("key #0"));
    }
}