Skip to content
Closed
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
19 changes: 13 additions & 6 deletions src/Spectre.Console/Prompts/List/ListPrompt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public async Task<ListPromptState<T>> Show(
ListPromptTree<T> tree,
CancellationToken cancellationToken,
int requestedPageSize = 15,
bool wrapAround = false)
bool wrapAround = false,
bool allowAbort = true)
{
if (tree is null)
{
Expand Down Expand Up @@ -52,16 +53,22 @@ public async Task<ListPromptState<T>> Show(
if (rawKey == null)
{
continue;
}
}

ConsoleKeyInfo key = rawKey.Value;
ListPromptInputResult result = _strategy.HandleInput(key, state);
if (allowAbort && result is ListPromptInputResult.Abort)
{
break;
}

var key = rawKey.Value;
var result = _strategy.HandleInput(key, state);
if (result == ListPromptInputResult.Submit)
if (result is ListPromptInputResult.Submit)
{
break;
}

if (state.Update(key.Key) || result == ListPromptInputResult.Refresh)
if (state.Update(key.Key)
|| result is ListPromptInputResult.Refresh)
{
hook.Refresh();
}
Expand Down
5 changes: 5 additions & 0 deletions src/Spectre.Console/Prompts/MultiSelectionPrompt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ ListPromptInputResult IListPromptStrategy<T>.HandleInput(ConsoleKeyInfo key, Lis
else
{
current.IsSelected = !current.IsSelected;
}

if (key.Key == ConsoleKey.Escape)
{
return ListPromptInputResult.Abort;
}

// Refresh the list
Expand Down
9 changes: 7 additions & 2 deletions src/Spectre.Console/Prompts/SelectionPrompt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,13 @@ ListPromptInputResult IListPromptStrategy<T>.HandleInput(ConsoleKeyInfo key, Lis
}

return ListPromptInputResult.Submit;
}

}

if (key.Key == ConsoleKey.Escape)
{
return ListPromptInputResult.Abort;
}

return ListPromptInputResult.None;
}

Expand Down
13 changes: 13 additions & 0 deletions test/Spectre.Console.Tests/Unit/Prompts/ListPromptStateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,18 @@ public void Should_Wrap_Index_If_Wrap_And_Offset_And_Page_Down()

// Then
state.Index.ShouldBe(8);
}

[Fact]
public void Should_Do_Nothing()
{
// Given
var state = CreateListPromptState(100, 10, true);

// When
state.Update(ConsoleKey.Escape);

// Then
state.Index.ShouldBe(0);
}
}