tinsel 1.1.0

Clean terminal output with a little sparkle: live step lists, spinners, tables, trees, prompts and full-screen apps.
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
//! Yes or no, and a line of text.

use super::*;

/// Yes or no. Enter takes the default. [`Confirm`] has more options.
///
/// Without a terminal it reads a line instead: `y`, `yes`, `n` or `no` in any case, and an
/// empty line (or the end of the input) takes the default. Anything else is an
/// [`Error::Invalid`], unless the question is [`lenient`](Confirm::lenient).
///
/// ```no_run
/// if tinsel::prompt::confirm("Deploy to production?", false)? {
///     tinsel::success("deploying");
/// }
/// # Ok::<(), tinsel::prompt::Error>(())
/// ```
///
/// # Errors
///
/// [`Error::Cancelled`] on Ctrl-C or Esc, [`Error::NotATerminal`] when there's no terminal to ask
/// on and no line to read, [`Error::Invalid`] when a line read from a pipe isn't an answer it
/// takes, and [`Error::Io`] when the terminal can't be used.
pub fn confirm(question: &str, default: bool) -> Result<bool> {
    Confirm::new(question).default(default).ask()
}

/// A yes or no question with options. [`confirm`] is the shortcut. Answers no unless given
/// a [`default`](Confirm::default).
///
/// ```no_run
/// use tinsel::prompt::Confirm;
///
/// let tag = Confirm::new("Tag the release?").default(true).back().ask()?;
/// # Ok::<(), tinsel::prompt::Error>(())
/// ```
#[derive(Clone, Debug)]
pub struct Confirm {
    pub(super) question: String,
    pub(super) default: bool,
    pub(super) back: bool,
    at_end: Option<bool>,
    lenient: bool,
}

impl Confirm {
    /// A yes or no question.
    pub fn new(question: impl Into<String>) -> Self {
        Confirm {
            question: question.into(),
            default: false,
            back: false,
            at_end: None,
            lenient: false,
        }
    }

    /// The answer when Enter or → is pressed (or an empty line is piped in).
    #[must_use]
    pub fn default(mut self, yes: bool) -> Self {
        self.default = yes;
        self
    }

    /// Let ← go back to the question before, as [`Error::Back`].
    #[must_use]
    pub fn back(mut self) -> Self {
        self.back = true;
        self
    }

    /// The answer when there's no terminal and the piped input has already ended, so a
    /// script that runs out of answers can say no. The [`default`](Confirm::default) unless set.
    #[must_use]
    pub fn end_of_input(mut self, yes: bool) -> Self {
        self.at_end = Some(yes);
        self
    }

    /// Read a piped answer the way shell scripts often do: anything starting with `y` or `Y`
    /// is yes, anything else is no, and an empty line takes the [`default`](Confirm::default).
    /// Without it, only `y`, `yes`, `n` and `no` (in any case) are answers, and anything else
    /// is an [`Error::Invalid`]. Keys typed at a terminal work the same either way.
    ///
    /// ```no_run
    /// use tinsel::prompt::Confirm;
    ///
    /// // `echo yep | my-cli` says yes, `echo nope | my-cli` says no
    /// let go = Confirm::new("Go?").lenient().ask()?;
    /// # Ok::<(), tinsel::prompt::Error>(())
    /// ```
    #[must_use]
    pub fn lenient(mut self) -> Self {
        self.lenient = true;
        self
    }

    /// Ask the question. Without a terminal, reads the answer from stdin as [`confirm`] does.
    ///
    /// # Errors
    ///
    /// [`Error::Cancelled`] on Ctrl-C or Esc, [`Error::Back`] on ← when asked with `.back()`,
    /// [`Error::NotATerminal`] when there's no terminal to ask on and no line to read,
    /// [`Error::Invalid`] when a line read from a pipe isn't an answer it takes, and [`Error::Io`] when
    /// the terminal can't be used.
    pub fn ask(self) -> Result<bool> {
        if !interactive() {
            return self.unasked();
        }
        self.run(&mut Ui::open()?)
    }

    /// The answer without a terminal: a line read from stdin.
    pub(super) fn unasked(&self) -> Result<bool> {
        match read_piped()? {
            Some(line) => yes_or_no(&line, self.default, self.lenient),
            None => Ok(self.at_end.unwrap_or(self.default)),
        }
    }

