forked from trinitrix/core
1
0
Fork 0

fix(keymaps/crossterm): Implement it fully

This commit is contained in:
Benedikt Peetz 2023-11-07 20:25:55 +01:00
parent f3b3cada71
commit 5f69311dfa
Signed by: bpeetz
GPG Key ID: A5E94010C3A642AD
5 changed files with 16 additions and 11 deletions

View File

@ -38,3 +38,9 @@ pub enum KeyValueParseError {
#[error("The number associate with the F key ('{0}') can't be parsed as u8!")]
CantParseFNumber(#[from] ParseIntError),
}
#[derive(Error, Debug)]
pub enum KeyFromCrossterm {
#[error("Can not parse non Key event to a Key code: ('{0:?}')")]
OnlyKey(crossterm::event::Event),
}

View File

@ -1,7 +1,9 @@
use log::{debug, info};
use crossterm::event::{Event, KeyCode, KeyEvent, KeyEventKind, KeyEventState, KeyModifiers};
use crate::{error, key_repr::KeyValue};
use super::Key;
impl Into<Event> for Key {
fn into(self) -> Event {
let mut modifiers;
@ -43,9 +45,8 @@ impl Into<Event> for Key {
}
}
#[cfg(crossterm)]
impl TryFrom<&Event> for Key {
type Error = anyhow::Error;
type Error = error::KeyFromCrossterm;
fn try_from(value: &Event) -> std::result::Result<Self, Self::Error> {
let mut output_key: Key = Key::new();
@ -69,11 +70,7 @@ impl TryFrom<&Event> for Key {
Ok(output_key)
}
Event::Mouse(_)
| Event::Paste(_)
| Event::Resize(_, _)
| Event::FocusGained
| Event::FocusLost => bail!("Only supports parsing from key event"),
event => Err(error::KeyFromCrossterm::OnlyKey(event.to_owned())),
}
}
}

View File

@ -1,4 +1,4 @@
#[cfg(crossterm)]
#[cfg(feature = "crossterm")]
mod crossterm;
mod parsing;

View File

@ -1,5 +1,7 @@
use crossterm::event::KeyCode;
use super::KeyValue;
impl From<KeyCode> for KeyValue {
fn from(value: KeyCode) -> Self {
match value {

View File

@ -1,4 +1,4 @@
#[cfg(crossterm)]
#[cfg(feature = "crossterm")]
mod crossterm;
use std::{