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>
This commit is contained in:
Mitch Denny
2025-08-20 04:09:12 +10:00
committed by GitHub
parent a8b2f1f1e0
commit 3a6d3e4520
2 changed files with 29 additions and 2 deletions

View File

@@ -109,7 +109,9 @@ public sealed class SelectionPrompt<T> : IPrompt<T>, IListPromptStrategy<T>
/// <inheritdoc/>
ListPromptInputResult IListPromptStrategy<T>.HandleInput(ConsoleKeyInfo key, ListPromptState<T> 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)

View File

@@ -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<InvalidOperationException>();
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<string>()
.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