pub fn spawn_stdin_stream() -> Receiver<Vec<u8>>
Expand description
Spawns a background thread that continuously reads from stdin as a binary stream.
This function returns an mpsc Receiver
, allowing non-blocking polling
of stdin input just like spawn_stdin_channel
.
Handling Interactive Mode:
- If stdin is a terminal (interactive mode), this function immediately returns an empty receiver.
- This prevents blocking behavior when running interactively.
- When reading from a file or pipe, the background thread captures input as raw bytes.
§Returns
A Receiver<Vec<u8>>
that emits binary data from stdin.
§Example
use stdin_nonblocking::spawn_stdin_stream;
use std::sync::mpsc::TryRecvError;
use std::time::Duration;
let stdin_stream = spawn_stdin_stream();
loop {
match stdin_stream.try_recv() {
Ok(bytes) => println!("Received: {:?}", bytes), // Always raw bytes
Err(TryRecvError::Empty) => {
// No input yet; continue execution
}
Err(TryRecvError::Disconnected) => {
println!("Input stream closed. Exiting...");
break;
}
}
std::thread::sleep(Duration::from_millis(500));
}