    pub(super) fn run(&self, ui: &mut Ui) -> Result<bool> {
        let question = self.question.as_str();
        let choices = if self.default { "Y/n" } else { "y/N" };
        ui.draw(&[format!(
            "{}  {}{}",
            ask(question),
            paint(MUTED, choices),
            back_hint(self.back)
        )]);
        loop {
            let answer = match ui.key()? {
                Key::Char('y' | 'Y') => true,
                Key::Right => self.default,
                Key::Char('n' | 'N') => false,
                Key::Enter => self.default,
                Key::Left if self.back => return Err(ui.back()),
                Key::Esc | Key::Interrupt => return Err(cancel(&mut ui.frame, question)),
                _ => continue,
            };
            ui.draw(&[answered(question, if answer { "yes" } else { "no" })]);
            return Ok(answer);
        }
    }
}

/// A yes or no answer read from a pipe. Leniently, anything but an empty line is an answer.
pub(super) fn yes_or_no(line: &str, default: bool, lenient: bool) -> Result<bool> {
    let answer = line.trim().to_lowercase();
    if lenient && !answer.is_empty() {
        return Ok(answer.starts_with('y'));
    }
    match answer.as_str() {
        "" => Ok(default),
        "y" | "yes" => Ok(true),
        "n" | "no" => Ok(false),
        _ => Err(Error::Invalid(format!("“{}” isn't yes or no", line.trim()))),
    }
}

// MARK: - Text

/// What a validation check returns: why the answer won't do, if it won't.
pub(super) type Checked = std::result::Result<(), String>;
pub(super) type ManyCheck<'a> = Box<dyn Fn(&[usize]) -> Checked + 'a>;
pub(super) type Validator<'a> = Box<dyn Fn(&str) -> std::result::Result<(), String> + 'a>;
pub(super) type Completer<'a> = Box<dyn Fn(&str) -> Vec<String> + 'a>;

/// A text question with options: a default, a placeholder, hidden input, and validation that
/// asks again until the answer passes. [`input`] and [`password`] are shortcuts for the
/// common cases.
///
/// The answer can be edited as you'd expect: ←→ move, Home/End (or Ctrl-A/Ctrl-E) jump,
/// Ctrl-U clears, Ctrl-W deletes a word. With [`suggestions`](Input::suggestions),
/// [`complete`](Input::complete) or [`paths`](Input::paths), the best match shows dim
/// after the cursor; Tab takes it, and pressing Tab again moves through the others.
pub struct Input<'a> {
    pub(super) question: String,
    default: Option<String>,
    pub(super) initial: Option<String>,
    placeholder: Option<String>,
    pub(super) secret: bool,
    validate: Option<Validator<'a>>,
    complete: Option<Completer<'a>>,
    pub(super) back: bool,
}

impl std::fmt::Debug for Input<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        // a password's default or starting text is a secret too
        fn hide(s: &Option<String>, secret: bool) -> Option<&str> {
            s.as_deref().map(|s| if secret { "…" } else { s })
        }
        f.debug_struct("Input")
            .field("question", &self.question)
            .field("default", &hide(&self.default, self.secret))
            .field("initial", &hide(&self.initial, self.secret))
            .field("placeholder", &self.placeholder)
            .field("password", &self.secret)
            .field("back", &self.back)
            .finish_non_exhaustive()
    }
}

impl<'a> Input<'a> {
    /// A text question.
    pub fn new(question: impl Into<String>) -> Self {
        Input {
            question: question.into(),
            default: None,
            initial: None,
            placeholder: None,
            secret: false,
            validate: None,
            complete: None,
            back: false,
        }
    }

    /// Offer these answers as you type: those starting with what's typed (ignoring case).
    ///
    /// ```no_run
    /// let region = tinsel::prompt::Input::new("Region")
    ///     .suggestions(["eu-west-1", "eu-west-2", "us-east-1"])
    ///     .ask()?;
    /// # Ok::<(), tinsel::prompt::Error>(())
    /// ```
    #[must_use]
    pub fn suggestions<S: Into<String>>(self, options: impl IntoIterator<Item = S>) -> Self {
        let options: Vec<String> = options.into_iter().map(Into::into).collect();
        self.complete(move |typed| {
            let typed = typed.to_lowercase();
            options
                .iter()
                .filter(|o| o.to_lowercase().starts_with(&typed))
                .cloned()
                .collect()
        })
    }

