revue 2.71.1

A Vue-style TUI framework for Rust with CSS styling
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
//! Layered key handling pattern
//!
//! Provides a standard pattern for handling keyboard input with proper priority.
//! Ensures modals, dialogs, and popups take precedence over normal navigation.
//!
//! # Priority Layers
//!
//! 1. **Quit check** - Always check first (unless in blocking modal)
//! 2. **Modals/Dialogs** - Full-screen overlays (help, forms)
//! 3. **Confirm dialogs** - Yes/No confirmations
//! 4. **Popups** - Temporary overlays (search, filters)
//! 5. **View-specific** - Normal navigation and actions
//!
//! # Example
//!
//! ```ignore
//! use revue::patterns::KeyHandler;
//! use crossterm::event::KeyCode;
//!
//! struct App {
//!     view_mode: ViewMode,
//!     confirm: ConfirmState,
//!     popup_active: bool,
//!     quit: bool,
//! }
//!
//! impl KeyHandler for App {
//!     fn should_quit(&self, key: &KeyCode) -> bool {
//!         matches!(key, KeyCode::Char('q')) && !self.confirm.is_active()
//!     }
//!
//!     fn in_modal(&self) -> bool {
//!         matches!(self.view_mode, ViewMode::Help | ViewMode::Form)
//!     }
//!
//!     fn handle_modal_key(&mut self, key: &KeyCode) -> bool {
//!         match self.view_mode {
//!             ViewMode::Help => self.handle_help_key(key),
//!             ViewMode::Form => self.handle_form_key(key),
//!             _ => true,
//!         }
//!     }
//!
//!     fn has_confirm(&self) -> bool {
//!         self.confirm.is_active()
//!     }
//!
//!     fn handle_confirm_key(&mut self, key: &KeyCode) -> bool {
//!         match key {
//!             KeyCode::Char('y') | KeyCode::Enter => {
//!                 self.confirm.execute(|action| self.do_action(action));
//!             }
//!             _ => self.confirm.cancel(),
//!         }
//!         true
//!     }
//!
//!     fn handle_view_key(&mut self, key: &KeyCode) -> bool {
//!         match self.view_mode {
//!             ViewMode::Main => self.handle_main_key(key),
//!             ViewMode::Detail => self.handle_detail_key(key),
//!             _ => true,
//!         }
//!     }
//! }
//! ```

use crossterm::event::KeyCode;

/// Layered key handling
///
/// Implement this trait to get standardized key handling with proper priority.
///
/// The default `handle_key` implementation checks layers in order:
/// 1. Quit (if `should_quit` returns true, returns false to exit)
/// 2. Modals (if `in_modal`, calls `handle_modal_key`)
/// 3. Confirm (if `has_confirm`, calls `handle_confirm_key`)
/// 4. Popups (if `has_popup`, calls `handle_popup_key`)
/// 5. View (calls `handle_view_key`)
pub trait KeyHandler {
    /// Handle a key press
    ///
    /// Returns `false` if app should quit, `true` otherwise.
    ///
    /// This is the main entry point for key handling.
    /// Override this if you need custom priority logic.
    fn handle_key(&mut self, key: &KeyCode) -> bool {
        // 1. Quit check (highest priority)
        if self.should_quit(key) {
            return false; // Exit app
        }

        // 2. Modal/Dialog layer
        if self.in_modal() {
            return self.handle_modal_key(key);
        }

        // 3. Confirm dialog layer
        if self.has_confirm() {
            return self.handle_confirm_key(key);
        }

        // 4. Popup layer
        if self.has_popup() {
            return self.handle_popup_key(key);
        }

        // 5. Normal view handling
        self.handle_view_key(key)
    }

    /// Check if app should quit
    ///
    /// # Example
    ///
    /// ```ignore
    /// fn should_quit(&self, key: &KeyCode) -> bool {
    ///     matches!(key, KeyCode::Char('q'))
    ///         && !self.confirm.is_active()
    ///         && !self.in_modal()
    /// }
    /// ```
    fn should_quit(&self, key: &KeyCode) -> bool;

    /// Check if in modal/dialog mode
    ///
    /// Modals are full-screen overlays like help screens or forms.
    ///
    /// # Example
    ///
    /// ```ignore
    /// fn in_modal(&self) -> bool {
    ///     matches!(self.view_mode, ViewMode::Help | ViewMode::Form)
    /// }
    /// ```
    fn in_modal(&self) -> bool {
        false
    }

