Struct inquire::Select[][src]

pub struct Select<'a> {
    pub message: &'a str,
    pub options: &'a [&'a str],
    pub help_message: Option<&'a str>,
    pub page_size: usize,
    pub vim_mode: bool,
    pub starting_cursor: usize,
    pub filter: Filter<'a>,
    pub formatter: OptionFormatter<'a>,
    pub render_config: &'a RenderConfig,
}
Expand description

Prompt suitable for when you need the user to select one option among many.

The user can select and submit the current highlighted option by pressing space or enter.

This prompt requires a prompt message and a non-empty list of options to be displayed to the user. If the list is empty, the prompt operation will fail with an InquireError::InvalidConfiguration error.

This prompt does not support custom validators because of its nature. A submission always selects exactly one of the options. If this option was not supposed to be selected or is invalid in some way, it probably should not be included in the options list.

The options are paginated in order to provide a smooth experience to the user, with the default page size being 7. The user can move from the options and the pages will be updated accordingly, including moving from the last to the first options (or vice-versa).

Like all others, this prompt also allows you to customize several aspects of it:

  • Prompt message: Required when creating the prompt.
  • Options list: Options displayed to the user. Must be non-empty.
  • Starting cursor: Index of the cursor when the prompt is first rendered. Default is 0 (first option). If the index is out-of-range of the option list, the prompt will fail with an InquireError::InvalidConfiguration error.
  • Help message: Message displayed at the line below the prompt.
  • Formatter: Custom formatter in case you need to pre-process the user input before showing it as the final answer.
    • Prints the selected option string value by default.
  • Page size: Number of options displayed at once, 7 by default.
  • Filter function: Function that defines if an option is displayed or not based on the current filter input.

Example

use inquire::Select;

let options = vec!["Banana", "Apple", "Strawberry", "Grapes",
    "Lemon", "Tangerine", "Watermelon", "Orange", "Pear", "Avocado", "Pineapple",
];

let ans = Select::new("What's your favorite fruit?", &options).prompt();

match ans {
    Ok(choice) => println!("{}! That's mine too!", choice.value),
    Err(_) => println!("There was an error, please try again"),
}

Fields

message: &'a str

Message to be presented to the user.

options: &'a [&'a str]

Options displayed to the user.

help_message: Option<&'a str>

Help message to be presented to the user.

page_size: usize

Page size of the options displayed to the user.

vim_mode: bool

Whether vim mode is enabled. When enabled, the user can navigate through the options using hjkl.

starting_cursor: usize

Starting cursor index of the selection.

filter: Filter<'a>

Function called with the current user input to filter the provided options.

formatter: OptionFormatter<'a>

Function that formats the user input and presents it to the user as the final rendering of the prompt.

render_config: &'a RenderConfig

RenderConfig to apply to the rendered interface.

Implementations

Default formatter, set to DEFAULT_OPTION_FORMATTER

Default filter, equal to the global default filter config::DEFAULT_FILTER.

Default page size.

Default value of vim mode.

Default starting cursor index.

Default help message.

Creates a Select with the provided message and options, along with default configuration values.

Sets the help message of the prompt.

Removes the set help message.

Sets the page size.

Enables or disabled vim_mode.

Sets the filter function.

Sets the formatter.

Sets the starting cursor index.

Sets the provided color theme to this prompt.

Parses the provided behavioral and rendering options and prompts the CLI user for input according to the defined rules.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. 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

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

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.