standout-input 7.5.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
//! Editor-based input source.
//!
//! Opens the user's preferred text editor for multi-line input.

use std::fs;
use std::io::{self, Write};
use std::path::Path;
use std::process::Command;
use std::sync::Arc;
use std::time::SystemTime;

use clap::ArgMatches;

use crate::collector::InputCollector;
use crate::InputError;

/// Abstraction over editor invocation for testability.
pub trait EditorRunner: Send + Sync {
    /// Detect the editor to use.
    ///
    /// Returns `None` if no editor is available.
    fn detect_editor(&self) -> Option<String>;

    /// Run the editor on the given file path.
    ///
    /// Returns `Ok(())` if the editor exited successfully, `Err` otherwise.
    fn run(&self, editor: &str, path: &Path) -> io::Result<()>;
}

/// Real editor runner using system commands.
#[derive(Debug, Default, Clone, Copy)]
pub struct RealEditorRunner;

impl EditorRunner for RealEditorRunner {
    fn detect_editor(&self) -> Option<String> {
        // Try VISUAL first (supports GUI editors)
        if let Ok(editor) = std::env::var("VISUAL") {
            if !editor.is_empty() && editor_exists(&editor) {
                return Some(editor);
            }
        }

        // Fall back to EDITOR
        if let Ok(editor) = std::env::var("EDITOR") {
            if !editor.is_empty() && editor_exists(&editor) {
                return Some(editor);
            }
        }

        // Platform-specific fallbacks
        #[cfg(unix)]
        {
            for fallback in ["vim", "vi", "nano"] {
                if editor_exists(fallback) {
                    return Some(fallback.to_string());
                }
            }
        }

        #[cfg(windows)]
        {
            if editor_exists("notepad") {
                return Some("notepad".to_string());
            }
        }

        None
    }

    fn run(&self, editor: &str, path: &Path) -> io::Result<()> {
        // Parse the editor command to handle cases like "code --wait" or "vim -u NONE"
        let parts = shell_words::split(editor).map_err(|e| {
            io::Error::other(format!(
                "Failed to parse editor command '{}': {}",
                editor, e
            ))
        })?;

        if parts.is_empty() {
            return Err(io::Error::other("Editor command is empty"));
        }

        let (cmd, args) = parts.split_first().unwrap();
        let status = Command::new(cmd).args(args).arg(path).status()?;

        if status.success() {
            Ok(())
        } else {
            Err(io::Error::other(format!(
                "Editor exited with status: {}",
                status
            )))
        }
    }
}

/// Check if an editor command exists.
fn editor_exists(editor: &str) -> bool {
    // Extract the command name (first word) in case of "vim -u NONE" etc.
    let cmd = editor.split_whitespace().next().unwrap_or(editor);
    which::which(cmd).is_ok()
}

/// Collect input via an external text editor.
///
/// Opens the user's preferred editor (from `$VISUAL` or `$EDITOR`) with a
/// temporary file, waits for the user to save and close, then reads the result.
///
/// # Editor Detection
///
/// Editors are detected in this order:
/// 1. `$VISUAL` environment variable (supports GUI editors)
/// 2. `$EDITOR` environment variable
/// 3. Platform fallbacks: `vim`, `vi`, `nano` on Unix; `notepad` on Windows
///
/// # Example
///
/// ```ignore
/// use standout_input::{InputChain, ArgSource, EditorSource};
///
/// // Fall back to editor if no CLI argument
/// let chain = InputChain::<String>::new()
///     .try_source(ArgSource::new("message"))
///     .try_source(EditorSource::new());
///
/// let message = chain.resolve(&matches)?;
/// ```
///
/// # Configuration
///
/// ```ignore
/// let source = EditorSource::new()
///     .initial_content("# Enter your message\n\n")
///     .extension(".md")
///     .require_save(true);
/// ```
#[derive(Clone)]
pub struct EditorSource<R: EditorRunner = RealEditorRunner> {
    runner: Arc<R>,
    initial_content: Option<String>,
    extension: String,
    require_save: bool,
    trim: bool,
}

impl EditorSource<RealEditorRunner> {
    /// Create a new editor source using the system editor.
    pub fn new() -> Self {
        Self {
            runner: Arc::new(RealEditorRunner),
            initial_content: None,
            extension: ".txt".to_string(),
            require_save: false,
            trim: true,
        }
    }
}

