fix(keymaps): Reimplement crossterm integration

This commit is contained in:
Benedikt Peetz 2023-11-07 20:09:56 +01:00
parent 8d3a421e4c
commit f3b3cada71
Signed by: bpeetz
GPG Key ID: A5E94010C3A642AD
3 changed files with 9 additions and 58 deletions

View File

@ -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"

View File

@ -2,7 +2,6 @@ use log::{debug, info};
use crossterm::event::{Event, KeyCode, KeyEvent, KeyEventKind, KeyEventState, KeyModifiers};
impl Into<Event> for Key {
fn into(self) -> Event {
let mut modifiers;
@ -30,28 +29,8 @@ impl Into<Event> 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::<Vec<char>>();
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 "<S-A>" as
// Key representation, which can't be parsed. So we simply unshift the 'A'
// here, turning the representation into: "<S-a>"
if let KeyCode::Char(char) = key_code {
let lower_case_char: char = {
let chars = char.to_lowercase().collect::<Vec<char>>();
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::<KeyValue>::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)

View File

@ -1,5 +1,6 @@
#[cfg(crossterm)]
mod crossterm;
mod parsing;
use std::fmt::Display;