diff --git a/keymaps/Cargo.toml b/keymaps/Cargo.toml index 727e3cd..373cece 100644 --- a/keymaps/Cargo.toml +++ b/keymaps/Cargo.toml @@ -5,7 +5,12 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[features] +default = [] +crossterm = ["dep:crossterm"] + [dependencies] +crossterm = { version = "0.25", optional = true } log = "0.4.20" thiserror = "1.0.50" pest = "2.7.5" diff --git a/keymaps/src/key_repr/key/crossterm.rs b/keymaps/src/key_repr/key/crossterm.rs index 8730907..720a009 100644 --- a/keymaps/src/key_repr/key/crossterm.rs +++ b/keymaps/src/key_repr/key/crossterm.rs @@ -2,7 +2,6 @@ use log::{debug, info}; use crossterm::event::{Event, KeyCode, KeyEvent, KeyEventKind, KeyEventState, KeyModifiers}; - impl Into for Key { fn into(self) -> Event { let mut modifiers; @@ -30,28 +29,8 @@ impl Into for Key { let output = Event::Key(KeyEvent { code: { - // We sorta hit a edge case here, if we have a shifted char we need to tell - // crossterm about that. Thus we need to manually apply the shift modifier on the - // value. - if self.shift { - if let Some(KeyValue::Char(char)) = self.value { - let upper_case_char: char = { - let chars = char.to_uppercase().collect::>(); - if chars.len() != 1 { - unimplemented!( - " - I have no idea how we handle this case, - we'll just have to hope, that it never comes up. - " - ); - } else { - *chars.first().expect("We checked the length") - } - }; - KeyCode::Char(upper_case_char) - } else { - self.value.unwrap_or(KeyValue::Null).into() - } + if let Some(KeyValue::Char(char)) = self.value { + KeyCode::Char(char) } else { self.value.unwrap_or(KeyValue::Null).into() } @@ -85,41 +64,7 @@ impl TryFrom<&Event> for Key { } { - let key_code = key_event.code; - if output_key.shift { - // We need to deal have an edge case for shift, as the value will be in the - // shifted form (for example 'A'). If we left that, we would get "" as - // Key representation, which can't be parsed. So we simply unshift the 'A' - // here, turning the representation into: "" - if let KeyCode::Char(char) = key_code { - let lower_case_char: char = { - let chars = char.to_lowercase().collect::>(); - if chars.len() != 1 { - unimplemented!( - " - I have no idea how we handle this case, - we'll just have to hope, that it never comes up. - " - ); - } else { - *chars.first().expect("We checked the length") - } - }; - info!( - "Had to translate key ('{}') to it's lowercase variant ('{}')", - char, lower_case_char - ); - output_key.value = Some(KeyValue::Char(lower_case_char)); - } else { - debug!( - "Key ('{}') is shifted but not a character!", - Into::::into(key_code) - ); - output_key.value = Some(key_code.into()); - } - } else { - output_key.value = Some(key_code.into()); - } + output_key.value = Some(key_event.code.into()); } Ok(output_key) diff --git a/keymaps/src/key_repr/key/mod.rs b/keymaps/src/key_repr/key/mod.rs index 1580ba9..66a744d 100644 --- a/keymaps/src/key_repr/key/mod.rs +++ b/keymaps/src/key_repr/key/mod.rs @@ -1,5 +1,6 @@ #[cfg(crossterm)] mod crossterm; + mod parsing; use std::fmt::Display;