Skip to content

Commit

Permalink
add shift modifier to uppercase char events
Browse files Browse the repository at this point in the history
fixes #421
  • Loading branch information
Stephan Dilly committed Apr 22, 2020
1 parent 128edde commit 6e77c1d
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion src/event/sys/unix/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 [

Expand Down Expand Up @@ -714,4 +723,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
)))),
);
}
}

0 comments on commit 6e77c1d

Please sign in to comment.