agent_core/tui/keys/
nav.rs1use crossterm::event::KeyEvent;
8
9use super::bindings::KeyBindings;
10
11pub struct NavigationHelper<'a> {
36 bindings: &'a KeyBindings,
37}
38
39impl<'a> NavigationHelper<'a> {
40 pub fn new(bindings: &'a KeyBindings) -> Self {
42 Self { bindings }
43 }
44
45 pub fn is_move_up(&self, key: &KeyEvent) -> bool {
47 KeyBindings::matches_any(&self.bindings.move_up, key)
48 }
49
50 pub fn is_move_down(&self, key: &KeyEvent) -> bool {
52 KeyBindings::matches_any(&self.bindings.move_down, key)
53 }
54
55 pub fn is_move_left(&self, key: &KeyEvent) -> bool {
57 KeyBindings::matches_any(&self.bindings.move_left, key)
58 }
59
60 pub fn is_move_right(&self, key: &KeyEvent) -> bool {
62 KeyBindings::matches_any(&self.bindings.move_right, key)
63 }
64
65 pub fn is_select(&self, key: &KeyEvent) -> bool {
67 KeyBindings::matches_any(&self.bindings.select, key)
68 }
69
70 pub fn is_cancel(&self, key: &KeyEvent) -> bool {
72 KeyBindings::matches_any(&self.bindings.cancel, key)
73 }
74
75 pub fn is_submit(&self, key: &KeyEvent) -> bool {
77 KeyBindings::matches_any(&self.bindings.submit, key)
78 }
79}
80
81#[cfg(test)]
82mod tests {
83 use super::*;
84 use crossterm::event::{KeyCode, KeyModifiers};
85
86 #[test]
87 fn test_navigation_helper_emacs_bindings() {
88 let bindings = KeyBindings::emacs();
89 let nav = NavigationHelper::new(&bindings);
90
91 let ctrl_p = KeyEvent::new(KeyCode::Char('p'), KeyModifiers::CONTROL);
93 assert!(nav.is_move_up(&ctrl_p));
94
95 let up = KeyEvent::new(KeyCode::Up, KeyModifiers::NONE);
97 assert!(nav.is_move_up(&up));
98
99 let ctrl_n = KeyEvent::new(KeyCode::Char('n'), KeyModifiers::CONTROL);
101 assert!(nav.is_move_down(&ctrl_n));
102
103 let down = KeyEvent::new(KeyCode::Down, KeyModifiers::NONE);
105 assert!(nav.is_move_down(&down));
106 }
107
108 #[test]
109 fn test_navigation_helper_select_cancel() {
110 let bindings = KeyBindings::emacs();
111 let nav = NavigationHelper::new(&bindings);
112
113 let enter = KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE);
115 assert!(nav.is_select(&enter));
116
117 let space = KeyEvent::new(KeyCode::Char(' '), KeyModifiers::NONE);
119 assert!(nav.is_select(&space));
120
121 let esc = KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE);
123 assert!(nav.is_cancel(&esc));
124 }
125
126 #[test]
127 fn test_navigation_helper_minimal_bindings() {
128 let bindings = KeyBindings::minimal();
129 let nav = NavigationHelper::new(&bindings);
130
131 let up = KeyEvent::new(KeyCode::Up, KeyModifiers::NONE);
133 assert!(nav.is_move_up(&up));
134
135 let ctrl_p = KeyEvent::new(KeyCode::Char('p'), KeyModifiers::CONTROL);
137 assert!(!nav.is_move_up(&ctrl_p));
138 }
139}