standout-input 7.6.1

Declarative input collection for CLI applications
Documentation
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//! Environment abstractions for testability.
//!
//! This module provides traits that abstract over OS interactions,
//! allowing tests to run without depending on actual terminal state,
//! stdin piping, or clipboard contents.
//!
//! # Default readers and test overrides
//!
//! [`StdinSource::new`](crate::StdinSource::new) and
//! [`ClipboardSource::new`](crate::ClipboardSource::new) both use "default"
//! readers ([`DefaultStdin`], [`DefaultClipboard`]) that consult a
//! process-global override before falling back to the real OS-backed
//! implementation.
//!
//! Tests can swap in a mock without touching handler code by calling
//! [`set_default_stdin_reader`] / [`set_default_clipboard_reader`]. The
//! `TestHarness` in the `standout-test` crate wires these automatically.

use once_cell::sync::Lazy;
use std::io::{self, IsTerminal, Read};
use std::sync::{Arc, Mutex};

use crate::InputError;

/// Abstraction over stdin reading.
///
/// This trait allows tests to mock stdin without actually piping data.
pub trait StdinReader: Send + Sync {
    /// Check if stdin is a terminal (TTY).
    ///
    /// Returns `true` if stdin is interactive, `false` if piped.
    fn is_terminal(&self) -> bool;

    /// Read all content from stdin.
    ///
    /// This should only be called if `is_terminal()` returns `false`.
    fn read_to_string(&self) -> io::Result<String>;
}

/// Abstraction over environment variables.
pub trait EnvReader: Send + Sync {
    /// Get an environment variable value.
    fn var(&self, name: &str) -> Option<String>;
}

/// Abstraction over clipboard access.
pub trait ClipboardReader: Send + Sync {
    /// Read text from the system clipboard.
    fn read(&self) -> Result<Option<String>, InputError>;
}

// === Real implementations ===

/// Real stdin reader using std::io.
#[derive(Debug, Default, Clone, Copy)]
pub struct RealStdin;

impl StdinReader for RealStdin {
    fn is_terminal(&self) -> bool {
        std::io::stdin().is_terminal()
    }

    fn read_to_string(&self) -> io::Result<String> {
        let mut buffer = String::new();
        std::io::stdin().read_to_string(&mut buffer)?;
        Ok(buffer)
    }
}

/// Real environment variable reader.
#[derive(Debug, Default, Clone, Copy)]
pub struct RealEnv;

impl EnvReader for RealEnv {
    fn var(&self, name: &str) -> Option<String> {
        std::env::var(name).ok()
    }
}

/// Real clipboard reader using platform commands.
#[derive(Debug, Default, Clone, Copy)]
pub struct RealClipboard;

impl ClipboardReader for RealClipboard {
    fn read(&self) -> Result<Option<String>, InputError> {
        read_clipboard_impl()
    }
}

#[cfg(target_os = "macos")]
fn read_clipboard_impl() -> Result<Option<String>, InputError> {
    let output = std::process::Command::new("pbpaste")
        .output()
        .map_err(|e| InputError::ClipboardFailed(e.to_string()))?;

    if output.status.success() {
        let content = String::from_utf8_lossy(&output.stdout).to_string();
        if content.is_empty() {
            Ok(None)
        } else {
            Ok(Some(content))
        }
    } else {
        Ok(None)
    }
}

#[cfg(target_os = "linux")]
fn read_clipboard_impl() -> Result<Option<String>, InputError> {
    let output = std::process::Command::new("xclip")
        .args(["-selection", "clipboard", "-o"])
        .output()
        .map_err(|e| InputError::ClipboardFailed(e.to_string()))?;

    if output.status.success() {
        let content = String::from_utf8_lossy(&output.stdout).to_string();
        if content.is_empty() {
            Ok(None)
        } else {
            Ok(Some(content))
        }
    } else {
        Ok(None)
    }
}

#[cfg(not(any(target_os = "macos", target_os = "linux")))]
fn read_clipboard_impl() -> Result<Option<String>, InputError> {
    Err(InputError::ClipboardFailed(
        "Clipboard not supported on this platform".to_string(),
    ))
}

// === Mock implementations for testing ===

