From 3a6d3e4520af640b6f5c9daab1901dd477c100ef Mon Sep 17 00:00:00 2001 From: Mitch Denny Date: Wed, 20 Aug 2025 04:09:12 +1000 Subject: [PATCH] Fix space triggering selection when items in selection list have a space. (#1881) * Changes Search in SelectionPrompt to accept Space Key as text --------- Co-authored-by: Philipp <30900810+DerReparator@users.noreply.github.com> --- .../Prompts/SelectionPrompt.cs | 4 ++- .../Unit/Prompts/SelectionPromptTests.cs | 27 ++++++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/Spectre.Console/Prompts/SelectionPrompt.cs b/src/Spectre.Console/Prompts/SelectionPrompt.cs index c9f55690..472bfd87 100644 --- a/src/Spectre.Console/Prompts/SelectionPrompt.cs +++ b/src/Spectre.Console/Prompts/SelectionPrompt.cs @@ -109,7 +109,9 @@ public sealed class SelectionPrompt : IPrompt, IListPromptStrategy /// ListPromptInputResult IListPromptStrategy.HandleInput(ConsoleKeyInfo key, ListPromptState state) { - if (key.Key == ConsoleKey.Enter || key.Key == ConsoleKey.Spacebar || key.Key == ConsoleKey.Packet) + if (key.Key == ConsoleKey.Enter + || key.Key == ConsoleKey.Packet + || (!state.SearchEnabled && key.Key == ConsoleKey.Spacebar)) { // Selecting a non leaf in "leaf mode" is not allowed if (state.Current.IsGroup && Mode == SelectionMode.Leaf) diff --git a/src/Tests/Spectre.Console.Tests/Unit/Prompts/SelectionPromptTests.cs b/src/Tests/Spectre.Console.Tests/Unit/Prompts/SelectionPromptTests.cs index 2e066c5f..a91aea59 100644 --- a/src/Tests/Spectre.Console.Tests/Unit/Prompts/SelectionPromptTests.cs +++ b/src/Tests/Spectre.Console.Tests/Unit/Prompts/SelectionPromptTests.cs @@ -115,7 +115,8 @@ public sealed class SelectionPromptTests selection.ShouldBe(choices[1]); } - [Fact] public void Should_Throw_Meaningful_Exception_For_Empty_Prompt() + [Fact] + public void Should_Throw_Meaningful_Exception_For_Empty_Prompt() { // Given var console = new TestConsole(); @@ -130,6 +131,30 @@ public sealed class SelectionPromptTests var exception = action.ShouldThrow(); exception.Message.ShouldBe("Cannot show an empty selection prompt. Please call the AddChoice() method to configure the prompt."); } + + [Fact] + public void Should_Append_Space_To_Search_If_Search_Is_Enabled() + { + // Given + var console = new TestConsole(); + console.Profile.Capabilities.Interactive = true; + console.EmitAnsiSequences(); + console.Input.PushText("Item"); + console.Input.PushKey(ConsoleKey.Spacebar); + console.Input.PushKey(ConsoleKey.Enter); + + // When + var prompt = new SelectionPrompt() + .Title("Search for something with space") + .EnableSearch() + .AddChoices("Item1") + .AddChoices("Item 2"); + string result = prompt.Show(console); + + // Then + result.ShouldBe("Item 2"); + console.Output.ShouldContain($"{ESC}[38;5;12m> {ESC}[0m{ESC}[1;38;5;12;48;5;11mItem {ESC}[0m{ESC}[38;5;12m2{ESC}[0m "); + } } file sealed class CustomSelectionItem