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
//! Several questions asked as one: back and forth between them, then a look over the answers.

use super::*;

/// One question in a [`Form`]: any of the builders, which turn into one with `into()`.
#[non_exhaustive]
pub enum Question<'a> {
    /// Yes or no.
    Confirm(Confirm),
    /// A line of text.
    Input(Input<'a>),
    /// One of a list.
    Select(Select<'a>),
    /// Several of a list.
    MultiSelect(MultiSelect<'a>),
    /// One of a long list, or an answer typed in.
    Dropdown(Dropdown<'a>),
}

impl std::fmt::Debug for Question<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Question::Confirm(q) => q.fmt(f),
            Question::Input(q) => q.fmt(f),
            Question::Select(q) => q.fmt(f),
            Question::MultiSelect(q) => q.fmt(f),
            Question::Dropdown(q) => q.fmt(f),
        }
    }
}

impl From<Confirm> for Question<'_> {
    fn from(q: Confirm) -> Self {
        Question::Confirm(q)
    }
}

impl<'a> From<Input<'a>> for Question<'a> {
    fn from(q: Input<'a>) -> Self {
        Question::Input(q)
    }
}

impl<'a> From<Select<'a>> for Question<'a> {
    fn from(q: Select<'a>) -> Self {
        Question::Select(q)
    }
}

impl<'a> From<MultiSelect<'a>> for Question<'a> {
    fn from(q: MultiSelect<'a>) -> Self {
        Question::MultiSelect(q)
    }
}

impl<'a> From<Dropdown<'a>> for Question<'a> {
    fn from(q: Dropdown<'a>) -> Self {
        Question::Dropdown(q)
    }
}

/// What a question was answered with.
#[derive(Clone, Debug, PartialEq, Eq)]
enum Answer {
    Yes(bool),
    Text(String),
    Choice(usize),
    Choices(Vec<usize>),
}

impl Question<'_> {
    fn question(&self) -> &str {
        match self {
            Question::Confirm(q) => &q.question,
            Question::Input(q) => &q.question,
            Question::Select(q) => &q.choices.question,
            Question::MultiSelect(q) => &q.choices.question,
            Question::Dropdown(q) => &q.choices.question,
        }
    }

    /// Whether there's anything to choose, for lists.
    fn answerable(&self) -> bool {
        match self {
            Question::Select(q) => q.choices.any_enabled(),
            Question::Dropdown(q) => q.custom || q.choices.any_enabled(),
            _ => true,
        }
    }

    /// Ask it on `ui`, starting from the answer it was given last time, if any. Returns the
    /// answer, and how it's shown.
    fn run(&mut self, ui: &mut Ui, back: bool, last: Option<&Answer>) -> Result<(Answer, String)> {
        match self {
            Question::Confirm(q) => {
                q.back = back;
                if let Some(Answer::Yes(yes)) = last {
                    q.default = *yes;
                }
                let yes = q.run(ui)?;
                Ok((Answer::Yes(yes), if yes { "yes" } else { "no" }.into()))
            }
            Question::Input(q) => {
                q.back = back;
                if let Some(Answer::Text(text)) = last {
                    q.initial = Some(text.clone());
                }
                let text = q.run(ui)?;
                let shown = if q.secret {
                    "•".repeat(text.chars().count())
                } else {
                    text.clone()
                };
                Ok((Answer::Text(text), shown))
            }
            Question::Select(q) => {
                q.choices.back = back;
                if let Some(Answer::Choice(i)) = last {
                    q.default = Some(*i);
                }
                let i = q.run(ui)?;
                Ok((Answer::Choice(i), q.choices.list[i].label.clone()))
            }
            Question::MultiSelect(q) => {
                q.choices.back = back;
                if let Some(Answer::Choices(picked)) = last {
                    q.default = Some(picked.clone());
                }
                let picked = q.run(ui)?;
                let names: Vec<&str> = picked.iter().map(|&i| q.choices.list[i].label.as_str()).collect();
                let shown = summary(&names);
                Ok((Answer::Choices(picked), shown))
            }
            Question::Dropdown(q) => {
                q.choices.back = back;
                if let Some(Answer::Text(text)) = last {
                    q.default = q.choices.list.iter().position(|c| &c.label == text);
                }
                let text = q.run(ui)?;
                Ok((Answer::Text(text.clone()), text))
            }
        }
    }

    /// The answer without a terminal, as the question asked on its own would find it.
    fn unasked(&self) -> Result<Answer> {
        match self {
            Question::Confirm(q) => q.unasked().map(Answer::Yes),
            Question::Input(q) => q.unasked().map(Answer::Text),
            Question::Select(q) => q.unasked().map(Answer::Choice),
            Question::MultiSelect(q) => q.unasked().map(Answer::Choices),
            Question::Dropdown(q) => q.unasked().map(Answer::Text),
        }
    }
}