/// Mock stdin reader for testing.
///
/// Allows tests to simulate both terminal and piped stdin.
#[derive(Debug, Clone)]
pub struct MockStdin {
    is_terminal: bool,
    content: Option<String>,
}

impl MockStdin {
    /// Create a mock that simulates a terminal (no piped input).
    pub fn terminal() -> Self {
        Self {
            is_terminal: true,
            content: None,
        }
    }

    /// Create a mock that simulates piped input.
    pub fn piped(content: impl Into<String>) -> Self {
        Self {
            is_terminal: false,
            content: Some(content.into()),
        }
    }

    /// Create a mock that simulates empty piped input.
    pub fn piped_empty() -> Self {
        Self {
            is_terminal: false,
            content: Some(String::new()),
        }
    }
}

impl StdinReader for MockStdin {
    fn is_terminal(&self) -> bool {
        self.is_terminal
    }

    fn read_to_string(&self) -> io::Result<String> {
        Ok(self.content.clone().unwrap_or_default())
    }
}

/// Mock environment variable reader for testing.
#[derive(Debug, Clone, Default)]
pub struct MockEnv {
    vars: std::collections::HashMap<String, String>,
}

impl MockEnv {
    /// Create an empty mock environment.
    pub fn new() -> Self {
        Self::default()
    }

    /// Add an environment variable.
    pub fn with_var(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
        self.vars.insert(name.into(), value.into());
        self
    }
}

impl EnvReader for MockEnv {
    fn var(&self, name: &str) -> Option<String> {
        self.vars.get(name).cloned()
    }
}

/// Mock clipboard reader for testing.
#[derive(Debug, Clone, Default)]
pub struct MockClipboard {
    content: Option<String>,
}

impl MockClipboard {
    /// Create an empty clipboard mock.
    pub fn empty() -> Self {
        Self { content: None }
    }

    /// Create a clipboard mock with content.
    pub fn with_content(content: impl Into<String>) -> Self {
        Self {
            content: Some(content.into()),
        }
    }
}

impl ClipboardReader for MockClipboard {
    fn read(&self) -> Result<Option<String>, InputError> {
        Ok(self.content.clone())
    }
}

// === Process-global default reader overrides ===
//
// `StdinSource::new()` and `ClipboardSource::new()` resolve their reader
// through the `DefaultStdin` / `DefaultClipboard` shims below, which consult
// these slots before falling back to the real OS-backed readers. Tests use
// `set_default_*_reader` to install a mock for a serial scope; the
// `standout-test` `TestHarness` handles teardown via its `Drop` impl.

type SharedStdin = Arc<dyn StdinReader + Send + Sync>;
type SharedClipboard = Arc<dyn ClipboardReader + Send + Sync>;

static STDIN_OVERRIDE: Lazy<Mutex<Option<SharedStdin>>> = Lazy::new(|| Mutex::new(None));
static CLIPBOARD_OVERRIDE: Lazy<Mutex<Option<SharedClipboard>>> = Lazy::new(|| Mutex::new(None));

/// Installs a process-global stdin reader that [`DefaultStdin`] (and
/// therefore [`StdinSource::new`](crate::StdinSource::new)) will delegate
/// to until [`reset_default_stdin_reader`] is called.
///
/// Intended for test harnesses. Tests using this must run serially (e.g.
/// via `#[serial]`) because the override is process-global.
pub fn set_default_stdin_reader(reader: SharedStdin) {
    *STDIN_OVERRIDE.lock().unwrap() = Some(reader);
}

/// Clears the stdin override installed by [`set_default_stdin_reader`].
pub fn reset_default_stdin_reader() {
    *STDIN_OVERRIDE.lock().unwrap() = None;
}

/// Installs a process-global clipboard reader that [`DefaultClipboard`]
/// (and therefore [`ClipboardSource::new`](crate::ClipboardSource::new))
/// will delegate to until [`reset_default_clipboard_reader`] is called.
pub fn set_default_clipboard_reader(reader: SharedClipboard) {
    *CLIPBOARD_OVERRIDE.lock().unwrap() = Some(reader);
}

/// Clears the clipboard override installed by
/// [`set_default_clipboard_reader`].
pub fn reset_default_clipboard_reader() {
    *CLIPBOARD_OVERRIDE.lock().unwrap() = None;
}

