Added Ctrl/Alt/Shift+Navigation key support.

This commit is contained in:
Sebastian Egbers 2020-10-16 11:26:49 +02:00
parent 189222555e
commit d57468cdc1
No known key found for this signature in database
GPG Key ID: 13DEA21EC785E0C2
1 changed files with 237 additions and 177 deletions

View File

@ -57,16 +57,44 @@ pub enum Key {
Backspace,
/// Left arrow.
Left,
/// Shift Left arrow.
ShiftLeft,
/// Alt Left arrow.
AltLeft,
/// Ctrl Left arrow.
CtrlLeft,
/// Right arrow.
Right,
/// Shift Right arrow.
ShiftRight,
/// Alt Right arrow.
AltRight,
/// Ctrl Right arrow.
CtrlRight,
/// Up arrow.
Up,
/// Shift Up arrow.
ShiftUp,
/// Alt Up arrow.
AltUp,
/// Ctrl Up arrow.
CtrlUp,
/// Down arrow.
Down,
/// Shift Down arrow.
ShiftDown,
/// Alt Down arrow.
AltDown,
/// Ctrl Down arrow
CtrlDown,
/// Home key.
Home,
/// Ctrl Home key.
CtrlHome,
/// End key.
End,
/// Ctrl End key.
CtrlEnd,
/// Page Up key.
PageUp,
/// Page Down key.
@ -100,7 +128,8 @@ pub enum Key {
/// Parse an Event from `item` and possibly subsequent bytes through `iter`.
pub fn parse_event<I>(item: u8, iter: &mut I) -> Result<Event, Error>
where I: Iterator<Item = Result<u8, Error>>
where
I: Iterator<Item = Result<u8, Error>>,
{
let error = Error::new(ErrorKind::Other, "Could not parse an event");
match item {
@ -131,12 +160,10 @@ pub fn parse_event<I>(item: u8, iter: &mut I) -> Result<Event, Error>
c @ b'\x01'..=b'\x1A' => Ok(Event::Key(Key::Ctrl((c as u8 - 0x1 + b'a') as char))),
c @ b'\x1C'..=b'\x1F' => Ok(Event::Key(Key::Ctrl((c as u8 - 0x1C + b'4') as char))),
b'\0' => Ok(Event::Key(Key::Null)),
c => {
Ok({
c => Ok({
let ch = parse_utf8_char(c, iter)?;
Event::Key(Key::Char(ch))
})
}
}),
}
}
@ -144,13 +171,49 @@ pub fn parse_event<I>(item: u8, iter: &mut I) -> Result<Event, Error>
///
/// Returns None if an unrecognized sequence is found.
fn parse_csi<I>(iter: &mut I) -> Option<Event>
where I: Iterator<Item = Result<u8, Error>>
where
I: Iterator<Item = Result<u8, Error>>,
{
Some(match iter.next() {
Some(Ok(b'[')) => match iter.next() {
Some(Ok(val @ b'A'..=b'E')) => Event::Key(Key::F(1 + val - b'A')),
_ => return None,
},
Some(Ok(b'1')) => {
iter.next(); // Consume next char (;)
match iter.next() {
Some(Ok(b'2')) => { // Shift Modifier
match iter.next() {
Some(Ok(b'D')) => Event::Key(Key::ShiftLeft),
Some(Ok(b'C')) => Event::Key(Key::ShiftRight),
Some(Ok(b'A')) => Event::Key(Key::ShiftUp),
Some(Ok(b'B')) => Event::Key(Key::ShiftDown),
_ => return None,
}
}
Some(Ok(b'3')) => { // Alt Modifier
match iter.next() {
Some(Ok(b'D')) => Event::Key(Key::AltLeft),
Some(Ok(b'C')) => Event::Key(Key::AltRight),
Some(Ok(b'A')) => Event::Key(Key::AltUp),
Some(Ok(b'B')) => Event::Key(Key::AltDown),
_ => return None,
}
}
Some(Ok(b'5')) => { // Ctrl Modifier
match iter.next() {
Some(Ok(b'D')) => Event::Key(Key::CtrlLeft),
Some(Ok(b'C')) => Event::Key(Key::CtrlRight),
Some(Ok(b'A')) => Event::Key(Key::CtrlUp),
Some(Ok(b'B')) => Event::Key(Key::CtrlDown),
Some(Ok(b'H')) => Event::Key(Key::CtrlHome),
Some(Ok(b'F')) => Event::Key(Key::CtrlEnd),
_ => return None,
}
}
_ => return None,
}
}
Some(Ok(b'D')) => Event::Key(Key::Left),
Some(Ok(b'C')) => Event::Key(Key::Right),
Some(Ok(b'A')) => Event::Key(Key::Up),
@ -201,18 +264,9 @@ fn parse_csi<I>(iter: &mut I) -> Option<Event>
let str_buf = String::from_utf8(buf).unwrap();
let nums = &mut str_buf.split(';');
let cb = nums.next()
.unwrap()
.parse::<u16>()
.unwrap();
let cx = nums.next()
.unwrap()
.parse::<u16>()
.unwrap();
let cy = nums.next()
.unwrap()
.parse::<u16>()
.unwrap();
let cb = nums.next().unwrap().parse::<u16>().unwrap();
let cx = nums.next().unwrap().parse::<u16>().unwrap();
let cy = nums.next().unwrap().parse::<u16>().unwrap();
let event = match cb {
0..=2 | 64..=65 => {
@ -301,7 +355,10 @@ fn parse_csi<I>(iter: &mut I) -> Option<Event>
v @ 11..=15 => Event::Key(Key::F(v - 10)),
v @ 17..=21 => Event::Key(Key::F(v - 11)),
v @ 23..=24 => Event::Key(Key::F(v - 12)),
_ => return None,
v => {
println!("{:?}", v);
return None;
}
}
}
_ => return None,
@ -309,14 +366,17 @@ fn parse_csi<I>(iter: &mut I) -> Option<Event>
}
_ => return None,
})
}
/// Parse `c` as either a single byte ASCII char or a variable size UTF-8 char.
fn parse_utf8_char<I>(c: u8, iter: &mut I) -> Result<char, Error>
where I: Iterator<Item = Result<u8, Error>>
where
I: Iterator<Item = Result<u8, Error>>,
{
let error = Err(Error::new(ErrorKind::Other, "Input character is not valid UTF-8"));
let error = Err(Error::new(
ErrorKind::Other,
"Input character is not valid UTF-8",
));
if c.is_ascii() {
Ok(c as char)
} else {