standout-input 7.2.0

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
# standout-input Design

## Overview

`standout-input` is a standalone crate for declarative input collection in CLI applications. It provides a unified way to acquire user input from multiple sources—CLI arguments, stdin, environment variables, editors, and interactive prompts—with automatic fallback chains.

This is the symmetric counterpart to `standout-pipe`:

```
standout-pipe:   Handler → Render → [jq/tee/clipboard]   (output flows OUT)
standout-input:  [arg/stdin/editor/prompt] → Handler     (input flows IN)
```

## Goals

1. **Declarative input chains** - Define fallback sequences (arg → stdin → editor) without imperative logic
2. **Pluggable backends** - Support multiple prompt libraries via a common trait
3. **Minimal by default** - Core has ~2 deps; heavy backends are opt-in via features
4. **Standalone value** - Useful for any CLI, not just standout users
5. **Validation integration** - Chain-level and collector-level validation with retry support
6. **Testable** - Handlers receive resolved content; sources can be mocked

## Architecture

```
┌─────────────────────────────────────────────────────────────────┐
│                    standout-input (core)                         │
│                                                                  │
│  InputCollector<T> trait     InputChain<T> builder               │
│  ArgSource, StdinSource      EnvSource, ClipboardSource          │
│  DefaultSource<T>            Validation hooks                    │
│                                                                  │
│  feature = "simple-prompts"  feature = "editor"                  │
│  ├── SimpleText              └── EditorCollector                 │
│  └── SimpleConfirm               (tempfile + which)              │
│                                                                  │
│  feature = "inquire"         feature = "dialoguer" (future)      │
│  ├── InquireText             ├── DialoguerText                   │
│  ├── InquireConfirm          ├── DialoguerConfirm                │
│  ├── InquireSelect           ├── DialoguerSelect                 │
│  ├── InquireMultiSelect      └── DialoguerEditor                 │
│  ├── InquirePassword                                             │
│  └── InquireEditor                                               │
│                                                                  │
│  feature = "validify"                                            │
│  └── Validify rule integration                                   │
└─────────────────────────────────────────────────────────────────┘
```

## Core Trait

```rust
/// A source that can collect input of type T.
pub trait InputCollector<T>: Send + Sync {
    /// Human-readable name for errors and debugging.
    fn name(&self) -> &'static str;

    /// Can this collector provide input in the current environment?
    ///
    /// Returns false if:
    /// - Interactive collector but no TTY
    /// - Stdin source but stdin is not piped
    /// - Arg source but argument not provided
    fn is_available(&self, matches: &ArgMatches) -> bool;

    /// Attempt to collect input.
    ///
    /// Returns:
    /// - Ok(Some(value)) if input was collected
    /// - Ok(None) if this source should be skipped (try next in chain)
    /// - Err(_) on failure (abort the chain)
    fn collect(&self, matches: &ArgMatches) -> Result<Option<T>, InputError>;

    /// Validate collected value. Called after successful collect().
    /// Default implementation accepts all values.
    fn validate(&self, _value: &T) -> Result<(), String> {
        Ok(())
    }

    /// Can this collector retry after validation failure?
    /// Returns true for interactive collectors (prompts, editor).
    fn can_retry(&self) -> bool {
        false
    }
}
```

## Input Chain

```rust
/// Chain multiple input sources with fallback behavior.
pub struct InputChain<T> {
    sources: Vec<Box<dyn InputCollector<T>>>,
    validators: Vec<Box<dyn Fn(&T) -> Result<(), String> + Send + Sync>>,
    default: Option<T>,
}

impl<T: Clone> InputChain<T> {
    pub fn new() -> Self { ... }

    /// Add any collector to the chain.
    pub fn try_source<C: InputCollector<T> + 'static>(mut self, source: C) -> Self {
        self.sources.push(Box::new(source));
        self
    }

    /// Add a validation rule to the chain.
    pub fn validate<F>(mut self, f: F, error_msg: &str) -> Self
    where F: Fn(&T) -> bool + Send + Sync + 'static { ... }

    /// Use this value if no source provides content.
    pub fn default(mut self, value: T) -> Self {
        self.default = Some(value);
        self
    }

    /// Resolve the chain: try each source in order.
    pub fn resolve(&self, matches: &ArgMatches) -> Result<T, InputError> {
        for source in &self.sources {
            if !source.is_available(matches) {
                continue;
            }

            loop {
                match source.collect(matches)? {
                    Some(value) => {
                        // Source-level validation
                        if let Err(msg) = source.validate(&value) {
                            if source.can_retry() {
                                eprintln!("Invalid: {}", msg);
                                continue;
                            }
                            return Err(InputError::ValidationFailed(msg));
                        }

                        // Chain-level validation
                        for validator in &self.validators {
                            validator(&value)?;
                        }

                        return Ok(value);
                    }
                    None => break, // Try next source
                }
            }
        }

        self.default.clone().ok_or(InputError::NoInput)
    }
}
```

