1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use ;
use ;
use thread;
use Duration;
/// Spawns a background thread that continuously reads from stdin as a stream.
///
/// This function returns an `mpsc Receiver`, allowing non-blocking polling
/// of stdin input just like `spawn_stdin_channel`.
///
/// # Returns
/// A `Receiver<String>` that emits lines from stdin.
///
/// # Example
/// ```
/// use stdin_nonblocking::spawn_stdin_stream;
/// use std::sync::mpsc::TryRecvError;
/// use std::time::Duration;
///
/// fn main() {
/// let stdin_stream = spawn_stdin_stream();
///
/// loop {
/// match stdin_stream.try_recv() {
/// Ok(line) => println!("Received: {}", line),
/// Err(TryRecvError::Empty) => {
/// // No input yet; continue execution
/// }
/// Err(TryRecvError::Disconnected) => {
/// println!("Input stream closed. Exiting...");
/// break;
/// }
/// }
/// std::thread::sleep(Duration::from_millis(500));
/// }
/// }
/// ```
/// Reads from stdin if available, otherwise returns a default value.
///
/// **Non-blocking:** This function polls `stdin` once and immediately returns.
///
/// # Arguments
/// * `default` - A fallback value returned if no input is available.
///
/// # Returns
/// * `String` - Trimmed stdin input or `default` if no input is received.
///
/// # Example
/// ```
/// use stdin_nonblocking::get_stdin_or_default;
///
/// fn main() {
/// let input = get_stdin_or_default("fallback_value");
/// println!("Final input: {}", input);
/// }
/// ```