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
//! crossterm input -> Intent mapping for the TUI event loop.
//!
//! Defines the Intent enum (what the user wants to do) and maps crossterm
//! KeyEvents to Intents. Pure: no I/O beyond receiving the event.
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
/// What the user intends to do, independent of the actual key binding.
// spec: TUI-11
#[derive(Debug, Clone, PartialEq, Eq)]
#[allow(dead_code)] // all variants are part of the public TUI event API
pub enum Intent {
/// Move selection up.
MoveUp,
/// Move selection down.
MoveDown,
/// Page up.
PageUp,
/// Page down.
PageDown,
/// Expand the selected node.
Expand,
/// Collapse the selected node (or jump to parent).
Collapse,
/// Toggle expand/collapse on the selected node.
ToggleExpand,
/// Open the details-and-actions dialog for the focused node (TUI-26).
OpenDialog,
/// Jump to the search box.
JumpToSearch,
/// Append a character to the search string.
SearchChar(char),
/// Delete the last character of the search string.
SearchBackspace,
/// Clear the search string.
SearchClear,
/// Submit (close) the search box.
SearchSubmit,
/// Confirm a pending action.
ConfirmAction,
/// Cancel a pending action.
CancelAction,
/// Install the selected available item (TUI-20).
ActionLearn,
/// Uninstall the selected installed item (TUI-20).
ActionForget,
/// Sync all sources (TUI-22).
ActionSync,
/// Upgrade pending items (TUI-22).
ActionUpgrade,
/// Meld a source (TUI-21).
ActionMeld,
/// Unmeld a source (TUI-21).
ActionUnmeld,
/// Type a character into the spec-input box (TUI-30).
SpecInputChar(char),
/// Delete the last character in the spec-input box (TUI-30).
SpecInputBackspace,
/// Submit the spec-input box (TUI-30).
SpecInputSubmit,
/// Preview result ready: spec and name from the shallow clone (TUI-30).
PreviewReady { spec: String, name: String },
/// Preview failed with the given error message (TUI-30).
PreviewError { message: String },
/// Open the lobes management modal (TUI-23, CLI-111).
// spec: TUI-23
ActionLobes,
/// Initiate adding a lobe from within the lobes modal (TUI-23, CLI-112).
// spec: TUI-23
ActionLobeAdd,
/// Initiate removing the selected lobe from within the lobes modal (TUI-23, CLI-113).
// spec: TUI-23
ActionLobeRemove,
/// Type a character into the lobe-path input box (TUI-23).
// spec: TUI-23
LobeInputChar(char),
/// Delete the last character in the lobe-path input box (TUI-23).
// spec: TUI-23
LobeInputBackspace,
/// Submit the lobe-path input box (TUI-23).
// spec: TUI-23
LobeInputSubmit,
/// Move selection up within the lobes modal list.
LobeSelectUp,
/// Move selection down within the lobes modal list.
LobeSelectDown,
/// Type a character into the namespace-input box (TUI-53).
// spec: TUI-53
NamespaceInputChar(char),
/// Delete the last character in the namespace-input box (TUI-53).
// spec: TUI-53
NamespaceInputBackspace,
/// Quit the TUI.
Quit,
/// No recognized binding.
None,
}
/// Map a crossterm KeyEvent to an Intent.
// spec: TUI-11
pub fn key_to_intent(key: KeyEvent) -> Intent {
match (key.code, key.modifiers) {
// Navigation
(KeyCode::Up, _) | (KeyCode::Char('k'), KeyModifiers::NONE) => Intent::MoveUp,
(KeyCode::Down, _) | (KeyCode::Char('j'), KeyModifiers::NONE) => Intent::MoveDown,
(KeyCode::PageUp, _) | (KeyCode::Char('u'), KeyModifiers::CONTROL) => Intent::PageUp,
(KeyCode::PageDown, _) | (KeyCode::Char('d'), KeyModifiers::CONTROL) => Intent::PageDown,
(KeyCode::Right, _) | (KeyCode::Char('l'), KeyModifiers::NONE) => Intent::Expand,
(KeyCode::Left, _) | (KeyCode::Char('h'), KeyModifiers::NONE) => Intent::Collapse,
// Enter opens the details/action dialog (TUI-26); Space toggles expansion,
// since Enter no longer does on a source or item.
(KeyCode::Enter, _) => Intent::OpenDialog,
(KeyCode::Char(' '), KeyModifiers::NONE) => Intent::ToggleExpand,
// Search
(KeyCode::Char('/'), KeyModifiers::NONE) => Intent::JumpToSearch,
(KeyCode::Esc, _) => Intent::SearchClear,
(KeyCode::Backspace, _) => Intent::SearchBackspace,
(KeyCode::Tab, _) => Intent::SearchSubmit,
// Actions
(KeyCode::Char('i'), KeyModifiers::NONE) => Intent::ActionLearn,
(KeyCode::Char('d'), KeyModifiers::NONE) => Intent::ActionForget,
(KeyCode::Char('s'), KeyModifiers::NONE) => Intent::ActionSync,
(KeyCode::Char('u'), KeyModifiers::NONE) => Intent::ActionUpgrade,
(KeyCode::Char('m'), KeyModifiers::NONE) => Intent::ActionMeld,
(KeyCode::Char('M'), KeyModifiers::SHIFT) => Intent::ActionUnmeld,
// Lobe management: `C` opens the lobes modal (TUI-23).
// spec: TUI-23
(KeyCode::Char('C'), KeyModifiers::SHIFT) => Intent::ActionLobes,
// Confirm / cancel
(KeyCode::Char('y'), KeyModifiers::NONE) => Intent::ConfirmAction,
(KeyCode::Char('n'), KeyModifiers::NONE) => Intent::CancelAction,
// Quit
(KeyCode::Char('q'), KeyModifiers::NONE) | (KeyCode::Char('Q'), KeyModifiers::SHIFT) => {
Intent::Quit
}
(KeyCode::Char('c'), KeyModifiers::CONTROL) => Intent::Quit,
// Note: Ctrl+C maps to Quit here, not SearchClear (ESC handles clear).
// Pass other chars through to search if they are printable
(KeyCode::Char(c), KeyModifiers::NONE | KeyModifiers::SHIFT) => Intent::SearchChar(c),
_ => Intent::None,
}
}
#[cfg(test)]
mod tests {
use super::*;
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
fn key(code: KeyCode) -> KeyEvent {
KeyEvent::new(code, KeyModifiers::NONE)
}
fn key_mod(code: KeyCode, mods: KeyModifiers) -> KeyEvent {
KeyEvent::new(code, mods)
}
#[test]
fn up_arrow_maps_to_move_up() {
// spec: TUI-11
assert_eq!(key_to_intent(key(KeyCode::Up)), Intent::MoveUp);
}
#[test]
fn k_maps_to_move_up() {
// spec: TUI-11
assert_eq!(key_to_intent(key(KeyCode::Char('k'))), Intent::MoveUp);
}
#[test]
fn down_arrow_maps_to_move_down() {
// spec: TUI-11
assert_eq!(key_to_intent(key(KeyCode::Down)), Intent::MoveDown);
}
#[test]
fn j_maps_to_move_down() {
// spec: TUI-11
assert_eq!(key_to_intent(key(KeyCode::Char('j'))), Intent::MoveDown);
}
#[test]
fn right_arrow_maps_to_expand() {
// spec: TUI-11
assert_eq!(key_to_intent(key(KeyCode::Right)), Intent::Expand);
}
#[test]
fn left_arrow_maps_to_collapse() {
// spec: TUI-11
assert_eq!(key_to_intent(key(KeyCode::Left)), Intent::Collapse);
}
#[test]
fn page_up_maps_to_page_up() {
// spec: TUI-11
assert_eq!(key_to_intent(key(KeyCode::PageUp)), Intent::PageUp);
}
#[test]
fn page_down_maps_to_page_down() {
// spec: TUI-11
assert_eq!(key_to_intent(key(KeyCode::PageDown)), Intent::PageDown);
}
#[test]
fn enter_maps_to_open_dialog() {
// spec: TUI-26 - Enter opens the details/action dialog (not expand-toggle).
assert_eq!(key_to_intent(key(KeyCode::Enter)), Intent::OpenDialog);
}
#[test]
fn enter_with_any_modifier_still_opens_dialog() {
// spec: TUI-26 - Enter maps with a wildcard modifier, so a modified Enter
// (e.g. Shift+Enter from some terminals) still opens the dialog rather than
// falling through to the printable-char/search arm.
assert_eq!(
key_to_intent(key_mod(KeyCode::Enter, KeyModifiers::SHIFT)),
Intent::OpenDialog
);
assert_eq!(
key_to_intent(key_mod(KeyCode::Enter, KeyModifiers::CONTROL)),
Intent::OpenDialog
);
}
#[test]
fn space_maps_to_toggle_expand() {
// spec: TUI-11 - Space toggles expansion now that Enter opens the dialog.
assert_eq!(key_to_intent(key(KeyCode::Char(' '))), Intent::ToggleExpand);
}
#[test]
fn slash_maps_to_jump_to_search() {
// spec: TUI-11
assert_eq!(key_to_intent(key(KeyCode::Char('/'))), Intent::JumpToSearch);
}
#[test]
fn esc_maps_to_search_clear() {
// spec: TUI-11
assert_eq!(key_to_intent(key(KeyCode::Esc)), Intent::SearchClear);
}
#[test]
fn backspace_maps_to_search_backspace() {
// spec: TUI-11
assert_eq!(
key_to_intent(key(KeyCode::Backspace)),
Intent::SearchBackspace
);
}
#[test]
fn q_maps_to_quit() {
// spec: TUI-41
assert_eq!(key_to_intent(key(KeyCode::Char('q'))), Intent::Quit);
}
#[test]
fn ctrl_c_maps_to_quit() {
// spec: TUI-41
assert_eq!(
key_to_intent(key_mod(KeyCode::Char('c'), KeyModifiers::CONTROL)),
Intent::Quit
);
}
#[test]
fn i_maps_to_learn() {
// spec: TUI-20
assert_eq!(key_to_intent(key(KeyCode::Char('i'))), Intent::ActionLearn);
}
#[test]
fn d_maps_to_forget() {
// spec: TUI-20
assert_eq!(key_to_intent(key(KeyCode::Char('d'))), Intent::ActionForget);
}
#[test]
fn s_maps_to_sync() {
// spec: TUI-22
assert_eq!(key_to_intent(key(KeyCode::Char('s'))), Intent::ActionSync);
}
#[test]
fn u_maps_to_upgrade() {
// spec: TUI-22
assert_eq!(
key_to_intent(key(KeyCode::Char('u'))),
Intent::ActionUpgrade
);
}
#[test]
fn m_maps_to_meld() {
// spec: TUI-21
assert_eq!(key_to_intent(key(KeyCode::Char('m'))), Intent::ActionMeld);
}
#[test]
fn shift_m_maps_to_unmeld() {
// spec: TUI-21 - unmeld is `M` (Shift-m), pairing with lowercase `m` meld.
assert_eq!(
key_to_intent(key_mod(KeyCode::Char('M'), KeyModifiers::SHIFT)),
Intent::ActionUnmeld
);
}
#[test]
fn y_maps_to_confirm_action() {
// spec: TUI-24
assert_eq!(
key_to_intent(key(KeyCode::Char('y'))),
Intent::ConfirmAction
);
}
#[test]
fn n_maps_to_cancel_action() {
// spec: TUI-24
assert_eq!(key_to_intent(key(KeyCode::Char('n'))), Intent::CancelAction);
}
#[test]
fn printable_char_becomes_search_char() {
// spec: TUI-14
assert_eq!(
key_to_intent(key(KeyCode::Char('r'))),
Intent::SearchChar('r')
);
assert_eq!(
key_to_intent(key(KeyCode::Char('0'))),
Intent::SearchChar('0')
);
}
// spec: TUI-30 - spec-input intents are defined in the enum and match the
// expected variants (the event loop dispatches them when spec_input_active).
#[test]
fn spec_input_intents_are_distinct_and_constructible() {
// spec: TUI-30
// Verify the spec-input intents exist and compare equal to themselves.
let char_intent = Intent::SpecInputChar('a');
let bs_intent = Intent::SpecInputBackspace;
let sub_intent = Intent::SpecInputSubmit;
assert_eq!(char_intent, Intent::SpecInputChar('a'));
assert_ne!(char_intent, Intent::SpecInputChar('b'));
assert_eq!(bs_intent, Intent::SpecInputBackspace);
assert_eq!(sub_intent, Intent::SpecInputSubmit);
}
#[test]
fn preview_ready_and_error_intents_carry_payload() {
// spec: TUI-30
let ready = Intent::PreviewReady {
spec: "github.com/a/b".to_string(),
name: "b (3 items)".to_string(),
};
let err = Intent::PreviewError {
message: "clone failed".to_string(),
};
// These must not equal each other or their components.
assert_ne!(std::mem::discriminant(&ready), std::mem::discriminant(&err));
if let Intent::PreviewReady { spec, name } = &ready {
assert_eq!(spec, "github.com/a/b");
assert_eq!(name, "b (3 items)");
} else {
panic!("expected PreviewReady");
}
if let Intent::PreviewError { message } = &err {
assert_eq!(message, "clone failed");
} else {
panic!("expected PreviewError");
}
}
// --- TUI-23: lobe management key bindings ---
#[test]
fn shift_c_maps_to_action_lobes() {
// spec: TUI-23 CLI-111 - `C` (Shift+C) opens the lobes modal.
assert_eq!(
key_to_intent(key_mod(KeyCode::Char('C'), KeyModifiers::SHIFT)),
Intent::ActionLobes
);
}
#[test]
fn lobe_input_intents_are_distinct_and_constructible() {
// spec: TUI-23 CLI-112 CLI-113 - lobe-input intents exist and are comparable.
let char_intent = Intent::LobeInputChar('x');
let bs_intent = Intent::LobeInputBackspace;
let sub_intent = Intent::LobeInputSubmit;
assert_eq!(char_intent, Intent::LobeInputChar('x'));
assert_ne!(char_intent, Intent::LobeInputChar('y'));
assert_eq!(bs_intent, Intent::LobeInputBackspace);
assert_eq!(sub_intent, Intent::LobeInputSubmit);
}
#[test]
fn action_lobe_add_and_remove_intents_exist() {
// spec: TUI-23 CLI-112 CLI-113 - ActionLobeAdd and ActionLobeRemove are
// distinct Intent variants (used from the lobes modal, not from global keys).
assert_ne!(
std::mem::discriminant(&Intent::ActionLobeAdd),
std::mem::discriminant(&Intent::ActionLobeRemove)
);
assert_ne!(
std::mem::discriminant(&Intent::ActionLobeAdd),
std::mem::discriminant(&Intent::ActionLobes)
);
}
}