mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-09-17 09:55:34 +08:00

committed by
Phil Scott

parent
884cb8ddd4
commit
5f97f2300c
@@ -1,3 +1,6 @@
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Spectre.Console
|
||||
{
|
||||
/// <summary>
|
||||
@@ -50,6 +53,12 @@ namespace Spectre.Console
|
||||
|
||||
/// <inheritdoc/>
|
||||
public bool Show(IAnsiConsole console)
|
||||
{
|
||||
return ShowAsync(console, CancellationToken.None).GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task<bool> ShowAsync(IAnsiConsole console, CancellationToken cancellationToken)
|
||||
{
|
||||
var prompt = new TextPrompt<char>(_prompt)
|
||||
.InvalidChoiceMessage(InvalidChoiceMessage)
|
||||
@@ -60,7 +69,7 @@ namespace Spectre.Console
|
||||
.AddChoice(Yes)
|
||||
.AddChoice(No);
|
||||
|
||||
var result = prompt.Show(console);
|
||||
var result = await prompt.ShowAsync(console, cancellationToken).ConfigureAwait(false);
|
||||
return result == Yes;
|
||||
}
|
||||
}
|
||||
|
@@ -1,3 +1,6 @@
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Spectre.Console
|
||||
{
|
||||
/// <summary>
|
||||
@@ -12,5 +15,13 @@ namespace Spectre.Console
|
||||
/// <param name="console">The console.</param>
|
||||
/// <returns>The prompt input result.</returns>
|
||||
T Show(IAnsiConsole console);
|
||||
|
||||
/// <summary>
|
||||
/// Shows the prompt asynchronously.
|
||||
/// </summary>
|
||||
/// <param name="console">The console.</param>
|
||||
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
|
||||
/// <returns>The prompt input result.</returns>
|
||||
Task<T> ShowAsync(IAnsiConsole console, CancellationToken cancellationToken);
|
||||
}
|
||||
}
|
||||
|
@@ -1,5 +1,7 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Spectre.Console.Rendering;
|
||||
|
||||
namespace Spectre.Console
|
||||
@@ -16,7 +18,10 @@ namespace Spectre.Console
|
||||
_strategy = strategy ?? throw new ArgumentNullException(nameof(strategy));
|
||||
}
|
||||
|
||||
public ListPromptState<T> Show(ListPromptTree<T> tree, int requestedPageSize = 15)
|
||||
public async Task<ListPromptState<T>> Show(
|
||||
ListPromptTree<T> tree,
|
||||
CancellationToken cancellationToken,
|
||||
int requestedPageSize = 15)
|
||||
{
|
||||
if (tree is null)
|
||||
{
|
||||
@@ -48,7 +53,7 @@ namespace Spectre.Console
|
||||
|
||||
while (true)
|
||||
{
|
||||
var rawKey = _console.Input.ReadKey(true);
|
||||
var rawKey = await _console.Input.ReadKeyAsync(true, cancellationToken).ConfigureAwait(false);
|
||||
if (rawKey == null)
|
||||
{
|
||||
continue;
|
||||
|
@@ -2,6 +2,8 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Spectre.Console.Rendering;
|
||||
|
||||
namespace Spectre.Console
|
||||
@@ -85,10 +87,16 @@ namespace Spectre.Console
|
||||
|
||||
/// <inheritdoc/>
|
||||
public List<T> Show(IAnsiConsole console)
|
||||
{
|
||||
return ShowAsync(console, CancellationToken.None).GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task<List<T>> ShowAsync(IAnsiConsole console, CancellationToken cancellationToken)
|
||||
{
|
||||
// Create the list prompt
|
||||
var prompt = new ListPrompt<T>(console, this);
|
||||
var result = prompt.Show(Tree, PageSize);
|
||||
var result = await prompt.Show(Tree, cancellationToken, PageSize).ConfigureAwait(false);
|
||||
|
||||
if (Mode == SelectionMode.Leaf)
|
||||
{
|
||||
|
@@ -1,6 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Spectre.Console.Rendering;
|
||||
|
||||
namespace Spectre.Console
|
||||
@@ -74,10 +76,16 @@ namespace Spectre.Console
|
||||
|
||||
/// <inheritdoc/>
|
||||
public T Show(IAnsiConsole console)
|
||||
{
|
||||
return ShowAsync(console, CancellationToken.None).GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task<T> ShowAsync(IAnsiConsole console, CancellationToken cancellationToken)
|
||||
{
|
||||
// Create the list prompt
|
||||
var prompt = new ListPrompt<T>(console, this);
|
||||
var result = prompt.Show(_tree, PageSize);
|
||||
var result = await prompt.Show(_tree, cancellationToken, PageSize).ConfigureAwait(false);
|
||||
|
||||
// Return the selected item
|
||||
return result.Items[result.Index].Data;
|
||||
|
@@ -5,6 +5,8 @@ using System.Diagnostics.CodeAnalysis;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Spectre.Console
|
||||
{
|
||||
@@ -94,13 +96,19 @@ namespace Spectre.Console
|
||||
/// <returns>The user input converted to the expected type.</returns>
|
||||
/// <inheritdoc/>
|
||||
public T Show(IAnsiConsole console)
|
||||
{
|
||||
return ShowAsync(console, CancellationToken.None).GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task<T> ShowAsync(IAnsiConsole console, CancellationToken cancellationToken)
|
||||
{
|
||||
if (console is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(console));
|
||||
}
|
||||
|
||||
return console.RunExclusive(() =>
|
||||
return await console.RunExclusive(async () =>
|
||||
{
|
||||
var promptStyle = PromptStyle ?? Style.Plain;
|
||||
var converter = Converter ?? TypeConverterHelper.ConvertToString;
|
||||
@@ -111,7 +119,7 @@ namespace Spectre.Console
|
||||
|
||||
while (true)
|
||||
{
|
||||
var input = console.ReadLine(promptStyle, IsSecret, choices);
|
||||
var input = await console.ReadLine(promptStyle, IsSecret, choices, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
// Nothing entered?
|
||||
if (string.IsNullOrWhiteSpace(input))
|
||||
@@ -162,7 +170,7 @@ namespace Spectre.Console
|
||||
|
||||
return result;
|
||||
}
|
||||
});
|
||||
}).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
Reference in New Issue
Block a user