From 5f923e87069177841fb744853af0da88286425d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E9=9B=85=20=C2=B7=20Misaki=20Masa?= Date: Fri, 9 Aug 2024 16:55:07 +0800 Subject: [PATCH] feat: allows shorthand forms like `` as `` (#1448) --- yazi-config/preset/keymap.toml | 2 +- yazi-config/src/keymap/key.rs | 14 ++++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/yazi-config/preset/keymap.toml b/yazi-config/preset/keymap.toml index d2350efe..9a2b7c28 100644 --- a/yazi-config/preset/keymap.toml +++ b/yazi-config/preset/keymap.toml @@ -231,7 +231,7 @@ keymap = [ { on = "", run = "move 999", desc = "Move to the EOL" }, # Delete - { on = "", run = "backspace", desc = "Delete the character before the cursor" }, + { on = "", run = "backspace", desc = "Delete the character before the cursor" }, { on = "", run = "backspace --under", desc = "Delete the character under the cursor" }, { on = "", run = "backspace", desc = "Delete the character before the cursor" }, { on = "", run = "backspace --under", desc = "Delete the character under the cursor" }, diff --git a/yazi-config/src/keymap/key.rs b/yazi-config/src/keymap/key.rs index d9a78106..5aeb9872 100644 --- a/yazi-config/src/keymap/key.rs +++ b/yazi-config/src/keymap/key.rs @@ -67,9 +67,8 @@ impl FromStr for Key { let mut key = Self::default(); if !s.starts_with('<') || !s.ends_with('>') { - let c = s.chars().next().unwrap(); - key.code = KeyCode::Char(c); - key.shift = c.is_ascii_uppercase(); + key.code = KeyCode::Char(s.chars().next().unwrap()); + key.shift = matches!(key.code, KeyCode::Char(c) if c.is_ascii_uppercase()); return Ok(key); } @@ -118,7 +117,11 @@ impl FromStr for Key { "esc" => key.code = KeyCode::Esc, _ => match next { - s if it.peek().is_none() => key.code = KeyCode::Char(s.chars().next().unwrap()), + s if it.peek().is_none() => { + let c = s.chars().next().unwrap(); + key.shift |= c.is_ascii_uppercase(); + key.code = KeyCode::Char(if key.shift { c.to_ascii_uppercase() } else { c }); + } s => bail!("unknown key: {s}"), }, } @@ -134,7 +137,6 @@ impl FromStr for Key { impl Display for Key { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { if let Some(c) = self.plain() { - let c = if self.shift { c.to_ascii_uppercase() } else { c }; return if c == ' ' { write!(f, "") } else { f.write_char(c) }; } @@ -190,7 +192,7 @@ impl Display for Key { KeyCode::Char(' ') => "Space", KeyCode::Char(c) => { - f.write_char(if self.shift { c.to_ascii_uppercase() } else { c })?; + f.write_char(c)?; "" } _ => "Unknown",