Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

## [Unreleased] <!-- ReleaseDate -->

- No changes since the latest release below.
### Features

- Added `select_on_empty_submit` to MultiSelect to toggle option under cursor when submitting an empty prompt.

## [0.9.3] - 2026-02-06

Expand Down
17 changes: 17 additions & 0 deletions inquire/src/prompts/multiselect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,13 @@ pub struct MultiSelect<'a, T> {
/// 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.
pub render_config: RenderConfig<'a>,

/// If true, will select the option under the cursor if the prompt is submitted without any
/// selections.
///
/// This allows the MultiSelect to operate like a [Select](crate::Select) if the normal toggle
/// selection key has not been pressed.
select_on_empty_submit: Option<bool>,
}

impl<'a, T> MultiSelect<'a, T>
Expand Down Expand Up @@ -236,6 +243,7 @@ where
formatter: Self::DEFAULT_FORMATTER,
validator: None,
render_config: get_configuration(),
select_on_empty_submit: None,
}
}

Expand All @@ -257,6 +265,15 @@ where
self
}

/// Enables selecting the current option when submitting without any selected options.
///
/// This allows the MultiSelect to operate like a [Select](crate::Select) if the normal toggle
/// selection key has not been pressed.
pub fn with_select_on_empty_submit(mut self) -> Self {
self.select_on_empty_submit = Some(true);
self
}

/// Enables or disables vim_mode.
pub fn with_vim_mode(mut self, vim_mode: bool) -> Self {
self.vim_mode = vim_mode;
Expand Down
8 changes: 8 additions & 0 deletions inquire/src/prompts/multiselect/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub struct MultiSelectPrompt<'a, T> {
options: Vec<T>,
string_options: Vec<String>,
help_message: Option<&'a str>,
select_on_empty_submit: Option<bool>,
cursor_index: usize,
checked: BTreeSet<usize>,
input: Option<Input>,
Expand Down Expand Up @@ -80,6 +81,7 @@ where
string_options,
scored_options,
help_message: mso.help_message,
select_on_empty_submit: mso.select_on_empty_submit,
cursor_index: mso.starting_cursor,
input,
scorer: mso.scorer,
Expand Down Expand Up @@ -267,6 +269,12 @@ where
}

fn submit(&mut self) -> InquireResult<Option<Vec<ListOption<T>>>> {
if self.select_on_empty_submit.is_some_and(|ose| ose) && self.checked.is_empty() {
if let Some(idx) = self.scored_options.get(self.cursor_index) {
self.checked.insert(*idx);
};
}

let answer = match self.validate_current_answer()? {
Validation::Valid => Some(self.get_final_answer()),
Validation::Invalid(msg) => {
Expand Down
55 changes: 55 additions & 0 deletions inquire/src/prompts/multiselect/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,58 @@ fn first_option_renders_on_new_line_without_filtering() {
match_text(&mut output, "\r");
match_text(&mut output, "\n");
}

#[test]
fn select_on_empty_submit_does_nothing_when_false() {
let mut backend = fake_backend(vec![
Key::Char('1', KeyModifiers::NONE), // filter to option 1
Key::Enter,
]);

let options = vec![1, 2, 3, 4, 5];

let ans = MultiSelect::new("Question", options)
.prompt_with_backend(&mut backend)
.unwrap();

let expected_result = Vec::<ListOption<i32>>::new();
assert_eq!(expected_result, ans);
}

#[test]
fn select_on_empty_submit_selects_option_under_cursor_when_true() {
let mut backend = fake_backend(vec![
Key::Char('1', KeyModifiers::NONE), // filter to option 1
Key::Enter,
]);

let options = vec![1, 2, 3, 4, 5];

let ans = MultiSelect::new("Question", options)
.with_select_on_empty_submit()
.prompt_with_backend(&mut backend)
.unwrap();

let expected_result = vec![ListOption::new(0, 1)];
assert_eq!(expected_result, ans);
}

#[test]
fn select_on_empty_submit_does_nothing_when_not_empty() {
let mut backend = fake_backend(vec![
Key::Char('2', KeyModifiers::NONE), // filter to option 2
Key::Char(' ', KeyModifiers::NONE), // toggle option 2 and reset filter
Key::Char('1', KeyModifiers::NONE), // filter to option 1
Key::Enter,
]);

let options = vec![1, 2, 3, 4, 5];

let ans = MultiSelect::new("Question", options)
.with_select_on_empty_submit()
.prompt_with_backend(&mut backend)
.unwrap();

let expected_result = vec![ListOption::new(1, 2)];
assert_eq!(expected_result, ans);
}