From 538a8b1fe49cbd1f2097db04a1183c5b30ee2faa 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 1/4] 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 c9f55690e..472bfd87d 100644 --- a/src/Spectre.Console/Prompts/SelectionPrompt.cs +++ b/src/Spectre.Console/Prompts/SelectionPrompt.cs @@ -109,7 +109,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/src/Tests/Spectre.Console.Tests/Unit/Prompts/SelectionPromptTests.cs b/src/Tests/Spectre.Console.Tests/Unit/Prompts/SelectionPromptTests.cs index 2e066c5f6..676582646 100644 --- a/src/Tests/Spectre.Console.Tests/Unit/Prompts/SelectionPromptTests.cs +++ b/src/Tests/Spectre.Console.Tests/Unit/Prompts/SelectionPromptTests.cs @@ -87,6 +87,7 @@ public void Should_Highlight_Search_Term() } [Fact] +<<<<<<< HEAD:src/Tests/Spectre.Console.Tests/Unit/Prompts/SelectionPromptTests.cs public void Should_Search_In_Remapped_Result() { // Given @@ -141,5 +142,28 @@ public CustomSelectionItem(int value, string name) { Value = value; Name = name ?? throw new ArgumentNullException(nameof(name)); +======= + 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 "); +>>>>>>> b402054 (changes Search in SelectionPrompt to accept Space Key as text):test/Spectre.Console.Tests/Unit/Prompts/SelectionPromptTests.cs } } From a8f8065f3cf2a33613d261f6ba42e85d3f8f0f6a Mon Sep 17 00:00:00 2001 From: Mitch Denny Date: Tue, 19 Aug 2025 09:05:40 +1000 Subject: [PATCH 2/4] Fix up file. --- .../Unit/Prompts/SelectionPromptTests.cs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Tests/Spectre.Console.Tests/Unit/Prompts/SelectionPromptTests.cs b/src/Tests/Spectre.Console.Tests/Unit/Prompts/SelectionPromptTests.cs index 676582646..43698e193 100644 --- a/src/Tests/Spectre.Console.Tests/Unit/Prompts/SelectionPromptTests.cs +++ b/src/Tests/Spectre.Console.Tests/Unit/Prompts/SelectionPromptTests.cs @@ -87,7 +87,6 @@ public void Should_Highlight_Search_Term() } [Fact] -<<<<<<< HEAD:src/Tests/Spectre.Console.Tests/Unit/Prompts/SelectionPromptTests.cs public void Should_Search_In_Remapped_Result() { // Given @@ -116,7 +115,8 @@ public void Should_Search_In_Remapped_Result() 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(); @@ -142,7 +142,6 @@ public CustomSelectionItem(int value, string name) { Value = value; Name = name ?? throw new ArgumentNullException(nameof(name)); -======= public void Should_Append_Space_To_Search_If_Search_Is_Enabled() { /// Given @@ -164,6 +163,5 @@ public void Should_Append_Space_To_Search_If_Search_Is_Enabled() // 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 "); ->>>>>>> b402054 (changes Search in SelectionPrompt to accept Space Key as text):test/Spectre.Console.Tests/Unit/Prompts/SelectionPromptTests.cs } } From 6e833fb14f8656d0ebbf404ff6323c10b5e9988c Mon Sep 17 00:00:00 2001 From: Mitch Denny Date: Tue, 19 Aug 2025 09:18:14 +1000 Subject: [PATCH 3/4] Fix up brackets. --- .../Spectre.Console.Tests/Unit/Prompts/SelectionPromptTests.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Tests/Spectre.Console.Tests/Unit/Prompts/SelectionPromptTests.cs b/src/Tests/Spectre.Console.Tests/Unit/Prompts/SelectionPromptTests.cs index 43698e193..2c2142ab0 100644 --- a/src/Tests/Spectre.Console.Tests/Unit/Prompts/SelectionPromptTests.cs +++ b/src/Tests/Spectre.Console.Tests/Unit/Prompts/SelectionPromptTests.cs @@ -142,6 +142,8 @@ public CustomSelectionItem(int value, string name) { Value = value; Name = name ?? throw new ArgumentNullException(nameof(name)); + } + public void Should_Append_Space_To_Search_If_Search_Is_Enabled() { /// Given From afb1a59962a52c2e7e0e1bba0ba22497e01b1f45 Mon Sep 17 00:00:00 2001 From: Mitch Denny Date: Tue, 19 Aug 2025 09:23:08 +1000 Subject: [PATCH 4/4] Fix up test file. --- .../Unit/Prompts/SelectionPromptTests.cs | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/Tests/Spectre.Console.Tests/Unit/Prompts/SelectionPromptTests.cs b/src/Tests/Spectre.Console.Tests/Unit/Prompts/SelectionPromptTests.cs index 2c2142ab0..a91aea598 100644 --- a/src/Tests/Spectre.Console.Tests/Unit/Prompts/SelectionPromptTests.cs +++ b/src/Tests/Spectre.Console.Tests/Unit/Prompts/SelectionPromptTests.cs @@ -131,22 +131,11 @@ public void Should_Throw_Meaningful_Exception_For_Empty_Prompt() var exception = action.ShouldThrow(); exception.Message.ShouldBe("Cannot show an empty selection prompt. Please call the AddChoice() method to configure the prompt."); } -} -file sealed class CustomSelectionItem -{ - public int Value { get; } - public string Name { get; } - - public CustomSelectionItem(int value, string name) - { - Value = value; - Name = name ?? throw new ArgumentNullException(nameof(name)); - } - + [Fact] public void Should_Append_Space_To_Search_If_Search_Is_Enabled() { - /// Given + // Given var console = new TestConsole(); console.Profile.Capabilities.Interactive = true; console.EmitAnsiSequences(); @@ -167,3 +156,15 @@ public void Should_Append_Space_To_Search_If_Search_Is_Enabled() 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 +{ + public int Value { get; } + public string Name { get; } + + public CustomSelectionItem(int value, string name) + { + Value = value; + Name = name ?? throw new ArgumentNullException(nameof(name)); + } +}