vig 0.14.0

Read-only TUI cockpit for busy repositories - git, GitHub PRs/CI/projects, containers and processes at a glance
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
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use std::collections::HashMap;
use std::fmt;
use std::str::FromStr;

/// Normalized key input for use as HashMap key.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct KeyInput {
    pub code: KeyCode,
    pub modifiers: KeyModifiers,
}

impl From<KeyEvent> for KeyInput {
    fn from(key: KeyEvent) -> Self {
        // SHIFT is redundant for printable chars (the char itself already
        // encodes the shifted form, e.g. 'G' or '?') and for BackTab (which is
        // Shift+Tab by definition). crossterm reports it inconsistently across
        // terminals, so strip it to keep the same logical key matchable.
        let mods = if matches!(key.code, KeyCode::Char(_) | KeyCode::BackTab) {
            key.modifiers & !KeyModifiers::SHIFT
        } else {
            key.modifiers
        };
        Self {
            code: key.code,
            modifiers: mods,
        }
    }
}

impl fmt::Display for KeyInput {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let key = match self.code {
            KeyCode::Char(' ') => "Space".to_string(),
            KeyCode::Char(c) => c.to_string(),
            KeyCode::Enter => "Enter".to_string(),
            KeyCode::Esc => "Esc".to_string(),
            KeyCode::Tab => "Tab".to_string(),
            KeyCode::BackTab => "S-Tab".to_string(),
            KeyCode::Up => "".to_string(),
            KeyCode::Down => "".to_string(),
            KeyCode::Left => "".to_string(),
            KeyCode::Right => "".to_string(),
            _ => format!("{:?}", self.code),
        };
        if self.modifiers.contains(KeyModifiers::CONTROL) {
            write!(f, "Ctrl+{key}")
        } else {
            write!(f, "{key}")
        }
    }
}

/// Parse a key name string into a KeyCode.
fn parse_key_code(name: &str) -> Result<KeyCode, String> {
    match name {
        "Enter" | "Return" | "CR" => Ok(KeyCode::Enter),
        "Esc" | "Escape" => Ok(KeyCode::Esc),
        "Tab" => Ok(KeyCode::Tab),
        "BackTab" | "S-Tab" => Ok(KeyCode::BackTab),
        "Backspace" | "BS" => Ok(KeyCode::Backspace),
        "Delete" | "Del" => Ok(KeyCode::Delete),
        "Up" => Ok(KeyCode::Up),
        "Down" => Ok(KeyCode::Down),
        "Left" => Ok(KeyCode::Left),
        "Right" => Ok(KeyCode::Right),
        "Home" => Ok(KeyCode::Home),
        "End" => Ok(KeyCode::End),
        "PageUp" => Ok(KeyCode::PageUp),
        "PageDown" => Ok(KeyCode::PageDown),
        "Space" => Ok(KeyCode::Char(' ')),
        s if s.len() == 1 => Ok(KeyCode::Char(s.chars().next().unwrap())),
        _ => Err(format!("Unknown key: {name}")),
    }
}

impl FromStr for KeyInput {
    type Err = String;

    /// Parse a human-readable key string into a KeyInput.
    ///
    /// Supported formats:
    /// - `"j"`, `"G"`, `"/"` — plain character keys
    /// - `"Enter"`, `"Esc"`, `"Tab"`, `"BackTab"`, `"Up"`, `"Down"` etc.
    /// - `"Ctrl+d"`, `"Ctrl+u"` — with Ctrl modifier
    /// - `"Space"` — space character
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let s = s.trim();
        if s.is_empty() {
            return Err("Empty key string".to_string());
        }

        if let Some(rest) = s.strip_prefix("Ctrl+") {
            let code = parse_key_code(rest)?;
            Ok(Self {
                code,
                modifiers: KeyModifiers::CONTROL,
            })
        } else {
            let code = parse_key_code(s)?;
            Ok(Self {
                code,
                modifiers: KeyModifiers::NONE,
            })
        }
    }
}

/// Trait for action enums that can provide help descriptions.
/// Return `None` to hide the action from help.
pub trait ActionHelp {
    fn label(&self) -> Option<&'static str>;
}

