make the event parser a proper iterator and block while waiting for input
This commit is contained in:
parent
572f64cb73
commit
2b9a9a0ed8
2 changed files with 15 additions and 9 deletions
|
@ -6,14 +6,12 @@ use ants::{
|
||||||
fn main() {
|
fn main() {
|
||||||
raw_mode::enter().unwrap();
|
raw_mode::enter().unwrap();
|
||||||
print!("Press Ctrl+q to exit.\r\n");
|
print!("Press Ctrl+q to exit.\r\n");
|
||||||
let mut events = Events::new();
|
|
||||||
loop {
|
for event in Events::new() {
|
||||||
if let Some(event) = events.next() {
|
|
||||||
print!("{:?}\r\n", event);
|
print!("{:?}\r\n", event);
|
||||||
if let Event::Key(Key::Char('q'), Mod::Ctrl) = event {
|
if let Event::Key(Key::Char('q'), Mod::Ctrl) = event {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
raw_mode::exit().unwrap();
|
raw_mode::exit().unwrap();
|
||||||
}
|
}
|
||||||
|
|
12
src/event.rs
12
src/event.rs
|
@ -116,9 +116,17 @@ impl Iterator for Events {
|
||||||
type Item = Event;
|
type Item = Event;
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
self.source.read_to_end(&mut self.buffer).ok()?;
|
|
||||||
if self.buffer.is_empty() {
|
if self.buffer.is_empty() {
|
||||||
return None;
|
let mut buf = [0];
|
||||||
|
while let Ok(0) = self.source.read(&mut buf) {
|
||||||
|
// there has to be a better way to make this blocking...
|
||||||
|
std::thread::sleep(std::time::Duration::from_millis(5));
|
||||||
|
}
|
||||||
|
// TODO handle read errors
|
||||||
|
self.buffer.push(buf[0]);
|
||||||
|
while let Ok(1) = self.source.read(&mut buf) {
|
||||||
|
self.buffer.push(buf[0]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Some(self.parse())
|
Some(self.parse())
|
||||||
|
|
Loading…
Reference in a new issue