From 83686f10539096b58910b57c9b0814517190c306 Mon Sep 17 00:00:00 2001 From: Tim Oram Date: Thu, 15 Feb 2024 10:14:03 -0330 Subject: Extract map_keybindings --- src/input.rs | 4 +++- src/input/key_bindings.rs | 54 +++----------------------------------------- src/input/map_keybindings.rs | 53 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 52 deletions(-) create mode 100644 src/input/map_keybindings.rs 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, } -/// 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 { - 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::().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 { + 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::().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() +} -- cgit v1.2.3