/// A single help entry: key display string + description.
pub type HelpEntry = (String, String);

/// Key-to-action mapping.
pub struct Keymap<A: Clone> {
    bindings: HashMap<KeyInput, A>,
    order: Vec<KeyInput>,
}

impl<A: Clone> Keymap<A> {
    pub fn new() -> Self {
        Self {
            bindings: HashMap::new(),
            order: Vec::new(),
        }
    }

    /// Bind a key with modifiers to an action (builder pattern).
    pub fn bind(mut self, code: KeyCode, mods: KeyModifiers, action: A) -> Self {
        let ki = KeyInput {
            code,
            modifiers: mods,
        };
        if !self.bindings.contains_key(&ki) {
            self.order.push(ki);
        }
        self.bindings.insert(ki, action);
        self
    }

    /// Remove the binding for a key, if any.
    pub fn unbind(mut self, code: KeyCode, mods: KeyModifiers) -> Self {
        let ki = KeyInput {
            code,
            modifiers: mods,
        };
        if self.bindings.remove(&ki).is_some() {
            self.order.retain(|k| *k != ki);
        }
        self
    }

    /// Bind a plain key (no modifiers) to an action.
    pub fn key(self, code: KeyCode, action: A) -> Self {
        self.bind(code, KeyModifiers::NONE, action)
    }

    /// Bind Ctrl+c to an action.
    pub fn ctrl(self, c: char, action: A) -> Self {
        self.bind(KeyCode::Char(c), KeyModifiers::CONTROL, action)
    }

    /// Add multiple bindings at once.
    pub fn bindings(mut self, entries: Vec<(KeyCode, KeyModifiers, A)>) -> Self {
        for (code, mods, action) in entries {
            let ki = KeyInput {
                code,
                modifiers: mods,
            };
            if !self.bindings.contains_key(&ki) {
                self.order.push(ki);
            }
            self.bindings.insert(ki, action);
        }
        self
    }

    /// Look up an action for a key event.
    pub fn lookup(&self, key: KeyEvent) -> Option<&A> {
        self.bindings.get(&KeyInput::from(key))
    }

    /// All bindings in insertion order (a rebound key keeps its original
    /// position; an unbound-then-rebound key moves to the end).
    pub fn entries(&self) -> impl Iterator<Item = (&KeyInput, &A)> {
        self.order
            .iter()
            .filter_map(|ki| self.bindings.get(ki).map(|a| (ki, a)))
    }
}

