pub struct MultiSelect<'a, T> {Show 15 fields
pub message: &'a str,
pub options: Vec<T>,
pub default: Option<Vec<usize>>,
pub help_message: Option<&'a str>,
pub page_size: usize,
pub vim_mode: bool,
pub starting_cursor: usize,
pub starting_filter_input: Option<&'a str>,
pub reset_cursor: bool,
pub filter_input_enabled: bool,
pub scorer: &'a dyn Fn(&str, &T, &str, usize) -> Option<i64>,
pub keep_filter: bool,
pub formatter: &'a dyn Fn(&[ListOption<&T>]) -> String,
pub validator: Option<Box<dyn MultiOptionValidator<T>>>,
pub render_config: RenderConfig<'a>,
}Expand description
Prompt suitable for when you need the user to select many options (including none if applicable) among a list of them.
The user can select (or deselect) the current highlighted option by pressing space, clean all selections by pressing the left arrow and select all options by pressing the right arrow.
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 ownership of the Vec after the user submits, with only the selected options inside it.
- If the list is empty, the prompt operation will fail with an
InquireError::InvalidConfigurationerror.
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).
Customizable options:
- Prompt message: Required when creating the prompt.
- Options list: Options displayed to the user. Must be non-empty.
- Default selections: Options that are selected by default when the prompt is first rendered. The user can unselect them. If any of the indices is out-of-range of the option list, the prompt will fail with an
InquireError::InvalidConfigurationerror. - 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::InvalidConfigurationerror. - Starting filter input: Sets the initial value of the filter section of the prompt.
- 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 options string value, joined using a comma as the separator, by default.
- Validator: Custom validator to make sure a given submitted input pass the specified requirements, e.g. not allowing 0 selected options or limiting the number of options that the user is allowed to select.
- No validators are on 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. - Scorer function: Function that defines the order of options and if displayed as all.
- Keep filter flag: Whether the current filter input should be cleared or not after a selection is made. Defaults to true.
§Example
For a full-featured example, check the GitHub repository.
Fields§
§message: &'a strMessage to be presented to the user.
options: Vec<T>Options displayed to the user.
default: Option<Vec<usize>>Default indexes of options to be selected from the start.
help_message: Option<&'a str>Help message to be presented to the user.
page_size: usizePage size of the options displayed to the user.
vim_mode: boolWhether vim mode is enabled. When enabled, the user can navigate through the options using hjkl.
starting_cursor: usizeStarting cursor index of the selection.
starting_filter_input: Option<&'a str>Starting filter input
reset_cursor: boolReset cursor position to first option on filter input change. Defaults to true.
filter_input_enabled: boolWhether to allow the option list to be filtered by user input or not.
Defaults to true.
scorer: &'a dyn Fn(&str, &T, &str, usize) -> Option<i64>Function called with the current user input to score the provided options. The list of options is sorted in descending order (highest score first)
keep_filter: boolWhether the current filter typed by the user is kept or cleaned after a selection is made.
formatter: &'a dyn Fn(&[ListOption<&T>]) -> StringFunction that formats the user input and presents it to the user as the final rendering of the prompt.
validator: Option<Box<dyn MultiOptionValidator<T>>>Validator to apply to the user input.
In case of error, the message is displayed one line above the prompt.
render_config: RenderConfig<'a>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 support NO_COLOR, you will have to do this on your end.
Implementations§
Source§impl<'a, T> MultiSelect<'a, T>where
T: Display,
impl<'a, T> MultiSelect<'a, T>where
T: Display,
Sourcepub const DEFAULT_FORMATTER: &'a dyn Fn(&[ListOption<&T>]) -> String
pub const DEFAULT_FORMATTER: &'a dyn Fn(&[ListOption<&T>]) -> String
String formatter used by default in MultiSelect prompts. Prints the string value of all selected options, separated by commas.
§Examples
use inquire::list_option::ListOption;
use inquire::MultiSelect;
let formatter = MultiSelect::<&str>::DEFAULT_FORMATTER;
let mut ans = vec![ListOption::new(0, &"New York")];
assert_eq!(String::from("New York"), formatter(&ans));
ans.push(ListOption::new(3, &"Seattle"));
assert_eq!(String::from("New York, Seattle"), formatter(&ans));
ans.push(ListOption::new(7, &"Vancouver"));
assert_eq!(String::from("New York, Seattle, Vancouver"), formatter(&ans));pub const DEFAULT_SCORER: &'a dyn Fn(&str, &T, &str, usize) -> Option<i64>
Sourcepub const DEFAULT_PAGE_SIZE: usize = 7usize
pub const DEFAULT_PAGE_SIZE: usize = 7usize
Default page size, equal to the global default page size [config::DEFAULT_PAGE_SIZE]
Sourcepub const DEFAULT_VIM_MODE: bool = false
pub const DEFAULT_VIM_MODE: bool = false
Default value of vim mode, equal to the global default value [config::DEFAULT_PAGE_SIZE]
Sourcepub const DEFAULT_STARTING_CURSOR: usize = 0usize
pub const DEFAULT_STARTING_CURSOR: usize = 0usize
Default starting cursor index.
Sourcepub const DEFAULT_RESET_CURSOR: bool = true
pub const DEFAULT_RESET_CURSOR: bool = true
Default cursor behaviour on filter input change. Defaults to true.
Sourcepub const DEFAULT_FILTER_INPUT_ENABLED: bool = true
pub const DEFAULT_FILTER_INPUT_ENABLED: bool = true
Default filter input enabled behaviour. Defaults to true.
Sourcepub const DEFAULT_KEEP_FILTER: bool = true
pub const DEFAULT_KEEP_FILTER: bool = true
Default behavior of keeping or cleaning the current filter value.
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>) -> MultiSelect<'a, T>
pub fn new(message: &'a str, options: Vec<T>) -> MultiSelect<'a, T>
Creates a MultiSelect with the provided message and options, along with default configuration values.
Sourcepub fn with_help_message(self, message: &'a str) -> MultiSelect<'a, T>
pub fn with_help_message(self, message: &'a str) -> MultiSelect<'a, T>
Sets the help message of the prompt.
Sourcepub fn without_help_message(self) -> MultiSelect<'a, T>
pub fn without_help_message(self) -> MultiSelect<'a, T>
Removes the set help message.
Sourcepub fn with_page_size(self, page_size: usize) -> MultiSelect<'a, T>
pub fn with_page_size(self, page_size: usize) -> MultiSelect<'a, T>
Sets the page size.
Sourcepub fn with_vim_mode(self, vim_mode: bool) -> MultiSelect<'a, T>
pub fn with_vim_mode(self, vim_mode: bool) -> MultiSelect<'a, T>
Enables or disables vim_mode.
Sourcepub fn with_keep_filter(self, keep_filter: bool) -> MultiSelect<'a, T>
pub fn with_keep_filter(self, keep_filter: bool) -> MultiSelect<'a, T>
Sets the keep filter behavior.
Sourcepub fn with_scorer(
self,
scorer: &'a dyn Fn(&str, &T, &str, usize) -> Option<i64>,
) -> MultiSelect<'a, T>
pub fn with_scorer( self, scorer: &'a dyn Fn(&str, &T, &str, usize) -> Option<i64>, ) -> MultiSelect<'a, T>
Sets the scoring function.
Sourcepub fn with_formatter(
self,
formatter: &'a dyn Fn(&[ListOption<&T>]) -> String,
) -> MultiSelect<'a, T>
pub fn with_formatter( self, formatter: &'a dyn Fn(&[ListOption<&T>]) -> String, ) -> MultiSelect<'a, T>
Sets the formatter.
Sourcepub fn with_validator<V>(self, validator: V) -> MultiSelect<'a, T>where
V: MultiOptionValidator<T> + 'static,
pub fn with_validator<V>(self, validator: V) -> MultiSelect<'a, T>where
V: MultiOptionValidator<T> + 'static,
Sets the validator to apply to the user input. You might want to use this feature in case you need to limit the user to specific choices, such as limiting the number of selections.
In case of error, the message is displayed one line above the prompt.
Sourcepub fn with_default(self, default: &'a [usize]) -> MultiSelect<'a, T>
pub fn with_default(self, default: &'a [usize]) -> MultiSelect<'a, T>
Sets the indexes to be selected by default.
The values should be valid indexes for the given option list. Any numbers larger than the option list or duplicates will be ignored.
Sourcepub fn with_all_selected_by_default(self) -> MultiSelect<'a, T>
pub fn with_all_selected_by_default(self) -> MultiSelect<'a, T>
Sets all options to be selected by default.
This overrides any previously set default and is equivalent to calling
with_default with a slice containing all indexes for the given
option list.
Sourcepub fn with_starting_cursor(self, starting_cursor: usize) -> MultiSelect<'a, T>
pub fn with_starting_cursor(self, starting_cursor: usize) -> MultiSelect<'a, T>
Sets the starting cursor index.
This index might be overridden if the reset_cursor option is set to true (default)
and starting_filter_input is set to something other than None.
Sourcepub fn with_starting_filter_input(
self,
starting_filter_input: &'a str,
) -> MultiSelect<'a, T>
pub fn with_starting_filter_input( self, starting_filter_input: &'a str, ) -> MultiSelect<'a, T>
Sets the starting filter input
Sourcepub fn with_reset_cursor(self, reset_cursor: bool) -> MultiSelect<'a, T>
pub fn with_reset_cursor(self, reset_cursor: bool) -> MultiSelect<'a, T>
Sets the reset_cursor behaviour. Defaults to true.
When there’s an input change that results in a different list of options being displayed, whether by filtering or re-ordering, the cursor will be reset to highlight the first option.
Sourcepub fn without_filtering(self) -> MultiSelect<'a, T>
pub fn without_filtering(self) -> MultiSelect<'a, T>
Disables the filter input, which means the user will not be able to filter the options by typing.
This is useful when you want to simplify the UX if the filter does not add any value, such as when the list is already short.
Sourcepub fn with_render_config(
self,
render_config: RenderConfig<'a>,
) -> MultiSelect<'a, T>
pub fn with_render_config( self, render_config: RenderConfig<'a>, ) -> MultiSelect<'a, T>
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 support NO_COLOR, you will have to do this on your end.
Sourcepub fn prompt_skippable(self) -> Result<Option<Vec<T>>, InquireError>
pub fn prompt_skippable(self) -> Result<Option<Vec<T>>, InquireError>
Parses the provided behavioral and rendering options and prompts the CLI user for input according to the defined rules.
Returns the owned objects selected by the user.
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 prompt(self) -> Result<Vec<T>, InquireError>
pub fn prompt(self) -> Result<Vec<T>, InquireError>
Parses the provided behavioral and rendering options and prompts the CLI user for input according to the defined rules.
Returns the owned objects selected by the user.
Sourcepub fn raw_prompt_skippable(
self,
) -> Result<Option<Vec<ListOption<T>>>, InquireError>
pub fn raw_prompt_skippable( self, ) -> Result<Option<Vec<ListOption<T>>>, InquireError>
Parses the provided behavioral and rendering options and prompts the CLI user for input according to the defined rules.
Returns a vector of ListOptions containing
the index of the selections and the owned objects selected by the user.
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) -> Result<Vec<ListOption<T>>, InquireError>
pub fn raw_prompt(self) -> Result<Vec<ListOption<T>>, InquireError>
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§
Source§impl<'a, T> Clone for MultiSelect<'a, T>where
T: Clone,
impl<'a, T> Clone for MultiSelect<'a, T>where
T: Clone,
Source§fn clone(&self) -> MultiSelect<'a, T>
fn clone(&self) -> MultiSelect<'a, T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl<'a, T> Freeze for MultiSelect<'a, T>
impl<'a, T> !RefUnwindSafe for MultiSelect<'a, T>
impl<'a, T> !Send for MultiSelect<'a, T>
impl<'a, T> !Sync for MultiSelect<'a, T>
impl<'a, T> Unpin for MultiSelect<'a, T>where
T: Unpin,
impl<'a, T> !UnwindSafe for MultiSelect<'a, T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more