Default to the TTY device in async_stdin.

This commit is contained in:
ticki 2016-09-24 20:04:46 +02:00
parent 0d1025c532
commit f2cec304e8
2 changed files with 11 additions and 6 deletions

View File

@ -1,6 +1,6 @@
[package]
name = "termion"
version = "1.1.0"
version = "1.1.1"
authors = ["ticki <Ticki@users.noreply.github.com>"]
description = "A bindless library for manipulating terminals."
repository = "https://github.com/ticki/termion"

View File

@ -2,20 +2,23 @@ use std::io::{self, Read};
use std::sync::mpsc;
use std::thread;
/// Construct an asynchronous handle to the standard input.
use tty;
/// Construct an asynchronous handle to the TTY standard input.
///
/// This allows you to read from standard input _without blocking_ the current thread.
/// Specifically, it works by firing up another thread to handle the event stream, which will then
/// be buffered in a mpsc queue, which will eventually be read by the current thread.
///
/// Note that this will acquire the Mutex lock on the standard input, making all future stdin
/// construction hang the program until the reader is dropped.
/// This will not read the piped standard input, but rather read from the TTY device, since reading
/// asyncronized from piped input would rarely make sense. In other words, if you pipe standard
/// output from another process, it won't be reflected in the stream returned by this function, as
/// this represents the TTY device, and not the piped standard input.
pub fn async_stdin() -> AsyncReader {
let (send, recv) = mpsc::channel();
thread::spawn(move || {
let stdin = io::stdin();
for i in stdin.lock().bytes() {
for i in tty::get_tty().unwrap().bytes() {
if send.send(i).is_err() {
return;
}
@ -36,6 +39,8 @@ pub struct AsyncReader {
recv: mpsc::Receiver<io::Result<u8>>,
}
// FIXME: Allow constructing an async reader from an arbitrary stream.
impl Read for AsyncReader {
/// Read from the byte stream.
///