/// Questions asked one after another as a group: ← goes back to the one before (with its
/// answer still there to change), and once they're all answered, a last question shows the
/// answers and offers to change any of them before going on.
///
/// Each question is given a name to find its answer by. Without a terminal the questions are
/// answered as they would be on their own (lines read from stdin, or what's `selected`), and
/// there's nothing to look over.
///
/// ```no_run
/// use tinsel::prompt::{Confirm, Form, Input, MultiSelect, Select};
///
/// let answers = Form::new()
///     .question("name", Input::new("Project name").default("my-app"))
///     .question("target", Select::new("Deploy to").options(["staging", "production"]))
///     .question("services", MultiSelect::new("Services").options(["api", "web", "worker"]))
///     .question("tag", Confirm::new("Tag the release?").default(true))
///     .ask()?;
///
/// let name = answers.text("name");
/// let target = answers.choice("target");
/// let services = answers.choices("services");
/// let tag = answers.yes("tag");
/// # Ok::<(), tinsel::prompt::Error>(())
/// ```
///
/// ```text
///   ●  Project name  my-app
///   ●  Deploy to  production
///   ●  Services  api, worker
///   ●  Tag the release?  yes
///   ?  All good?
///     › yes, carry on
///     change
///       Project name                     my-app
///       Deploy to                    production
///       …
/// ```
#[derive(Debug, Default)]
pub struct Form<'a> {
    questions: Vec<(String, Question<'a>)>,
}

impl<'a> Form<'a> {
    /// A form with no questions yet.
    pub fn new() -> Self {
        Form::default()
    }

    /// Add a question, and the name its answer is found by in the [`Answers`]. Its own
    /// `.back()` needn't be set: every question after the first can go back.
    #[must_use]
    pub fn question(mut self, name: impl Into<String>, question: impl Into<Question<'a>>) -> Self {
        self.questions.push((name.into(), question.into()));
        self
    }

    /// Ask the questions, then offer to change any answer before returning them all.
    ///
    /// # Errors
    ///
    /// [`Error::Cancelled`] on Ctrl-C or Esc at any question, [`Error::NoOptions`] when a list has
    /// nothing to choose, and otherwise whatever a question asked on its own would return
    /// without a terminal ([`Error::NotATerminal`], [`Error::Invalid`]) or when the terminal can't
    /// be used ([`Error::Io`]).
    pub fn ask(mut self) -> Result<Answers> {
        if !interactive() {
            let answers = self.questions.iter().map(|(name, q)| Ok((name.clone(), q.unasked()?)));
            return Ok(Answers {
                list: answers.collect::<Result<_>>()?,
            });
        }
        if self.questions.is_empty() {
            return Ok(Answers { list: Vec::new() });
        }
        if !self.questions.iter().all(|(_, q)| q.answerable()) {
            return Err(Error::NoOptions);
        }
        let mut ui = Ui::open()?;
        let result = self.run(&mut ui);
        // what's left on screen is the answers alone, or the question that was cancelled
        if result.is_ok() {
            ui.draw(&[]);
        }
        result
    }

    fn run(&mut self, ui: &mut Ui) -> Result<Answers> {
        let count = self.questions.len();
        let mut answers: Vec<Option<Answer>> = vec![None; count];
        // the answered lines, and each answer as it's shown, one per question
        let mut lines: Vec<String> = vec![String::new(); count];
        let mut shown: Vec<String> = vec![String::new(); count];
        let mut step = 0;
        // which question is being changed from the look over the answers, if one is
        let mut changing: Option<usize> = None;
        loop {
            let asking = changing.unwrap_or(step);
            if asking < count {
                // everything answered so far stays on screen above, except the one being asked
                ui.frame.above = (0..count)
                    .filter(|&i| i != asking && answers[i].is_some() && (changing.is_some() || i < asking))
                    .map(|i| lines[i].clone())
                    .collect();
                let back = changing.is_none() && asking > 0;
                let (_, question) = &mut self.questions[asking];
                match question.run(ui, back, answers[asking].as_ref()) {
                    Ok((answer, text)) => {
                        lines[asking] = ui.frame.lines.last().cloned().unwrap_or_default();
                        answers[asking] = Some(answer);
                        shown[asking] = text;
                        match changing.take() {
                            Some(_) => step = count,
                            None => step += 1,
                        }
                    }
                    Err(Error::Back) => step -= 1,
                    Err(e) => return Err(e),
                }
                continue;
            }
            // all answered: look them over
            ui.frame.above = lines.clone();
            let mut review = Select::new("All good?").option("yes, carry on").heading("change");
            for ((_, q), answer) in self.questions.iter().zip(&shown) {
                review = review.option(q.question()).note(text::truncate(answer, 30));
            }
            // room for every answer and the heading over them, so none scroll out of sight
            let review = review.rows(count + 2).back();
            match review.run(ui) {
                Ok(0) => break,
                Ok(n) => changing = Some(n - 1),
                Err(Error::Back) => step = count - 1,
                Err(e) => return Err(e),
            }
        }
        ui.frame.above = lines;
        let list = self
            .questions
            .iter()
            .map(|(name, _)| name.clone())
            .zip(answers.into_iter().flatten());
        Ok(Answers { list: list.collect() })
    }
}

