1use ratatui::crossterm::event::{KeyCode, KeyModifiers};
5
6use crate::reveal::Part;
7use crate::tui::state::Action;
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11pub struct Binding {
12 pub action: Action,
14 pub keys: &'static [(KeyCode, KeyModifiers)],
16 pub label: &'static str,
18 pub hint: &'static str,
20 pub does: &'static str,
22}
23
24const PLAIN: KeyModifiers = KeyModifiers::NONE;
25
26pub const BINDINGS: [Binding; 18] = [
28 Binding {
29 action: Action::Down,
30 keys: &[(KeyCode::Char('j'), PLAIN), (KeyCode::Down, PLAIN)],
31 label: "j",
32 hint: "down",
33 does: "Select the vCon below (also Down)",
34 },
35 Binding {
36 action: Action::Up,
37 keys: &[(KeyCode::Char('k'), PLAIN), (KeyCode::Up, PLAIN)],
38 label: "k",
39 hint: "up",
40 does: "Select the vCon above (also Up)",
41 },
42 Binding {
43 action: Action::Top,
44 keys: &[(KeyCode::Char('g'), PLAIN), (KeyCode::Home, PLAIN)],
45 label: "g",
46 hint: "first",
47 does: "Select the first vCon (also Home)",
48 },
49 Binding {
50 action: Action::Bottom,
51 keys: &[(KeyCode::Char('G'), PLAIN), (KeyCode::End, PLAIN)],
52 label: "G",
53 hint: "last",
54 does: "Select the last vCon read (also End)",
55 },
56 Binding {
57 action: Action::PageUp,
58 keys: &[(KeyCode::PageUp, PLAIN)],
59 label: "PgUp",
60 hint: "page up",
61 does: "Move up one screen of the list or of the detail view's text",
62 },
63 Binding {
64 action: Action::PageDown,
65 keys: &[(KeyCode::PageDown, PLAIN)],
66 label: "PgDn",
67 hint: "page down",
68 does: "Move down one screen of the list or of the detail view's text",
69 },
70 Binding {
71 action: Action::Open,
72 keys: &[(KeyCode::Enter, PLAIN)],
73 label: "Enter",
74 hint: "open",
75 does: "Open the selected vCon: its metadata and masked view",
76 },
77 Binding {
78 action: Action::Back,
79 keys: &[(KeyCode::Esc, PLAIN), (KeyCode::Backspace, PLAIN)],
80 label: "Esc",
81 hint: "back",
82 does: "Go back to the view before (also Backspace)",
83 },
84 Binding {
85 action: Action::Reveal(Part::Parties),
86 keys: &[(KeyCode::Char('p'), PLAIN)],
87 label: "p",
88 hint: "parties",
89 does: "Reveal the parties of the vCon open: names, numbers, addresses",
90 },
91 Binding {
92 action: Action::Reveal(Part::Subject),
93 keys: &[(KeyCode::Char('s'), PLAIN)],
94 label: "s",
95 hint: "subject",
96 does: "Reveal the subject of the vCon open",
97 },
98 Binding {
99 action: Action::Reveal(Part::Dialog),
100 keys: &[(KeyCode::Char('d'), PLAIN)],
101 label: "d",
102 hint: "dialog",
103 does: "Reveal the dialog of the vCon open: what was said",
104 },
105 Binding {
106 action: Action::Reveal(Part::Analysis),
107 keys: &[(KeyCode::Char('a'), PLAIN)],
108 label: "a",
109 hint: "analysis",
110 does: "Reveal the analysis of the vCon open, transcripts among it",
111 },
112 Binding {
113 action: Action::Reveal(Part::Links),
114 keys: &[(KeyCode::Char('l'), PLAIN)],
115 label: "l",
116 hint: "links",
117 does: "Reveal where the vCon open links to",
118 },
119 Binding {
120 action: Action::Reveal(Part::All),
121 keys: &[(KeyCode::Char('w'), PLAIN)],
122 label: "w",
123 hint: "whole",
124 does: "Reveal the whole vCon open",
125 },
126 Binding {
127 action: Action::Refresh,
128 keys: &[(KeyCode::Char('r'), PLAIN)],
129 label: "r",
130 hint: "refresh",
131 does: "Read the list again from the server",
132 },
133 Binding {
134 action: Action::More,
135 keys: &[(KeyCode::Char('n'), PLAIN)],
136 label: "n",
137 hint: "more",
138 does: "Read the next page of vCons",
139 },
140 Binding {
141 action: Action::Help,
142 keys: &[(KeyCode::Char('?'), PLAIN)],
143 label: "?",
144 hint: "help",
145 does: "Show or close this help",
146 },
147 Binding {
148 action: Action::Quit,
149 keys: &[
150 (KeyCode::Char('q'), PLAIN),
151 (KeyCode::Char('c'), KeyModifiers::CONTROL),
152 ],
153 label: "q",
154 hint: "quit",
155 does: "Quit (also Ctrl-C)",
156 },
157];
158
159#[must_use]
163pub fn action(code: KeyCode, modifiers: KeyModifiers) -> Option<Action> {
164 let modifiers = if matches!(code, KeyCode::Char(_)) {
165 modifiers.difference(KeyModifiers::SHIFT)
166 } else {
167 modifiers
168 };
169 BINDINGS
170 .iter()
171 .find(|binding| binding.keys.contains(&(code, modifiers)))
172 .map(|binding| binding.action)
173}
174
175#[must_use]
177pub fn binding(action: Action) -> Option<&'static Binding> {
178 BINDINGS.iter().find(|binding| binding.action == action)
179}
180
181#[cfg(test)]
182mod tests {
183 use ratatui::crossterm::event::{KeyCode, KeyModifiers};
184
185 use super::{BINDINGS, action};
186 use crate::tui::state::Action;
187
188 #[test]
189 fn every_action_has_a_binding() {
190 for wanted in [
191 Action::Up,
192 Action::Down,
193 Action::Top,
194 Action::Bottom,
195 Action::PageUp,
196 Action::PageDown,
197 Action::Open,
198 Action::Back,
199 Action::Refresh,
200 Action::More,
201 Action::Help,
202 Action::Quit,
203 ] {
204 assert!(
205 BINDINGS.iter().any(|b| b.action == wanted),
206 "{wanted:?} can be reached from the keyboard"
207 );
208 }
209 }
210
211 #[test]
212 fn every_listed_key_gives_its_action() {
213 for binding in &BINDINGS {
214 assert!(!binding.keys.is_empty(), "{:?} lists a key", binding.action);
215 for (code, modifiers) in binding.keys {
216 assert_eq!(
217 action(*code, *modifiers),
218 Some(binding.action),
219 "{code:?} is listed for {:?}",
220 binding.action
221 );
222 }
223 }
224 }
225
226 #[test]
227 fn no_key_is_listed_twice() {
228 let mut seen = Vec::new();
229 for binding in &BINDINGS {
230 for key in binding.keys {
231 assert!(!seen.contains(key), "{key:?} has one meaning");
232 seen.push(*key);
233 }
234 }
235 }
236
237 #[test]
238 fn each_reveal_key_reveals_its_part() {
239 use crate::reveal::Part;
240 for (key, part) in [
241 ('p', Part::Parties),
242 ('s', Part::Subject),
243 ('d', Part::Dialog),
244 ('a', Part::Analysis),
245 ('l', Part::Links),
246 ('w', Part::All),
247 ] {
248 assert_eq!(
249 action(KeyCode::Char(key), KeyModifiers::NONE),
250 Some(Action::Reveal(part)),
251 "{key}"
252 );
253 }
254 }
255
256 #[test]
258 fn no_key_reveals_a_recording() {
259 assert!(
260 BINDINGS
261 .iter()
262 .all(|b| b.action != Action::Reveal(crate::reveal::Part::Audio)),
263 "audio is the console's"
264 );
265 }
266
267 #[test]
268 fn an_upper_case_reveal_letter_is_no_key() {
269 for key in ['P', 'S', 'D', 'A', 'L', 'W'] {
270 assert_eq!(
271 action(KeyCode::Char(key), KeyModifiers::SHIFT),
272 None,
273 "{key}"
274 );
275 }
276 }
277
278 #[test]
279 fn control_c_quits() {
280 assert_eq!(
281 action(KeyCode::Char('c'), KeyModifiers::CONTROL),
282 Some(Action::Quit),
283 "the terminal's interrupt key ends the program in raw mode too"
284 );
285 }
286
287 #[test]
288 fn a_key_with_no_binding_is_no_action() {
289 assert_eq!(
290 action(KeyCode::Char('x'), KeyModifiers::NONE),
291 None,
292 "x is unbound"
293 );
294 assert_eq!(
295 action(KeyCode::Char('q'), KeyModifiers::CONTROL),
296 None,
297 "a modifier makes another key"
298 );
299 }
300
301 #[test]
302 fn shift_is_part_of_an_upper_case_letter() {
303 assert_eq!(
304 action(KeyCode::Char('G'), KeyModifiers::SHIFT),
305 Some(Action::Bottom),
306 "terminals report G with shift held"
307 );
308 assert_eq!(
309 action(KeyCode::Char('?'), KeyModifiers::SHIFT),
310 Some(Action::Help),
311 "and ? likewise"
312 );
313 }
314
315 #[test]
316 fn each_binding_says_what_it_does_and_how_it_is_written() {
317 for binding in &BINDINGS {
318 assert!(
319 !binding.label.is_empty(),
320 "{:?} has a label",
321 binding.action
322 );
323 assert!(
324 !binding.does.is_empty(),
325 "{:?} has a description",
326 binding.action
327 );
328 assert!(
329 !binding.hint.is_empty(),
330 "{:?} has a footer word",
331 binding.action
332 );
333 }
334 }
335}