## Built-in Sources (Core)

Always available, no feature flags:

```rust
/// Read from a clap argument.
pub struct ArgSource {
    name: String,
}

/// Read from stdin if piped (not a TTY).
pub struct StdinSource;

/// Read from environment variable.
pub struct EnvSource {
    var_name: String,
}

/// Read from system clipboard.
pub struct ClipboardSource;

/// Provide a default value.
pub struct DefaultSource<T> {
    value: T,
}
```

## Simple Prompts (feature = "simple-prompts")

Minimal prompts using only std::io, no external deps:

```rust
/// Basic text input prompt.
pub struct SimpleText {
    message: String,
    default: Option<String>,
}

/// Basic yes/no confirmation.
pub struct SimpleConfirm {
    message: String,
    default: bool,
}
```

These provide bare-bones functionality for users who don't want inquire's TUI.

## Editor (feature = "editor")

Opens the user's preferred editor:

```rust
pub struct EditorCollector {
    initial: Option<String>,
    extension: Option<String>,
    require_save: bool,
    trim_newlines: bool,
    env_vars: Vec<String>,  // ["VISUAL", "EDITOR"] by default
}

impl EditorCollector {
    pub fn new() -> Self { ... }
    pub fn initial(mut self, content: impl Into<String>) -> Self { ... }
    pub fn extension(mut self, ext: impl Into<String>) -> Self { ... }
    pub fn require_save(mut self, require: bool) -> Self { ... }
    pub fn trim_newlines(mut self, trim: bool) -> Self { ... }
    pub fn env_precedence(mut self, vars: Vec<String>) -> Self { ... }
}
```

Editor detection follows conventions:
1. Check env vars in order (default: `VISUAL`, then `EDITOR`)
2. Fall back to platform defaults (`vim` on Unix, `notepad` on Windows)
3. Search PATH for common editors

## Inquire Backend (feature = "inquire")

Full-featured prompts using the inquire crate:

```rust
/// Rich text input with autocomplete, validation display.
pub struct InquireText { ... }

/// Confirmation with customizable yes/no labels.
pub struct InquireConfirm { ... }

/// Single selection from a list.
pub struct InquireSelect<T> { ... }

/// Multiple selection from a list.
pub struct InquireMultiSelect<T> { ... }

/// Hidden password input.
pub struct InquirePassword { ... }

/// Editor with inquire's two-step UX (press 'e' to open).
pub struct InquireEditor { ... }
```

## Dialoguer Backend (feature = "dialoguer") — Future

Reserved for future implementation. Dialoguer shares the `console` crate with standout-render, making it lightweight for existing standout users.

## Error Types

```rust
#[derive(Debug, thiserror::Error)]
pub enum InputError {
    #[error("No editor found. Set VISUAL or EDITOR environment variable.")]
    NoEditor,

    #[error("Editor cancelled without saving.")]
    EditorCancelled,

    #[error("Editor failed: {0}")]
    EditorFailed(#[source] std::io::Error),

    #[error("Failed to read stdin: {0}")]
    StdinFailed(#[source] std::io::Error),

    #[error("Failed to read clipboard: {0}")]
    ClipboardFailed(String),

    #[error("Prompt cancelled by user.")]
    PromptCancelled,

    #[error("Prompt failed: {0}")]
    PromptFailed(String),

    #[error("Validation failed: {0}")]
    ValidationFailed(String),

    #[error("No input provided and no default available.")]
    NoInput,
}
```

## Usage Examples

### Basic: Arg with Editor Fallback

```rust
use standout_input::{InputChain, ArgSource, EditorCollector};

let message = InputChain::<String>::new()
    .try_source(ArgSource::new("message"))
    .try_source(EditorCollector::new()
        .initial("# Enter commit message\n\n")
        .extension(".md"))
    .resolve(&matches)?;
```

### The gh pr create Pattern

```rust
use standout_input::{InputChain, ArgSource, StdinSource, EditorCollector};

// arg → stdin → editor
let body = InputChain::<String>::new()
    .try_source(ArgSource::new("body"))
    .try_source(StdinSource)
    .try_source(EditorCollector::new()
        .initial("# PR Description\n\n"))
    .validate(|s| !s.trim().is_empty(), "Body cannot be empty")
    .resolve(&matches)?;
```

### Interactive Confirmation

