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
//! `requestty` (request-tty) is an easy-to-use collection of interactive cli prompts inspired by
//! [Inquirer.js].
//!
//! [Inquirer.js]: https://github.com/SBoudrias/Inquirer.js/
//!
//! # Questions
//!
//! This crate is based on creating [`Question`]s, and then prompting them to the user. There are 11
//! in-built [`Question`]s, but if none of them fit your need, you can [create your own!](#custom-prompts)
//!
//! There are 2 ways of creating [`Question`]s.
//!
//! ### Using builders
//!
//! ```
//! use requestty::{Question, Answers};
//!
//! let question = Question::expand("toppings")
//! .message("What toppings do you want?")
//! .when(|answers: &Answers| !answers["custom_toppings"].as_bool().unwrap())
//! .choice('p', "Pepperoni and cheese")
//! .choice('a', "All dressed")
//! .choice('w', "Hawaiian")
//! .build();
//! ```
//!
//! See [`Question`] for more information on the builders.
//!
//! ### Using macros (only with `macros` feature)
//!
//! Unlike the builder api, the macros can only be used to create a list of questions.
//!
//! use requestty::{questions, Answers};
//!
//! let questions = questions! [
//! Expand {
//! name: "toppings",
//! message: "What toppings do you want?",
//! when: |answers: &Answers| !answers["custom_toppings"].as_bool().unwrap(),
//! choices: [
//! ('p', "Pepperoni and cheese"),
//! ('a', "All dressed"),
//! ('w', "Hawaiian"),
//! ]
//! }
//! ];
//! ```
//!
//! See [`questions`] and [`prompt_module`](prompt_module!) for more information on the macros.
//!
//! ### Prompting
//!
//! [`Question`]s can be asked in 2 main ways.
//!
//! - Using direct [functions](#functions) provided by the crate.
//! ```no_run
//! let questions = vec![
//! // Declare the questions you want to ask
//! ];
//!
//! let answers = requestty::prompt(questions)?;
//! # Result::<_, requestty::ErrorKind>::Ok(())
//! ```
//!
//! - Using [`PromptModule`]
//! ```no_run
//! use requestty::PromptModule;
//!
//! let questions = PromptModule::new(vec![
//! // Declare the questions you want to ask
//! ]);
//!
//! let answers = questions.prompt_all()?;
//! # Result::<_, requestty::ErrorKind>::Ok(())
//! ```
//! This is mainly useful if you need more control over prompting the questions, and using
//! previous [`Answers`].
//!
//! See the documentation of [`Question`] for more information on the different in-built questions.
//!
//! # Terminal Interaction
//!
//! Terminal interaction is handled by 2 traits: [`Backend`] and [`EventIterator`].
//!
//! The traits are already implemented and can be enabled with features for the following terminal
//! libraries:
//! - [`crossterm`](https://crates.io/crates/crossterm) (default)
//! - [`termion`](https://crates.io/crates/termion)
//!
//! The default is `crossterm` for the following reasons:
//! - Wider terminal support
//! - Better event processing (in my experience)
//!
//! [`Backend`]: prompt::Backend
//! [`EventIterator`]: prompt::EventIterator
//!
//! # Custom Prompts
//!
//! If the crate's in-built prompts does not satisfy your needs, you can build your own custom
//! prompts using the [`Prompt`](question::Prompt) trait.
//!
//! # Optional features
//!
//! - `macros`: Enabling this feature will allow you to use the [`questions`] and
//! [`prompt_module`](prompt_module!) macros.
//!
//! - `smallvec` (default): Enabling this feature will use [`SmallVec`] instead of [`Vec`] for [auto
//! completions]. This allows inlining single completions.
//!
//! - `crossterm` (default): Enabling this feature will use the [`crossterm`](https://crates.io/crates/crossterm)
//! library for terminal interactions such as drawing and receiving events.
//!
//! - `termion`: Enabling this feature will use the [`termion`](https://crates.io/crates/termion)
//! library for terminal interactions such as drawing and receiving events.
//!
//! [`SmallVec`]: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html
//! [auto completions]: crate::question::InputBuilder::auto_complete
//!
//! # Examples
//!
//! ```no_run
//! use requestty::Question;
//!
//! let password = Question::password("password")
//! .message("What is your password?")
//! .mask('*')
//! .build();
//!
//! let answer = requestty::prompt_one(password)?;
//!
//! println!("Your password was: {}", answer.as_string().expect("password returns a string"));
//! # Result::<_, requestty::ErrorKind>::Ok(())
//! ```
//!
//! For more examples, see the documentation for the various in-built questions, and the
//! [`examples`] directory.
//!
//! [`examples`]: https://github.com/lutetium-vanadium/requestty/tree/master/examples
use ;
/// A macro to easily write an iterator of [`Question`]s.
///
/// [`Question`]: crate::Question
///
/// # Usage
///
/// You can specify questions similar to a `vec![]` of struct instantiations. Each field corresponds
/// to calling the builder method of the same name for the respective question kind.
/// ```
/// # let some_variable = "message";
/// # let when = true;
/// # fn get_default() -> bool { true }
/// use requestty::questions;
///
/// let questions = questions![
/// MultiSelect {
/// // Each field takes a value, which can result in anything that implements `Into`
/// // for the required type
/// name: "name",
/// // The value can be any expression, for example a local variable.
/// message: some_variable,
/// // If the field name and the variable name are the same, this shorthand can be
/// // used.
/// when,
/// // While most values are generic expressions, if a array literal is passed to
/// // choices, some special syntax applies.
/// // - For `MultiSelect`, default can be specified
/// // - For `OrderSelect`, separators cannot be specified
/// choices: [
/// // By default array entries are taken as `Choice(_)`s.
/// "Choice 1",
/// // For `MultiSelect` if the word 'default' follows the initial expression, a
/// // default can be set for that choice.
/// "Choice 2" default get_default(),
/// // The word 'separator' or 'sep' can be used to create separators. If no
/// // expression is given along with 'separator', it is taken as a `DefaultSeparator`.
/// separator,
/// // Otherwise if there is an expression, it is taken as a `Separator(_)`,
/// sep "Separator text!",
/// ],
/// },
/// ];
/// ```
///
/// # Inline
///
/// By default, the questions are stored in a [`Vec`]. However, if you wish to store the questions
/// on the stack, prefix the questions with `inline`:
/// ```
/// use requestty::questions;
///
/// let questions = questions! [ inline
/// Input {
/// name: "input"
/// },
/// ];
/// ```
///
/// Note that inlining only works for rust version 1.51 onwards. Pre 1.51, a [`Vec`] is still used.
///
/// See also [`prompt_module`](prompt_module!).
pub use r#macroquestions;
pub use ;
pub use PromptModule;
pub use ;
pub use ;
/// A module that re-exports all the things required for writing custom [`Prompt`]s.
///
/// [`Prompt`]: prompt::Prompt
/// Prompt all the questions in the given iterator, with the default [`Backend`] and [`EventIterator`].
/// Prompt the given question, with the default [`Backend`] and [`EventIterator`].
///
/// # Panics
///
/// This will panic if `when` on the [`Question`] prevents the question from being asked.
/// Prompt all the questions in the given iterator, with the given [`Backend`] and [`EventIterator`].
/// Prompt the given question, with the given [`Backend`] and [`EventIterator`].
///
/// # Panics
///
/// This will panic if `when` on the [`Question`] prevents the question from being asked.