From a1c73faf4e0fa97057a3a466d18b1d067b5e1a5e Mon Sep 17 00:00:00 2001 From: Payton Swick Date: Sun, 8 Feb 2026 18:28:58 -0500 Subject: [PATCH 1/3] Add select_on_empty_submit option and behavior to MultiSelect --- inquire/src/prompts/multiselect/mod.rs | 17 +++++++++++++++++ inquire/src/prompts/multiselect/prompt.rs | 8 ++++++++ 2 files changed, 25 insertions(+) diff --git a/inquire/src/prompts/multiselect/mod.rs b/inquire/src/prompts/multiselect/mod.rs index 585514fa..453dd9aa 100644 --- a/inquire/src/prompts/multiselect/mod.rs +++ b/inquire/src/prompts/multiselect/mod.rs @@ -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, } impl<'a, T> MultiSelect<'a, T> @@ -236,6 +243,7 @@ where formatter: Self::DEFAULT_FORMATTER, validator: None, render_config: get_configuration(), + select_on_empty_submit: None, } } @@ -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; diff --git a/inquire/src/prompts/multiselect/prompt.rs b/inquire/src/prompts/multiselect/prompt.rs index dc8d0838..aab76bbf 100644 --- a/inquire/src/prompts/multiselect/prompt.rs +++ b/inquire/src/prompts/multiselect/prompt.rs @@ -21,6 +21,7 @@ pub struct MultiSelectPrompt<'a, T> { options: Vec, string_options: Vec, help_message: Option<&'a str>, + select_on_empty_submit: Option, cursor_index: usize, checked: BTreeSet, input: Option, @@ -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, @@ -267,6 +269,12 @@ where } fn submit(&mut self) -> InquireResult>>> { + 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) => { From 195b80e10deab6db91bb24ea25f732fcb2f8b1c4 Mon Sep 17 00:00:00 2001 From: Payton Swick Date: Sun, 8 Feb 2026 18:46:30 -0500 Subject: [PATCH 2/3] Add tests for select_on_empty_submit --- inquire/src/prompts/multiselect/test.rs | 55 +++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/inquire/src/prompts/multiselect/test.rs b/inquire/src/prompts/multiselect/test.rs index 64b49494..ae86549f 100644 --- a/inquire/src/prompts/multiselect/test.rs +++ b/inquire/src/prompts/multiselect/test.rs @@ -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::>::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); +} From e75d1c1a0d919272aad2ed6ed427c47595862086 Mon Sep 17 00:00:00 2001 From: Payton Swick Date: Sun, 8 Feb 2026 19:13:47 -0500 Subject: [PATCH 3/3] Add CHANGELOG entry --- CHANGELOG.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 37d3b1b3..9731d2f1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,9 @@ ## [Unreleased] -- 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