    /// Handle key in modal mode
    ///
    /// # Example
    ///
    /// ```ignore
    /// fn handle_modal_key(&mut self, key: &KeyCode) -> bool {
    ///     match key {
    ///         KeyCode::Esc => {
    ///             self.view_mode = ViewMode::Main;
    ///         }
    ///         // ... modal-specific keys ...
    ///     }
    ///     true
    /// }
    /// ```
    fn handle_modal_key(&mut self, _key: &KeyCode) -> bool {
        true
    }

    /// Check if confirmation dialog is active
    ///
    /// # Example
    ///
    /// ```ignore
    /// fn has_confirm(&self) -> bool {
    ///     self.confirm.is_active()
    /// }
    /// ```
    fn has_confirm(&self) -> bool {
        false
    }

    /// Handle key in confirmation mode
    ///
    /// # Example
    ///
    /// ```ignore
    /// fn handle_confirm_key(&mut self, key: &KeyCode) -> bool {
    ///     match key {
    ///         KeyCode::Char('y') | KeyCode::Enter => {
    ///             self.confirm.execute(|action| self.do_action(action));
    ///         }
    ///         _ => self.confirm.cancel(),
    ///     }
    ///     true
    /// }
    /// ```
    fn handle_confirm_key(&mut self, _key: &KeyCode) -> bool {
        true
    }

    /// Check if popup is active
    ///
    /// Popups are temporary overlays like search or filter palettes.
    ///
    /// # Example
    ///
    /// ```ignore
    /// fn has_popup(&self) -> bool {
    ///     self.search_active || self.filter_active
    /// }
    /// ```
    fn has_popup(&self) -> bool {
        false
    }

    /// Handle key in popup mode
    ///
    /// # Example
    ///
    /// ```ignore
    /// fn handle_popup_key(&mut self, key: &KeyCode) -> bool {
    ///     if self.search_active {
    ///         return self.handle_search_key(key);
    ///     }
    ///     true
    /// }
    /// ```
    fn handle_popup_key(&mut self, _key: &KeyCode) -> bool {
        true
    }

    /// Handle key in normal view mode
    ///
    /// This is where main navigation and actions happen.
    ///
    /// # Example
    ///
    /// ```ignore
    /// fn handle_view_key(&mut self, key: &KeyCode) -> bool {
    ///     match self.view_mode {
    ///         ViewMode::Main => match key {
    ///             KeyCode::Char('j') => self.next(),
    ///             KeyCode::Char('k') => self.prev(),
    ///             KeyCode::Enter => self.open_detail(),
    ///             // ...
    ///         }
    ///         ViewMode::Detail => {
    ///             // ... detail keys ...
    ///         }
    ///     }
    ///     true
    /// }
    /// ```
    fn handle_view_key(&mut self, key: &KeyCode) -> bool;
}

/// Common key patterns
///
/// Helper functions for common key checks.
pub mod common {
    use crossterm::event::KeyCode;

    /// Check if key is a navigation key (j/k/up/down)
    pub fn is_nav_key(key: &KeyCode) -> bool {
        matches!(
            key,
            KeyCode::Char('j')
                | KeyCode::Char('k')
                | KeyCode::Up
                | KeyCode::Down
                | KeyCode::Char('g')
                | KeyCode::Char('G')
        )
    }

    /// Check if key is horizontal navigation (h/l/left/right)
    pub fn is_horizontal_nav(key: &KeyCode) -> bool {
        matches!(
            key,
            KeyCode::Char('h') | KeyCode::Char('l') | KeyCode::Left | KeyCode::Right
        )
    }

    /// Check if key is a confirm key (y/Y/Enter)
    pub fn is_confirm_key(key: &KeyCode) -> bool {
        matches!(
            key,
            KeyCode::Char('y') | KeyCode::Char('Y') | KeyCode::Enter
        )
    }

    /// Check if key is a cancel key (n/N/Esc)
    pub fn is_cancel_key(key: &KeyCode) -> bool {
        matches!(key, KeyCode::Char('n') | KeyCode::Char('N') | KeyCode::Esc)
    }

    /// Check if key is a quit key (q/Q)
    pub fn is_quit_key(key: &KeyCode) -> bool {
        matches!(key, KeyCode::Char('q') | KeyCode::Char('Q'))
    }

    /// Check if key is help (?)
    pub fn is_help_key(key: &KeyCode) -> bool {
        matches!(key, KeyCode::Char('?'))
    }

