@@ -5,6 +5,34 @@ use crate::theme::{SimpleTheme, TermThemeRenderer, Theme};
5
5
use console:: { Key , Term } ;
6
6
7
7
/// Renders a select prompt.
8
+ ///
9
+ /// User can select from one or more options.
10
+ /// Interaction returns index of an item selected in the order they appear in `item` invocation or `items` slice.
11
+ ///
12
+ /// ## Examples
13
+ ///
14
+ /// ```rust,no_run
15
+ /// use dialoguer::{
16
+ /// Select,
17
+ /// theme::ColorfulTheme
18
+ /// };
19
+ /// use console::Term;
20
+ ///
21
+ /// fn main() -> std::io::Result<()> {
22
+ /// let items = vec!["Item 1", "item 2"];
23
+ /// let selection = Select::with_theme(&ColorfulTheme::default())
24
+ /// .items(&items)
25
+ /// .default(0)
26
+ /// .interact_on_opt(&Term::stderr())?;
27
+ ///
28
+ /// match selection {
29
+ /// Some(index) => println!("User selected item : {}", items[index]),
30
+ /// None => println!("User did not select anything")
31
+ /// }
32
+ ///
33
+ /// Ok(())
34
+ /// }
35
+ /// ```
8
36
pub struct Select < ' a > {
9
37
default : usize ,
10
38
items : Vec < String > ,
@@ -21,12 +49,29 @@ impl<'a> Default for Select<'a> {
21
49
}
22
50
23
51
impl < ' a > Select < ' a > {
24
- /// Creates a select prompt.
52
+ /// Creates a select prompt builder with default theme .
25
53
pub fn new ( ) -> Select < ' static > {
26
54
Select :: with_theme ( & SimpleTheme )
27
55
}
28
56
29
- /// Creates a select prompt with a specific theme.
57
+ /// Creates a select prompt builder with a specific theme.
58
+ ///
59
+ /// ## Examples
60
+ /// ```rust,no_run
61
+ /// use dialoguer::{
62
+ /// Select,
63
+ /// theme::ColorfulTheme
64
+ /// };
65
+ ///
66
+ /// fn main() -> std::io::Result<()> {
67
+ /// let selection = Select::with_theme(&ColorfulTheme::default())
68
+ /// .item("Option A")
69
+ /// .item("Option B")
70
+ /// .interact()?;
71
+ ///
72
+ /// Ok(())
73
+ /// }
74
+ /// ```
30
75
pub fn with_theme ( theme : & ' a dyn Theme ) -> Select < ' a > {
31
76
Select {
32
77
default : !0 ,
@@ -39,32 +84,66 @@ impl<'a> Select<'a> {
39
84
}
40
85
41
86
/// Enables or disables paging
87
+ ///
88
+ /// Paging is disabled by default
42
89
pub fn paged ( & mut self , val : bool ) -> & mut Select < ' a > {
43
90
self . paged = val;
44
91
self
45
92
}
46
93
47
- /// Sets the clear behavior of the menu .
94
+ /// Indicates whether select menu should be ereased from the screen after interaction .
48
95
///
49
96
/// The default is to clear the menu.
50
97
pub fn clear ( & mut self , val : bool ) -> & mut Select < ' a > {
51
98
self . clear = val;
52
99
self
53
100
}
54
101
55
- /// Sets a default for the menu
102
+ /// Sets initial selected element when select menu is rendered
103
+ ///
104
+ /// Element is indicated by the index at which it appears in `item` method invocation or `items` slice.
56
105
pub fn default ( & mut self , val : usize ) -> & mut Select < ' a > {
57
106
self . default = val;
58
107
self
59
108
}
60
109
61
110
/// Add a single item to the selector.
111
+ ///
112
+ /// ## Examples
113
+ /// ```rust,no_run
114
+ /// use dialoguer::Select;
115
+ ///
116
+ /// fn main() -> std::io::Result<()> {
117
+ /// let selection: usize = Select::new()
118
+ /// .item("Item 1")
119
+ /// .item("Item 2")
120
+ /// .interact()?;
121
+ ///
122
+ /// Ok(())
123
+ /// }
124
+ /// ```
62
125
pub fn item < T : ToString > ( & mut self , item : T ) -> & mut Select < ' a > {
63
126
self . items . push ( item. to_string ( ) ) ;
64
127
self
65
128
}
66
129
67
130
/// Adds multiple items to the selector.
131
+ ///
132
+ /// ## Examples
133
+ /// ```rust,no_run
134
+ /// use dialoguer::Select;
135
+ ///
136
+ /// fn main() -> std::io::Result<()> {
137
+ /// let items = vec!["Item 1", "Item 2"];
138
+ /// let selection: usize = Select::new()
139
+ /// .items(&items)
140
+ /// .interact()?;
141
+ ///
142
+ /// println!("{}", items[selection]);
143
+ ///
144
+ /// Ok(())
145
+ /// }
146
+ /// ```
68
147
pub fn items < T : ToString > ( & mut self , items : & [ T ] ) -> & mut Select < ' a > {
69
148
for item in items {
70
149
self . items . push ( item. to_string ( ) ) ;
@@ -76,35 +155,88 @@ impl<'a> Select<'a> {
76
155
///
77
156
/// When a prompt is set the system also prints out a confirmation after
78
157
/// the selection.
158
+ ///
159
+ /// ## Examples
160
+ /// ```rust,no_run
161
+ /// use dialoguer::Select;
162
+ ///
163
+ /// fn main() -> std::io::Result<()> {
164
+ /// let selection = Select::new()
165
+ /// .with_prompt("Which option do you prefer?")
166
+ /// .item("Option A")
167
+ /// .item("Option B")
168
+ /// .interact()?;
169
+ ///
170
+ /// Ok(())
171
+ /// }
172
+ /// ```
79
173
pub fn with_prompt < S : Into < String > > ( & mut self , prompt : S ) -> & mut Select < ' a > {
80
174
self . prompt = Some ( prompt. into ( ) ) ;
81
175
self
82
176
}
83
177
84
178
/// Enables user interaction and returns the result.
85
179
///
86
- /// The index of the selected item .
180
+ /// Similar to [interact_on](#method.interact_on) except for the fact that it does not allow selection of the terminal .
87
181
/// The dialog is rendered on stderr.
182
+ /// Result contains index of a selected item.
88
183
pub fn interact ( & self ) -> io:: Result < usize > {
89
184
self . interact_on ( & Term :: stderr ( ) )
90
185
}
91
186
92
187
/// Enables user interaction and returns the result.
93
188
///
94
- /// The index of the selected item. None if the user
95
- /// cancelled with Esc or 'q'.
189
+ /// This method is similar to [interact_on_opt](#method.interact_on_opt) except for the fact that it does not allow selection of the terminal.
96
190
/// The dialog is rendered on stderr.
191
+ /// Result contains `Some(index)` if user selected one of items or `None` if user cancelled with 'Esc' or 'q'.
97
192
pub fn interact_opt ( & self ) -> io:: Result < Option < usize > > {
98
193
self . interact_on_opt ( & Term :: stderr ( ) )
99
194
}
100
195
101
- /// Like `interact` but allows a specific terminal to be set.
196
+ /// Like [interact](#method.interact) but allows a specific terminal to be set.
197
+ ///
198
+ /// ## Examples
199
+ ///```rust,no_run
200
+ /// use dialoguer::Select;
201
+ /// use console::Term;
202
+ ///
203
+ /// fn main() -> std::io::Result<()> {
204
+ /// let selection = Select::new()
205
+ /// .item("Option A")
206
+ /// .item("Option B")
207
+ /// .interact_on(&Term::stderr())?;
208
+ ///
209
+ /// println!("User selected option at index {}", selection);
210
+ ///
211
+ /// Ok(())
212
+ /// }
213
+ ///```
102
214
pub fn interact_on ( & self , term : & Term ) -> io:: Result < usize > {
103
215
self . _interact_on ( term, false ) ?
104
216
. ok_or_else ( || io:: Error :: new ( io:: ErrorKind :: Other , "Quit not allowed in this case" ) )
105
217
}
106
218
107
- /// Like `interact_opt` but allows a specific terminal to be set.
219
+ /// Like [interact_opt](#method.interact_opt) but allows a specific terminal to be set.
220
+ ///
221
+ /// ## Examples
222
+ /// ```rust,no_run
223
+ /// use dialoguer::Select;
224
+ /// use console::Term;
225
+ ///
226
+ /// fn main() -> std::io::Result<()> {
227
+ /// let selection = Select::new()
228
+ /// .item("Option A")
229
+ /// .item("Option B")
230
+ /// .interact_on_opt(&Term::stdout())?;
231
+ ///
232
+ /// match selection {
233
+ /// Some(position) => println!("User selected option at index {}", position),
234
+ /// None => println!("User did not select anything")
235
+ /// }
236
+ ///
237
+ /// Ok(())
238
+ /// }
239
+ /// ```
108
240
#[ inline]
109
241
pub fn interact_on_opt ( & self , term : & Term ) -> io:: Result < Option < usize > > {
110
242
self . _interact_on ( term, true )
0 commit comments