Function get_stdin_or_default

Source
pub fn get_stdin_or_default(default: Option<&[u8]>) -> Option<Vec<u8>>
Expand description

Reads stdin if available; otherwise, returns a default value.

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<Vec<u8>> - The stdin input if available, otherwise the provided default.

§Example

use stdin_nonblocking::get_stdin_or_default;

let input = get_stdin_or_default(Some(b"fallback_value"));

assert_eq!(input, Some(b"fallback_value".to_vec()));