impl Default for EditorSource<RealEditorRunner> {
    fn default() -> Self {
        Self::new()
    }
}

impl<R: EditorRunner> EditorSource<R> {
    /// Create an editor source with a custom runner.
    ///
    /// Primarily used for testing to mock editor invocation.
    pub fn with_runner(runner: R) -> Self {
        Self {
            runner: Arc::new(runner),
            initial_content: None,
            extension: ".txt".to_string(),
            require_save: false,
            trim: true,
        }
    }

    /// Set initial content to populate the editor with.
    ///
    /// This can be used to provide a template or instructions.
    pub fn initial_content(mut self, content: impl Into<String>) -> Self {
        self.initial_content = Some(content.into());
        self
    }

    /// Set the file extension for the temporary file.
    ///
    /// This affects syntax highlighting in the editor.
    /// Default is `.txt`.
    pub fn extension(mut self, ext: impl Into<String>) -> Self {
        self.extension = ext.into();
        self
    }

    /// Require the user to actually save the file.
    ///
    /// If `true`, the source will return `None` if the file's modification
    /// time hasn't changed (i.e., the user closed without saving).
    /// Default is `false`.
    pub fn require_save(mut self, require: bool) -> Self {
        self.require_save = require;
        self
    }

    /// Control whether to trim whitespace from the result.
    ///
    /// Default is `true`.
    pub fn trim(mut self, trim: bool) -> Self {
        self.trim = trim;
        self
    }
}

impl<R: EditorRunner + 'static> EditorSource<R> {
    /// Open the editor and return the saved content.
    ///
    /// This is the standalone counterpart to [`InputCollector::collect`]:
    /// it skips the chain machinery (no `&ArgMatches` to plumb through) and
    /// is intended for wizard or REPL flows that drive standout themselves.
    ///
    /// Returns [`InputError::NoInput`] if stdin is not a TTY or no editor
    /// can be detected (the same conditions under which a chain would skip
    /// this source). User cancellation is reported as
    /// [`InputError::EditorCancelled`] when `require_save` is set and the
    /// user exits without saving.
    ///
    /// Routes through any installed
    /// [`PromptResponder`](crate::PromptResponder), so wizard tests can
    /// supply the editor's "saved" content directly without launching
    /// `$EDITOR`.
    pub fn prompt(&self) -> Result<String, InputError> {
        if let Some(value) = crate::responder::intercept_text(
            crate::PromptKind::Editor,
            // EditorSource has no user-facing "message" — use the file
            // extension as the diagnostic hint so panic messages still
            // identify which editor source failed.
            &self.extension,
        )? {
            return Ok(value);
        }
        let matches = crate::collector::empty_matches();
        if !self.is_available(matches) {
            return Err(InputError::NoInput);
        }
        self.collect(matches)?.ok_or(InputError::NoInput)
    }
}

impl<R: EditorRunner + 'static> InputCollector<String> for EditorSource<R> {
    fn name(&self) -> &'static str {
        "editor"
    }

    fn is_available(&self, _matches: &ArgMatches) -> bool {
        // Editor is available if we can detect one and we have a TTY
        self.runner.detect_editor().is_some() && std::io::stdin().is_terminal()
    }

    fn collect(&self, _matches: &ArgMatches) -> Result<Option<String>, InputError> {
        let editor = self.runner.detect_editor().ok_or(InputError::NoEditor)?;

        // Create a temporary file with the specified extension
        let mut builder = tempfile::Builder::new();
        builder.suffix(&self.extension);
        let temp_file = builder.tempfile().map_err(InputError::EditorFailed)?;

        let path = temp_file.path();

        // Write initial content if provided
        if let Some(content) = &self.initial_content {
            fs::write(path, content).map_err(InputError::EditorFailed)?;
        }

        // Record initial modification time if we need to check for save
        let initial_mtime = if self.require_save {
            get_mtime(path).ok()
        } else {
            None
        };

        // Run the editor
        self.runner
            .run(&editor, path)
            .map_err(InputError::EditorFailed)?;

        // Check if user actually saved (if required)
        if let Some(initial) = initial_mtime {
            if let Ok(final_mtime) = get_mtime(path) {
                if initial == final_mtime {
                    return Err(InputError::EditorCancelled);
                }
            }
        }

        // Read the result
        let content = fs::read_to_string(path).map_err(InputError::EditorFailed)?;

        let result = if self.trim {
            content.trim().to_string()
        } else {
            content
        };

        if result.is_empty() {
            Ok(None)
        } else {
            Ok(Some(result))
        }
    }

    fn can_retry(&self) -> bool {
        // Editor is interactive, so we can retry on validation failure
        true
    }
}

