1
0
Fork 0

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 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]
default = []
crossterm = ["dep:crossterm"]
[dependencies] [dependencies]
crossterm = { version = "0.25", optional = true }
log = "0.4.20" log = "0.4.20"
thiserror = "1.0.50" thiserror = "1.0.50"
pest = "2.7.5" pest = "2.7.5"

View File

@ -2,7 +2,6 @@ use log::{debug, info};
use crossterm::event::{Event, KeyCode, KeyEvent, KeyEventKind, KeyEventState, KeyModifiers}; use crossterm::event::{Event, KeyCode, KeyEvent, KeyEventKind, KeyEventState, KeyModifiers};
impl Into<Event> for Key { impl Into<Event> for Key {
fn into(self) -> Event { fn into(self) -> Event {
let mut modifiers; let mut modifiers;
@ -30,28 +29,8 @@ impl Into<Event> for Key {
let output = Event::Key(KeyEvent { let output = Event::Key(KeyEvent {
code: { code: {
// We sorta hit a edge case here, if we have a shifted char we need to tell if let Some(KeyValue::Char(char)) = self.value {
// crossterm about that. Thus we need to manually apply the shift modifier on the KeyCode::Char(char)
// 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()
}
} else { } else {
self.value.unwrap_or(KeyValue::Null).into() self.value.unwrap_or(KeyValue::Null).into()
} }
@ -85,41 +64,7 @@ impl TryFrom<&Event> for Key {
} }
{ {
let key_code = key_event.code; output_key.value = Some(key_event.code.into());
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());
}
} }
Ok(output_key) Ok(output_key)

View File

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