    /// Offer whatever `complete` returns for the text typed so far, best first. Each
    /// suggestion is a whole answer, not just the rest of it.
    #[must_use]
    pub fn complete(mut self, complete: impl Fn(&str) -> Vec<String> + 'a) -> Self {
        self.complete = Some(Box::new(complete));
        self
    }

    /// Complete file and folder paths, like a shell: Tab fills in names from the folder
    /// typed so far (`~` is your home folder), and folders end in `/`.
    #[must_use]
    pub fn paths(self) -> Self {
        self.complete(complete_path)
    }

    /// The answer if Enter is pressed without typing anything, shown dim until you type.
    #[must_use]
    pub fn default(mut self, value: impl Into<String>) -> Self {
        self.default = Some(value.into());
        self
    }

    /// Text already typed in when the question opens, with the cursor at its end, to edit or
    /// take as it is: the current value, say. Without a terminal, an empty line takes it.
    ///
    /// ```no_run
    /// let name = tinsel::prompt::Input::new("Name").initial("api-gateway").ask()?;
    /// # Ok::<(), tinsel::prompt::Error>(())
    /// ```
    #[must_use]
    pub fn initial(mut self, text: impl Into<String>) -> Self {
        self.initial = Some(text.into());
        self
    }

    /// Dim example text shown until you type. Unlike a default, it's never the answer.
    #[must_use]
    pub fn placeholder(mut self, text: impl Into<String>) -> Self {
        self.placeholder = Some(text.into());
        self
    }

    /// Show what's typed as dots. A password is never read from a pipe: without a terminal,
    /// [`ask`](Input::ask) is an [`Error::NotATerminal`]. A [`default`](Input::default) shows
    /// as dots too, and how long it is stays hidden.
    #[must_use]
    pub fn password(mut self) -> Self {
        self.secret = true;
        self
    }

    /// Check the answer when Enter is pressed. An `Err` is shown under the question, which
    /// is asked again. The default is checked too, if it's used.
    #[must_use]
    pub fn validate(mut self, check: impl Fn(&str) -> std::result::Result<(), String> + 'a) -> Self {
        self.validate = Some(Box::new(check));
        self
    }

    /// Let ← go back to the question before, as [`Error::Back`], when the cursor is at the
    /// start of the line (so ← still moves through what's typed).
    ///
    /// ```no_run
    /// use tinsel::prompt::{Error, Input};
    ///
    /// match Input::new("Project name").back().ask() {
    ///     Ok(name) => println!("{name}"),
    ///     Err(Error::Back) => { /* ask the question before again */ }
    ///     Err(e) => return Err(e),
    /// }
    /// # Ok::<(), Error>(())
    /// ```
    #[must_use]
    pub fn back(mut self) -> Self {
        self.back = true;
        self
    }

    fn check(&self, value: &str) -> std::result::Result<(), String> {
        self.validate.as_ref().map_or(Ok(()), |check| check(value))
    }

    /// What was typed, or the default when nothing was.
    fn resolve(&self, typed: String) -> String {
        if typed.is_empty() {
            self.default.clone().unwrap_or_default()
        } else {
            typed
        }
    }

    /// Ask the question.
    ///
    /// # Errors
    ///
    /// [`Error::Cancelled`] on Ctrl-C or Esc, [`Error::Back`] on ← when asked with `.back()`,
    /// [`Error::NotATerminal`] when there's no terminal to ask on and no line to read,
    /// [`Error::Invalid`] when a line read from a pipe isn't an answer it takes, and [`Error::Io`] when
    /// the terminal can't be used. A [`password`](Input::password) is never read from a pipe, so
    /// without a terminal it's an [`Error::NotATerminal`].
    pub fn ask(self) -> Result<String> {
        if !interactive() {
            return self.unasked();
        }
        self.run(&mut Ui::open()?)
    }

