-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathkeybindings.rs
More file actions
632 lines (604 loc) · 20 KB
/
keybindings.rs
File metadata and controls
632 lines (604 loc) · 20 KB
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
//! Centralized keyboard shortcuts registry.
//!
//! This module provides a single source of truth for all keyboard shortcuts
//! used in the Operator TUI. It is consumed by:
//! - `HelpDialog` for displaying help text
//! - `ShortcutsDocGenerator` for generating documentation
use crossterm::event::{KeyCode, KeyModifiers};
/// A keyboard shortcut definition
#[derive(Debug, Clone)]
pub struct Shortcut {
/// Primary key for this shortcut
pub key: KeyCode,
/// Modifier keys required (e.g., Shift, Ctrl)
pub modifiers: KeyModifiers,
/// Alternative key (e.g., lowercase variant or arrow key)
pub alt_key: Option<KeyCode>,
/// Human-readable description of what this shortcut does
pub description: &'static str,
/// Category for grouping in help/docs
pub category: ShortcutCategory,
/// Context where this shortcut is active
pub context: ShortcutContext,
}
/// Categories for organizing shortcuts
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ShortcutCategory {
General,
Navigation,
Actions,
Dialogs,
}
/// Contexts where shortcuts are active
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ShortcutContext {
/// Active in the main dashboard
Global,
/// Active when the status panel is focused
StatusPanel,
/// Active in session preview mode
Preview,
/// Active in the launch confirmation dialog
LaunchDialog,
}
impl ShortcutCategory {
/// Display name for this category
pub fn display_name(&self) -> &'static str {
match self {
ShortcutCategory::General => "General",
ShortcutCategory::Navigation => "Navigation",
ShortcutCategory::Actions => "Actions",
ShortcutCategory::Dialogs => "Dialogs",
}
}
/// All categories in display order
pub fn all() -> &'static [ShortcutCategory] {
&[
ShortcutCategory::General,
ShortcutCategory::Navigation,
ShortcutCategory::Actions,
ShortcutCategory::Dialogs,
]
}
}
impl ShortcutContext {
/// Display name for this context
pub fn display_name(&self) -> &'static str {
match self {
ShortcutContext::Global => "Dashboard",
ShortcutContext::StatusPanel => "Status Panel",
ShortcutContext::Preview => "Session Preview",
ShortcutContext::LaunchDialog => "Launch Dialog",
}
}
/// All contexts in display order
pub fn all() -> &'static [ShortcutContext] {
&[
ShortcutContext::Global,
ShortcutContext::StatusPanel,
ShortcutContext::Preview,
ShortcutContext::LaunchDialog,
]
}
}
impl Shortcut {
/// Format key for display (e.g., "q", "Tab", "Shift+Enter", "j/↓")
pub fn key_display(&self) -> String {
let mut prefix = String::new();
if self.modifiers.contains(KeyModifiers::CONTROL) {
prefix.push_str("Ctrl+");
}
if self.modifiers.contains(KeyModifiers::SHIFT) {
prefix.push_str("Shift+");
}
if self.modifiers.contains(KeyModifiers::ALT) {
prefix.push_str("Alt+");
}
let primary = format!("{}{}", prefix, format_keycode(&self.key));
match &self.alt_key {
Some(alt) => format!("{}/{}", primary, format_keycode(alt)),
None => primary,
}
}
/// Format key for help dialog (left-padded to 7 chars)
pub fn key_display_padded(&self) -> String {
format!("{:<7}", self.key_display())
}
}
/// Format a `KeyCode` for display
fn format_keycode(key: &KeyCode) -> String {
match key {
KeyCode::Char(c) => c.to_string(),
KeyCode::Enter => "Enter".to_string(),
KeyCode::Esc => "Esc".to_string(),
KeyCode::Tab => "Tab".to_string(),
KeyCode::BackTab => "Shift+Tab".to_string(),
KeyCode::Up => "↑".to_string(),
KeyCode::Down => "↓".to_string(),
KeyCode::Left => "←".to_string(),
KeyCode::Right => "→".to_string(),
KeyCode::PageUp => "PgUp".to_string(),
KeyCode::PageDown => "PgDn".to_string(),
KeyCode::Home => "Home".to_string(),
KeyCode::End => "End".to_string(),
KeyCode::Delete => "Del".to_string(),
KeyCode::Backspace => "Backspace".to_string(),
KeyCode::F(n) => format!("F{n}"),
_ => format!("{key:?}"),
}
}
/// Static registry of all keyboard shortcuts
pub static SHORTCUTS: &[Shortcut] = &[
// === Global Context ===
// General
Shortcut {
key: KeyCode::Char('q'),
modifiers: KeyModifiers::NONE,
alt_key: None,
description: "Quit Operator",
category: ShortcutCategory::General,
context: ShortcutContext::Global,
},
Shortcut {
key: KeyCode::Char('?'),
modifiers: KeyModifiers::NONE,
alt_key: None,
description: "Toggle help",
category: ShortcutCategory::General,
context: ShortcutContext::Global,
},
// Navigation
Shortcut {
key: KeyCode::Tab,
modifiers: KeyModifiers::NONE,
alt_key: None,
description: "Switch between panels",
category: ShortcutCategory::Navigation,
context: ShortcutContext::Global,
},
Shortcut {
key: KeyCode::Char('j'),
modifiers: KeyModifiers::NONE,
alt_key: Some(KeyCode::Down),
description: "Move down",
category: ShortcutCategory::Navigation,
context: ShortcutContext::Global,
},
Shortcut {
key: KeyCode::Char('k'),
modifiers: KeyModifiers::NONE,
alt_key: Some(KeyCode::Up),
description: "Move up",
category: ShortcutCategory::Navigation,
context: ShortcutContext::Global,
},
Shortcut {
key: KeyCode::Char('Q'),
modifiers: KeyModifiers::NONE,
alt_key: None,
description: "Focus Queue panel",
category: ShortcutCategory::Navigation,
context: ShortcutContext::Global,
},
Shortcut {
key: KeyCode::Char('A'),
modifiers: KeyModifiers::NONE,
alt_key: Some(KeyCode::Char('a')),
description: "Focus Agents panel",
category: ShortcutCategory::Navigation,
context: ShortcutContext::Global,
},
Shortcut {
key: KeyCode::Char('h'),
modifiers: KeyModifiers::NONE,
alt_key: Some(KeyCode::Left),
description: "Previous panel",
category: ShortcutCategory::Navigation,
context: ShortcutContext::Global,
},
Shortcut {
key: KeyCode::Char('l'),
modifiers: KeyModifiers::NONE,
alt_key: Some(KeyCode::Right),
description: "Next panel",
category: ShortcutCategory::Navigation,
context: ShortcutContext::Global,
},
// Actions
Shortcut {
key: KeyCode::Enter,
modifiers: KeyModifiers::NONE,
alt_key: None,
description: "Select / Confirm",
category: ShortcutCategory::Actions,
context: ShortcutContext::Global,
},
Shortcut {
key: KeyCode::Enter,
modifiers: KeyModifiers::SHIFT,
alt_key: None,
description: "Auto-launch (delegator chain)",
category: ShortcutCategory::Actions,
context: ShortcutContext::Global,
},
Shortcut {
key: KeyCode::Esc,
modifiers: KeyModifiers::NONE,
alt_key: None,
description: "Cancel / Close",
category: ShortcutCategory::Actions,
context: ShortcutContext::Global,
},
Shortcut {
key: KeyCode::Char('L'),
modifiers: KeyModifiers::NONE,
alt_key: None,
description: "Launch selected ticket",
category: ShortcutCategory::Actions,
context: ShortcutContext::Global,
},
Shortcut {
key: KeyCode::Char('P'),
modifiers: KeyModifiers::NONE,
alt_key: Some(KeyCode::Char('p')),
description: "Pause queue processing",
category: ShortcutCategory::Actions,
context: ShortcutContext::Global,
},
Shortcut {
key: KeyCode::Char('R'),
modifiers: KeyModifiers::NONE,
alt_key: Some(KeyCode::Char('r')),
description: "Resume queue processing",
category: ShortcutCategory::Actions,
context: ShortcutContext::Global,
},
Shortcut {
key: KeyCode::Char('S'),
modifiers: KeyModifiers::NONE,
alt_key: None,
description: "Sync kanban collections",
category: ShortcutCategory::Actions,
context: ShortcutContext::Global,
},
Shortcut {
key: KeyCode::Char('Y'),
modifiers: KeyModifiers::NONE,
alt_key: Some(KeyCode::Char('y')),
description: "Approve review (agents panel)",
category: ShortcutCategory::Actions,
context: ShortcutContext::Global,
},
Shortcut {
key: KeyCode::Char('X'),
modifiers: KeyModifiers::NONE,
alt_key: Some(KeyCode::Char('x')),
description: "Reject review (agents panel)",
category: ShortcutCategory::Actions,
context: ShortcutContext::Global,
},
Shortcut {
key: KeyCode::Char('W'),
modifiers: KeyModifiers::NONE,
alt_key: Some(KeyCode::Char('w')),
description: "Toggle Backstage server",
category: ShortcutCategory::Actions,
context: ShortcutContext::Global,
},
Shortcut {
key: KeyCode::Char('V'),
modifiers: KeyModifiers::NONE,
alt_key: Some(KeyCode::Char('v')),
description: "Show session preview",
category: ShortcutCategory::Actions,
context: ShortcutContext::Global,
},
Shortcut {
key: KeyCode::Char('F'),
modifiers: KeyModifiers::NONE,
alt_key: None,
description: "Focus cmux window",
category: ShortcutCategory::Actions,
context: ShortcutContext::Global,
},
// Dialogs
Shortcut {
key: KeyCode::Char('C'),
modifiers: KeyModifiers::NONE,
alt_key: None,
description: "Create new ticket",
category: ShortcutCategory::Dialogs,
context: ShortcutContext::Global,
},
Shortcut {
key: KeyCode::Char('J'),
modifiers: KeyModifiers::NONE,
alt_key: None,
description: "Open Projects menu",
category: ShortcutCategory::Dialogs,
context: ShortcutContext::Global,
},
Shortcut {
key: KeyCode::Char('T'),
modifiers: KeyModifiers::NONE,
alt_key: Some(KeyCode::Char('t')),
description: "Switch issue type collection",
category: ShortcutCategory::Dialogs,
context: ShortcutContext::Global,
},
Shortcut {
key: KeyCode::Char('K'),
modifiers: KeyModifiers::NONE,
alt_key: None,
description: "Open Kanban providers view",
category: ShortcutCategory::Dialogs,
context: ShortcutContext::Global,
},
// === Status Panel Context ===
Shortcut {
key: KeyCode::Enter,
modifiers: KeyModifiers::NONE,
alt_key: None,
description: "Activate (A)",
category: ShortcutCategory::Actions,
context: ShortcutContext::StatusPanel,
},
Shortcut {
key: KeyCode::Esc,
modifiers: KeyModifiers::NONE,
alt_key: Some(KeyCode::Backspace),
description: "Go back (B)",
category: ShortcutCategory::Navigation,
context: ShortcutContext::StatusPanel,
},
Shortcut {
key: KeyCode::Enter,
modifiers: KeyModifiers::SHIFT,
alt_key: None,
description: "Special action (X) *",
category: ShortcutCategory::Actions,
context: ShortcutContext::StatusPanel,
},
Shortcut {
key: KeyCode::Enter,
modifiers: KeyModifiers::CONTROL,
alt_key: None,
description: "Refresh (Y) \u{27F3}",
category: ShortcutCategory::Actions,
context: ShortcutContext::StatusPanel,
},
// === Preview Context ===
Shortcut {
key: KeyCode::Char('g'),
modifiers: KeyModifiers::NONE,
alt_key: None,
description: "Scroll to top",
category: ShortcutCategory::Navigation,
context: ShortcutContext::Preview,
},
Shortcut {
key: KeyCode::Char('G'),
modifiers: KeyModifiers::NONE,
alt_key: None,
description: "Scroll to bottom",
category: ShortcutCategory::Navigation,
context: ShortcutContext::Preview,
},
Shortcut {
key: KeyCode::PageUp,
modifiers: KeyModifiers::NONE,
alt_key: None,
description: "Page up",
category: ShortcutCategory::Navigation,
context: ShortcutContext::Preview,
},
Shortcut {
key: KeyCode::PageDown,
modifiers: KeyModifiers::NONE,
alt_key: None,
description: "Page down",
category: ShortcutCategory::Navigation,
context: ShortcutContext::Preview,
},
Shortcut {
key: KeyCode::Esc,
modifiers: KeyModifiers::NONE,
alt_key: Some(KeyCode::Char('q')),
description: "Close preview",
category: ShortcutCategory::Actions,
context: ShortcutContext::Preview,
},
// === Launch Dialog Context ===
Shortcut {
key: KeyCode::Char('L'),
modifiers: KeyModifiers::NONE,
alt_key: Some(KeyCode::Char('l')),
description: "Launch agent",
category: ShortcutCategory::Actions,
context: ShortcutContext::LaunchDialog,
},
Shortcut {
key: KeyCode::Char('V'),
modifiers: KeyModifiers::NONE,
alt_key: Some(KeyCode::Char('v')),
description: "View ticket ($VISUAL or open)",
category: ShortcutCategory::Actions,
context: ShortcutContext::LaunchDialog,
},
Shortcut {
key: KeyCode::Char('E'),
modifiers: KeyModifiers::NONE,
alt_key: Some(KeyCode::Char('e')),
description: "Edit ticket ($EDITOR)",
category: ShortcutCategory::Actions,
context: ShortcutContext::LaunchDialog,
},
Shortcut {
key: KeyCode::Char('N'),
modifiers: KeyModifiers::NONE,
alt_key: Some(KeyCode::Char('n')),
description: "Cancel",
category: ShortcutCategory::Actions,
context: ShortcutContext::LaunchDialog,
},
Shortcut {
key: KeyCode::Char('M'),
modifiers: KeyModifiers::NONE,
alt_key: Some(KeyCode::Char('m')),
description: "Cycle provider/model",
category: ShortcutCategory::Actions,
context: ShortcutContext::LaunchDialog,
},
Shortcut {
key: KeyCode::Char('D'),
modifiers: KeyModifiers::NONE,
alt_key: Some(KeyCode::Char('d')),
description: "Toggle Docker mode",
category: ShortcutCategory::Actions,
context: ShortcutContext::LaunchDialog,
},
Shortcut {
key: KeyCode::Char('Y'),
modifiers: KeyModifiers::NONE,
alt_key: Some(KeyCode::Char('y')),
description: "Toggle Auto-accept (YOLO)",
category: ShortcutCategory::Actions,
context: ShortcutContext::LaunchDialog,
},
];
/// Get all shortcuts for a given context
#[allow(dead_code)]
pub fn shortcuts_for_context(context: ShortcutContext) -> impl Iterator<Item = &'static Shortcut> {
SHORTCUTS.iter().filter(move |s| s.context == context)
}
/// Get shortcuts grouped by category for a given context
pub fn shortcuts_by_category_for_context(
context: ShortcutContext,
) -> Vec<(ShortcutCategory, Vec<&'static Shortcut>)> {
let mut result = Vec::new();
for category in ShortcutCategory::all() {
let shortcuts: Vec<&Shortcut> = SHORTCUTS
.iter()
.filter(|s| s.context == context && s.category == *category)
.collect();
if !shortcuts.is_empty() {
result.push((*category, shortcuts));
}
}
result
}
/// Grouped shortcuts by category
pub type GroupedByCategory = Vec<(ShortcutCategory, Vec<&'static Shortcut>)>;
/// Get all shortcuts grouped by context, then by category
pub fn all_shortcuts_grouped() -> Vec<(ShortcutContext, GroupedByCategory)> {
ShortcutContext::all()
.iter()
.map(|ctx| (*ctx, shortcuts_by_category_for_context(*ctx)))
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_all_shortcuts_have_descriptions() {
for shortcut in SHORTCUTS {
assert!(
!shortcut.description.is_empty(),
"Shortcut {:?} has empty description",
shortcut.key
);
}
}
#[test]
fn test_key_display_single_key() {
let shortcut = Shortcut {
key: KeyCode::Char('q'),
modifiers: KeyModifiers::NONE,
alt_key: None,
description: "Test",
category: ShortcutCategory::General,
context: ShortcutContext::Global,
};
assert_eq!(shortcut.key_display(), "q");
}
#[test]
fn test_key_display_with_alt() {
let shortcut = Shortcut {
key: KeyCode::Char('j'),
modifiers: KeyModifiers::NONE,
alt_key: Some(KeyCode::Down),
description: "Test",
category: ShortcutCategory::Navigation,
context: ShortcutContext::Global,
};
assert_eq!(shortcut.key_display(), "j/↓");
}
#[test]
fn test_key_display_with_modifiers() {
let shortcut = Shortcut {
key: KeyCode::Enter,
modifiers: KeyModifiers::SHIFT,
alt_key: None,
description: "Test",
category: ShortcutCategory::Actions,
context: ShortcutContext::StatusPanel,
};
assert_eq!(shortcut.key_display(), "Shift+Enter");
let shortcut = Shortcut {
key: KeyCode::Enter,
modifiers: KeyModifiers::CONTROL,
alt_key: None,
description: "Test",
category: ShortcutCategory::Actions,
context: ShortcutContext::StatusPanel,
};
assert_eq!(shortcut.key_display(), "Ctrl+Enter");
}
#[test]
fn test_key_display_special_keys() {
assert_eq!(format_keycode(&KeyCode::Enter), "Enter");
assert_eq!(format_keycode(&KeyCode::Esc), "Esc");
assert_eq!(format_keycode(&KeyCode::Tab), "Tab");
assert_eq!(format_keycode(&KeyCode::PageUp), "PgUp");
assert_eq!(format_keycode(&KeyCode::PageDown), "PgDn");
}
#[test]
fn test_shortcuts_for_context() {
let global_shortcuts: Vec<_> = shortcuts_for_context(ShortcutContext::Global).collect();
assert!(!global_shortcuts.is_empty());
assert!(global_shortcuts
.iter()
.all(|s| s.context == ShortcutContext::Global));
}
#[test]
fn test_shortcuts_by_category_for_context() {
let grouped = shortcuts_by_category_for_context(ShortcutContext::Global);
assert!(!grouped.is_empty());
// Should have at least General, Navigation, Actions
let categories: Vec<_> = grouped.iter().map(|(cat, _)| cat).collect();
assert!(categories.contains(&&ShortcutCategory::General));
assert!(categories.contains(&&ShortcutCategory::Navigation));
assert!(categories.contains(&&ShortcutCategory::Actions));
}
#[test]
fn test_category_display_names() {
assert_eq!(ShortcutCategory::General.display_name(), "General");
assert_eq!(ShortcutCategory::Navigation.display_name(), "Navigation");
assert_eq!(ShortcutCategory::Actions.display_name(), "Actions");
assert_eq!(ShortcutCategory::Dialogs.display_name(), "Dialogs");
}
#[test]
fn test_context_display_names() {
assert_eq!(ShortcutContext::Global.display_name(), "Dashboard");
assert_eq!(ShortcutContext::Preview.display_name(), "Session Preview");
assert_eq!(
ShortcutContext::LaunchDialog.display_name(),
"Launch Dialog"
);
}
#[test]
fn test_all_shortcuts_grouped() {
let grouped = all_shortcuts_grouped();
assert_eq!(grouped.len(), 4); // Global, StatusPanel, Preview, LaunchDialog
}
}