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§
Source§impl<'a> Select<'a>
impl<'a> Select<'a>
Sourcepub fn with_theme(theme: &'a dyn Theme) -> Select<'a>
pub fn with_theme(theme: &'a dyn Theme) -> Select<'a>
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(())
}
Sourcepub fn paged(&mut self, val: bool) -> &mut Select<'a>
pub fn paged(&mut self, val: bool) -> &mut Select<'a>
Enables or disables paging
Paging is disabled by default
Sourcepub fn clear(&mut self, val: bool) -> &mut Select<'a>
pub fn clear(&mut self, val: bool) -> &mut Select<'a>
Indicates whether select menu should be ereased from the screen after interaction.
The default is to clear the menu.
Sourcepub fn default(&mut self, val: usize) -> &mut Select<'a>
pub fn default(&mut self, val: usize) -> &mut Select<'a>
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 item<T: ToString>(&mut self, item: T) -> &mut Select<'a>
pub fn item<T: ToString>(&mut self, item: T) -> &mut Select<'a>
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 Select<'a>
pub fn items<T: ToString>(&mut self, items: &[T]) -> &mut Select<'a>
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 Select<'a>
pub fn with_prompt<S: Into<String>>(&mut self, prompt: S) -> &mut Select<'a>
Sets the select prompt.
When a prompt is set the system also prints out a confirmation after the selection.
§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 interact(&self) -> Result<usize>
pub fn interact(&self) -> Result<usize>
Enables user interaction and returns the result.
Similar to interact_on except for the fact that it does not allow selection of the terminal. The dialog is rendered on stderr. Result contains index of a selected item.
Sourcepub fn interact_opt(&self) -> Result<Option<usize>>
pub fn interact_opt(&self) -> Result<Option<usize>>
Enables user interaction and returns the result.
This method is similar to interact_on_opt except for the fact that it does not allow selection of the terminal.
The dialog is rendered on stderr.
Result contains Some(index)
if user selected one of items 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")
}
Ok(())
}