shep 0.20.0

The shep binary: a process manager that keeps a flock of long-running processes alive on macOS, Linux and Windows, with logs, watch and cron restarts, and webhook alerts
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
//! `crossterm::event::Event` -> [`KeyPress`]. The whole crossterm-typed edge
//! of the keyboard, kept in one small file so `super::app` never imports a
//! terminal crate and its reducer tests never construct one.

use crossterm::event::{Event, KeyCode, KeyEventKind, KeyModifiers};

use super::app::{ActionVerb, InputMode, KeyPress};
use super::textbox::TextEdit;

/// The [`KeyPress`] this event means under `mode`, or `None` for a key
/// lookout does not bind there.
///
/// Only `KeyEventKind::Press` counts: a repeat or release would fire the
/// action again. `Ctrl-C` binds in either mode, since raw mode delivers it
/// as an ordinary key event. `Ctrl-D`/`Ctrl-U` page only in `Normal`.
#[must_use]
pub fn map_key(event: &Event, mode: InputMode) -> Option<KeyPress> {
    let Event::Key(key) = event else {
        return None;
    };
    if key.kind != KeyEventKind::Press {
        return None;
    }
    if key.modifiers.contains(KeyModifiers::CONTROL) {
        return match key.code {
            KeyCode::Char('c') => Some(KeyPress::Quit),
            // Guarded on `mode` here, unlike every other binding in this
            // branch. This CONTROL match runs ahead of the `InputMode::Text`
            // check below. An unguarded `ctrl-d`/`ctrl-u` would page the pane
            // while an operator types a match into the bleats box.
            KeyCode::Char('d') if mode == InputMode::Normal => Some(KeyPress::PageDown),
            KeyCode::Char('u') if mode == InputMode::Normal => Some(KeyPress::PageUp),
            // The same guard from the other side: these have no meaning in
            // `Normal`, and `ctrl-u` swaps its paging for a kill.
            KeyCode::Char(letter) if mode == InputMode::Text => text_edit_ctrl(letter),
            _ => None,
        };
    }
    if mode == InputMode::Text {
        let alt = key.modifiers.contains(KeyModifiers::ALT);
        return match key.code {
            KeyCode::Char('b') if alt => edit(TextEdit::WordLeft),
            KeyCode::Char('f') if alt => edit(TextEdit::WordRight),
            KeyCode::Left if alt => edit(TextEdit::WordLeft),
            KeyCode::Right if alt => edit(TextEdit::WordRight),
            KeyCode::Backspace if alt => edit(TextEdit::DeleteWordBack),
            // SHIFT stays: crossterm delivers a capital as `Char('W')` with
            // SHIFT set. ALT is filtered, since `Alt-w` is never a letter.
            // Only ALT's absence is checked, so a shifted `?` takes the same
            // branch as an unshifted one.
            KeyCode::Char(typed) if !alt => Some(KeyPress::TextChar(typed)),
            KeyCode::Backspace => Some(KeyPress::TextBackspace),
            KeyCode::Left => edit(TextEdit::Left),
            KeyCode::Right => edit(TextEdit::Right),
            KeyCode::Home => edit(TextEdit::Home),
            KeyCode::End => edit(TextEdit::End),
            KeyCode::Delete => edit(TextEdit::Delete),
            KeyCode::Enter => Some(KeyPress::TextApply),
            KeyCode::Esc => Some(KeyPress::TextAbandon),
            // The key/value editor's two fields. `Tab` and `Shift-Tab` mean
            // groups in `Normal`, and a box owns the keyboard while it is
            // open, so the same two keys mean fields here.
            KeyCode::Tab => Some(KeyPress::NextField),
            KeyCode::BackTab => Some(KeyPress::PrevField),
            _ => None,
        };
    }
    match key.code {
        KeyCode::Char('q') => Some(KeyPress::Quit),
        KeyCode::Esc => Some(KeyPress::Escape),
        KeyCode::Char('/') => Some(KeyPress::FilterStart),
        KeyCode::Tab => Some(KeyPress::NextGroup),
        KeyCode::BackTab => Some(KeyPress::PrevGroup),
        KeyCode::Char(digit @ '1'..='9') => Some(KeyPress::Group(digit as u8 - b'0')),
        KeyCode::Char('u') => Some(KeyPress::Undo),
        KeyCode::Char('c') => Some(KeyPress::Continue),
        KeyCode::Char('j') | KeyCode::Down => Some(KeyPress::SelectDown),
        KeyCode::Char('k') | KeyCode::Up => Some(KeyPress::SelectUp),
        KeyCode::Char('g') | KeyCode::Home => Some(KeyPress::SelectFirst),
        KeyCode::Char('G') | KeyCode::End => Some(KeyPress::SelectLast),
        KeyCode::Char('r') => Some(KeyPress::Refresh),
        KeyCode::Char('x') => Some(KeyPress::Action(ActionVerb::Stop)),
        KeyCode::Char('R') => Some(KeyPress::Action(ActionVerb::Restart)),
        KeyCode::Char('L') => Some(KeyPress::Action(ActionVerb::Reload)),
        KeyCode::Char('s') => Some(KeyPress::Settings),
        KeyCode::Char('S') => Some(KeyPress::Secrets),
        KeyCode::Char('v') => Some(KeyPress::Reveal),
        KeyCode::Char('y') => Some(KeyPress::Copy),
        KeyCode::Left => Some(KeyPress::TabPrev),
        KeyCode::Right => Some(KeyPress::TabNext),
        KeyCode::Char('e') => Some(KeyPress::Edit),
        // `h` and `?` mean the same thing here. Both are plain keys:
        // crossterm delivers `?` as `Char('?')` with SHIFT set on most
        // layouts, and SHIFT is not filtered in this branch, so the shifted
        // form lands on the same arm.
        KeyCode::Char('h' | '?') => Some(KeyPress::Help),
        KeyCode::Char(' ') => Some(KeyPress::Cycle),
        KeyCode::Char('d') => Some(KeyPress::Remove),
        KeyCode::Char('D') => Some(KeyPress::ArmDelete),
        KeyCode::Char('K') => Some(KeyPress::StepUp),
        KeyCode::Char('J') => Some(KeyPress::StepDown),
        KeyCode::Char('F') => Some(KeyPress::FoldView),
        KeyCode::Char('z') => Some(KeyPress::Collapse),
        KeyCode::Char('b') => Some(KeyPress::Bleats),
        KeyCode::Char('o') => Some(KeyPress::StreamCycle),
        KeyCode::Char('m') => Some(KeyPress::LevelCycle),
        KeyCode::Char('f') => Some(KeyPress::FollowToggle),
        KeyCode::Char('w') => Some(KeyPress::WrapToggle),
        KeyCode::Char('n') => Some(KeyPress::MatchNext),
        KeyCode::Char('N') => Some(KeyPress::MatchPrev),
        KeyCode::Enter => Some(KeyPress::Confirm),
        _ => None,
    }
}

