Skip to content

Commit ef0cf6d

Browse files
authored
Merge pull request console-rs#60 from SzaryKot/documentation
2 parents a0c6c1e + e8b97bc commit ef0cf6d

File tree

1 file changed

+141
-9
lines changed

1 file changed

+141
-9
lines changed

src/prompts/select.rs

Lines changed: 141 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,34 @@ use crate::theme::{SimpleTheme, TermThemeRenderer, Theme};
55
use console::{Key, Term};
66

77
/// 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+
/// ```
836
pub struct Select<'a> {
937
default: usize,
1038
items: Vec<String>,
@@ -21,12 +49,29 @@ impl<'a> Default for Select<'a> {
2149
}
2250

2351
impl<'a> Select<'a> {
24-
/// Creates a select prompt.
52+
/// Creates a select prompt builder with default theme.
2553
pub fn new() -> Select<'static> {
2654
Select::with_theme(&SimpleTheme)
2755
}
2856

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+
/// ```
3075
pub fn with_theme(theme: &'a dyn Theme) -> Select<'a> {
3176
Select {
3277
default: !0,
@@ -39,32 +84,66 @@ impl<'a> Select<'a> {
3984
}
4085

4186
/// Enables or disables paging
87+
///
88+
/// Paging is disabled by default
4289
pub fn paged(&mut self, val: bool) -> &mut Select<'a> {
4390
self.paged = val;
4491
self
4592
}
4693

47-
/// Sets the clear behavior of the menu.
94+
/// Indicates whether select menu should be ereased from the screen after interaction.
4895
///
4996
/// The default is to clear the menu.
5097
pub fn clear(&mut self, val: bool) -> &mut Select<'a> {
5198
self.clear = val;
5299
self
53100
}
54101

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.
56105
pub fn default(&mut self, val: usize) -> &mut Select<'a> {
57106
self.default = val;
58107
self
59108
}
60109

61110
/// 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+
/// ```
62125
pub fn item<T: ToString>(&mut self, item: T) -> &mut Select<'a> {
63126
self.items.push(item.to_string());
64127
self
65128
}
66129

67130
/// 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+
/// ```
68147
pub fn items<T: ToString>(&mut self, items: &[T]) -> &mut Select<'a> {
69148
for item in items {
70149
self.items.push(item.to_string());
@@ -76,35 +155,88 @@ impl<'a> Select<'a> {
76155
///
77156
/// When a prompt is set the system also prints out a confirmation after
78157
/// 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+
/// ```
79173
pub fn with_prompt<S: Into<String>>(&mut self, prompt: S) -> &mut Select<'a> {
80174
self.prompt = Some(prompt.into());
81175
self
82176
}
83177

84178
/// Enables user interaction and returns the result.
85179
///
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.
87181
/// The dialog is rendered on stderr.
182+
/// Result contains index of a selected item.
88183
pub fn interact(&self) -> io::Result<usize> {
89184
self.interact_on(&Term::stderr())
90185
}
91186

92187
/// Enables user interaction and returns the result.
93188
///
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.
96190
/// 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'.
97192
pub fn interact_opt(&self) -> io::Result<Option<usize>> {
98193
self.interact_on_opt(&Term::stderr())
99194
}
100195

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+
///```
102214
pub fn interact_on(&self, term: &Term) -> io::Result<usize> {
103215
self._interact_on(term, false)?
104216
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, "Quit not allowed in this case"))
105217
}
106218

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+
/// ```
108240
#[inline]
109241
pub fn interact_on_opt(&self, term: &Term) -> io::Result<Option<usize>> {
110242
self._interact_on(term, true)

0 commit comments

Comments
 (0)