summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTim Oram <dev@mitmaro.ca>2024-02-15 10:14:03 -0330
committerTim Oram <dev@mitmaro.ca>2024-02-15 20:27:06 -0330
commit83686f10539096b58910b57c9b0814517190c306 (patch)
treedb72c3612e0d7429773076e9b5eec52bed801980
parent3d63f7fa3326f2a1dd0cfa12d183742e0b33a17a (diff)
Extract map_keybindings
-rw-r--r--src/input.rs4
-rw-r--r--src/input/key_bindings.rs54
-rw-r--r--src/input/map_keybindings.rs53
3 files changed, 59 insertions, 52 deletions
diff --git a/src/input.rs b/src/input.rs
index 0111011..38663c7 100644
--- a/src/input.rs
+++ b/src/input.rs
@@ -14,6 +14,7 @@ mod event_provider;
mod input_options;
mod key_bindings;
mod key_event;
+mod map_keybindings;
mod standard_event;
mod thread;
@@ -24,8 +25,9 @@ pub(crate) use self::{
event_handler::EventHandler,
event_provider::{read_event, EventReaderFn},
input_options::InputOptions,
- key_bindings::{map_keybindings, KeyBindings},
+ key_bindings::KeyBindings,
key_event::KeyEvent,
+ map_keybindings::map_keybindings,
standard_event::StandardEvent,
thread::{State, Thread, THREAD_NAME},
};
diff --git a/src/input/key_bindings.rs b/src/input/key_bindings.rs
index 66cb122..bf45601 100644
--- a/src/input/key_bindings.rs
+++ b/src/input/key_bindings.rs
@@ -1,4 +1,4 @@
-use crate::input::{Event, KeyCode, KeyEvent, KeyModifiers};
+use crate::input::{map_keybindings, Event};
/// Represents a mapping between an input event and an action.
#[derive(Debug)]
@@ -100,56 +100,6 @@ pub(crate) struct KeyBindings {
pub(crate) fixup_keep_message_with_editor: Vec<Event>,
}
-/// 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()
-}
-
impl KeyBindings {
/// Create a new instance from the configuration keybindings.
#[must_use]
@@ -206,9 +156,11 @@ impl KeyBindings {
#[cfg(test)]
mod tests {
+ use crossterm::event::{KeyCode, KeyModifiers};
use rstest::rstest;
use super::*;
+ use crate::input::KeyEvent;
#[test]
fn new() {
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()
+}