From b528730621151cf2337046882b1db1f9466761de Mon Sep 17 00:00:00 2001 From: Jeremy Harris Date: Wed, 12 Mar 2025 23:41:16 -0500 Subject: [PATCH 1/5] Fix issue where larger files could not reliably stream in --- src/lib.rs | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 9d9a822..4a71a8b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -91,18 +91,15 @@ pub fn spawn_stdin_stream() -> Receiver> { /// assert_eq!(input, Some(b"fallback_value".to_vec())); /// ``` pub fn get_stdin_or_default(default: Option<&[u8]>) -> Option> { - let stdin_channel = spawn_stdin_stream(); + if !io::stdin().is_terminal() { + 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 Some(data); + // Blocking recv() waits until data arrives or EOF occurs + match stdin_channel.recv() { + Ok(data) => return Some(data), + Err(e) => eprintln!("Channel closed without data: {}", e), + } } - // Return `None` if no default - default?; - - // No input available, return the default value - Some(default.unwrap_or(b"").to_vec()) -} + default.map(|val| val.to_vec()) +} \ No newline at end of file From f8ca6acaefc73c0dcda6e1ced7e48fb6acbc1b62 Mon Sep 17 00:00:00 2001 From: Jeremy Harris Date: Wed, 12 Mar 2025 23:45:58 -0500 Subject: [PATCH 2/5] Update comments --- src/lib.rs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 4a71a8b..763c766 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -66,21 +66,19 @@ pub fn spawn_stdin_stream() -> Receiver> { rx } -/// Reads from stdin if available, otherwise returns a default value. +/// Reads stdin if available; otherwise, returns a default value. /// -/// **Non-blocking:** This function polls `stdin` once and immediately returns. -/// If no input is available within the polling time, it returns the provided default value. -/// -/// **Handling Interactive Mode:** -/// - If running interactively (stdin is a terminal), this function returns the default value immediately. -/// - This prevents hanging while waiting for user input in interactive sessions. -/// - When used with redirected input (e.g., from a file or pipe), it collects available **binary** input. +/// This function intelligently determines whether to block: +/// - **Interactive Mode**: If stdin is a terminal, the function immediately returns the default without blocking. +/// - **Redirected Input**: If stdin is redirected from a file or pipe, it spawns a thread to read stdin and waits briefly (50ms). +/// - If data arrives promptly, it returns immediately. +/// - If no data is available within that short duration, it returns the provided default value. /// /// # Arguments /// * `default` - An optional fallback value returned if no input is available. /// /// # Returns -/// * `Option>` - The full stdin input (or default value as bytes). +/// * `Option>` - The stdin input if available, otherwise the provided default. /// /// # Example /// ``` From 45c0fc2b7eacbf772a933545b17bc9e62d487360 Mon Sep 17 00:00:00 2001 From: Jeremy Harris Date: Wed, 12 Mar 2025 23:46:16 -0500 Subject: [PATCH 3/5] Bump to v0.4.1 --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a4928ce..c005477 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -214,7 +214,7 @@ dependencies = [ [[package]] name = "stdin-nonblocking" -version = "0.4.0" +version = "0.4.1" dependencies = [ "doc-comment", ] diff --git a/Cargo.toml b/Cargo.toml index ea7b417..654f385 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "stdin-nonblocking" -version = "0.4.0" +version = "0.4.1" authors = ["Jeremy Harris "] description = "Dependency-less non-blocking stdin reader using background threads. Supports streaming and immediate fallback defaults." repository = "https://github.com/jzombie/rust-stdin-nonblocking" From b2caa35c366246b9fa2d9c634235b2943631d82b Mon Sep 17 00:00:00 2001 From: Jeremy Harris Date: Wed, 12 Mar 2025 23:47:27 -0500 Subject: [PATCH 4/5] Apply `cargo fmt` --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 763c766..86e2ac1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -100,4 +100,4 @@ pub fn get_stdin_or_default(default: Option<&[u8]>) -> Option> { } default.map(|val| val.to_vec()) -} \ No newline at end of file +} From 9b607d0a4141b91fe6e2b4cd74ab0b3450725227 Mon Sep 17 00:00:00 2001 From: Jeremy Harris Date: Wed, 12 Mar 2025 23:48:26 -0500 Subject: [PATCH 5/5] Apply `cargo clippy --fix` --- src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 86e2ac1..4597d36 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,7 +4,6 @@ doc_comment::doctest!("../README.md"); use std::io::{self, IsTerminal, Read}; use std::sync::mpsc::{self, Receiver, Sender}; use std::thread; -use std::time::Duration; /// Spawns a background thread that continuously reads from stdin as a binary stream. ///