/// Get the modification time of a file.
fn get_mtime(path: &Path) -> io::Result<SystemTime> {
    fs::metadata(path)?.modified()
}

use std::io::IsTerminal;

/// Mock editor runner for testing.
///
/// Simulates editor behavior without actually launching an editor.
#[derive(Debug, Clone)]
pub struct MockEditorRunner {
    editor: Option<String>,
    result: MockEditorResult,
}

/// The result of a mock editor invocation.
#[derive(Debug, Clone)]
pub enum MockEditorResult {
    /// Editor writes this content and exits successfully.
    Success(String),
    /// Editor fails with an error.
    Failure(String),
    /// Editor exits without saving (for require_save tests).
    NoSave,
}

impl MockEditorRunner {
    /// Create a mock that simulates no editor available.
    pub fn no_editor() -> Self {
        Self {
            editor: None,
            result: MockEditorResult::Failure("no editor".to_string()),
        }
    }

    /// Create a mock that simulates successful editor input.
    pub fn with_result(content: impl Into<String>) -> Self {
        Self {
            editor: Some("mock-editor".to_string()),
            result: MockEditorResult::Success(content.into()),
        }
    }

    /// Create a mock that simulates editor failure.
    pub fn failure(message: impl Into<String>) -> Self {
        Self {
            editor: Some("mock-editor".to_string()),
            result: MockEditorResult::Failure(message.into()),
        }
    }

    /// Create a mock that simulates closing without saving.
    pub fn no_save() -> Self {
        Self {
            editor: Some("mock-editor".to_string()),
            result: MockEditorResult::NoSave,
        }
    }
}

impl EditorRunner for MockEditorRunner {
    fn detect_editor(&self) -> Option<String> {
        self.editor.clone()
    }

