2016-03-15 19:32:25 +00:00
|
|
|
use std::io::{self, Read};
|
|
|
|
use std::sync::mpsc;
|
|
|
|
use std::thread;
|
|
|
|
|
2017-08-01 04:17:47 +01:00
|
|
|
use sys::tty::get_tty;
|
2016-09-24 19:04:46 +01:00
|
|
|
|
2018-05-08 01:13:55 +01:00
|
|
|
/// Construct an asynchronous handle to the TTY standard input, with a delimiter byte.
|
|
|
|
///
|
|
|
|
/// This has the same advantages as async_stdin(), but also allows specifying a delimiter byte. The
|
|
|
|
/// reader will stop reading after consuming the delimiter byte.
|
|
|
|
pub fn async_stdin_until(delimiter: u8) -> AsyncReader {
|
|
|
|
let (send, recv) = mpsc::channel();
|
|
|
|
|
|
|
|
thread::spawn(move || for i in get_tty().unwrap().bytes() {
|
|
|
|
|
|
|
|
match i {
|
|
|
|
Ok(byte) => {
|
|
|
|
let end_of_stream = &byte == &delimiter;
|
|
|
|
let send_error = send.send(Ok(byte)).is_err();
|
|
|
|
|
|
|
|
if end_of_stream || send_error { return; }
|
|
|
|
},
|
2018-05-09 01:34:43 +01:00
|
|
|
Err(_) => { return; }
|
2018-05-08 01:13:55 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
AsyncReader { recv: recv }
|
|
|
|
}
|
|
|
|
|
2016-09-24 19:04:46 +01:00
|
|
|
/// Construct an asynchronous handle to the TTY standard input.
|
2016-03-15 19:32:25 +00:00
|
|
|
///
|
|
|
|
/// 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.
|
|
|
|
///
|
2016-09-24 19:04:46 +01:00
|
|
|
/// 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.
|
2016-03-15 19:32:25 +00:00
|
|
|
pub fn async_stdin() -> AsyncReader {
|
|
|
|
let (send, recv) = mpsc::channel();
|
|
|
|
|
2017-08-01 04:17:47 +01:00
|
|
|
thread::spawn(move || for i in get_tty().unwrap().bytes() {
|
2017-03-24 20:53:05 +00:00
|
|
|
if send.send(i).is_err() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
2016-03-15 19:32:25 +00:00
|
|
|
|
2017-03-24 20:53:05 +00:00
|
|
|
AsyncReader { recv: recv }
|
2016-03-15 19:32:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// An asynchronous reader.
|
2016-09-07 10:39:32 +01:00
|
|
|
///
|
|
|
|
/// This acts as any other stream, with the exception that reading from it won't block. Instead,
|
|
|
|
/// the buffer will only be partially updated based on how much the internal buffer holds.
|
2016-03-15 19:32:25 +00:00
|
|
|
pub struct AsyncReader {
|
|
|
|
/// The underlying mpsc receiver.
|
2016-09-07 10:39:32 +01:00
|
|
|
recv: mpsc::Receiver<io::Result<u8>>,
|
2016-03-15 19:32:25 +00:00
|
|
|
}
|
|
|
|
|
2016-09-24 19:04:46 +01:00
|
|
|
// FIXME: Allow constructing an async reader from an arbitrary stream.
|
|
|
|
|
2016-03-15 19:32:25 +00:00
|
|
|
impl Read for AsyncReader {
|
|
|
|
/// Read from the byte stream.
|
|
|
|
///
|
|
|
|
/// This will never block, but try to drain the event queue until empty. If the total number of
|
|
|
|
/// bytes written is lower than the buffer's length, the event queue is empty or that the event
|
|
|
|
/// stream halted.
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
|
|
|
let mut total = 0;
|
|
|
|
|
|
|
|
loop {
|
2016-10-27 21:02:29 +01:00
|
|
|
if total >= buf.len() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-03-15 19:32:25 +00:00
|
|
|
match self.recv.try_recv() {
|
|
|
|
Ok(Ok(b)) => {
|
|
|
|
buf[total] = b;
|
|
|
|
total += 1;
|
2017-03-24 20:53:05 +00:00
|
|
|
}
|
2016-03-15 19:32:25 +00:00
|
|
|
Ok(Err(e)) => return Err(e),
|
|
|
|
Err(_) => break,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(total)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
use std::io::Read;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_async_stdin() {
|
|
|
|
let stdin = async_stdin();
|
|
|
|
stdin.bytes().next();
|
|
|
|
}
|
|
|
|
}
|