From b4020549bb63123da54b018ebb384b9cfa9832d1 Mon Sep 17 00:00:00 2001 From: Philipp <30900810+DerReparator@users.noreply.github.com> Date: Tue, 6 Aug 2024 13:26:31 +0200 Subject: [PATCH] changes Search in SelectionPrompt to accept Space Key as text --- .../Prompts/SelectionPrompt.cs | 4 +++- .../Unit/Prompts/SelectionPromptTests.cs | 24 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/Spectre.Console/Prompts/SelectionPrompt.cs b/src/Spectre.Console/Prompts/SelectionPrompt.cs index e6ed46b87..7d257e1ec 100644 --- a/src/Spectre.Console/Prompts/SelectionPrompt.cs +++ b/src/Spectre.Console/Prompts/SelectionPrompt.cs @@ -108,7 +108,9 @@ public async Task ShowAsync(IAnsiConsole console, CancellationToken cancellat /// 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/test/Spectre.Console.Tests/Unit/Prompts/SelectionPromptTests.cs b/test/Spectre.Console.Tests/Unit/Prompts/SelectionPromptTests.cs index 5878c9df8..4da609daf 100644 --- a/test/Spectre.Console.Tests/Unit/Prompts/SelectionPromptTests.cs +++ b/test/Spectre.Console.Tests/Unit/Prompts/SelectionPromptTests.cs @@ -85,4 +85,28 @@ public void Should_Highlight_Search_Term() // Then console.Output.ShouldContain($"{ESC}[38;5;12m> Item {ESC}[0m{ESC}[1;38;5;12;48;5;11m1{ESC}[0m"); } + + [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 "); + } }