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
//! Questions: yes/no, a line of text, a password, a number, a date, one of a list, several
//! of a list, or a longer answer in the user's editor.
//!
//! Drawn on stderr, so stdout stays clean for piping. Each question collapses to a single
//! `● question answer` line once answered.
//!
//! Every question returns a [`Result`], whose error says what happened in a way you can
//! match on: [`Error::Cancelled`] when someone presses Ctrl-C or Esc, [`Error::NotATerminal`]
//! when there's no one to ask (no terminal on stdin, or none on stderr to show the question
//! on). Without a terminal on stdin, `confirm` and the text questions (`input`, `number`,
//! `date`) read a line, so scripts can still drive them (`echo yes | my-cli`); an answer
//! read that way that doesn't make sense or fails validation is an [`Error::Invalid`].
//!
//! ```no_run
//! use tinsel::prompt;
//!
//! fn main() -> Result<(), prompt::Error> {
//! let name = prompt::input("Project name", Some("my-app"))?;
//! let target = prompt::select("Deploy to", &["staging", "production"])?;
//! if prompt::confirm("Go?", true)? {
//! tinsel::success(&format!("deploying {name} to target #{target}"));
//! }
//! Ok(())
//! }
//! ```
//!
//! For more control over a text question (a placeholder, validation), build an [`Input`]:
//!
//! ```no_run
//! use tinsel::prompt::Input;
//!
//! let port = Input::new("Port")
//! .placeholder("8080")
//! .validate(|s| match s.parse::<u16>() {
//! Ok(p) if p >= 1024 => Ok(()),
//! _ => Err("pick a port from 1024 to 65535".into()),
//! })
//! .ask()?;
//! # Ok::<(), tinsel::prompt::Error>(())
//! ```
//!
//! Lists have builders too: [`Select`] and [`MultiSelect`] add groups under headings,
//! options that can't be picked (and why), a hint and validation, and [`Dropdown`] is for
//! long lists like versions, with notes and badges and room to type an answer of your own.
//! [`Select`] and [`MultiSelect`] answer with the numbers of the options chosen, so the same
//! label can appear twice; [`Dropdown`] answers with the text, since an answer typed in
//! has no number. All three start on the options given to `selected`.
//!
//! # Testing a flow of questions
//!
//! [`script`] answers questions from a list of keys instead of the keyboard, on the current
//! thread, for as long as the [`Script`] it returns lives. Questions then draw nothing, and
//! [`Script::answered`] says what each one ended on, so a test can run the real code that
//! asks them.
//!
//! ```
//! use tinsel::app::Key;
//! use tinsel::prompt::{self, Select};
//!
//! let keys = prompt::script([Key::Down, Key::Enter]);
//! let picked = Select::new("Deploy to").options(["staging", "production"]).ask()?;
//! assert_eq!(picked, 1);
//! assert_eq!(keys.answered(), ["● Deploy to production"]);
//! # Ok::<(), prompt::Error>(())
//! ```
//!
//! # Going back
//!
//! A [`Form`] asks several questions as one, going back and forth between them, and shows
//! all the answers at the end to change any of them.
//!
//! For questions asked one after another, `.back()` lets ← go back: the question returns
//! [`Error::Back`] and you ask the one before it again. → always answers, as Enter does (in
//! typed answers, from the end of the line), so a flow can be stepped through with the arrows.
//!
//! ```no_run
//! use tinsel::prompt::{Error, Input, Select};
//!
//! let (mut name, mut target) = (String::new(), 0);
//! let mut step = 0;
//! while step < 2 {
//! let asked = match step {
//! 0 => Input::new("Project name").ask().map(|n| name = n),
//! _ => Select::new("Deploy to").options(["staging", "production"]).back().ask().map(|t| target = t),
//! };
//! match asked {
//! Ok(()) => step += 1,
//! Err(Error::Back) => step -= 1,
//! Err(e) => return Err(e),
//! }
//! }
//! # Ok::<(), Error>(())
//! ```
use fmt;
use ;
use FromStr;
use crate;
use crate;
use crate;
use cratetext;
/// Why a question didn't get an answer.
/// What every question returns.
pub type Result<T> = Result;
use *;
pub use ;