impl<A: Clone + ActionHelp> Keymap<A> {
    /// Generate help entries by grouping keys that map to the same label.
    /// Keys are joined with " / " (e.g., "j / ↓" → "Next item").
    /// Actions returning `None` from `label()` are excluded.
    pub fn help_entries(&self) -> Vec<HelpEntry> {
        let mut groups: Vec<(&'static str, Vec<&KeyInput>)> = Vec::new();

        for ki in &self.order {
            if let Some(action) = self.bindings.get(ki) {
                if let Some(label) = action.label() {
                    if let Some(group) = groups.iter_mut().find(|(l, _)| *l == label) {
                        group.1.push(ki);
                    } else {
                        groups.push((label, vec![ki]));
                    }
                }
            }
        }

        groups
            .into_iter()
            .map(|(label, keys)| {
                let key_str = keys
                    .iter()
                    .map(|k| k.to_string())
                    .collect::<Vec<_>>()
                    .join(" / ");
                (key_str, label.to_string())
            })
            .collect()
    }
}

/// Implement `FromStr` for simple action enums (no nested variants).
macro_rules! impl_action_from_str {
    ($ty:ty, $( $variant:ident ),+ $(,)?) => {
        impl std::str::FromStr for $ty {
            type Err = String;
            fn from_str(s: &str) -> Result<Self, Self::Err> {
                match s {
                    $( stringify!($variant) => Ok(Self::$variant), )+
                    _ => Err(format!("Unknown action: {s}")),
                }
            }
        }
    };
}

/// Implement `FromStr` for pane action enums that wrap `Nav(NavAction)`, `Search(SearchAction)`,
/// and have their own plain variants. Nested actions use dot notation: `"Nav.MoveDown"`, `"Search.Start"`.
///
/// When the `search:` + `esc:` variant is provided, also generates `HasSearchEsc` impl.
#[macro_export]
macro_rules! impl_pane_action_from_str {
    ($ty:ty, nav: $nav_wrap:ident, search: $search_wrap:ident, esc: $esc_variant:ident, $( $variant:ident ),+ $(,)?) => {
        // FromStr
        impl std::str::FromStr for $ty {
            type Err = String;
            fn from_str(s: &str) -> Result<Self, Self::Err> {
                if let Some(rest) = s.strip_prefix("Nav.") {
                    return rest.parse::<$crate::core::keymap::NavAction>()
                        .map(Self::$nav_wrap);
                }
                if let Some(rest) = s.strip_prefix("Search.") {
                    return rest.parse::<$crate::core::keymap::SearchAction>()
                        .map(Self::$search_wrap);
                }
                match s {
                    stringify!($esc_variant) => Ok(Self::$esc_variant),
                    $( stringify!($variant) => Ok(Self::$variant), )+
                    _ => Err(format!("Unknown action: {s}")),
                }
            }
        }
        // HasSearchEsc
        impl $crate::core::pane::HasSearchEsc for $ty {
            fn as_search(&self) -> Option<&$crate::core::keymap::SearchAction> {
                match self {
                    Self::$search_wrap(sa) => Some(sa),
                    _ => None,
                }
            }
            fn is_esc(&self) -> bool {
                matches!(self, Self::$esc_variant)
            }
        }
    };
    ($ty:ty, nav: $nav_wrap:ident, search: $search_wrap:ident, $( $variant:ident ),+ $(,)?) => {
        impl std::str::FromStr for $ty {
            type Err = String;
            fn from_str(s: &str) -> Result<Self, Self::Err> {
                if let Some(rest) = s.strip_prefix("Nav.") {
                    return rest.parse::<$crate::core::keymap::NavAction>()
                        .map(Self::$nav_wrap);
                }
                if let Some(rest) = s.strip_prefix("Search.") {
                    return rest.parse::<$crate::core::keymap::SearchAction>()
                        .map(Self::$search_wrap);
                }
                match s {
                    $( stringify!($variant) => Ok(Self::$variant), )+
                    _ => Err(format!("Unknown action: {s}")),
                }
            }
        }
    };
    ($ty:ty, nav: $nav_wrap:ident, $( $variant:ident ),+ $(,)?) => {
        impl std::str::FromStr for $ty {
            type Err = String;
            fn from_str(s: &str) -> Result<Self, Self::Err> {
                if let Some(rest) = s.strip_prefix("Nav.") {
                    return rest.parse::<$crate::core::keymap::NavAction>()
                        .map(Self::$nav_wrap);
                }
                match s {
                    $( stringify!($variant) => Ok(Self::$variant), )+
                    _ => Err(format!("Unknown action: {s}")),
                }
            }
        }
    };
}

/// Common navigation actions shared across list panes.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NavAction {
    MoveDown,
    MoveUp,
    HalfPageDown,
    HalfPageUp,
    JumpTop,
    JumpBottom,
}

impl_action_from_str!(
    NavAction,
    MoveDown,
    MoveUp,
    HalfPageDown,
    HalfPageUp,
    JumpTop,
    JumpBottom
);

impl ActionHelp for NavAction {
    fn label(&self) -> Option<&'static str> {
        Some(match self {
            NavAction::MoveDown => "Next item",
            NavAction::MoveUp => "Prev item",
            NavAction::HalfPageDown => "Half page down",
            NavAction::HalfPageUp => "Half page up",
            NavAction::JumpTop => "Top",
            NavAction::JumpBottom => "Bottom",
        })
    }
}

