Skip to content
Open
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
6 changes: 6 additions & 0 deletions src/Spectre.Console/Prompts/List/ListPrompt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,17 @@ public async Task<ListPromptState<T>> Show(

var key = rawKey.Value;
var result = _strategy.HandleInput(key, state);

if (result == ListPromptInputResult.Submit)
{
break;
}

if (result == ListPromptInputResult.Cancel)
{
throw new OperationCanceledException("Prompt cancelled via ESC key.");
}

if (state.Update(key) || result == ListPromptInputResult.Refresh)
{
hook.Refresh();
Expand Down
1 change: 1 addition & 0 deletions src/Spectre.Console/Prompts/List/ListPromptInputResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ internal enum ListPromptInputResult
Refresh = 1,
Submit = 2,
Abort = 3,
Cancel = 4,
}
5 changes: 5 additions & 0 deletions src/Spectre.Console/Prompts/SelectionPrompt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ public async Task<T> ShowAsync(IAnsiConsole console, CancellationToken cancellat
/// <inheritdoc/>
ListPromptInputResult IListPromptStrategy<T>.HandleInput(ConsoleKeyInfo key, ListPromptState<T> state)
{
if (key.Key == ConsoleKey.Escape)
{
return ListPromptInputResult.Cancel;
}

if (key.Key == ConsoleKey.Enter || key.Key == ConsoleKey.Spacebar || key.Key == ConsoleKey.Packet)
{
// Selecting a non leaf in "leaf mode" is not allowed
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,26 @@ [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 SelectionPrompt_Should_Cancel_On_Esc()
{
// Arrange
var console = new TestConsole();
console.Profile.Capabilities.Ansi = true;
console.Profile.Capabilities.Interactive = true;
console.Input.PushKey(new ConsoleKeyInfo('\0', ConsoleKey.Escape, false, false, false));

var prompt = new SelectionPrompt<string>();
prompt.AddChoice("Option A");
prompt.AddChoice("Option B");

// Act & Assert
Assert.Throws<OperationCanceledException>(() =>
{
prompt.Show(console);
});
}
}

file sealed class CustomSelectionItem
Expand All @@ -143,3 +164,5 @@ public CustomSelectionItem(int value, string name)
Name = name ?? throw new ArgumentNullException(nameof(name));
}
}


1,133 changes: 1,133 additions & 0 deletions src/Tests/Spectre.Console.Tests/sdk-9.0.202-linux-x64-binaries

Large diffs are not rendered by default.