pub struct Select<'a> { /* private fields */ }
Expand description
Renders a select prompt.
User can select from one or more options.
Interaction returns index of an item selected in the order they appear in item
invocation or items
slice.
§Example
use dialoguer::Select;
fn main() {
let items = vec!["foo", "bar", "baz"];
let selection = Select::new()
.with_prompt("What do you choose?")
.items(&items)
.interact()
.unwrap();
println!("You chose: {}", items[selection]);
}
Implementations§
source§impl Select<'_>
impl Select<'_>
sourcepub fn clear(self, val: bool) -> Self
pub fn clear(self, val: bool) -> Self
Indicates whether select menu should be erased from the screen after interaction.
The default is to clear the menu.
sourcepub fn max_length(self, val: usize) -> Self
pub fn max_length(self, val: usize) -> Self
Sets an optional max length for a page.
Max length is disabled by None
sourcepub fn item<T: ToString>(self, item: T) -> Self
pub fn item<T: ToString>(self, item: T) -> Self
Add a single item to the selector.
§Example
use dialoguer::Select;
fn main() {
let selection = Select::new()
.item("Item 1")
.item("Item 2")
.interact()
.unwrap();
}
sourcepub fn with_prompt<S: Into<String>>(self, prompt: S) -> Self
pub fn with_prompt<S: Into<String>>(self, prompt: S) -> Self
Sets the select prompt.
By default, when a prompt is set the system also prints out a confirmation after
the selection. You can opt-out of this with report
.
sourcepub fn report(self, val: bool) -> Self
pub fn report(self, val: bool) -> Self
Indicates whether to report the selected value after interaction.
The default is to report the selection.
sourcepub fn interact(self) -> Result<usize>
pub fn interact(self) -> Result<usize>
Enables user interaction and returns the result.
The user can select the items with the ‘Space’ bar or ‘Enter’ and the index of selected item will be returned.
The dialog is rendered on stderr.
Result contains index
if user selected one of items using ‘Enter’.
This unlike interact_opt
does not allow to quit with ‘Esc’ or ‘q’.
sourcepub fn interact_opt(self) -> Result<Option<usize>>
pub fn interact_opt(self) -> Result<Option<usize>>
Enables user interaction and returns the result.
The user can select the items with the ‘Space’ bar or ‘Enter’ and the index of selected item will be returned.
The dialog is rendered on stderr.
Result contains Some(index)
if user selected one of items using ‘Enter’ or None
if user cancelled with ‘Esc’ or ‘q’.
§Example
use dialoguer::Select;
fn main() {
let items = vec!["foo", "bar", "baz"];
let selection = Select::new()
.with_prompt("What do you choose?")
.items(&items)
.interact_opt()
.unwrap();
match selection {
Some(index) => println!("You chose: {}", items[index]),
None => println!("You did not choose anything.")
}
}
sourcepub fn interact_on(self, term: &Term) -> Result<usize>
pub fn interact_on(self, term: &Term) -> Result<usize>
Like interact
but allows a specific terminal to be set.
sourcepub fn interact_on_opt(self, term: &Term) -> Result<Option<usize>>
pub fn interact_on_opt(self, term: &Term) -> Result<Option<usize>>
Like interact_opt
but allows a specific terminal to be set.
source§impl<'a> Select<'a>
impl<'a> Select<'a>
sourcepub fn with_theme(theme: &'a dyn Theme) -> Self
pub fn with_theme(theme: &'a dyn Theme) -> Self
Creates a select prompt with a specific theme.
§Example
use dialoguer::{theme::ColorfulTheme, Select};
fn main() {
let selection = Select::with_theme(&ColorfulTheme::default())
.items(&["foo", "bar", "baz"])
.interact()
.unwrap();
}
Trait Implementations§
Auto Trait Implementations§
impl<'a> Freeze for Select<'a>
impl<'a> !RefUnwindSafe for Select<'a>
impl<'a> !Send for Select<'a>
impl<'a> !Sync for Select<'a>
impl<'a> Unpin for Select<'a>
impl<'a> !UnwindSafe for Select<'a>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)