/// Generate default navigation key bindings, wrapping each NavAction with the given function.
pub fn nav_bindings<A: Clone>(wrap: impl Fn(NavAction) -> A) -> Vec<(KeyCode, KeyModifiers, A)> {
    vec![
        (
            KeyCode::Char('j'),
            KeyModifiers::NONE,
            wrap(NavAction::MoveDown),
        ),
        (KeyCode::Down, KeyModifiers::NONE, wrap(NavAction::MoveDown)),
        (
            KeyCode::Char('k'),
            KeyModifiers::NONE,
            wrap(NavAction::MoveUp),
        ),
        (KeyCode::Up, KeyModifiers::NONE, wrap(NavAction::MoveUp)),
        (
            KeyCode::Char('d'),
            KeyModifiers::CONTROL,
            wrap(NavAction::HalfPageDown),
        ),
        (
            KeyCode::Char('u'),
            KeyModifiers::CONTROL,
            wrap(NavAction::HalfPageUp),
        ),
        (
            KeyCode::Char('g'),
            KeyModifiers::NONE,
            wrap(NavAction::JumpTop),
        ),
        (
            KeyCode::Char('G'),
            KeyModifiers::NONE,
            wrap(NavAction::JumpBottom),
        ),
    ]
}

/// Common search actions shared across panes.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SearchAction {
    Start,
    Next,
    Prev,
}

impl_action_from_str!(SearchAction, Start, Next, Prev);

impl ActionHelp for SearchAction {
    fn label(&self) -> Option<&'static str> {
        Some(match self {
            SearchAction::Start => "Search",
            SearchAction::Next => "Next match",
            SearchAction::Prev => "Prev match",
        })
    }
}

/// Generate search key bindings (/, n, N), wrapping each SearchAction with the given function.
pub fn search_bindings<A: Clone>(
    wrap: impl Fn(SearchAction) -> A,
) -> Vec<(KeyCode, KeyModifiers, A)> {
    vec![
        (
            KeyCode::Char('/'),
            KeyModifiers::NONE,
            wrap(SearchAction::Start),
        ),
        (
            KeyCode::Char('n'),
            KeyModifiers::NONE,
            wrap(SearchAction::Next),
        ),
        (
            KeyCode::Char('N'),
            KeyModifiers::NONE,
            wrap(SearchAction::Prev),
        ),
    ]
}

/// Page-level view actions (quit, help, refresh, tab navigation, etc.).
/// Each page maps these to its own concrete behavior.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ViewAction {
    Quit,
    Help,
    Refresh,
    OpenEditor,
    OpenDefault,
    OpenWith,
    PrevTab,
    NextTab,
    CyclePaneForward,
    CyclePaneBackward,
    /// Projects page: show the next / previous linked project's board.
    NextProject,
    PrevProject,
    /// Files page: toggle Markdown rendering in the preview.
    ToggleMarkdown,
}

impl_action_from_str!(
    ViewAction,
    Quit,
    Help,
    Refresh,
    OpenEditor,
    OpenDefault,
    OpenWith,
    PrevTab,
    NextTab,
    CyclePaneForward,
    CyclePaneBackward,
    NextProject,
    PrevProject,
    ToggleMarkdown
);

impl ActionHelp for ViewAction {
    fn label(&self) -> Option<&'static str> {
        Some(match self {
            ViewAction::Quit => "Quit",
            ViewAction::Help => "Toggle help",
            ViewAction::Refresh => "Refresh",
            ViewAction::OpenEditor => "Open in $EDITOR",
            ViewAction::OpenDefault => "Open with default app",
            ViewAction::OpenWith => "Open with...",
            ViewAction::PrevTab => "Prev tab",
            ViewAction::NextTab => "Next tab",
            ViewAction::CyclePaneForward => "Next pane",
            ViewAction::CyclePaneBackward => "Prev pane",
            ViewAction::NextProject => "Next linked project",
            ViewAction::PrevProject => "Prev linked project",
            ViewAction::ToggleMarkdown => "Toggle markdown rendering",
        })
    }
}