    fn run(&self, _editor: &str, path: &Path) -> io::Result<()> {
        match &self.result {
            MockEditorResult::Success(content) => {
                // Write the mock content to the file
                let mut file = fs::OpenOptions::new()
                    .write(true)
                    .truncate(true)
                    .open(path)?;
                file.write_all(content.as_bytes())?;
                Ok(())
            }
            MockEditorResult::Failure(msg) => Err(io::Error::other(msg.clone())),
            MockEditorResult::NoSave => {
                // Don't modify the file at all
                Ok(())
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use clap::Command;

    fn empty_matches() -> ArgMatches {
        Command::new("test").try_get_matches_from(["test"]).unwrap()
    }

    #[test]
    fn editor_unavailable_when_no_editor() {
        let source = EditorSource::with_runner(MockEditorRunner::no_editor());
        assert!(!source.is_available(&empty_matches()));
    }

    #[test]
    fn editor_collects_input() {
        let source = EditorSource::with_runner(MockEditorRunner::with_result("hello from editor"));
        let result = source.collect(&empty_matches()).unwrap();
        assert_eq!(result, Some("hello from editor".to_string()));
    }

    #[test]
    fn editor_trims_whitespace() {
        let source = EditorSource::with_runner(MockEditorRunner::with_result("  hello  \n\n"));
        let result = source.collect(&empty_matches()).unwrap();
        assert_eq!(result, Some("hello".to_string()));
    }

    #[test]
    fn editor_no_trim() {
        let source =
            EditorSource::with_runner(MockEditorRunner::with_result("  hello  \n")).trim(false);
        let result = source.collect(&empty_matches()).unwrap();
        assert_eq!(result, Some("  hello  \n".to_string()));
    }

    #[test]
    fn editor_returns_none_for_empty() {
        let source = EditorSource::with_runner(MockEditorRunner::with_result(""));
        let result = source.collect(&empty_matches()).unwrap();
        assert_eq!(result, None);
    }

    #[test]
    fn editor_returns_none_for_whitespace_only() {
        let source = EditorSource::with_runner(MockEditorRunner::with_result("   \n\t  "));
        let result = source.collect(&empty_matches()).unwrap();
        assert_eq!(result, None);
    }

    #[test]
    fn editor_handles_failure() {
        let source = EditorSource::with_runner(MockEditorRunner::failure("editor crashed"));
        let result = source.collect(&empty_matches());
        assert!(matches!(result, Err(InputError::EditorFailed(_))));
    }

    #[test]
    fn editor_with_initial_content() {
        // The mock runner ignores initial content since it writes its own result
        let source = EditorSource::with_runner(MockEditorRunner::with_result("user input"))
            .initial_content("# Template\n\n");
        let result = source.collect(&empty_matches()).unwrap();
        assert_eq!(result, Some("user input".to_string()));
    }

    #[test]
    fn editor_can_retry() {
        let source = EditorSource::with_runner(MockEditorRunner::with_result("test"));
        assert!(source.can_retry());
    }

    #[test]
    fn editor_no_editor_error() {
        let source = EditorSource::with_runner(MockEditorRunner::no_editor());
        let result = source.collect(&empty_matches());
        assert!(matches!(result, Err(InputError::NoEditor)));
    }

    // === .prompt() shortcut ===
    //
    // EditorSource::is_available checks std::io::stdin().is_terminal() directly,
    // so under `cargo test` (no TTY) prompt() always short-circuits to NoInput.
    // The happy path with the mock runner is covered by the existing
    // editor_collects_content / editor_failure / editor_no_editor_error tests
    // on collect(), which prompt() delegates to once the TTY gate passes.
    //
    // Every test that calls .prompt() shares one #[serial] axis
    // (`prompt_responder`) because the global responder override is
    // process-wide; without serialization a responder installed by a
    // parallel responder-using test would leak into these vanilla
    // shortcut tests.

    use crate::{
        reset_default_prompt_responder, set_default_prompt_responder, PromptResponse,
        ScriptedResponder,
    };
    use serial_test::serial;
    use std::sync::Arc;

    #[test]
    #[serial(prompt_responder)]
    fn editor_prompt_shortcut_returns_no_input_in_non_tty() {
        let source = EditorSource::with_runner(MockEditorRunner::with_result("hello"));
        let err = source.prompt().unwrap_err();
        assert!(matches!(err, InputError::NoInput));
    }

    #[test]
    #[serial(prompt_responder)]
    fn editor_prompt_shortcut_no_input_when_no_editor_detected() {
        // No TTY *and* no editor — both fail is_available, so NoInput either way.
        let source = EditorSource::with_runner(MockEditorRunner::no_editor());
        let err = source.prompt().unwrap_err();
        assert!(matches!(err, InputError::NoInput));
    }

    // === .prompt() via PromptResponder ===

    struct ResponderGuard;
    impl ResponderGuard {
        fn install(responder: ScriptedResponder) -> Self {
            set_default_prompt_responder(Arc::new(responder));
            Self
        }
    }
    impl Drop for ResponderGuard {
        fn drop(&mut self) {
            reset_default_prompt_responder();
        }
    }

    #[test]
    #[serial(prompt_responder)]
    fn editor_prompt_routes_through_responder_without_launching_editor() {
        let _g = ResponderGuard::install(ScriptedResponder::new([PromptResponse::text(
            "edited body",
        )]));
        // Even with a no-editor mock runner, the responder gate runs first
        // and wins — the editor is never launched in tests.
        let source = EditorSource::with_runner(MockEditorRunner::no_editor());
        let value = source.prompt().unwrap();
        assert_eq!(value, "edited body");
    }

    #[test]
    #[serial(prompt_responder)]
    fn editor_prompt_responder_cancel_propagates() {
        let _g = ResponderGuard::install(ScriptedResponder::new([PromptResponse::Cancel]));
        let source = EditorSource::with_runner(MockEditorRunner::no_editor());
        let err = source.prompt().unwrap_err();
        assert!(matches!(err, InputError::PromptCancelled));
    }
}