fn current_stdin_override() -> Option<SharedStdin> {
    STDIN_OVERRIDE.lock().unwrap().clone()
}

fn current_clipboard_override() -> Option<SharedClipboard> {
    CLIPBOARD_OVERRIDE.lock().unwrap().clone()
}

/// Stdin reader used by [`StdinSource::new`](crate::StdinSource::new).
///
/// Delegates to a reader installed via [`set_default_stdin_reader`] if one
/// is present; otherwise falls back to [`RealStdin`]. The indirection lets
/// a test harness inject a [`MockStdin`] without reconstructing sources
/// inside handler code.
#[derive(Debug, Default, Clone, Copy)]
pub struct DefaultStdin;

impl StdinReader for DefaultStdin {
    fn is_terminal(&self) -> bool {
        if let Some(r) = current_stdin_override() {
            return r.is_terminal();
        }
        RealStdin.is_terminal()
    }

    fn read_to_string(&self) -> io::Result<String> {
        if let Some(r) = current_stdin_override() {
            return r.read_to_string();
        }
        RealStdin.read_to_string()
    }
}

/// Clipboard reader used by
/// [`ClipboardSource::new`](crate::ClipboardSource::new).
///
/// Delegates to a reader installed via [`set_default_clipboard_reader`] if
/// one is present; otherwise falls back to [`RealClipboard`].
#[derive(Debug, Default, Clone, Copy)]
pub struct DefaultClipboard;

impl ClipboardReader for DefaultClipboard {
    fn read(&self) -> Result<Option<String>, InputError> {
        if let Some(r) = current_clipboard_override() {
            return r.read();
        }
        RealClipboard.read()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serial_test::serial;

    #[test]
    fn mock_stdin_terminal() {
        let stdin = MockStdin::terminal();
        assert!(stdin.is_terminal());
    }

    #[test]
    fn mock_stdin_piped() {
        let stdin = MockStdin::piped("hello world");
        assert!(!stdin.is_terminal());
        assert_eq!(stdin.read_to_string().unwrap(), "hello world");
    }

    #[test]
    fn mock_stdin_piped_empty() {
        let stdin = MockStdin::piped_empty();
        assert!(!stdin.is_terminal());
        assert_eq!(stdin.read_to_string().unwrap(), "");
    }

    #[test]
    fn mock_env_empty() {
        let env = MockEnv::new();
        assert_eq!(env.var("MISSING"), None);
    }

    #[test]
    fn mock_env_with_vars() {
        let env = MockEnv::new()
            .with_var("EDITOR", "vim")
            .with_var("HOME", "/home/user");

        assert_eq!(env.var("EDITOR"), Some("vim".to_string()));
        assert_eq!(env.var("HOME"), Some("/home/user".to_string()));
        assert_eq!(env.var("MISSING"), None);
    }

    #[test]
    fn mock_clipboard_empty() {
        let clipboard = MockClipboard::empty();
        assert_eq!(clipboard.read().unwrap(), None);
    }

    #[test]
    fn mock_clipboard_with_content() {
        let clipboard = MockClipboard::with_content("clipboard text");
        assert_eq!(
            clipboard.read().unwrap(),
            Some("clipboard text".to_string())
        );
    }

    #[test]
    #[serial]
    fn default_stdin_uses_override() {
        set_default_stdin_reader(Arc::new(MockStdin::piped("overridden")));
        let reader = DefaultStdin;
        assert!(!reader.is_terminal());
        assert_eq!(reader.read_to_string().unwrap(), "overridden");
        reset_default_stdin_reader();
    }

    #[test]
    #[serial]
    fn default_stdin_falls_back_without_override() {
        reset_default_stdin_reader();
        // Behaviour matches RealStdin; we can only assert it doesn't panic
        // and reports a terminal state consistent with the current process.
        let reader = DefaultStdin;
        let _ = reader.is_terminal();
    }

    #[test]
    #[serial]
    fn default_clipboard_uses_override() {
        set_default_clipboard_reader(Arc::new(MockClipboard::with_content("paste")));
        let reader = DefaultClipboard;
        assert_eq!(reader.read().unwrap(), Some("paste".to_string()));
        reset_default_clipboard_reader();
    }
}