Replies: 4 comments 3 replies
-
|
I just submitted one possible solution as #711 |
Beta Was this translation helpful? Give feedback.
2 replies
-
|
Any progress here? We also interested in this functionality. |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
I'm closing this discussion as I'm no longer interested in working on this feature. |
Beta Was this translation helpful? Give feedback.
0 replies
-
var console = new EscapeCancellableConsole(AnsiConsole.Console);
string? selected;
try
{
selected = await console.PromptAsync(selectionPrompt);
}
catch (OperationCanceledException)
{
selected = null;
}
// Processing selected...
exit;
sealed class EscapeCancellableConsole : IAnsiConsole
{
readonly EscapeCancellableInput input;
readonly IAnsiConsole console;
public EscapeCancellableConsole(IAnsiConsole console)
{
this.console = console;
input = new EscapeCancellableInput(console.Input, EscapeCancellationTokenSource);
}
public CancellationTokenSource EscapeCancellationTokenSource { get; } = new();
public Profile Profile => console.Profile;
public IAnsiConsoleCursor Cursor => console.Cursor;
public IAnsiConsoleInput Input => input;
public IExclusivityMode ExclusivityMode => console.ExclusivityMode;
public RenderPipeline Pipeline => console.Pipeline;
public void Clear(bool home) => console.Clear(home);
public void Write(IRenderable renderable) => console.Write(renderable);
public Task<T> PromptAsync<T>(IPrompt<T> prompt, CancellationToken cancellationToken = default)
=> AnsiConsoleExtensions.PromptAsync(this, prompt, GetMergedCancellationToken(cancellationToken));
CancellationToken GetMergedCancellationToken(CancellationToken cancellationToken)
=> cancellationToken == CancellationToken.None || cancellationToken == EscapeCancellationTokenSource.Token
? EscapeCancellationTokenSource.Token
: CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, EscapeCancellationTokenSource.Token).Token;
}
sealed class EscapeCancellableInput(IAnsiConsoleInput originalInput, CancellationTokenSource cts) : IAnsiConsoleInput
{
public bool IsKeyAvailable() => originalInput.IsKeyAvailable();
public ConsoleKeyInfo? ReadKey(bool intercept)
{
var key = originalInput.ReadKey(intercept);
if (key?.Key == ConsoleKey.Escape)
{
cts.Cancel();
}
return key;
}
public async Task<ConsoleKeyInfo?> ReadKeyAsync(bool intercept, CancellationToken cancellationToken)
{
var key = await originalInput.ReadKeyAsync(intercept, cancellationToken);
if (key?.Key == ConsoleKey.Escape)
{
await cts.CancelAsync();
}
return key;
}
}https://gist.github.com/ZelAnton/9b631ad409f6a2ebb6730824596a3b3d |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Let's talk about
SelectionPromptin its simplest form e.g.User can use
UpandDownarrows to change selected item andSpacebarorEnterto confirm the selection.However in my application I'm missing an option to cancel the selection entirely e.g by pressing
Escapekey.As a workaround I'm adding a special item at the beginning or at the end of the list and I handle its selection in a special way.
However in longer lists this first requires user to get to the top or bottom of the list by pressing
HomeorEndkey.I believe
Escapekey would be more convenient and most importantly it would help consumer code to be shorter and easier to understand.So I would like to add support for
Escapekey toSelectionPromptandMultiSelectionPrompt.Basic idea is that prompt ends when user presses
Escapekey and it somehow indicates to the caller it was cancelled (e.g. by returningnull, setting some new property totrue, throwing some specific exception etc.).Is this a feature that you would be willing to merge to Spectre.Console?
Beta Was this translation helpful? Give feedback.
All reactions