    /// The answer without a terminal: a line read from stdin.
    pub(super) fn unasked(&self) -> Result<String> {
        // a secret piped in could have come from anywhere, and would sit in shell history
        if self.secret {
            return Err(Error::NotATerminal);
        }
        let line = read_piped_line()?;
        let value = match &self.initial {
            Some(initial) if line.is_empty() => initial.clone(),
            _ => self.resolve(line),
        };
        self.check(&value).map_err(Error::Invalid)?;
        Ok(value)
    }

    pub(super) fn run(&self, ui: &mut Ui) -> Result<String> {
        let mut edit = LineEdit::default();
        if let Some(initial) = &self.initial {
            edit.set(initial);
        }
        let mut trouble: Option<String> = None;
        let dots = |n: usize| "•".repeat(n);
        // Tab cycles through what was offered for the text as it was before the first Tab
        let mut cycle: Option<(String, usize)> = None;
        loop {
            let offered: Vec<String> = match (&self.complete, &cycle) {
                (Some(c), Some((base, _))) => c(base),
                (Some(c), None) if edit.at == edit.text.len() && !self.secret => c(&edit.text),
                _ => Vec::new(),
            };
            let pick = cycle.as_ref().map_or(0, |(_, n)| *n);
            // the rest of the best suggestion, shown dim after the cursor
            let ghost = offered
                .get(pick)
                .filter(|_| cycle.is_none() && !edit.is_empty())
                .and_then(|s| {
                    let head: String = s.chars().take(edit.text.chars().count()).collect();
                    head.eq_ignore_ascii_case(&edit.text)
                        .then(|| s.chars().skip(head.chars().count()).collect::<String>())
                })
                .unwrap_or_default();
            let caret = paint(ROSE, "▏");
            let shown = if edit.is_empty() {
                let hint = match (&self.default, &self.placeholder) {
                    // a password's default as dots, as many whatever its length
                    (Some(_), _) if self.secret => dots(6),
                    (Some(d), _) => d.clone(),
                    (None, p) => p.clone().unwrap_or_default(),
                };
                format!("{caret}{}", paint(MUTED, &hint))
            } else {
                let (before, after) = edit.split();
                let color = if self.secret { BLUSH } else { TEXT };
                let show = |s: &str| {
                    if self.secret {
                        dots(s.chars().count())
                    } else {
                        s.to_string()
                    }
                };
                format!(
                    "{}{caret}{}{}",
                    paint(color, &show(before)),
                    paint(color, &show(after)),
                    paint(MUTED, &ghost)
                )
            };
            let mut lines = vec![format!(
                "{}  {shown}{}",
                ask(&self.question),
                back_hint(self.back && edit.at == 0)
            )];
            lines.extend(trouble.as_deref().map(problem));
            if offered.len() > 1 {
                let list: Vec<String> = offered
                    .iter()
                    .enumerate()
                    .take(6)
                    .map(|(i, s)| {
                        let here = cycle.is_some() && i == pick;
                        paint(if here { ROSE } else { MUTED }, s)
                    })
                    .collect();
                let more = if offered.len() > 6 {
                    format!("  +{}", offered.len() - 6)
                } else {
                    String::new()
                };
                lines.push(format!("     {}{}", list.join("  "), paint(MUTED, &more)));
            }
            ui.draw(&lines);
            let key = match ui.key()? {
                // → answers from the end of the line, once there's no suggestion left to take
                Key::Right if edit.at == edit.text.len() && ghost.is_empty() => Key::Enter,
                key => key,
            };
            match key {
                Key::Enter => {
                    let value = self.resolve(edit.text.clone());
                    match self.check(&value) {
                        Ok(()) => {
                            let shown = if self.secret {
                                dots(value.chars().count())
                            } else {
                                value.clone()
                            };
                            ui.draw(&[answered(&self.question, &shown)]);
                            return Ok(value);
                        }
                        Err(why) => trouble = Some(why),
                    }
                }
                Key::Esc | Key::Interrupt => return Err(cancel(&mut ui.frame, &self.question)),
                Key::Left if self.back && edit.at == 0 => return Err(ui.back()),
                Key::Tab if !offered.is_empty() => {
                    let next = match &cycle {
                        Some((base, n)) => (base.clone(), (n + 1) % offered.len()),
                        None => (edit.text.clone(), 0),
                    };
                    edit.set(&offered[next.1]);
                    // with a single match there's nothing to cycle through: take it and carry on
                    cycle = (offered.len() > 1).then_some(next);
                    trouble = None;
                }
                // → at the end of the line takes the dim suggestion, as in a shell
                Key::Right if !ghost.is_empty() => {
                    edit.set(&offered[pick]);
                    trouble = None;
                }
                key => {
                    cycle = None;
                    if edit.key(key) {
                        trouble = None;
                    }
                }
            }
        }
    }
}

