Compare commits

..

No commits in common. "572f64cb7345bbfbc63fdff39d2e04884ea95321" and "8ffd34dcfa296dcfa10ff5ba9cb4605c6acbc10c" have entirely different histories.

2 changed files with 18 additions and 34 deletions

View file

@ -1,2 +0,0 @@
# ants
An ANSI terminal library for writing simple TUI applications.

View file

@ -67,47 +67,22 @@ impl Events {
} }
} }
fn first_byte(&mut self) -> u8 {
let byte = self.buffer[0];
self.buffer = self.buffer[1..].to_vec();
byte
}
fn parse(&mut self) -> Event {
let byte = self.first_byte();
match byte {
ESC => self.parse_esc(),
BS => Event::Key(Key::Backspace, Mod::None),
DEL => Event::Key(Key::Delete, Mod::None),
LF | CR => Event::Key(Key::Enter, Mod::None),
TAB => Event::Key(Key::Tab, Mod::None),
x @ 1..=26 => Event::Key(Key::Char((x + 96).into()), Mod::Ctrl), // ctrl + letter
x @ b' '..=b'~' => Event::Key(Key::Char(x.into()), Mod::None), // regular ascii charaters
x => Event::Unsupported(vec![x]),
}
}
fn parse_esc(&mut self) -> Event { fn parse_esc(&mut self) -> Event {
if self.buffer.is_empty() { if self.buffer.is_empty() {
return Event::Key(Key::Escape, Mod::None); return Event::Key(Key::Escape, Mod::None);
} }
let byte = self.first_byte(); let byte = self.buffer[0];
self.buffer = self.buffer[1..].to_vec();
match byte { match byte {
ESC => { ESC => Event::Key(Key::Escape, Mod::None),
// Two ESC chars in a row probably means that the first one was an escape key press
// and the second one was for something else, possibly because input is coming all at once from a file.
// Therefore reinsert the second ESC byte so it can be processed next iteration.
self.buffer.insert(0, ESC);
Event::Key(Key::Escape, Mod::None)
}
BS => Event::Key(Key::Backspace, Mod::Alt), BS => Event::Key(Key::Backspace, Mod::Alt),
DEL => Event::Key(Key::Delete, Mod::Alt), DEL => Event::Key(Key::Delete, Mod::Alt),
LF | CR => Event::Key(Key::Enter, Mod::Alt), LF | CR => Event::Key(Key::Enter, Mod::Alt),
TAB => Event::Key(Key::Tab, Mod::Alt), TAB => Event::Key(Key::Tab, Mod::Alt),
x @ 1..=26 => Event::Key(Key::Char((x + 96).into()), Mod::CtrlAlt), // ctrl + letter c @ 1..=26 => Event::Key(Key::Char((c + 96).into()), Mod::CtrlAlt), // ctrl + letter
x @ b' '..=b'~' => Event::Key(Key::Char(x.into()), Mod::Alt), // regular ascii charaters c @ b' '..=b'~' => Event::Key(Key::Char(c.into()), Mod::Alt), // regular ascii charaters
x => Event::Unsupported(vec![ESC, x]), _ => Event::Unsupported(vec![ESC, byte]),
} }
} }
} }
@ -121,6 +96,17 @@ impl Iterator for Events {
return None; return None;
} }
Some(self.parse()) let byte = self.buffer[0];
self.buffer = self.buffer[1..].to_vec();
Some(match byte {
ESC => self.parse_esc(),
BS => Event::Key(Key::Backspace, Mod::None),
DEL => Event::Key(Key::Delete, Mod::None),
LF | CR => Event::Key(Key::Enter, Mod::None),
TAB => Event::Key(Key::Tab, Mod::None),
c @ 1..=26 => Event::Key(Key::Char((c + 96).into()), Mod::Ctrl), // ctrl + letter
c @ b' '..=b'~' => Event::Key(Key::Char(c.into()), Mod::None), // regular ascii charaters
_ => Event::Unsupported(vec![byte]),
})
} }
} }