/// Generate default view-level key bindings. Kept for regression tests.
#[allow(dead_code)]
pub fn view_bindings<A: Clone>(wrap: impl Fn(ViewAction) -> A) -> Vec<(KeyCode, KeyModifiers, A)> {
    vec![
        (
            KeyCode::Char('q'),
            KeyModifiers::NONE,
            wrap(ViewAction::Quit),
        ),
        (
            KeyCode::Char('?'),
            KeyModifiers::NONE,
            wrap(ViewAction::Help),
        ),
        (
            KeyCode::Char('r'),
            KeyModifiers::NONE,
            wrap(ViewAction::Refresh),
        ),
        (
            KeyCode::Char('h'),
            KeyModifiers::NONE,
            wrap(ViewAction::PrevTab),
        ),
        (
            KeyCode::Char('l'),
            KeyModifiers::NONE,
            wrap(ViewAction::NextTab),
        ),
        (
            KeyCode::Tab,
            KeyModifiers::NONE,
            wrap(ViewAction::CyclePaneForward),
        ),
        (
            KeyCode::BackTab,
            KeyModifiers::NONE,
            wrap(ViewAction::CyclePaneBackward),
        ),
    ]
}

/// Build a section header for help display.
pub fn help_section(title: &str) -> Vec<HelpEntry> {
    vec![
        (String::new(), String::new()),
        (String::new(), format!("── {title} ──")),
    ]
}

/// Number of rows a half-page navigation should move, given the viewport
/// height. Always at least 1 so a half-page step is never a no-op on very
/// short viewports.
pub fn half_page_step(view_height: u16) -> u16 {
    (view_height / 2).max(1)
}

/// Reserved action name that removes a key binding instead of adding one.
pub const UNBIND_ACTION: &str = "None";

/// Application-level actions for global key bindings (Ctrl+c, page switching).
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AppAction {
    /// Quit the application.
    Quit,
    /// Switch to the page at the given 0-indexed position.
    SwitchPage(usize),
}

impl FromStr for AppAction {
    type Err = String;

    /// Parse action strings for `AppAction`.
    ///
    /// - `"Quit"` → `AppAction::Quit`
    /// - `"SwitchPage:<n>"` → `AppAction::SwitchPage(n)`
    ///
    /// Note: `"page:<name>"` strings from the KDL config are **not** handled here.
    /// `build_app_keymap` resolves page names to indices and constructs
    /// `AppAction::SwitchPage` directly, without going through `FromStr`.
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if s == "Quit" {
            return Ok(Self::Quit);
        }
        if let Some(idx_str) = s.strip_prefix("SwitchPage:") {
            let idx: usize = idx_str
                .parse()
                .map_err(|_| format!("Invalid page index in SwitchPage: {idx_str:?}"))?;
            return Ok(Self::SwitchPage(idx));
        }
        Err(format!("Unknown AppAction: {s:?}"))
    }
}

/// Build an app-level keymap from `(key_str, action_str)` pairs.
///
/// Page-name strings like `"page:git"` are resolved to indices using `page_names`.
/// `"None"` removes the binding for that key (used by user configs to unbind).
/// Returns `Err` on the first invalid key, unknown page name, or invalid action.
pub fn build_app_keymap(
    entries: &[(String, String)],
    page_names: &[&str],
) -> Result<Keymap<AppAction>, String> {
    let mut km = Keymap::new();
    for (key_str, action_str) in entries {
        let ki = key_str
            .parse::<KeyInput>()
            .map_err(|e| format!("invalid app key {key_str:?}: {e}"))?;
        if action_str == UNBIND_ACTION {
            km = km.unbind(ki.code, ki.modifiers);
            continue;
        }
        let action = if action_str == "Quit" {
            AppAction::Quit
        } else if let Some(name) = action_str.strip_prefix("page:") {
            let idx = page_names
                .iter()
                .position(|p| *p == name)
                .ok_or_else(|| format!("unknown page name in app keymap: {name:?}"))?;
            AppAction::SwitchPage(idx)
        } else {
            action_str
                .parse::<AppAction>()
                .map_err(|e| format!("invalid app action {action_str:?}: {e}"))?
        };
        km = km.bind(ki.code, ki.modifiers, action);
    }
    Ok(km)
}