fn edit(text_edit: TextEdit) -> Option<KeyPress> {
    Some(KeyPress::TextEdit(text_edit))
}

/// The readline-style `Ctrl-<letter>` edits a text box takes.
fn text_edit_ctrl(letter: char) -> Option<KeyPress> {
    match letter {
        'a' => edit(TextEdit::Home),
        'e' => edit(TextEdit::End),
        'w' => edit(TextEdit::DeleteWordBack),
        'u' => edit(TextEdit::DeleteToStart),
        'k' => edit(TextEdit::DeleteToEnd),
        'r' => edit(TextEdit::Reveal),
        _ => None,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyModifiers};

    fn key(code: KeyCode) -> Event {
        Event::Key(KeyEvent::new(code, KeyModifiers::NONE))
    }

    fn press(code: KeyCode) -> Option<KeyPress> {
        map_key(&key(code), InputMode::Normal)
    }

    #[test]
    fn every_bound_key_resolves_to_its_press() {
        assert_eq!(
            map_key(&key(KeyCode::Char('q')), InputMode::Normal),
            Some(KeyPress::Quit)
        );
        assert_eq!(
            map_key(&key(KeyCode::Esc), InputMode::Normal),
            Some(KeyPress::Escape)
        );
        assert_eq!(
            map_key(&key(KeyCode::Char('j')), InputMode::Normal),
            Some(KeyPress::SelectDown)
        );
        assert_eq!(
            map_key(&key(KeyCode::Down), InputMode::Normal),
            Some(KeyPress::SelectDown)
        );
        assert_eq!(
            map_key(&key(KeyCode::Char('k')), InputMode::Normal),
            Some(KeyPress::SelectUp)
        );
        assert_eq!(
            map_key(&key(KeyCode::Up), InputMode::Normal),
            Some(KeyPress::SelectUp)
        );
        assert_eq!(
            map_key(&key(KeyCode::Char('g')), InputMode::Normal),
            Some(KeyPress::SelectFirst)
        );
        assert_eq!(
            map_key(&key(KeyCode::Char('G')), InputMode::Normal),
            Some(KeyPress::SelectLast)
        );
        assert_eq!(
            map_key(&key(KeyCode::Char('r')), InputMode::Normal),
            Some(KeyPress::Refresh)
        );
        assert_eq!(
            map_key(&key(KeyCode::Char('x')), InputMode::Normal),
            Some(KeyPress::Action(ActionVerb::Stop))
        );
        assert_eq!(
            map_key(&key(KeyCode::Char('R')), InputMode::Normal),
            Some(KeyPress::Action(ActionVerb::Restart))
        );
        assert_eq!(
            map_key(&key(KeyCode::Char('L')), InputMode::Normal),
            Some(KeyPress::Action(ActionVerb::Reload))
        );
        assert_eq!(
            map_key(&key(KeyCode::Char('s')), InputMode::Normal),
            Some(KeyPress::Settings)
        );
        assert_eq!(
            map_key(&key(KeyCode::Char(' ')), InputMode::Normal),
            Some(KeyPress::Cycle)
        );
        assert_eq!(
            map_key(&key(KeyCode::Char('e')), InputMode::Normal),
            Some(KeyPress::Edit)
        );
        assert_eq!(
            map_key(&key(KeyCode::Char('E')), InputMode::Normal),
            None,
            "the config pane is lower-case `e`; `E` is unbound"
        );
        assert_eq!(
            map_key(&key(KeyCode::Char('h')), InputMode::Normal),
            Some(KeyPress::Help)
        );
        assert_eq!(
            map_key(&key(KeyCode::Char('d')), InputMode::Normal),
            Some(KeyPress::Remove)
        );
        assert_eq!(
            map_key(&key(KeyCode::Char('D')), InputMode::Normal),
            Some(KeyPress::ArmDelete)
        );
        assert_eq!(
            map_key(&key(KeyCode::Char('K')), InputMode::Normal),
            Some(KeyPress::StepUp)
        );
        assert_eq!(
            map_key(&key(KeyCode::Char('J')), InputMode::Normal),
            Some(KeyPress::StepDown)
        );
        assert_eq!(
            map_key(&key(KeyCode::Char('F')), InputMode::Normal),
            Some(KeyPress::FoldView)
        );
        assert_eq!(
            map_key(&key(KeyCode::Char('z')), InputMode::Normal),
            Some(KeyPress::Collapse)
        );
    }

    /// `b` opens the full-screen bleats pane. Pinned because `map_key`
    /// dispatches on mode rather than pane, so a key taken here is taken
    /// everywhere in `Normal`.
    #[test]
    fn b_opens_the_bleats_pane() {
        assert_eq!(
            map_key(&key(KeyCode::Char('b')), InputMode::Normal),
            Some(KeyPress::Bleats)
        );
    }

    #[test]
    fn the_movement_keys_are_unchanged_and_now_mean_selection() {
        assert_eq!(
            map_key(&key(KeyCode::Char('j')), InputMode::Normal),
            Some(KeyPress::SelectDown)
        );
        assert_eq!(
            map_key(&key(KeyCode::Down), InputMode::Normal),
            Some(KeyPress::SelectDown)
        );
        assert_eq!(
            map_key(&key(KeyCode::Char('k')), InputMode::Normal),
            Some(KeyPress::SelectUp)
        );
        assert_eq!(
            map_key(&key(KeyCode::Up), InputMode::Normal),
            Some(KeyPress::SelectUp)
        );
        assert_eq!(
            map_key(&key(KeyCode::Char('g')), InputMode::Normal),
            Some(KeyPress::SelectFirst)
        );
        assert_eq!(
            map_key(&key(KeyCode::Home), InputMode::Normal),
            Some(KeyPress::SelectFirst)
        );
        assert_eq!(
            map_key(&key(KeyCode::Char('G')), InputMode::Normal),
            Some(KeyPress::SelectLast)
        );
        assert_eq!(
            map_key(&key(KeyCode::End), InputMode::Normal),
            Some(KeyPress::SelectLast)
        );
    }

    #[test]
    fn capital_s_opens_the_secrets_pane_and_lower_s_still_opens_settings() {
        assert_eq!(
            map_key(&key(KeyCode::Char('S')), InputMode::Normal),
            Some(KeyPress::Secrets)
        );
        assert_eq!(
            map_key(&key(KeyCode::Char('s')), InputMode::Normal),
            Some(KeyPress::Settings),
            "the settings screen keeps its own key"
        );
        assert_eq!(
            map_key(&key(KeyCode::Char('g')), InputMode::Normal),
            Some(KeyPress::SelectFirst),
            "the frame wanted `g` for secrets; `g` is still SelectFirst"
        );
    }

    #[test]
    fn lower_v_is_the_reveal() {
        assert_eq!(
            map_key(&key(KeyCode::Char('v')), InputMode::Normal),
            Some(KeyPress::Reveal)
        );
        assert_eq!(
            map_key(&key(KeyCode::Char('V')), InputMode::Normal),
            None,
            "one key puts a value on screen, and it is not a shifted one"
        );
    }

    #[test]
    fn the_arrow_keys_move_the_environment_tab() {
        assert_eq!(
            map_key(&key(KeyCode::Left), InputMode::Normal),
            Some(KeyPress::TabPrev)
        );
        assert_eq!(
            map_key(&key(KeyCode::Right), InputMode::Normal),
            Some(KeyPress::TabNext)
        );
        assert_eq!(
            map_key(&key(KeyCode::Up), InputMode::Normal),
            Some(KeyPress::SelectUp),
            "the vertical arrows keep the meaning they already have"
        );
    }

    /// `Tab` and `Shift+Tab` belong to the config pane's group cycling, so
    /// the secrets pane's tab row names `<-/->` and nothing else. In `Text`
    /// they belong to the key/value editor instead: a tab typed into a box
    /// is not a character.
    #[test]
    fn tab_walks_the_config_pane_groups_and_the_tab_row_does_not_claim_it() {
        assert_eq!(
            map_key(&key(KeyCode::Tab), InputMode::Normal),
            Some(KeyPress::NextGroup)
        );
        assert_eq!(
            map_key(
                &Event::Key(KeyEvent::new(KeyCode::BackTab, KeyModifiers::SHIFT)),
                InputMode::Normal
            ),
            Some(KeyPress::PrevGroup),
            "shift-tab walks back where tab walks forward"
        );
        assert_eq!(
            map_key(&key(KeyCode::Tab), InputMode::Text),
            Some(KeyPress::NextField),
            "and in a key/value editor the same key steps to the other field"
        );
        assert_eq!(
            map_key(
                &Event::Key(KeyEvent::new(KeyCode::BackTab, KeyModifiers::SHIFT)),
                InputMode::Text
            ),
            Some(KeyPress::PrevField),
            "with shift-tab stepping back"
        );
    }

    #[test]
    fn ctrl_c_quits_because_raw_mode_swallows_the_signal() {
        let event = Event::Key(KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL));
        assert_eq!(map_key(&event, InputMode::Normal), Some(KeyPress::Quit));
        // A bare `c` is `Continue`, distinct from `ctrl-c`: the CONTROL
        // branch above is checked first and returns before the bare match
        // below is ever reached.
        assert_eq!(
            map_key(&key(KeyCode::Char('c')), InputMode::Normal),
            Some(KeyPress::Continue)
        );
    }

    #[test]
    fn only_a_press_counts() {
        let mut release = KeyEvent::new(KeyCode::Char('x'), KeyModifiers::NONE);
        release.kind = KeyEventKind::Release;
        assert_eq!(map_key(&Event::Key(release), InputMode::Normal), None);

        let mut repeat = KeyEvent::new(KeyCode::Char('x'), KeyModifiers::NONE);
        repeat.kind = KeyEventKind::Repeat;
        assert_eq!(map_key(&Event::Key(repeat), InputMode::Normal), None);
    }

    fn modified(code: KeyCode, modifiers: KeyModifiers) -> Event {
        Event::Key(KeyEvent::new(code, modifiers))
    }

    #[test]
    fn the_cursor_keys_move_the_cursor_in_text_mode() {
        let text = |event: Event| map_key(&event, InputMode::Text);
        assert_eq!(text(key(KeyCode::Left)), edit(TextEdit::Left));
        assert_eq!(text(key(KeyCode::Right)), edit(TextEdit::Right));
        assert_eq!(text(key(KeyCode::Home)), edit(TextEdit::Home));
        assert_eq!(text(key(KeyCode::End)), edit(TextEdit::End));
        assert_eq!(text(key(KeyCode::Delete)), edit(TextEdit::Delete));
        assert_eq!(
            text(modified(KeyCode::Char('a'), KeyModifiers::CONTROL)),
            edit(TextEdit::Home)
        );
        assert_eq!(
            text(modified(KeyCode::Char('e'), KeyModifiers::CONTROL)),
            edit(TextEdit::End)
        );
    }

    #[test]
    fn alt_arrows_and_alt_b_and_f_move_by_word_in_text_mode() {
        let text = |event: Event| map_key(&event, InputMode::Text);
        assert_eq!(
            text(modified(KeyCode::Left, KeyModifiers::ALT)),
            edit(TextEdit::WordLeft)
        );
        assert_eq!(
            text(modified(KeyCode::Right, KeyModifiers::ALT)),
            edit(TextEdit::WordRight)
        );
        assert_eq!(
            text(modified(KeyCode::Char('b'), KeyModifiers::ALT)),
            edit(TextEdit::WordLeft)
        );
        assert_eq!(
            text(modified(KeyCode::Char('f'), KeyModifiers::ALT)),
            edit(TextEdit::WordRight)
        );
    }

    #[test]
    fn the_kill_keys_delete_in_text_mode() {
        let text = |event: Event| map_key(&event, InputMode::Text);
        assert_eq!(
            text(modified(KeyCode::Char('w'), KeyModifiers::CONTROL)),
            edit(TextEdit::DeleteWordBack)
        );
        assert_eq!(
            text(modified(KeyCode::Backspace, KeyModifiers::ALT)),
            edit(TextEdit::DeleteWordBack)
        );
        assert_eq!(
            text(modified(KeyCode::Char('u'), KeyModifiers::CONTROL)),
            edit(TextEdit::DeleteToStart)
        );
        assert_eq!(
            text(modified(KeyCode::Char('k'), KeyModifiers::CONTROL)),
            edit(TextEdit::DeleteToEnd)
        );
        assert_eq!(
            text(modified(KeyCode::Char('r'), KeyModifiers::CONTROL)),
            edit(TextEdit::Reveal)
        );
    }

    /// Every edit key keeps its old meaning in `Normal`, or has none.
    #[test]
    fn the_edit_keys_keep_their_normal_mode_meaning_or_mean_nothing() {
        let normal = |event: Event| map_key(&event, InputMode::Normal);
        assert_eq!(normal(key(KeyCode::Left)), Some(KeyPress::TabPrev));
        assert_eq!(normal(key(KeyCode::Right)), Some(KeyPress::TabNext));
        assert_eq!(normal(key(KeyCode::Home)), Some(KeyPress::SelectFirst));
        assert_eq!(normal(key(KeyCode::End)), Some(KeyPress::SelectLast));
        assert_eq!(normal(key(KeyCode::Delete)), None);
        assert_eq!(
            normal(modified(KeyCode::Char('u'), KeyModifiers::CONTROL)),
            Some(KeyPress::PageUp)
        );
        for letter in ['a', 'e', 'w', 'k'] {
            assert_eq!(
                normal(modified(KeyCode::Char(letter), KeyModifiers::CONTROL)),
                None,
                "ctrl-{letter}"
            );
        }
        assert_eq!(
            normal(modified(KeyCode::Backspace, KeyModifiers::ALT)),
            None
        );
    }

    /// An Alt-modified letter is a word move, never a typed letter.
    #[test]
    fn an_alt_letter_is_not_typed_into_the_box() {
        assert_eq!(
            map_key(
                &modified(KeyCode::Char('x'), KeyModifiers::ALT),
                InputMode::Text
            ),
            None
        );
    }

    #[test]
    fn a_paste_event_is_no_key() {
        assert_eq!(
            map_key(&Event::Paste("ab\ncd".to_owned()), InputMode::Text),
            None
        );
    }

    #[test]
    fn typing_q_while_editing_types_a_letter() {
        assert_eq!(
            map_key(&key(KeyCode::Char('q')), InputMode::Text),
            Some(KeyPress::TextChar('q'))
        );
        assert_eq!(
            map_key(&key(KeyCode::Char('q')), InputMode::Normal),
            Some(KeyPress::Quit)
        );
    }

    #[test]
    fn the_text_mode_binds_exactly_the_box_s_keys() {
        assert_eq!(
            map_key(&key(KeyCode::Backspace), InputMode::Text),
            Some(KeyPress::TextBackspace)
        );
        assert_eq!(
            map_key(&key(KeyCode::Enter), InputMode::Text),
            Some(KeyPress::TextApply)
        );
        assert_eq!(
            map_key(&key(KeyCode::Esc), InputMode::Text),
            Some(KeyPress::TextAbandon)
        );
        let ctrl_c = Event::Key(KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL));
        assert_eq!(map_key(&ctrl_c, InputMode::Text), Some(KeyPress::Quit));
        assert_eq!(map_key(&key(KeyCode::F(5)), InputMode::Text), None);
    }

    #[test]
    fn a_shifted_letter_is_still_a_letter_in_the_box() {
        let shifted = Event::Key(KeyEvent::new(KeyCode::Char('W'), KeyModifiers::SHIFT));
        assert_eq!(
            map_key(&shifted, InputMode::Text),
            Some(KeyPress::TextChar('W'))
        );
    }

    /// `o` and `m` are global bindings, taken here so the bleats pane can
    /// cycle its stream and minimum-level axes; a key taken in `map_key` is
    /// taken everywhere in `Normal`, the same note `b_opens_the_bleats_pane`
    /// makes.
    #[test]
    fn o_and_m_cycle_the_stream_and_level_axes() {
        assert_eq!(
            map_key(&key(KeyCode::Char('o')), InputMode::Normal),
            Some(KeyPress::StreamCycle)
        );
        assert_eq!(
            map_key(&key(KeyCode::Char('m')), InputMode::Normal),
            Some(KeyPress::LevelCycle)
        );
    }

    /// `f` cycles the bleats pane's follow flag; a global binding, ignored
    /// on the dashboard the same way `o`/`m` are.
    #[test]
    fn f_toggles_follow() {
        assert_eq!(
            map_key(&key(KeyCode::Char('f')), InputMode::Normal),
            Some(KeyPress::FollowToggle)
        );
    }

    /// `ctrl-d`/`ctrl-u` page the bleats pane, and only fire in `Normal`:
    /// unguarded, they would page the pane behind an operator's back while
    /// the match box owns `InputMode::Text`.
    #[test]
    fn ctrl_d_and_ctrl_u_page_only_in_normal_mode() {
        let ctrl_d = Event::Key(KeyEvent::new(KeyCode::Char('d'), KeyModifiers::CONTROL));
        let ctrl_u = Event::Key(KeyEvent::new(KeyCode::Char('u'), KeyModifiers::CONTROL));
        assert_eq!(
            map_key(&ctrl_d, InputMode::Normal),
            Some(KeyPress::PageDown)
        );
        assert_eq!(map_key(&ctrl_u, InputMode::Normal), Some(KeyPress::PageUp));
        assert_eq!(map_key(&ctrl_d, InputMode::Text), None);
        assert_eq!(
            map_key(&ctrl_u, InputMode::Text),
            edit(TextEdit::DeleteToStart),
            "in a box ctrl-u kills to the start of the line instead"
        );
    }

    /// `?` is the keymap's second key, and `/` is untouched.
    ///
    /// They are the same physical key on most layouts, so an arm that took
    /// `?` by matching the unshifted position would have taken the name
    /// filter with it. The `/` assertion is not idle: `Char('/')` sits
    /// ABOVE this arm and wins by order alone, so an arm written below it
    /// cannot break the filter, and one written above it can.
    #[test]
    fn question_mark_opens_the_keymap_and_slash_still_filters() {
        assert_eq!(
            map_key(&key(KeyCode::Char('?')), InputMode::Normal),
            Some(KeyPress::Help)
        );
        assert_eq!(
            map_key(&key(KeyCode::Char('/')), InputMode::Normal),
            Some(KeyPress::FilterStart)
        );
        assert_eq!(
            map_key(&key(KeyCode::Char('?')), InputMode::Text),
            Some(KeyPress::TextChar('?')),
            "a question mark typed into a box is a character"
        );
    }

    /// And the shifted form reaches the same arm, which is how a real
    /// keyboard sends it.
    #[test]
    fn a_shifted_question_mark_still_opens_the_keymap() {
        let shifted = Event::Key(KeyEvent::new(KeyCode::Char('?'), KeyModifiers::SHIFT));
        assert_eq!(map_key(&shifted, InputMode::Normal), Some(KeyPress::Help));
    }

    #[test]
    fn slash_opens_the_filter_in_normal_mode() {
        assert_eq!(
            map_key(&key(KeyCode::Char('/')), InputMode::Normal),
            Some(KeyPress::FilterStart)
        );
    }

    /// `w` toggles the bleats pane's wrap; `n`/`N` step between matches. All
    /// three are global bindings, the same way `o`/`m`/`f` are: `map_key`
    /// dispatches on mode alone, ignored on the dashboard.
    #[test]
    fn w_and_n_and_shift_n_are_bound() {
        assert_eq!(
            map_key(&key(KeyCode::Char('w')), InputMode::Normal),
            Some(KeyPress::WrapToggle)
        );
        assert_eq!(
            map_key(&key(KeyCode::Char('n')), InputMode::Normal),
            Some(KeyPress::MatchNext)
        );
        assert_eq!(
            map_key(&key(KeyCode::Char('N')), InputMode::Normal),
            Some(KeyPress::MatchPrev)
        );
    }

    #[test]
    fn tab_asks_for_the_next_group() {
        assert_eq!(press(KeyCode::Tab), Some(KeyPress::NextGroup));
    }

    #[test]
    fn the_digits_one_through_nine_jump_to_a_group() {
        for (typed, wanted) in [
            ('1', 1_u8),
            ('2', 2),
            ('3', 3),
            ('4', 4),
            ('5', 5),
            ('6', 6),
            ('7', 7),
            ('8', 8),
            ('9', 9),
        ] {
            assert_eq!(press(KeyCode::Char(typed)), Some(KeyPress::Group(wanted)));
        }
    }

    /// Nine groups, so zero is not a group key and stays free.
    #[test]
    fn zero_is_unbound() {
        assert_eq!(press(KeyCode::Char('0')), None);
    }

    #[test]
    fn bare_u_undoes_and_ctrl_u_still_pages() {
        assert_eq!(press(KeyCode::Char('u')), Some(KeyPress::Undo));
        let ctrl_u = Event::Key(KeyEvent::new(KeyCode::Char('u'), KeyModifiers::CONTROL));
        assert_eq!(map_key(&ctrl_u, InputMode::Normal), Some(KeyPress::PageUp));
    }

    #[test]
    fn bare_c_continues() {
        assert_eq!(press(KeyCode::Char('c')), Some(KeyPress::Continue));
    }

    /// A digit typed into a text box is text, not a group jump.
    #[test]
    fn a_digit_in_text_mode_is_typed() {
        let one = Event::Key(KeyEvent::new(KeyCode::Char('1'), KeyModifiers::NONE));
        assert_eq!(
            map_key(&one, InputMode::Text),
            Some(KeyPress::TextChar('1'))
        );
    }

    #[test]
    fn d_removes() {
        assert_eq!(press(KeyCode::Char('d')), Some(KeyPress::Remove));
    }

    /// Named for the key, not for one pane's use of it. `map_key` dispatches
    /// on mode rather than on which body is showing, so the body is what
    /// decides whether a step reorders a list or walks to the next sheep.
    #[test]
    fn shift_j_and_shift_k_are_steps() {
        assert_eq!(
            map_key(&key(KeyCode::Char('J')), InputMode::Normal),
            Some(KeyPress::StepDown)
        );
        assert_eq!(
            map_key(&key(KeyCode::Char('K')), InputMode::Normal),
            Some(KeyPress::StepUp)
        );
    }
}