Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Spectre.Console/Prompts/SelectionPrompt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ public async Task<T> ShowAsync(IAnsiConsole console, CancellationToken cancellat
/// <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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,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();
Expand All @@ -130,6 +131,30 @@ [Fact] public void Should_Throw_Meaningful_Exception_For_Empty_Prompt()
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
Expand Down