/// File and folder names that complete `typed`, as whole paths. Hidden files only show once
/// a `.` is typed.
pub(super) fn complete_path(typed: &str) -> Vec<String> {
    let home = std::env::var("HOME").or(std::env::var("USERPROFILE"));
    complete_path_in(typed, &home.unwrap_or_default())
}

/// [`complete_path`], with `~` standing for `home`.
pub(super) fn complete_path_in(typed: &str, home: &str) -> Vec<String> {
    let separators = ['/', std::path::MAIN_SEPARATOR];
    // only `~` on its own or before a separator is home; `~bob` is someone else's
    let home_rest = typed
        .strip_prefix('~')
        .filter(|rest| !home.is_empty() && (rest.is_empty() || rest.starts_with(separators)));
    if home_rest == Some("") {
        return vec![format!("~{}", std::path::MAIN_SEPARATOR)];
    }
    let expanded = match home_rest {
        Some(rest) => format!("{home}{rest}"),
        None => typed.to_string(),
    };
    // the folder and the name being typed, split in what was typed and in what it stands for
    let split = |s: &str| s.rfind(separators).map_or(0, |i| i + 1);
    let (dir, prefix) = expanded.split_at(split(&expanded));
    let typed_dir = &typed[..split(typed)];
    let Ok(entries) = std::fs::read_dir(if dir.is_empty() { "." } else { dir }) else {
        return Vec::new();
    };
    let mut found: Vec<String> = entries
        .filter_map(|e| {
            let e = e.ok()?;
            let name = e.file_name().into_string().ok()?;
            if !name.starts_with(prefix) || (name.starts_with('.') && !prefix.starts_with('.')) {
                return None;
            }
            let slash = if e.file_type().ok()?.is_dir() { "/" } else { "" };
            Some(format!("{typed_dir}{name}{slash}"))
        })
        .collect();
    found.sort();
    found
}

/// A line being typed, with a cursor that can move through it.
#[derive(Default)]
pub(super) struct LineEdit {
    pub(super) text: String,
    /// The cursor, as a byte index on a char boundary.
    pub(super) at: usize,
}

impl LineEdit {
    fn is_empty(&self) -> bool {
        self.text.is_empty()
    }

    pub(super) fn split(&self) -> (&str, &str) {
        self.text.split_at(self.at)
    }

    /// Replace the text, with the cursor at the end.
    pub(super) fn set(&mut self, text: &str) {
        self.text = text.to_string();
        self.at = self.text.len();
    }

    fn prev(&self) -> usize {
        self.text[..self.at].char_indices().next_back().map_or(0, |(i, _)| i)
    }

    fn next(&self) -> usize {
        self.text[self.at..]
            .chars()
            .next()
            .map_or(self.at, |c| self.at + c.len_utf8())
    }

    /// Apply a key; returns whether the text changed.
    pub(super) fn key(&mut self, key: Key) -> bool {
        match key {
            Key::Char(c) | Key::Pasted(c) => {
                self.text.insert(self.at, c);
                self.at += c.len_utf8();
            }
            Key::Space => {
                self.text.insert(self.at, ' ');
                self.at += 1;
            }
            Key::Backspace if self.at > 0 => {
                let p = self.prev();
                self.text.replace_range(p..self.at, "");
                self.at = p;
            }
            Key::Delete | Key::Ctrl('d') if self.at < self.text.len() => {
                let n = self.next();
                self.text.replace_range(self.at..n, "");
            }
            Key::Ctrl('u') => {
                self.text.replace_range(..self.at, "");
                self.at = 0;
            }
            Key::Ctrl('w') => {
                let before = &self.text[..self.at];
                let start = before.trim_end().rfind(' ').map_or(0, |i| i + 1);
                self.text.replace_range(start..self.at, "");
                self.at = start;
            }
            Key::Left | Key::Ctrl('b') => {
                self.at = self.prev();
                return false;
            }
            Key::Right | Key::Ctrl('f') => {
                self.at = self.next();
                return false;
            }
            Key::Home | Key::Ctrl('a') => {
                self.at = 0;
                return false;
            }
            Key::End | Key::Ctrl('e') => {
                self.at = self.text.len();
                return false;
            }
            Key::WordLeft => {
                let before = self.text[..self.at].trim_end();
                self.at = before.rfind(' ').map_or(0, |i| i + 1);
                return false;
            }
            Key::WordRight => {
                let after = &self.text[self.at..];
                let skipped = after.len() - after.trim_start().len();
                let word = after[skipped..].find(' ').unwrap_or(after.len() - skipped);
                self.at += skipped + word;
                return false;
            }
            _ => return false,
        }
        true
    }
}

