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

Creates a select prompt builder with default theme.

Indicates whether select menu should be erased from the screen after interaction.

The default is to clear the menu.

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.

Sets an optional max length for a page.

Max length is disabled by None

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(())
}

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(())
}

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(())
}

Indicates whether to report the selected value after interaction.

The default is to report the selection.

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’.

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’.

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(())
 }

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(())
}

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

Returns the “default value” for a type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.