Add shift modifier to uppercase char events on unix (#423)

This commit is contained in:
Stephan Dilly 2020-04-24 07:19:23 +02:00 committed by GitHub
parent 128eddeafc
commit d4fc75cca1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -95,13 +95,22 @@ pub(crate) fn parse_event(buffer: &[u8], input_available: bool) -> Result<Option
_ => parse_utf8_char(buffer).map(|maybe_char| {
maybe_char
.map(KeyCode::Char)
.map(Into::into)
.map(char_code_to_event)
.map(Event::Key)
.map(InternalEvent::Event)
}),
}
}
// converts KeyCode to KeyEvent (adds shift modifier in case of uppercase characters)
fn char_code_to_event(code: KeyCode) -> KeyEvent {
let modifiers = match code {
KeyCode::Char(c) if c.is_uppercase() => KeyModifiers::SHIFT,
_ => KeyModifiers::empty(),
};
KeyEvent::new(code, modifiers)
}
pub(crate) fn parse_csi(buffer: &[u8]) -> Result<Option<InternalEvent>> {
assert!(buffer.starts_with(&[b'\x1B', b'['])); // ESC [
@ -553,7 +562,10 @@ mod tests {
// parse_utf8_char
assert_eq!(
parse_event("Ž".as_bytes(), false).unwrap(),
Some(InternalEvent::Event(Event::Key(KeyCode::Char('Ž').into()))),
Some(InternalEvent::Event(Event::Key(KeyEvent::new(
KeyCode::Char('Ž'),
KeyModifiers::SHIFT
)))),
);
}
@ -714,4 +726,26 @@ mod tests {
// 'Invalid 4 Octet Sequence (in 4th Octet)' => "\xf0\x28\x8c\x28",
assert!(parse_utf8_char(&[0xF0, 0x28, 0x8C, 0x28]).is_err());
}
#[test]
fn test_parse_char_event_lowercase() {
assert_eq!(
parse_event("c".as_bytes(), false).unwrap(),
Some(InternalEvent::Event(Event::Key(KeyEvent::new(
KeyCode::Char('c'),
KeyModifiers::empty()
)))),
);
}
#[test]
fn test_parse_char_event_uppercase() {
assert_eq!(
parse_event("C".as_bytes(), false).unwrap(),
Some(InternalEvent::Event(Event::Key(KeyEvent::new(
KeyCode::Char('C'),
KeyModifiers::SHIFT
)))),
);
}
}