Default to the TTY device in async_stdin.
This commit is contained in:
parent
0d1025c532
commit
f2cec304e8
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "termion"
|
name = "termion"
|
||||||
version = "1.1.0"
|
version = "1.1.1"
|
||||||
authors = ["ticki <Ticki@users.noreply.github.com>"]
|
authors = ["ticki <Ticki@users.noreply.github.com>"]
|
||||||
description = "A bindless library for manipulating terminals."
|
description = "A bindless library for manipulating terminals."
|
||||||
repository = "https://github.com/ticki/termion"
|
repository = "https://github.com/ticki/termion"
|
||||||
|
|
15
src/async.rs
15
src/async.rs
|
@ -2,20 +2,23 @@ use std::io::{self, Read};
|
||||||
use std::sync::mpsc;
|
use std::sync::mpsc;
|
||||||
use std::thread;
|
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.
|
/// 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
|
/// 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.
|
/// 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
|
/// This will not read the piped standard input, but rather read from the TTY device, since reading
|
||||||
/// construction hang the program until the reader is dropped.
|
/// 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 {
|
pub fn async_stdin() -> AsyncReader {
|
||||||
let (send, recv) = mpsc::channel();
|
let (send, recv) = mpsc::channel();
|
||||||
|
|
||||||
thread::spawn(move || {
|
thread::spawn(move || {
|
||||||
let stdin = io::stdin();
|
for i in tty::get_tty().unwrap().bytes() {
|
||||||
for i in stdin.lock().bytes() {
|
|
||||||
if send.send(i).is_err() {
|
if send.send(i).is_err() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -36,6 +39,8 @@ pub struct AsyncReader {
|
||||||
recv: mpsc::Receiver<io::Result<u8>>,
|
recv: mpsc::Receiver<io::Result<u8>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FIXME: Allow constructing an async reader from an arbitrary stream.
|
||||||
|
|
||||||
impl Read for AsyncReader {
|
impl Read for AsyncReader {
|
||||||
/// Read from the byte stream.
|
/// Read from the byte stream.
|
||||||
///
|
///
|
||||||
|
|
Loading…
Reference in New Issue