    /// Check if key is refresh (r/R)
    pub fn is_refresh_key(key: &KeyCode) -> bool {
        matches!(key, KeyCode::Char('r') | KeyCode::Char('R'))
    }
}

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

    struct TestApp {
        in_modal: bool,
        has_confirm: bool,
        has_popup: bool,
        #[allow(dead_code)]
        quit: bool,
        last_key: Option<KeyCode>,
        layer_handled: String,
    }

    impl TestApp {
        fn new() -> Self {
            Self {
                in_modal: false,
                has_confirm: false,
                has_popup: false,
                quit: false,
                last_key: None,
                layer_handled: String::new(),
            }
        }
    }

    impl KeyHandler for TestApp {
        fn should_quit(&self, key: &KeyCode) -> bool {
            matches!(key, KeyCode::Char('q'))
        }

        fn in_modal(&self) -> bool {
            self.in_modal
        }

        fn handle_modal_key(&mut self, key: &KeyCode) -> bool {
            self.last_key = Some(*key);
            self.layer_handled = "modal".to_string();
            true
        }

        fn has_confirm(&self) -> bool {
            self.has_confirm
        }

        fn handle_confirm_key(&mut self, key: &KeyCode) -> bool {
            self.last_key = Some(*key);
            self.layer_handled = "confirm".to_string();
            true
        }

        fn has_popup(&self) -> bool {
            self.has_popup
        }

        fn handle_popup_key(&mut self, key: &KeyCode) -> bool {
            self.last_key = Some(*key);
            self.layer_handled = "popup".to_string();
            true
        }

        fn handle_view_key(&mut self, key: &KeyCode) -> bool {
            self.last_key = Some(*key);
            self.layer_handled = "view".to_string();
            true
        }
    }

    // KeyHandler trait tests
    #[test]
    fn test_quit_returns_false() {
        let mut app = TestApp::new();
        assert!(!app.handle_key(&KeyCode::Char('q')));
    }

    #[test]
    fn test_non_quit_key_returns_true() {
        let mut app = TestApp::new();
        assert!(app.handle_key(&KeyCode::Char('j')));
    }

    #[test]
    fn test_modal_priority_over_confirm() {
        let mut app = TestApp::new();
        app.in_modal = true;
        app.has_confirm = true;
        app.has_popup = true;

        app.handle_key(&KeyCode::Char('j'));
        assert_eq!(app.layer_handled, "modal");
    }

    #[test]
    fn test_confirm_priority_over_popup() {
        let mut app = TestApp::new();
        app.has_confirm = true;
        app.has_popup = true;

        app.handle_key(&KeyCode::Char('y'));
        assert_eq!(app.layer_handled, "confirm");
    }

    #[test]
    fn test_popup_priority_over_view() {
        let mut app = TestApp::new();
        app.has_popup = true;

        app.handle_key(&KeyCode::Char('x'));
        assert_eq!(app.layer_handled, "popup");
    }

    #[test]
    fn test_view_when_no_layers() {
        let mut app = TestApp::new();

        app.handle_key(&KeyCode::Char('j'));
        assert_eq!(app.layer_handled, "view");
        assert_eq!(app.last_key, Some(KeyCode::Char('j')));
    }

    #[test]
    fn test_quit_takes_priority_over_modal() {
        let mut app = TestApp::new();
        app.in_modal = true;

        // 'q' should quit even in modal
        let result = app.handle_key(&KeyCode::Char('q'));
        assert!(!result);
    }

    // Default implementations test
    struct MinimalApp;

    impl KeyHandler for MinimalApp {
        fn should_quit(&self, key: &KeyCode) -> bool {
            matches!(key, KeyCode::Char('q'))
        }

        fn handle_view_key(&mut self, _key: &KeyCode) -> bool {
            true
        }
    }

    #[test]
    fn test_default_in_modal() {
        let app = MinimalApp;
        assert!(!app.in_modal());
    }

    #[test]
    fn test_default_has_confirm() {
        let app = MinimalApp;
        assert!(!app.has_confirm());
    }

    #[test]
    fn test_default_has_popup() {
        let app = MinimalApp;
        assert!(!app.has_popup());
    }

    #[test]
    fn test_default_handle_modal_key() {
        let mut app = MinimalApp;
        assert!(app.handle_modal_key(&KeyCode::Char('x')));
    }

    #[test]
    fn test_default_handle_confirm_key() {
        let mut app = MinimalApp;
        assert!(app.handle_confirm_key(&KeyCode::Enter));
    }

    #[test]
    fn test_default_handle_popup_key() {
        let mut app = MinimalApp;
        assert!(app.handle_popup_key(&KeyCode::Esc));
    }

    // Common key helper tests
    mod common_tests {
        use super::common::*;
        use crossterm::event::KeyCode;

        #[test]
        fn test_is_nav_key_j() {
            assert!(is_nav_key(&KeyCode::Char('j')));
        }

        #[test]
        fn test_is_nav_key_k() {
            assert!(is_nav_key(&KeyCode::Char('k')));
        }

        #[test]
        fn test_is_nav_key_up() {
            assert!(is_nav_key(&KeyCode::Up));
        }

        #[test]
        fn test_is_nav_key_down() {
            assert!(is_nav_key(&KeyCode::Down));
        }

        #[test]
        fn test_is_nav_key_g() {
            assert!(is_nav_key(&KeyCode::Char('g')));
        }

        #[test]
        fn test_is_nav_key_upper_g() {
            assert!(is_nav_key(&KeyCode::Char('G')));
        }

        #[test]
        fn test_is_nav_key_false() {
            assert!(!is_nav_key(&KeyCode::Char('a')));
            assert!(!is_nav_key(&KeyCode::Enter));
        }

        #[test]
        fn test_is_horizontal_nav_h() {
            assert!(is_horizontal_nav(&KeyCode::Char('h')));
        }

        #[test]
        fn test_is_horizontal_nav_l() {
            assert!(is_horizontal_nav(&KeyCode::Char('l')));
        }

        #[test]
        fn test_is_horizontal_nav_left() {
            assert!(is_horizontal_nav(&KeyCode::Left));
        }

        #[test]
        fn test_is_horizontal_nav_right() {
            assert!(is_horizontal_nav(&KeyCode::Right));
        }

        #[test]
        fn test_is_horizontal_nav_false() {
            assert!(!is_horizontal_nav(&KeyCode::Char('j')));
            assert!(!is_horizontal_nav(&KeyCode::Up));
        }

        #[test]
        fn test_is_confirm_key_y_lower() {
            assert!(is_confirm_key(&KeyCode::Char('y')));
        }

        #[test]
        fn test_is_confirm_key_y_upper() {
            assert!(is_confirm_key(&KeyCode::Char('Y')));
        }

        #[test]
        fn test_is_confirm_key_enter() {
            assert!(is_confirm_key(&KeyCode::Enter));
        }

        #[test]
        fn test_is_confirm_key_false() {
            assert!(!is_confirm_key(&KeyCode::Char('n')));
            assert!(!is_confirm_key(&KeyCode::Esc));
        }

        #[test]
        fn test_is_cancel_key_n_lower() {
            assert!(is_cancel_key(&KeyCode::Char('n')));
        }

        #[test]
        fn test_is_cancel_key_n_upper() {
            assert!(is_cancel_key(&KeyCode::Char('N')));
        }

        #[test]
        fn test_is_cancel_key_esc() {
            assert!(is_cancel_key(&KeyCode::Esc));
        }

        #[test]
        fn test_is_cancel_key_false() {
            assert!(!is_cancel_key(&KeyCode::Char('y')));
            assert!(!is_cancel_key(&KeyCode::Enter));
        }

        #[test]
        fn test_is_quit_key_q_lower() {
            assert!(is_quit_key(&KeyCode::Char('q')));
        }

        #[test]
        fn test_is_quit_key_q_upper() {
            assert!(is_quit_key(&KeyCode::Char('Q')));
        }

        #[test]
        fn test_is_quit_key_false() {
            assert!(!is_quit_key(&KeyCode::Char('x')));
            assert!(!is_quit_key(&KeyCode::Esc));
        }

        #[test]
        fn test_is_help_key_question() {
            assert!(is_help_key(&KeyCode::Char('?')));
        }

        #[test]
        fn test_is_help_key_false() {
            assert!(!is_help_key(&KeyCode::Char('h')));
            assert!(!is_help_key(&KeyCode::F(1)));
        }

        #[test]
        fn test_is_refresh_key_r_lower() {
            assert!(is_refresh_key(&KeyCode::Char('r')));
        }

        #[test]
        fn test_is_refresh_key_r_upper() {
            assert!(is_refresh_key(&KeyCode::Char('R')));
        }

        #[test]
        fn test_is_refresh_key_false() {
            assert!(!is_refresh_key(&KeyCode::F(5)));
            assert!(!is_refresh_key(&KeyCode::Char('f')));
        }
    }
}