simplify some keymap key names (#2677)

pull/2694/head
Henry 3 years ago committed by GitHub
parent 0035c29fc1
commit 8351a82c2c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -33,12 +33,7 @@ Control, Shift and Alt modifiers are encoded respectively with the prefixes
| Backspace | `"backspace"` | | Backspace | `"backspace"` |
| Space | `"space"` | | Space | `"space"` |
| Return/Enter | `"ret"` | | Return/Enter | `"ret"` |
| < | `"lt"` |
| \> | `"gt"` |
| \+ | `"plus"` |
| \- | `"minus"` | | \- | `"minus"` |
| ; | `"semicolon"` |
| % | `"percent"` |
| Left | `"left"` | | Left | `"left"` |
| Right | `"right"` | | Right | `"right"` |
| Up | `"up"` | | Up | `"up"` |

@ -41,12 +41,7 @@ pub(crate) mod keys {
pub(crate) const NULL: &str = "null"; pub(crate) const NULL: &str = "null";
pub(crate) const ESC: &str = "esc"; pub(crate) const ESC: &str = "esc";
pub(crate) const SPACE: &str = "space"; pub(crate) const SPACE: &str = "space";
pub(crate) const LESS_THAN: &str = "lt";
pub(crate) const GREATER_THAN: &str = "gt";
pub(crate) const PLUS: &str = "plus";
pub(crate) const MINUS: &str = "minus"; pub(crate) const MINUS: &str = "minus";
pub(crate) const SEMICOLON: &str = "semicolon";
pub(crate) const PERCENT: &str = "percent";
} }
impl fmt::Display for KeyEvent { impl fmt::Display for KeyEvent {
@ -86,12 +81,7 @@ impl fmt::Display for KeyEvent {
KeyCode::Null => f.write_str(keys::NULL)?, KeyCode::Null => f.write_str(keys::NULL)?,
KeyCode::Esc => f.write_str(keys::ESC)?, KeyCode::Esc => f.write_str(keys::ESC)?,
KeyCode::Char(' ') => f.write_str(keys::SPACE)?, KeyCode::Char(' ') => f.write_str(keys::SPACE)?,
KeyCode::Char('<') => f.write_str(keys::LESS_THAN)?,
KeyCode::Char('>') => f.write_str(keys::GREATER_THAN)?,
KeyCode::Char('+') => f.write_str(keys::PLUS)?,
KeyCode::Char('-') => f.write_str(keys::MINUS)?, KeyCode::Char('-') => f.write_str(keys::MINUS)?,
KeyCode::Char(';') => f.write_str(keys::SEMICOLON)?,
KeyCode::Char('%') => f.write_str(keys::PERCENT)?,
KeyCode::F(i) => f.write_fmt(format_args!("F{}", i))?, KeyCode::F(i) => f.write_fmt(format_args!("F{}", i))?,
KeyCode::Char(c) => f.write_fmt(format_args!("{}", c))?, KeyCode::Char(c) => f.write_fmt(format_args!("{}", c))?,
}; };
@ -119,12 +109,7 @@ impl UnicodeWidthStr for KeyEvent {
KeyCode::Null => keys::NULL.len(), KeyCode::Null => keys::NULL.len(),
KeyCode::Esc => keys::ESC.len(), KeyCode::Esc => keys::ESC.len(),
KeyCode::Char(' ') => keys::SPACE.len(), KeyCode::Char(' ') => keys::SPACE.len(),
KeyCode::Char('<') => keys::LESS_THAN.len(),
KeyCode::Char('>') => keys::GREATER_THAN.len(),
KeyCode::Char('+') => keys::PLUS.len(),
KeyCode::Char('-') => keys::MINUS.len(), KeyCode::Char('-') => keys::MINUS.len(),
KeyCode::Char(';') => keys::SEMICOLON.len(),
KeyCode::Char('%') => keys::PERCENT.len(),
KeyCode::F(1..=9) => 2, KeyCode::F(1..=9) => 2,
KeyCode::F(_) => 3, KeyCode::F(_) => 3,
KeyCode::Char(c) => c.width().unwrap_or(0), KeyCode::Char(c) => c.width().unwrap_or(0),
@ -168,12 +153,7 @@ impl std::str::FromStr for KeyEvent {
keys::NULL => KeyCode::Null, keys::NULL => KeyCode::Null,
keys::ESC => KeyCode::Esc, keys::ESC => KeyCode::Esc,
keys::SPACE => KeyCode::Char(' '), keys::SPACE => KeyCode::Char(' '),
keys::LESS_THAN => KeyCode::Char('<'),
keys::GREATER_THAN => KeyCode::Char('>'),
keys::PLUS => KeyCode::Char('+'),
keys::MINUS => KeyCode::Char('-'), keys::MINUS => KeyCode::Char('-'),
keys::SEMICOLON => KeyCode::Char(';'),
keys::PERCENT => KeyCode::Char('%'),
single if single.chars().count() == 1 => KeyCode::Char(single.chars().next().unwrap()), single if single.chars().count() == 1 => KeyCode::Char(single.chars().next().unwrap()),
function if function.len() > 1 && function.starts_with('F') => { function if function.len() > 1 && function.starts_with('F') => {
let function: String = function.chars().skip(1).collect(); let function: String = function.chars().skip(1).collect();
@ -336,6 +316,14 @@ mod test {
modifiers: KeyModifiers::NONE modifiers: KeyModifiers::NONE
} }
); );
assert_eq!(
str::parse::<KeyEvent>("%").unwrap(),
KeyEvent {
code: KeyCode::Char('%'),
modifiers: KeyModifiers::NONE
}
)
} }
#[test] #[test]
@ -375,6 +363,16 @@ mod test {
assert!(str::parse::<KeyEvent>("FU").is_err()); assert!(str::parse::<KeyEvent>("FU").is_err());
assert!(str::parse::<KeyEvent>("123").is_err()); assert!(str::parse::<KeyEvent>("123").is_err());
assert!(str::parse::<KeyEvent>("S--").is_err()); assert!(str::parse::<KeyEvent>("S--").is_err());
assert!(str::parse::<KeyEvent>("S-percent").is_err());
}
#[test]
fn parsing_unsupported_named_keys() {
assert!(str::parse::<KeyEvent>("lt").is_err());
assert!(str::parse::<KeyEvent>("gt").is_err());
assert!(str::parse::<KeyEvent>("plus").is_err());
assert!(str::parse::<KeyEvent>("percent").is_err());
assert!(str::parse::<KeyEvent>("semicolon").is_err());
} }
#[test] #[test]

Loading…
Cancel
Save