/// Execute a navigation action on a list, updating `selected_idx`.
/// Returns `true` if the selection actually changed.
pub fn execute_nav(
    nav: NavAction,
    selected_idx: &mut usize,
    item_count: usize,
    view_height: Option<u16>,
) -> bool {
    if item_count == 0 {
        return false;
    }
    let old = *selected_idx;
    match nav {
        NavAction::MoveDown => {
            if *selected_idx + 1 < item_count {
                *selected_idx += 1;
            }
        }
        NavAction::MoveUp => {
            if *selected_idx > 0 {
                *selected_idx -= 1;
            }
        }
        NavAction::HalfPageDown => {
            let half = half_page_step(view_height.unwrap_or(20)) as usize;
            *selected_idx = (*selected_idx + half).min(item_count - 1);
        }
        NavAction::HalfPageUp => {
            let half = half_page_step(view_height.unwrap_or(20)) as usize;
            *selected_idx = selected_idx.saturating_sub(half);
        }
        NavAction::JumpTop => {
            *selected_idx = 0;
        }
        NavAction::JumpBottom => {
            *selected_idx = item_count - 1;
        }
    }
    *selected_idx != old
}

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

    #[test]
    fn parse_plain_char() {
        let ki: KeyInput = "j".parse().unwrap();
        assert_eq!(ki.code, KeyCode::Char('j'));
        assert_eq!(ki.modifiers, KeyModifiers::NONE);
    }

    #[test]
    fn parse_uppercase_char() {
        let ki: KeyInput = "G".parse().unwrap();
        assert_eq!(ki.code, KeyCode::Char('G'));
        assert_eq!(ki.modifiers, KeyModifiers::NONE);
    }

    #[test]
    fn parse_ctrl_modifier() {
        let ki: KeyInput = "Ctrl+d".parse().unwrap();
        assert_eq!(ki.code, KeyCode::Char('d'));
        assert_eq!(ki.modifiers, KeyModifiers::CONTROL);
    }

    #[test]
    fn parse_special_keys() {
        assert_eq!("Enter".parse::<KeyInput>().unwrap().code, KeyCode::Enter);
        assert_eq!("Esc".parse::<KeyInput>().unwrap().code, KeyCode::Esc);
        assert_eq!("Tab".parse::<KeyInput>().unwrap().code, KeyCode::Tab);
        assert_eq!(
            "BackTab".parse::<KeyInput>().unwrap().code,
            KeyCode::BackTab
        );
        assert_eq!("Up".parse::<KeyInput>().unwrap().code, KeyCode::Up);
        assert_eq!("Down".parse::<KeyInput>().unwrap().code, KeyCode::Down);
        assert_eq!(
            "Space".parse::<KeyInput>().unwrap().code,
            KeyCode::Char(' ')
        );
    }

    #[test]
    fn parse_roundtrip() {
        for s in &["j", "G", "Enter", "Esc", "Tab"] {
            let ki: KeyInput = s.parse().unwrap();
            assert_eq!(ki.to_string(), *s);
        }
        let ctrl_d: KeyInput = "Ctrl+d".parse().unwrap();
        assert_eq!(ctrl_d.to_string(), "Ctrl+d");
    }

    #[test]
    fn parse_invalid() {
        assert!("".parse::<KeyInput>().is_err());
        assert!("FooBar".parse::<KeyInput>().is_err());
    }

    #[test]
    fn parse_nav_action() {
        assert_eq!(
            "MoveDown".parse::<NavAction>().unwrap(),
            NavAction::MoveDown
        );
        assert_eq!("JumpTop".parse::<NavAction>().unwrap(), NavAction::JumpTop);
        assert!("Unknown".parse::<NavAction>().is_err());
    }

    #[test]
    fn parse_search_action() {
        assert_eq!(
            "Start".parse::<SearchAction>().unwrap(),
            SearchAction::Start
        );
        assert_eq!("Prev".parse::<SearchAction>().unwrap(), SearchAction::Prev);
        assert!("Unknown".parse::<SearchAction>().is_err());
    }

    #[test]
    fn parse_view_action() {
        assert_eq!("Quit".parse::<ViewAction>().unwrap(), ViewAction::Quit);
        assert_eq!(
            "CyclePaneForward".parse::<ViewAction>().unwrap(),
            ViewAction::CyclePaneForward
        );
        assert!("Unknown".parse::<ViewAction>().is_err());
    }
}