summaryrefslogtreecommitdiffstats
path: root/src/input/map_keybindings.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/input/map_keybindings.rs')
-rw-r--r--src/input/map_keybindings.rs53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/input/map_keybindings.rs b/src/input/map_keybindings.rs
new file mode 100644
index 0000000..555f333
--- /dev/null
+++ b/src/input/map_keybindings.rs
@@ -0,0 +1,53 @@
+use crossterm::event::{KeyCode, KeyModifiers};
+
+use crate::input::{Event, KeyEvent};
+
+/// Map a keybinding to a list of events.
+#[must_use]
+#[allow(clippy::string_slice, clippy::missing_panics_doc)]
+pub(crate) fn map_keybindings(bindings: &[String]) -> Vec<Event> {
+ bindings
+ .iter()
+ .map(|b| {
+ let mut key = String::from(b);
+ let mut modifiers = KeyModifiers::empty();
+ if key.contains("Control") {
+ key = key.replace("Control", "");
+ modifiers.insert(KeyModifiers::CONTROL);
+ }
+ if key.contains("Alt") {
+ key = key.replace("Alt", "");
+ modifiers.insert(KeyModifiers::ALT);
+ }
+ if key.contains("Shift") {
+ key = key.replace("Shift", "");
+ modifiers.insert(KeyModifiers::SHIFT);
+ }
+
+ let code = match key.as_str() {
+ "Backspace" => KeyCode::Backspace,
+ "BackTab" => KeyCode::BackTab,
+ "Delete" => KeyCode::Delete,
+ "Down" => KeyCode::Down,
+ "End" => KeyCode::End,
+ "Enter" => KeyCode::Enter,
+ "Esc" => KeyCode::Esc,
+ "Home" => KeyCode::Home,
+ "Insert" => KeyCode::Insert,
+ "Left" => KeyCode::Left,
+ "PageDown" => KeyCode::PageDown,
+ "PageUp" => KeyCode::PageUp,
+ "Right" => KeyCode::Right,
+ "Tab" => KeyCode::Tab,
+ "Up" => KeyCode::Up,
+ // assume that this is an F key
+ k if k.len() > 1 => {
+ let key_number = k[1..].parse::<u8>().unwrap_or(1);
+ KeyCode::F(key_number)
+ },
+ k => KeyCode::Char(k.chars().next().expect("Expected only one character from Char KeyCode")),
+ };
+ Event::Key(KeyEvent::new(code, modifiers))
+ })
+ .collect()
+}