```rust
use standout_input::{InputChain, FlagSource, InquireConfirm};

// -y flag skips prompt
let proceed = InputChain::<bool>::new()
    .try_source(FlagSource::new("yes").inverted())  // -y means true
    .try_source(InquireConfirm::new("Delete 5 items?").default(false))
    .default(false)
    .resolve(&matches)?;
```

### Selection with Validation

```rust
use standout_input::{InputChain, ArgSource, InquireSelect};

#[derive(Clone, Debug)]
enum Format { Json, Yaml, Csv }

let format = InputChain::<Format>::new()
    .try_source(ArgSource::new("format").parse())
    .try_source(InquireSelect::new("Output format:")
        .option(Format::Json, "JSON - machine readable")
        .option(Format::Yaml, "YAML - human readable")
        .option(Format::Csv, "CSV - spreadsheet compatible"))
    .default(Format::Json)
    .resolve(&matches)?;
```

### Direct Library Use (Complex Logic)

For commands with intricate input logic, use primitives directly:

```rust
use standout_input::{editor, stdin, clipboard};

fn create_handler(matches: &ArgMatches) -> Result<Pad> {
    let no_editor = matches.get_flag("no-editor");
    let title_arg = matches.get_one::<String>("title");

    let content = if let Some(piped) = stdin::read_if_piped()? {
        piped
    } else if let Some(title) = title_arg {
        if no_editor {
            title.clone()
        } else {
            let body = editor::edit(EditorConfig::new()
                .initial(&format!("# {}\n\n", title)))?
                .unwrap_or_default();
            format!("{}\n\n{}", title, body)
        }
    } else if no_editor {
        return Err(anyhow!("No content provided"));
    } else {
        let initial = clipboard::read().unwrap_or_default();
        editor::edit(EditorConfig::new().initial(&initial))?
            .ok_or_else(|| anyhow!("Editor cancelled"))?
    };

    // ... create pad
}
```

## Cargo.toml

```toml
[package]
name = "standout-input"
version = "1.0.0"
edition = "2021"
description = "Declarative input collection for CLI applications"
license = "MIT"
keywords = ["cli", "input", "prompt", "editor", "terminal"]
categories = ["command-line-interface"]
repository = "https://github.com/arthur-debert/standout"

[features]
default = ["editor", "simple-prompts"]
editor = ["dep:tempfile", "dep:which"]
simple-prompts = []
inquire = ["dep:inquire"]
# dialoguer = ["dep:dialoguer"]  # Future
# validify = ["dep:validify"]    # Future

[dependencies]
thiserror = "2"
clap = { version = "4", default-features = false }

# Optional: editor support
tempfile = { version = "3", optional = true }
which = { version = "7", optional = true }

# Optional: inquire prompts
inquire = { version = "0.7", optional = true }

# Future
# dialoguer = { version = "0.11", optional = true }
# validify = { version = "...", optional = true }
```

## Dependency Analysis

| Configuration | Unique Deps | Use Case |
|---------------|-------------|----------|
| `default-features = false` | ~2 | Minimal: just arg/stdin/env |
| `features = ["editor"]` | ~16 | + Editor (tempfile, which) |
| `features = ["simple-prompts"]` | ~2 | + Basic TTY prompts |
| `features = ["inquire"]` | ~29 | + Rich TUI prompts |
| `features = ["dialoguer"]` | ~8 new* | + dialoguer (*shares console with standout) |

## Standout Integration

When used with the standout framework:

### Builder API

```rust
let app = App::builder()
    .command_with("create", handlers::create, |cfg| {
        cfg.template("create.jinja")
           .input("body", InputChain::<String>::new()
               .try_source(ArgSource::new("body"))
               .try_source(StdinSource)
               .try_source(EditorCollector::new()))
    })
    .build()?;
```

### Handler Macro (Future)

```rust
#[handler]
pub fn create(
    #[input(fallback = "editor")] body: String,
    #[flag] verbose: bool,
) -> Result<CreateResult, Error> {
    // body is resolved before handler runs
}
```

## Implementation Phases

### Phase 1: Core Structure
- `InputCollector<T>` trait
- `InputChain<T>` builder
- `InputError` type
- Core sources: `ArgSource`, `StdinSource`, `EnvSource`, `ClipboardSource`, `DefaultSource`
- Basic tests

### Phase 2: Backends
- `feature = "editor"`: `EditorCollector` with tempfile + which
- `feature = "simple-prompts"`: `SimpleText`, `SimpleConfirm`
- `feature = "inquire"`: Full inquire adapter suite

### Phase 3: Standout Integration
- Builder API support (`.input()` method)
- Pre-dispatch resolution hook
- Documentation and examples

### Future Considerations
- `feature = "dialoguer"`: Dialoguer adapter (shares console with standout-render)
- `feature = "validify"`: Deep validation integration
- `#[input]` attribute for handler macro