Struct Select

Source
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>

Source

pub fn new() -> Select<'static>

Creates a select prompt builder with default theme.

Source

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

pub fn paged(&mut self, val: bool) -> &mut Select<'a>

Enables or disables paging

Paging is disabled by default

Source

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.

Source

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.

Source

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

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

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

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.

Source

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

Source

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

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

Trait Implementations§

Source§

impl<'a> Default for Select<'a>

Source§

fn default() -> Select<'a>

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

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V