/// The answers to a [`Form`], by the name each question was given.
///
/// Asking for a name the form doesn't have, or for the wrong kind of answer (`text` for a
/// yes or no question, say), panics: it's a mistake in the code, not something the person
/// answering could have done.
#[derive(Clone, Debug)]
pub struct Answers {
    list: Vec<(String, Answer)>,
}

impl Answers {
    fn get(&self, name: &str) -> &Answer {
        match self.list.iter().find(|(n, _)| n == name) {
            Some((_, answer)) => answer,
            None => panic!("the form has no question called {name:?}"),
        }
    }

    /// A [`Confirm`]'s answer.
    pub fn yes(&self, name: &str) -> bool {
        match self.get(name) {
            Answer::Yes(yes) => *yes,
            other => panic!("{name:?} wasn't a yes or no question: {other:?}"),
        }
    }

    /// An [`Input`]'s or a [`Dropdown`]'s answer.
    pub fn text(&self, name: &str) -> &str {
        match self.get(name) {
            Answer::Text(text) => text,
            other => panic!("{name:?} wasn't answered with text: {other:?}"),
        }
    }

    /// A [`Select`]'s answer: the number of the option chosen.
    pub fn choice(&self, name: &str) -> usize {
        match self.get(name) {
            Answer::Choice(i) => *i,
            other => panic!("{name:?} wasn't a Select: {other:?}"),
        }
    }

