mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41:05 +00:00
parent
3ee8ef8547
commit
91ab4ea676
1 changed files with 12 additions and 27 deletions
|
|
@ -2,7 +2,7 @@ use anyhow::bail;
|
|||
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Eq)]
|
||||
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Hash)]
|
||||
#[serde(try_from = "String")]
|
||||
pub struct Key {
|
||||
pub code: KeyCode,
|
||||
|
|
@ -11,22 +11,6 @@ pub struct Key {
|
|||
pub alt: bool,
|
||||
}
|
||||
|
||||
impl PartialEq for Key {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
match (self.code, other.code) {
|
||||
(KeyCode::Char(_), KeyCode::Char(_)) => {
|
||||
self.code == other.code && self.ctrl == other.ctrl && self.alt == other.alt
|
||||
}
|
||||
_ => {
|
||||
self.code == other.code
|
||||
&& self.shift == other.shift
|
||||
&& self.ctrl == other.ctrl
|
||||
&& self.alt == other.alt
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Key {
|
||||
#[inline]
|
||||
pub fn plain(&self) -> Option<char> {
|
||||
|
|
@ -43,16 +27,16 @@ impl Key {
|
|||
}
|
||||
|
||||
impl Default for Key {
|
||||
fn default() -> Self {
|
||||
Self { code: KeyCode::Null, shift: false, ctrl: false, alt: false }
|
||||
}
|
||||
fn default() -> Self { Self { code: KeyCode::Null, shift: false, ctrl: false, alt: false } }
|
||||
}
|
||||
|
||||
impl From<KeyEvent> for Key {
|
||||
fn from(value: KeyEvent) -> Self {
|
||||
let shift = matches!(value.code, KeyCode::Char(c) if c.is_ascii_uppercase());
|
||||
|
||||
Self {
|
||||
code: value.code,
|
||||
shift: value.modifiers.contains(KeyModifiers::SHIFT),
|
||||
shift: shift || value.modifiers.contains(KeyModifiers::SHIFT),
|
||||
ctrl: value.modifiers.contains(KeyModifiers::CONTROL),
|
||||
alt: value.modifiers.contains(KeyModifiers::ALT),
|
||||
}
|
||||
|
|
@ -71,6 +55,7 @@ impl TryFrom<String> for Key {
|
|||
if !s.starts_with('<') || !s.ends_with('>') {
|
||||
let c = s.chars().next().unwrap();
|
||||
key.code = KeyCode::Char(c);
|
||||
key.shift = c.is_ascii_uppercase();
|
||||
return Ok(key);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue