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.
Examples
use dialoguer::{
Select,
theme::ColorfulTheme
};
use console::Term;
fn main() -> std::io::Result<()> {
let items = vec!["Item 1", "item 2"];
let selection = Select::with_theme(&ColorfulTheme::default())
.items(&items)
.default(0)
.interact_on_opt(&Term::stderr())?;
match selection {
Some(index) => println!("User selected item : {}", items[index]),
None => println!("User did not select anything")
}
Ok(())
}
Implementations
sourceimpl Select<'_>
impl Select<'_>
sourcepub fn clear(&mut self, val: bool) -> &mut Self
pub fn clear(&mut self, val: bool) -> &mut Self
Indicates whether select menu should be erased from the screen after interaction.
The default is to clear the menu.
sourcepub fn default(&mut self, val: usize) -> &mut Self
pub fn default(&mut self, val: usize) -> &mut Self
Sets initial selected element when select menu is rendered
Element is indicated by the index at which it appears in item
method invocation or items
slice.
sourcepub fn max_length(&mut self, val: usize) -> &mut Self
pub fn max_length(&mut self, val: usize) -> &mut Self
Sets an optional max length for a page.
Max length is disabled by None
sourcepub fn item<T: ToString>(&mut self, item: T) -> &mut Self
pub fn item<T: ToString>(&mut self, item: T) -> &mut Self
Add a single item to the selector.
Examples
use dialoguer::Select;
fn main() -> std::io::Result<()> {
let selection: usize = Select::new()
.item("Item 1")
.item("Item 2")
.interact()?;
Ok(())
}
sourcepub fn items<T: ToString>(&mut self, items: &[T]) -> &mut Self
pub fn items<T: ToString>(&mut self, items: &[T]) -> &mut Self
Adds multiple items to the selector.
Examples
use dialoguer::Select;
fn main() -> std::io::Result<()> {
let items = vec!["Item 1", "Item 2"];
let selection: usize = Select::new()
.items(&items)
.interact()?;
println!("{}", items[selection]);
Ok(())
}
sourcepub fn with_prompt<S: Into<String>>(&mut self, prompt: S) -> &mut Self
pub fn with_prompt<S: Into<String>>(&mut self, prompt: S) -> &mut 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
.
Examples
use dialoguer::Select;
fn main() -> std::io::Result<()> {
let selection = Select::new()
.with_prompt("Which option do you prefer?")
.item("Option A")
.item("Option B")
.interact()?;
Ok(())
}
sourcepub fn report(&mut self, val: bool) -> &mut Self
pub fn report(&mut self, val: bool) -> &mut 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’.
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.
Examples
use dialoguer::Select;
use console::Term;
fn main() -> std::io::Result<()> {
let selection = Select::new()
.item("Option A")
.item("Option B")
.interact_on(&Term::stderr())?;
println!("User selected option at index {}", selection);
Ok(())
}
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.
Examples
use dialoguer::Select;
use console::Term;
fn main() -> std::io::Result<()> {
let selection = Select::new()
.item("Option A")
.item("Option B")
.interact_on_opt(&Term::stdout())?;
match selection {
Some(position) => println!("User selected option at index {}", position),
None => println!("User did not select anything or exited using Esc or q")
}
Ok(())
}
sourceimpl<'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 builder with a specific theme.
Examples
use dialoguer::{
Select,
theme::ColorfulTheme
};
fn main() -> std::io::Result<()> {
let selection = Select::with_theme(&ColorfulTheme::default())
.item("Option A")
.item("Option B")
.interact()?;
Ok(())
}
Trait Implementations
Auto Trait Implementations
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
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more