pub struct Select<'a, T> {
pub message: &'a str,
pub options: Vec<T>,
pub help_message: Option<&'a str>,
pub page_size: usize,
pub vim_mode: bool,
pub starting_cursor: usize,
pub filter: Filter<'a, T>,
pub formatter: OptionFormatter<'a, T>,
pub render_config: 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 enter.
This prompt requires a prompt message and a non-empty Vec
of options to be displayed to the user. The options can be of any type as long as they implement the Display
trait. It is required that the Vec
is moved to the prompt, as the prompt will return the selected option (Vec
element) after the user submits.
- 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.
- Display option indexes: On long lists, it might be helpful to display the indexes of the options to the user. Via the
RenderConfig
, you can set the display mode of the indexes as a prefix of an option. The default configuration isNone
, to not render any index when displaying the options. - Filter function: Function that defines if an option is displayed or not based on the current filter input.
Example
use inquire::{error::InquireError, Select};
let options: Vec<&str> = vec!["Banana", "Apple", "Strawberry", "Grapes",
"Lemon", "Tangerine", "Watermelon", "Orange", "Pear", "Avocado", "Pineapple",
];
let ans: Result<&str, InquireError> = Select::new("What's your favorite fruit?", options).prompt();
match ans {
Ok(choice) => println!("{}! That's mine too!", choice),
Err(_) => println!("There was an error, please try again"),
}
Fields
message: &'a str
Message to be presented to the user.
options: Vec<T>
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, T>
Function called with the current user input to filter the provided options.
formatter: OptionFormatter<'a, T>
Function that formats the user input and presents it to the user as the final rendering of the prompt.
render_config: RenderConfig
RenderConfig to apply to the rendered interface.
Note: The default render config considers if the NO_COLOR environment variable is set to decide whether to render the colored config or the empty one.
When overriding the config in a prompt, NO_COLOR is no longer considered and your config is treated as the only source of truth. If you want to customize colors and still suport NO_COLOR, you will have to do this on your end.
Implementations
sourceimpl<'a, T> Select<'a, T> where
T: Display,
impl<'a, T> Select<'a, T> where
T: Display,
sourcepub const DEFAULT_FORMATTER: OptionFormatter<'a, T>
pub const DEFAULT_FORMATTER: OptionFormatter<'a, T>
String formatter used by default in Select prompts. Simply prints the string value contained in the selected option.
Examples
use inquire::list_option::ListOption;
use inquire::Select;
let formatter = Select::<&str>::DEFAULT_FORMATTER;
assert_eq!(String::from("First option"), formatter(ListOption::new(0, &"First option")));
assert_eq!(String::from("First option"), formatter(ListOption::new(11, &"First option")));
sourcepub const DEFAULT_FILTER: Filter<'a, T>
pub const DEFAULT_FILTER: Filter<'a, T>
Default filter function, which checks if the current filter value is a substring of the option value. If it is, the option is displayed.
Examples
use inquire::Select;
let filter = Select::<&str>::DEFAULT_FILTER;
assert_eq!(false, filter("sa", &"New York", "New York", 0));
assert_eq!(true, filter("sa", &"Sacramento", "Sacramento", 1));
assert_eq!(true, filter("sa", &"Kansas", "Kansas", 2));
assert_eq!(true, filter("sa", &"Mesa", "Mesa", 3));
assert_eq!(false, filter("sa", &"Phoenix", "Phoenix", 4));
assert_eq!(false, filter("sa", &"Philadelphia", "Philadelphia", 5));
assert_eq!(true, filter("sa", &"San Antonio", "San Antonio", 6));
assert_eq!(true, filter("sa", &"San Diego", "San Diego", 7));
assert_eq!(false, filter("sa", &"Dallas", "Dallas", 8));
assert_eq!(true, filter("sa", &"San Francisco", "San Francisco", 9));
assert_eq!(false, filter("sa", &"Austin", "Austin", 10));
assert_eq!(false, filter("sa", &"Jacksonville", "Jacksonville", 11));
assert_eq!(true, filter("sa", &"San Jose", "San Jose", 12));
sourcepub const DEFAULT_PAGE_SIZE: usize
pub const DEFAULT_PAGE_SIZE: usize
Default page size.
sourcepub const DEFAULT_VIM_MODE: bool
pub const DEFAULT_VIM_MODE: bool
Default value of vim mode.
sourcepub const DEFAULT_STARTING_CURSOR: usize
pub const DEFAULT_STARTING_CURSOR: usize
Default starting cursor index.
sourcepub const DEFAULT_HELP_MESSAGE: Option<&'a str>
pub const DEFAULT_HELP_MESSAGE: Option<&'a str>
Default help message.
sourcepub fn new(message: &'a str, options: Vec<T>) -> Self
pub fn new(message: &'a str, options: Vec<T>) -> Self
Creates a Select with the provided message and options, along with default configuration values.
sourcepub fn with_help_message(self, message: &'a str) -> Self
pub fn with_help_message(self, message: &'a str) -> Self
Sets the help message of the prompt.
sourcepub fn without_help_message(self) -> Self
pub fn without_help_message(self) -> Self
Removes the set help message.
sourcepub fn with_page_size(self, page_size: usize) -> Self
pub fn with_page_size(self, page_size: usize) -> Self
Sets the page size.
sourcepub fn with_vim_mode(self, vim_mode: bool) -> Self
pub fn with_vim_mode(self, vim_mode: bool) -> Self
Enables or disabled vim_mode.
sourcepub fn with_filter(self, filter: Filter<'a, T>) -> Self
pub fn with_filter(self, filter: Filter<'a, T>) -> Self
Sets the filter function.
sourcepub fn with_formatter(self, formatter: OptionFormatter<'a, T>) -> Self
pub fn with_formatter(self, formatter: OptionFormatter<'a, T>) -> Self
Sets the formatter.
sourcepub fn with_starting_cursor(self, starting_cursor: usize) -> Self
pub fn with_starting_cursor(self, starting_cursor: usize) -> Self
Sets the starting cursor index.
sourcepub fn with_render_config(self, render_config: RenderConfig) -> Self
pub fn with_render_config(self, render_config: RenderConfig) -> Self
Sets the provided color theme to this prompt.
Note: The default render config considers if the NO_COLOR environment variable is set to decide whether to render the colored config or the empty one.
When overriding the config in a prompt, NO_COLOR is no longer considered and your config is treated as the only source of truth. If you want to customize colors and still suport NO_COLOR, you will have to do this on your end.
sourcepub fn prompt(self) -> InquireResult<T>
pub fn prompt(self) -> InquireResult<T>
Parses the provided behavioral and rendering options and prompts the CLI user for input according to the defined rules.
Returns the owned object selected by the user.
sourcepub fn prompt_skippable(self) -> InquireResult<Option<T>>
pub fn prompt_skippable(self) -> InquireResult<Option<T>>
Parses the provided behavioral and rendering options and prompts the CLI user for input according to the defined rules.
This method is intended for flows where the user skipping/cancelling
the prompt - by pressing ESC - is considered normal behavior. In this case,
it does not return Err(InquireError::OperationCanceled)
, but Ok(None)
.
Meanwhile, if the user does submit an answer, the method wraps the return
type with Some
.
sourcepub fn raw_prompt(self) -> InquireResult<ListOption<T>>
pub fn raw_prompt(self) -> InquireResult<ListOption<T>>
Parses the provided behavioral and rendering options and prompts the CLI user for input according to the defined rules.
Returns a ListOption
containing
the index of the selection and the owned object selected by the user.
Trait Implementations
Auto Trait Implementations
impl<'a, T> !RefUnwindSafe for Select<'a, T>
impl<'a, T> !Send for Select<'a, T>
impl<'a, T> !Sync for Select<'a, T>
impl<'a, T> Unpin for Select<'a, T> where
T: Unpin,
impl<'a, T> !UnwindSafe for Select<'a, T>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcepub fn to_owned(&self) -> T
pub fn to_owned(&self) -> T
Creates owned data from borrowed data, usually by cloning. Read more
sourcepub fn clone_into(&self, target: &mut T)
pub fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more