/// A line of text. An empty answer takes the default, shown dim until you type.
///
/// # Errors
///
/// [`Error::Cancelled`] on Ctrl-C or Esc, [`Error::NotATerminal`] when there's no terminal to ask
/// on and no line to read, [`Error::Invalid`] when a line read from a pipe isn't an answer it
/// takes, and [`Error::Io`] when the terminal can't be used.
pub fn input(question: &str, default: Option<&str>) -> Result<String> {
    let mut q = Input::new(question);
    if let Some(d) = default {
        q = q.default(d);
    }
    q.ask()
}

/// A line of text shown as dots.
///
/// # Errors
///
/// [`Error::Cancelled`] on Ctrl-C or Esc, [`Error::NotATerminal`] without a terminal, since a
/// password is never read from a pipe, and [`Error::Io`] when the terminal can't be used.
pub fn password(question: &str) -> Result<String> {
    Input::new(question).password().ask()
}

/// Make someone type `expected` exactly before going on: for things that can't be undone,
/// like deleting an app. Returns once they have; Esc or Ctrl-C is [`Error::Cancelled`].
///
/// ```no_run
/// tinsel::prompt::confirm_typing("Delete my-app and all its data?", "my-app")?;
/// # Ok::<(), tinsel::prompt::Error>(())
/// ```
///
/// # Errors
///
/// [`Error::Cancelled`] on Ctrl-C or Esc, [`Error::NotATerminal`] when there's no terminal to ask
/// on and no line to read, [`Error::Invalid`] when a line read from a pipe isn't an answer it
/// takes, and [`Error::Io`] when the terminal can't be used.
pub fn confirm_typing(question: &str, expected: &str) -> Result<()> {
    Input::new(question)
        .placeholder(format!("type {expected} to confirm"))
        .validate(|s| {
            if s == expected {
                Ok(())
            } else {
                Err(format!("type “{expected}” exactly to go ahead"))
            }
        })
        .ask()
        .map(|_| ())
}

/// A number of any type that parses from text (`u16`, `f64`, …). Anything that doesn't parse
/// is refused and asked again.
///
/// ```no_run
/// let replicas: u32 = tinsel::prompt::number("Replicas", Some(3))?;
/// # Ok::<(), tinsel::prompt::Error>(())
/// ```
///
/// # Errors
///
/// [`Error::Cancelled`] on Ctrl-C or Esc, [`Error::NotATerminal`] when there's no terminal to ask
/// on and no line to read, [`Error::Invalid`] when a line read from a pipe isn't an answer it
/// takes, and [`Error::Io`] when the terminal can't be used.
pub fn number<T>(question: &str, default: Option<T>) -> Result<T>
where
    T: FromStr + fmt::Display,
{
    let mut q = Input::new(question).validate(|s| {
        s.trim()
            .parse::<T>()
            .map(|_| ())
            .map_err(|_| format!("“{s}” isn't a {}", kind_of_number(std::any::type_name::<T>())))
    });
    if let Some(d) = default {
        q = q.default(d.to_string());
    }
    let answer = q.ask()?;
    // already checked, so it parses
    let parsed = answer.trim().parse().ok();
    parsed.ok_or(Error::Invalid(answer))
}

/// "whole number" or "number", from the type's name.
pub(crate) fn kind_of_number(name: &str) -> &'static str {
    if name.starts_with('u') {
        "whole number of 0 or more"
    } else if name.starts_with('i') {
        "whole number"
    } else {
        "number"
    }
}