gchp / rustbox

Rust implementation of the termbox library
MIT License
469 stars 48 forks source link

Means of waking up the event loop #42

Open SirVer opened 9 years ago

SirVer commented 9 years ago

Right now, rustbox blocks in poll_event and there is no way of waking it up. An external event in another thread does not seem to have any means of triggering a redraw without using peek_event in a busy loop.

I think a cleaner design would be to replace poll_event through a mpsc::channel. The consumer could then select over multiple channels to act on multiple inputs and peek_event could just be idiomatically replaced through https://doc.rust-lang.org/nightly/std/iter/struct.Peekable.html. This would also allow for the very intuitive for loop syntax:

// rustbox.events is a mpsc::Receiver<rustbox::Event>;
while let (event) = rustbox.events {
match event {
....
}
}
SirVer commented 9 years ago

Another solution would be to add a thread-safe rustbox::wakeup() that would fire a wakeup event, so other threads could force a redraw.

droundy commented 9 years ago

I'll add to this that I really like the idea of using a channel for the event stream.