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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target
*.bin
173 changes: 172 additions & 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.2.1"
version = "0.3.0"
authors = ["Jeremy Harris <[email protected]>"]
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
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

Dependency-less non-blocking `stdin` reader using background threads. Supports streaming and immediate fallback defaults.

Supports **binary data**, streaming, and immediate fallback defaults.

## Install

```sh
Expand All @@ -29,9 +31,10 @@ use stdin_nonblocking::get_stdin_or_default;

// If running in interactive mode (stdin is a terminal),
// `get_stdin_or_default` returns the default value immediately.
let input = get_stdin_or_default(Some("fallback_value"));
let input = get_stdin_or_default(Some(b"fallback_value"));

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

### Read `stdin` as Stream
Expand All @@ -47,7 +50,7 @@ let stdin_stream = spawn_stdin_stream();

loop {
match stdin_stream.try_recv() {
Ok(line) => println!("Received: {}", line),
Ok(bytes) => println!("Received: {:?}", bytes), // Always raw bytes
Err(TryRecvError::Empty) => {
// No input yet; continue execution
}
Expand Down
2 changes: 1 addition & 1 deletion examples/stdin.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use stdin_nonblocking::get_stdin_or_default;

fn main() {
let input = get_stdin_or_default(Some("backup_value"));
let input = get_stdin_or_default(Some(b"backup_value"));
println!("Final input: {:?}", input);
}
9 changes: 7 additions & 2 deletions src/bin/test_binary.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
use std::io::{self, Write};
use stdin_nonblocking::get_stdin_or_default;

// Used for integration testing
fn main() {
let input = get_stdin_or_default(Some("fallback_value"));
println!("Received input: {:?}", input);
let input = get_stdin_or_default(Some(b"fallback_value"));

// Print raw binary data instead of Debug format
io::stdout()
.write_all(&input)
.expect("Failed to write output");
}
Loading