An adapter that exposes termion's input and key event iterators as asynchronous streams.
Compatible with Tokio v0.3.x.
```rust use futures::StreamExt; use std::future; use termion::{event::Key, raw::IntoRawMode}; use termioninputtokio::TermReadAsync;
async fn main() -> Result<(), std::io::Error> { // Disable line buffering, local echo, etc. let rawterm = std::io::stdout().intorawmode()?;
tokio::io::stdin()
.keys_stream()
// End the stream when 'q' is pressed.
.take_while(|event| {
future::ready(match event {
Ok(Key::Char('q')) => false,
_ => true,
})
})
// Print each key that was pressed.
.for_each(|event| async move {
println!("{:?}\r", event);
})
.await;
Ok(())
} ```
Reading from tokio::io::stdin() is still blocking operation. This can be problematic if you wish to gracefully tear down the input event stream as there's no way to interrupt an in-progress read
system call. To address this you can use tokio-fd's AsyncFd
to wrap stdin
. This will result in tokio polling stdin
for readability, as it would a socket. It will then only attempt to read from stdin
when the kernel indicates that data is available, avoiding blocking read
calls and allowing you to cleanly tear down your input event stream.
This is based on termion-tokio by Kayo Phoenix, which is in turn based on code within termion.