Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "stdin-nonblocking"
version = "0.3.0"
version = "0.4.0"
authors = ["Jeremy Harris <jeremy.harris@zenosmosis.com>"]
description = "Dependency-less non-blocking stdin reader using background threads. Supports streaming and immediate fallback defaults."
repository = "https://github.com/jzombie/rust-stdin-nonblocking"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use stdin_nonblocking::get_stdin_or_default;
let input = get_stdin_or_default(Some(b"fallback_value"));

// Input is always `Vec<u8>`, ensuring binary safety.
assert_eq!(input, b"fallback_value".to_vec());
assert_eq!(input, Some(b"fallback_value".to_vec()));
```

### Read `stdin` as Stream
Expand Down
2 changes: 1 addition & 1 deletion src/bin/test_binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ fn main() {

// Print raw binary data instead of Debug format
io::stdout()
.write_all(&input)
.write_all(&input.unwrap())
.expect("Failed to write output");
}
16 changes: 11 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#[cfg(doctest)]
doc_comment::doctest!("../README.md");

use std::io::{self, IsTerminal, Read};
use std::sync::mpsc::{self, Receiver, Sender};
use std::thread;
Expand Down Expand Up @@ -77,26 +80,29 @@ pub fn spawn_stdin_stream() -> Receiver<Vec<u8>> {
/// * `default` - An optional fallback value returned if no input is available.
///
/// # Returns
/// * `Vec<u8>` - The full stdin input (or default value as bytes).
/// * `Option<Vec<u8>>` - The full stdin input (or default value as bytes).
///
/// # Example
/// ```
/// use stdin_nonblocking::get_stdin_or_default;
///
/// let input = get_stdin_or_default(Some(b"fallback_value"));
///
/// assert_eq!(input, b"fallback_value".to_vec());
/// assert_eq!(input, Some(b"fallback_value".to_vec()));
/// ```
pub fn get_stdin_or_default(default: Option<&[u8]>) -> Vec<u8> {
pub fn get_stdin_or_default(default: Option<&[u8]>) -> Option<Vec<u8>> {
let stdin_channel = spawn_stdin_stream();

// Give the reader thread a short time to capture any available input
thread::sleep(Duration::from_millis(50));

if let Ok(data) = stdin_channel.try_recv() {
return data;
return Some(data);
}

// Return `None` if no default
default?;

// No input available, return the default value
default.unwrap_or(b"").to_vec()
Some(default.unwrap_or(b"").to_vec())
}