Support cancellation of prompts

Closes #417
This commit is contained in:
Patrik Svensson
2021-07-10 21:03:13 +02:00
committed by Phil Scott
parent 884cb8ddd4
commit 5f97f2300c
15 changed files with 117 additions and 26 deletions

View File

@ -46,7 +46,7 @@ namespace Spectre.Console.Internal
try
{
return await func();
return await func().ConfigureAwait(false);
}
finally
{

View File

@ -1,4 +1,6 @@
using System;
using System.Threading;
using System.Threading.Tasks;
namespace Spectre.Console
{
@ -18,7 +20,32 @@ namespace Spectre.Console
throw new InvalidOperationException("Failed to read input in non-interactive mode.");
}
if (!System.Console.KeyAvailable)
{
return null;
}
return System.Console.ReadKey(intercept);
}
public async Task<ConsoleKeyInfo?> ReadKeyAsync(bool intercept, CancellationToken cancellationToken)
{
while (true)
{
if (cancellationToken.IsCancellationRequested)
{
return null;
}
if (System.Console.KeyAvailable)
{
break;
}
await Task.Delay(5, cancellationToken).ConfigureAwait(false);
}
return ReadKey(intercept);
}
}
}