    /// A [`MultiSelect`]'s answer: the numbers of the options ticked, in order.
    pub fn choices(&self, name: &str) -> &[usize] {
        match self.get(name) {
            Answer::Choices(picked) => picked,
            other => panic!("{name:?} wasn't a MultiSelect: {other:?}"),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::app::Key;

    fn form() -> Form<'static> {
        Form::new()
            .question("name", Input::new("Project name").default("my-app"))
            .question("target", Select::new("Deploy to").options(["staging", "production"]))
            .question("tag", Confirm::new("Tag the release?").default(true))
    }

    #[test]
    fn asks_each_then_looks_them_over() {
        let mut keys = typed("web");
        keys.extend([Key::Enter, Key::Down, Key::Enter, Key::Enter, Key::Enter]);
        let script = script(keys);
        let answers = form().ask().unwrap();
        assert_eq!(answers.text("name"), "web");
        assert_eq!(answers.choice("target"), 1);
        assert!(answers.yes("tag"));
        assert_eq!(
            script.answered(),
            [
                "●  Project name  web",
                "●  Deploy to  production",
                "●  Tag the release?  yes"
            ]
        );
        assert_eq!(script.remaining(), 0);
    }

    #[test]
    fn going_back_keeps_the_answer_to_change() {
        // answer the name, go back to it from the list, add to it, then carry on
        let mut keys = typed("web");
        keys.extend([Key::Enter, Key::Left]);
        keys.extend(typed("site"));
        keys.extend([Key::Enter, Key::Enter, Key::Char('n'), Key::Enter]);
        let script = script(keys);
        let answers = form().ask().unwrap();
        assert_eq!(answers.text("name"), "website");
        assert!(!answers.yes("tag"));
        assert_eq!(script.answered()[0], "●  Project name  website");
    }

    #[test]
    fn any_answer_can_be_changed_from_the_look_over() {
        let mut keys = vec![Key::Enter, Key::Enter, Key::Enter];
        // on the look over: down to "Deploy to", change it, then carry on
        keys.extend([Key::Down, Key::Down, Key::Enter, Key::Down, Key::Enter, Key::Enter]);
        let script = script(keys);
        let answers = form().ask().unwrap();
        assert_eq!(answers.choice("target"), 1);
        assert_eq!(answers.text("name"), "my-app");
        // in the order they were asked, whichever was changed last
        assert_eq!(
            script.answered(),
            [
                "●  Project name  my-app",
                "●  Deploy to  production",
                "●  Tag the release?  yes"
            ]
        );
    }

    #[test]
    fn back_from_the_look_over_asks_the_last_again() {
        let keys = [
            Key::Enter,
            Key::Enter,
            Key::Enter,
            Key::Left,
            Key::Char('n'),
            Key::Enter,
        ];
        let _script = script(keys);
        assert!(!form().ask().unwrap().yes("tag"));
    }

    #[test]
    fn cancelling_any_question_cancels_the_form() {
        let script = script([Key::Enter, Key::Esc]);
        assert!(matches!(form().ask(), Err(Error::Cancelled)));
        assert_eq!(
            script.answered(),
            ["●  Project name  my-app", "✕  Deploy to  cancelled"]
        );
    }

    #[test]
    fn without_a_terminal_each_question_answers_as_it_would_alone() {
        let form = Form::new().question("target", Select::new("Deploy to").options(["a", "b"]).selected(1));
        let answers = crate::output::with_terminal(false, || form.ask()).unwrap();
        assert_eq!(answers.choice("target"), 1);
        let form = Form::new().question("target", Select::new("Deploy to").options(["a", "b"]));
        assert!(matches!(
            crate::output::with_terminal(false, || form.ask()),
            Err(Error::NotATerminal)
        ));
    }

    #[test]
    fn a_list_with_nothing_to_choose_is_refused_up_front() {
        let _script = script([]);
        let form = Form::new().question("x", Select::new("Pick"));
        assert!(matches!(form.ask(), Err(Error::NoOptions)));
        assert!(Form::new().ask().is_ok());
    }

    #[test]
    fn lists_of_every_kind_start_from_their_last_answer() {
        let form = || {
            Form::new()
                .question("parts", MultiSelect::new("Parts").options(["a", "b", "c"]))
                .question("tag", Dropdown::new("Tag").options(["v1", "v2"]))
        };
        // tick a and c, go back from the tag, take the ticks as they are, then v2
        let keys = [
            Key::Char(' '),
            Key::Down,
            Key::Down,
            Key::Char(' '),
            Key::Enter,
            Key::Left,
            Key::Enter,
            Key::Down,
            Key::Enter,
            // change the tag from the look over: it starts on v2, so Enter keeps it
            Key::Down,
            Key::Down,
            Key::Enter,
            Key::Enter,
            Key::Enter,
        ];
        let script = script(keys);
        let answers = form().ask().unwrap();
        assert_eq!(answers.choices("parts"), [0, 2]);
        assert_eq!(answers.text("tag"), "v2");
        assert_eq!(script.answered(), ["●  Parts  a, c", "●  Tag  v2"]);
    }

    #[test]
    fn passwords_stay_hidden_in_the_look_over() {
        let mut keys = typed("hunter2");
        keys.extend([Key::Enter, Key::Esc]);
        let script = script(keys);
        let form = Form::new().question("token", Input::new("Token").password());
        // Esc on the look over cancels, leaving the answers and the cancelled look over
        assert!(matches!(form.ask(), Err(Error::Cancelled)));
        assert_eq!(script.answered(), ["●  Token  •••••••", "✕  All good?  cancelled"]);
    }

    #[test]
    fn questions_show_what_they_are() {
        let form = Form::new()
            .question("a", Confirm::new("Go?"))
            .question("b", Input::new("Name"))
            .question("c", Select::new("Pick").option("x"))
            .question("d", MultiSelect::new("Tick").option("x"))
            .question("e", Dropdown::new("Tag").option("x"));
        let shown = format!("{form:?}");
        for q in ["Confirm", "Input", "Select", "MultiSelect", "Dropdown"] {
            assert!(shown.contains(&format!("{q} {{")), "{shown}");
        }
        assert!(
            form.questions
                .iter()
                .map(|(_, q)| q.question())
                .eq(["Go?", "Name", "Pick", "Tick", "Tag"])
        );
        let custom = Form::new().question("t", Dropdown::new("Tag").allow_custom());
        assert!(custom.questions[0].1.answerable());
    }

    fn answered() -> Answers {
        let _script = script([Key::Enter, Key::Enter, Key::Enter, Key::Enter]);
        form().ask().unwrap()
    }

    #[test]
    #[should_panic(expected = "wasn't a yes or no question")]
    fn the_wrong_kind_of_answer_panics() {
        answered().yes("name");
    }

    #[test]
    #[should_panic(expected = "wasn't answered with text")]
    fn text_from_a_list_panics() {
        answered().text("target");
    }

    #[test]
    #[should_panic(expected = "wasn't a Select")]
    fn a_choice_from_text_panics() {
        answered().choice("name");
    }

    #[test]
    #[should_panic(expected = "wasn't a MultiSelect")]
    fn choices_from_one_choice_panics() {
        answered().choices("target");
    }

    #[test]
    #[should_panic(expected = "the form has no question called \"nope\"")]
    fn a_name_the_form_doesnt_have_panics() {
        answered().yes("nope");
    }
}