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
use crate::services::hotkey::Hotkey;
use crate::services::hotkey::HotkeyRegistry;
use crate::services::hotkey::HotkeyScope;
use crossterm::event::KeyCode;
impl HotkeyRegistry {
/// Look up a hotkey by key code and scope.
///
/// Searches for a registered hotkey that matches the given key code
/// and is active in the given scope.
///
/// # Arguments
///
/// * `key` - The key code to look up
/// * `scope` - The scope to search in
///
/// # Returns
///
/// `Some(&Hotkey)` if found, `None` otherwise.
///
/// # Example
///
/// ```rust
/// use ratatui_toolkit::services::hotkey::{Hotkey, HotkeyRegistry, HotkeyScope};
/// use crossterm::event::KeyCode;
///
/// let mut registry = HotkeyRegistry::new();
/// registry.register(Hotkey::new("q", "Quit").scope(HotkeyScope::Global));
///
/// let found = registry.lookup(&KeyCode::Char('q'), &HotkeyScope::Global);
/// assert!(found.is_some());
/// ```
pub fn lookup(&self, key: &KeyCode, scope: &HotkeyScope) -> Option<&Hotkey> {
self.hotkeys.iter().find(|hotkey| {
let key_matches = match key {
KeyCode::Char(c) => hotkey.key.to_lowercase() == c.to_string().to_lowercase(),
KeyCode::Tab => hotkey.key.to_lowercase() == "tab",
KeyCode::Enter => hotkey.key.to_lowercase() == "enter",
KeyCode::Esc => {
hotkey.key.to_lowercase() == "escape" || hotkey.key.to_lowercase() == "esc"
}
KeyCode::Up => hotkey.key.to_lowercase() == "up",
KeyCode::Down => hotkey.key.to_lowercase() == "down",
KeyCode::Left => hotkey.key.to_lowercase() == "left",
KeyCode::Right => hotkey.key.to_lowercase() == "right",
KeyCode::Backspace => hotkey.key.to_lowercase() == "backspace",
_ => false,
};
if !key_matches {
return false;
}
match &hotkey.scope {
HotkeyScope::Global => true,
HotkeyScope::Modal(m) => matches!(scope, HotkeyScope::Modal(s) if s == m),
HotkeyScope::Tab(t) => matches!(scope, HotkeyScope::Tab(s) if s == t),
HotkeyScope::Custom(c) => matches!(scope, HotkeyScope::Custom(s) if s == c),
}
})
}
/// Get hotkeys filtered by scope.
///
/// # Arguments
///
/// * `scope` - The scope to filter by
///
/// # Returns
///
/// A slice of hotkeys that are active in the given scope.
///
/// # Example
///
/// ```rust
/// use ratatui_toolkit::services::hotkey::{Hotkey, HotkeyRegistry, HotkeyScope};
///
/// let mut registry = HotkeyRegistry::new();
/// registry.register(Hotkey::new("j", "Down").scope(HotkeyScope::Tab("Markdown")));
/// registry.register(Hotkey::new("k", "Up").scope(HotkeyScope::Tab("Markdown")));
/// registry.register(Hotkey::new("q", "Quit").scope(HotkeyScope::Global));
///
/// let markdown_hotkeys = registry.get_by_scope(&HotkeyScope::Tab("Markdown"));
/// assert_eq!(markdown_hotkeys.len(), 2);
/// ```
pub fn get_by_scope(&self, scope: &HotkeyScope) -> Vec<&Hotkey> {
self.hotkeys
.iter()
.filter(|hotkey| match &hotkey.scope {
HotkeyScope::Global => true,
HotkeyScope::Modal(m) => matches!(scope, HotkeyScope::Modal(s) if s == m),
HotkeyScope::Tab(t) => matches!(scope, HotkeyScope::Tab(s) if s == t),
HotkeyScope::Custom(c) => matches!(scope, HotkeyScope::Custom(s) if s == c),
})
.collect()
}
/// Get all global hotkeys.
///
/// # Returns
///
/// A slice of all global hotkeys.
///
/// # Example
///
/// ```rust
/// use ratatui_toolkit::services::hotkey::{Hotkey, HotkeyRegistry, HotkeyScope};
///
/// let mut registry = HotkeyRegistry::new();
/// registry.register(Hotkey::new("q", "Quit").scope(HotkeyScope::Global));
/// registry.register(Hotkey::new("j", "Down").scope(HotkeyScope::Tab("Markdown")));
///
/// let global = registry.get_global();
/// assert_eq!(global.len(), 1);
/// ```
pub fn get_global(&self) -> Vec<&Hotkey> {
self.hotkeys
.iter()
.filter(|h| matches!(h.scope, HotkeyScope::Global))
.collect()
}
}