mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-06-19 21:38:16 +08:00
Use file scoped namespace declarations
This commit is contained in:

committed by
Phil Scott

parent
1dbaf50935
commit
ec1188b837
@ -1,76 +1,75 @@
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Spectre.Console
|
||||
namespace Spectre.Console;
|
||||
|
||||
/// <summary>
|
||||
/// A prompt that is answered with a yes or no.
|
||||
/// </summary>
|
||||
public sealed class ConfirmationPrompt : IPrompt<bool>
|
||||
{
|
||||
private readonly string _prompt;
|
||||
|
||||
/// <summary>
|
||||
/// A prompt that is answered with a yes or no.
|
||||
/// Gets or sets the character that represents "yes".
|
||||
/// </summary>
|
||||
public sealed class ConfirmationPrompt : IPrompt<bool>
|
||||
public char Yes { get; set; } = 'y';
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the character that represents "no".
|
||||
/// </summary>
|
||||
public char No { get; set; } = 'n';
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether "yes" is the default answer.
|
||||
/// </summary>
|
||||
public bool DefaultValue { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the message for invalid choices.
|
||||
/// </summary>
|
||||
public string InvalidChoiceMessage { get; set; } = "[red]Please select one of the available options[/]";
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether or not
|
||||
/// choices should be shown.
|
||||
/// </summary>
|
||||
public bool ShowChoices { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether or not
|
||||
/// default values should be shown.
|
||||
/// </summary>
|
||||
public bool ShowDefaultValue { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ConfirmationPrompt"/> class.
|
||||
/// </summary>
|
||||
/// <param name="prompt">The prompt markup text.</param>
|
||||
public ConfirmationPrompt(string prompt)
|
||||
{
|
||||
private readonly string _prompt;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the character that represents "yes".
|
||||
/// </summary>
|
||||
public char Yes { get; set; } = 'y';
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the character that represents "no".
|
||||
/// </summary>
|
||||
public char No { get; set; } = 'n';
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether "yes" is the default answer.
|
||||
/// </summary>
|
||||
public bool DefaultValue { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the message for invalid choices.
|
||||
/// </summary>
|
||||
public string InvalidChoiceMessage { get; set; } = "[red]Please select one of the available options[/]";
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether or not
|
||||
/// choices should be shown.
|
||||
/// </summary>
|
||||
public bool ShowChoices { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether or not
|
||||
/// default values should be shown.
|
||||
/// </summary>
|
||||
public bool ShowDefaultValue { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ConfirmationPrompt"/> class.
|
||||
/// </summary>
|
||||
/// <param name="prompt">The prompt markup text.</param>
|
||||
public ConfirmationPrompt(string prompt)
|
||||
{
|
||||
_prompt = prompt ?? throw new System.ArgumentNullException(nameof(prompt));
|
||||
}
|
||||
|
||||
/// <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)
|
||||
.ValidationErrorMessage(InvalidChoiceMessage)
|
||||
.ShowChoices(ShowChoices)
|
||||
.ShowDefaultValue(ShowDefaultValue)
|
||||
.DefaultValue(DefaultValue ? Yes : No)
|
||||
.AddChoice(Yes)
|
||||
.AddChoice(No);
|
||||
|
||||
var result = await prompt.ShowAsync(console, cancellationToken).ConfigureAwait(false);
|
||||
return result == Yes;
|
||||
}
|
||||
_prompt = prompt ?? throw new System.ArgumentNullException(nameof(prompt));
|
||||
}
|
||||
}
|
||||
|
||||
/// <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)
|
||||
.ValidationErrorMessage(InvalidChoiceMessage)
|
||||
.ShowChoices(ShowChoices)
|
||||
.ShowDefaultValue(ShowDefaultValue)
|
||||
.DefaultValue(DefaultValue ? Yes : No)
|
||||
.AddChoice(Yes)
|
||||
.AddChoice(No);
|
||||
|
||||
var result = await prompt.ShowAsync(console, cancellationToken).ConfigureAwait(false);
|
||||
return result == Yes;
|
||||
}
|
||||
}
|
@ -1,12 +1,11 @@
|
||||
namespace Spectre.Console
|
||||
{
|
||||
internal sealed class DefaultPromptValue<T>
|
||||
{
|
||||
public T Value { get; }
|
||||
namespace Spectre.Console;
|
||||
|
||||
public DefaultPromptValue(T value)
|
||||
{
|
||||
Value = value;
|
||||
}
|
||||
internal sealed class DefaultPromptValue<T>
|
||||
{
|
||||
public T Value { get; }
|
||||
|
||||
public DefaultPromptValue(T value)
|
||||
{
|
||||
Value = value;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,21 +1,20 @@
|
||||
namespace Spectre.Console
|
||||
namespace Spectre.Console;
|
||||
|
||||
/// <summary>
|
||||
/// Represent a multi selection prompt item.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The data type.</typeparam>
|
||||
public interface IMultiSelectionItem<T> : ISelectionItem<T>
|
||||
where T : notnull
|
||||
{
|
||||
/// <summary>
|
||||
/// Represent a multi selection prompt item.
|
||||
/// Gets a value indicating whether or not this item is selected.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The data type.</typeparam>
|
||||
public interface IMultiSelectionItem<T> : ISelectionItem<T>
|
||||
where T : notnull
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether or not this item is selected.
|
||||
/// </summary>
|
||||
bool IsSelected { get; }
|
||||
bool IsSelected { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Selects the item.
|
||||
/// </summary>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
IMultiSelectionItem<T> Select();
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Selects the item.
|
||||
/// </summary>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
IMultiSelectionItem<T> Select();
|
||||
}
|
@ -1,27 +1,26 @@
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Spectre.Console
|
||||
namespace Spectre.Console;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a prompt.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
public interface IPrompt<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a prompt.
|
||||
/// Shows the prompt.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
public interface IPrompt<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// Shows the prompt.
|
||||
/// </summary>
|
||||
/// <param name="console">The console.</param>
|
||||
/// <returns>The prompt input result.</returns>
|
||||
T Show(IAnsiConsole 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);
|
||||
}
|
||||
}
|
||||
/// <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,17 +1,16 @@
|
||||
namespace Spectre.Console
|
||||
namespace Spectre.Console;
|
||||
|
||||
/// <summary>
|
||||
/// Represent a selection item.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The data type.</typeparam>
|
||||
public interface ISelectionItem<T>
|
||||
where T : notnull
|
||||
{
|
||||
/// <summary>
|
||||
/// Represent a selection item.
|
||||
/// Adds a child to the item.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The data type.</typeparam>
|
||||
public interface ISelectionItem<T>
|
||||
where T : notnull
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds a child to the item.
|
||||
/// </summary>
|
||||
/// <param name="child">The child to add.</param>
|
||||
/// <returns>A new <see cref="ISelectionItem{T}"/> instance representing the child.</returns>
|
||||
ISelectionItem<T> AddChild(T child);
|
||||
}
|
||||
}
|
||||
/// <param name="child">The child to add.</param>
|
||||
/// <returns>A new <see cref="ISelectionItem{T}"/> instance representing the child.</returns>
|
||||
ISelectionItem<T> AddChild(T child);
|
||||
}
|
@ -2,40 +2,39 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using Spectre.Console.Rendering;
|
||||
|
||||
namespace Spectre.Console
|
||||
namespace Spectre.Console;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a strategy for a list prompt.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The list data type.</typeparam>
|
||||
internal interface IListPromptStrategy<T>
|
||||
where T : notnull
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a strategy for a list prompt.
|
||||
/// Handles any input received from the user.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The list data type.</typeparam>
|
||||
internal interface IListPromptStrategy<T>
|
||||
where T : notnull
|
||||
{
|
||||
/// <summary>
|
||||
/// Handles any input received from the user.
|
||||
/// </summary>
|
||||
/// <param name="key">The key that was pressed.</param>
|
||||
/// <param name="state">The current state.</param>
|
||||
/// <returns>A result representing an action.</returns>
|
||||
ListPromptInputResult HandleInput(ConsoleKeyInfo key, ListPromptState<T> state);
|
||||
/// <param name="key">The key that was pressed.</param>
|
||||
/// <param name="state">The current state.</param>
|
||||
/// <returns>A result representing an action.</returns>
|
||||
ListPromptInputResult HandleInput(ConsoleKeyInfo key, ListPromptState<T> state);
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the page size.
|
||||
/// </summary>
|
||||
/// <param name="console">The console.</param>
|
||||
/// <param name="totalItemCount">The total number of items.</param>
|
||||
/// <param name="requestedPageSize">The requested number of items to show.</param>
|
||||
/// <returns>The page size that should be used.</returns>
|
||||
public int CalculatePageSize(IAnsiConsole console, int totalItemCount, int requestedPageSize);
|
||||
/// <summary>
|
||||
/// Calculates the page size.
|
||||
/// </summary>
|
||||
/// <param name="console">The console.</param>
|
||||
/// <param name="totalItemCount">The total number of items.</param>
|
||||
/// <param name="requestedPageSize">The requested number of items to show.</param>
|
||||
/// <returns>The page size that should be used.</returns>
|
||||
public int CalculatePageSize(IAnsiConsole console, int totalItemCount, int requestedPageSize);
|
||||
|
||||
/// <summary>
|
||||
/// Builds a <see cref="IRenderable"/> from the current state.
|
||||
/// </summary>
|
||||
/// <param name="console">The console.</param>
|
||||
/// <param name="scrollable">Whether or not the list is scrollable.</param>
|
||||
/// <param name="cursorIndex">The cursor index.</param>
|
||||
/// <param name="items">The visible items.</param>
|
||||
/// <returns>A <see cref="IRenderable"/> representing the items.</returns>
|
||||
public IRenderable Render(IAnsiConsole console, bool scrollable, int cursorIndex, IEnumerable<(int Index, ListPromptItem<T> Node)> items);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Builds a <see cref="IRenderable"/> from the current state.
|
||||
/// </summary>
|
||||
/// <param name="console">The console.</param>
|
||||
/// <param name="scrollable">Whether or not the list is scrollable.</param>
|
||||
/// <param name="cursorIndex">The cursor index.</param>
|
||||
/// <param name="items">The visible items.</param>
|
||||
/// <returns>A <see cref="IRenderable"/> representing the items.</returns>
|
||||
public IRenderable Render(IAnsiConsole console, bool scrollable, int cursorIndex, IEnumerable<(int Index, ListPromptItem<T> Node)> items);
|
||||
}
|
@ -4,117 +4,116 @@ using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Spectre.Console.Rendering;
|
||||
|
||||
namespace Spectre.Console
|
||||
namespace Spectre.Console;
|
||||
|
||||
internal sealed class ListPrompt<T>
|
||||
where T : notnull
|
||||
{
|
||||
internal sealed class ListPrompt<T>
|
||||
where T : notnull
|
||||
private readonly IAnsiConsole _console;
|
||||
private readonly IListPromptStrategy<T> _strategy;
|
||||
|
||||
public ListPrompt(IAnsiConsole console, IListPromptStrategy<T> strategy)
|
||||
{
|
||||
private readonly IAnsiConsole _console;
|
||||
private readonly IListPromptStrategy<T> _strategy;
|
||||
|
||||
public ListPrompt(IAnsiConsole console, IListPromptStrategy<T> strategy)
|
||||
{
|
||||
_console = console ?? throw new ArgumentNullException(nameof(console));
|
||||
_strategy = strategy ?? throw new ArgumentNullException(nameof(strategy));
|
||||
}
|
||||
|
||||
public async Task<ListPromptState<T>> Show(
|
||||
ListPromptTree<T> tree,
|
||||
CancellationToken cancellationToken,
|
||||
int requestedPageSize = 15)
|
||||
{
|
||||
if (tree is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(tree));
|
||||
}
|
||||
|
||||
if (!_console.Profile.Capabilities.Interactive)
|
||||
{
|
||||
throw new NotSupportedException(
|
||||
"Cannot show selection prompt since the current " +
|
||||
"terminal isn't interactive.");
|
||||
}
|
||||
|
||||
if (!_console.Profile.Capabilities.Ansi)
|
||||
{
|
||||
throw new NotSupportedException(
|
||||
"Cannot show selection prompt since the current " +
|
||||
"terminal does not support ANSI escape sequences.");
|
||||
}
|
||||
|
||||
var nodes = tree.Traverse().ToList();
|
||||
var state = new ListPromptState<T>(nodes, _strategy.CalculatePageSize(_console, nodes.Count, requestedPageSize));
|
||||
var hook = new ListPromptRenderHook<T>(_console, () => BuildRenderable(state));
|
||||
|
||||
using (new RenderHookScope(_console, hook))
|
||||
{
|
||||
_console.Cursor.Hide();
|
||||
hook.Refresh();
|
||||
|
||||
while (true)
|
||||
{
|
||||
var rawKey = await _console.Input.ReadKeyAsync(true, cancellationToken).ConfigureAwait(false);
|
||||
if (rawKey == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var key = rawKey.Value;
|
||||
var result = _strategy.HandleInput(key, state);
|
||||
if (result == ListPromptInputResult.Submit)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if (state.Update(key.Key) || result == ListPromptInputResult.Refresh)
|
||||
{
|
||||
hook.Refresh();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
hook.Clear();
|
||||
_console.Cursor.Show();
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
private IRenderable BuildRenderable(ListPromptState<T> state)
|
||||
{
|
||||
var pageSize = state.PageSize;
|
||||
var middleOfList = pageSize / 2;
|
||||
|
||||
var skip = 0;
|
||||
var take = state.ItemCount;
|
||||
var cursorIndex = state.Index;
|
||||
|
||||
var scrollable = state.ItemCount > pageSize;
|
||||
if (scrollable)
|
||||
{
|
||||
skip = Math.Max(0, state.Index - middleOfList);
|
||||
take = Math.Min(pageSize, state.ItemCount - skip);
|
||||
|
||||
if (state.ItemCount - state.Index < middleOfList)
|
||||
{
|
||||
// Pointer should be below the end of the list
|
||||
var diff = middleOfList - (state.ItemCount - state.Index);
|
||||
skip -= diff;
|
||||
take += diff;
|
||||
cursorIndex = middleOfList + diff;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Take skip into account
|
||||
cursorIndex -= skip;
|
||||
}
|
||||
}
|
||||
|
||||
// Build the renderable
|
||||
return _strategy.Render(
|
||||
_console,
|
||||
scrollable, cursorIndex,
|
||||
state.Items.Skip(skip).Take(take)
|
||||
.Select((node, index) => (index, node)));
|
||||
}
|
||||
_console = console ?? throw new ArgumentNullException(nameof(console));
|
||||
_strategy = strategy ?? throw new ArgumentNullException(nameof(strategy));
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ListPromptState<T>> Show(
|
||||
ListPromptTree<T> tree,
|
||||
CancellationToken cancellationToken,
|
||||
int requestedPageSize = 15)
|
||||
{
|
||||
if (tree is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(tree));
|
||||
}
|
||||
|
||||
if (!_console.Profile.Capabilities.Interactive)
|
||||
{
|
||||
throw new NotSupportedException(
|
||||
"Cannot show selection prompt since the current " +
|
||||
"terminal isn't interactive.");
|
||||
}
|
||||
|
||||
if (!_console.Profile.Capabilities.Ansi)
|
||||
{
|
||||
throw new NotSupportedException(
|
||||
"Cannot show selection prompt since the current " +
|
||||
"terminal does not support ANSI escape sequences.");
|
||||
}
|
||||
|
||||
var nodes = tree.Traverse().ToList();
|
||||
var state = new ListPromptState<T>(nodes, _strategy.CalculatePageSize(_console, nodes.Count, requestedPageSize));
|
||||
var hook = new ListPromptRenderHook<T>(_console, () => BuildRenderable(state));
|
||||
|
||||
using (new RenderHookScope(_console, hook))
|
||||
{
|
||||
_console.Cursor.Hide();
|
||||
hook.Refresh();
|
||||
|
||||
while (true)
|
||||
{
|
||||
var rawKey = await _console.Input.ReadKeyAsync(true, cancellationToken).ConfigureAwait(false);
|
||||
if (rawKey == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var key = rawKey.Value;
|
||||
var result = _strategy.HandleInput(key, state);
|
||||
if (result == ListPromptInputResult.Submit)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if (state.Update(key.Key) || result == ListPromptInputResult.Refresh)
|
||||
{
|
||||
hook.Refresh();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
hook.Clear();
|
||||
_console.Cursor.Show();
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
private IRenderable BuildRenderable(ListPromptState<T> state)
|
||||
{
|
||||
var pageSize = state.PageSize;
|
||||
var middleOfList = pageSize / 2;
|
||||
|
||||
var skip = 0;
|
||||
var take = state.ItemCount;
|
||||
var cursorIndex = state.Index;
|
||||
|
||||
var scrollable = state.ItemCount > pageSize;
|
||||
if (scrollable)
|
||||
{
|
||||
skip = Math.Max(0, state.Index - middleOfList);
|
||||
take = Math.Min(pageSize, state.ItemCount - skip);
|
||||
|
||||
if (state.ItemCount - state.Index < middleOfList)
|
||||
{
|
||||
// Pointer should be below the end of the list
|
||||
var diff = middleOfList - (state.ItemCount - state.Index);
|
||||
skip -= diff;
|
||||
take += diff;
|
||||
cursorIndex = middleOfList + diff;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Take skip into account
|
||||
cursorIndex -= skip;
|
||||
}
|
||||
}
|
||||
|
||||
// Build the renderable
|
||||
return _strategy.Render(
|
||||
_console,
|
||||
scrollable, cursorIndex,
|
||||
state.Items.Skip(skip).Take(take)
|
||||
.Select((node, index) => (index, node)));
|
||||
}
|
||||
}
|
@ -1,12 +1,11 @@
|
||||
namespace Spectre.Console
|
||||
namespace Spectre.Console;
|
||||
|
||||
internal sealed class ListPromptConstants
|
||||
{
|
||||
internal sealed class ListPromptConstants
|
||||
{
|
||||
public const string Arrow = ">";
|
||||
public const string Checkbox = "[[ ]]";
|
||||
public const string SelectedCheckbox = "[[[blue]X[/]]]";
|
||||
public const string GroupSelectedCheckbox = "[[[grey]X[/]]]";
|
||||
public const string InstructionsMarkup = "[grey](Press <space> to select, <enter> to accept)[/]";
|
||||
public const string MoreChoicesMarkup = "[grey](Move up and down to reveal more choices)[/]";
|
||||
}
|
||||
}
|
||||
public const string Arrow = ">";
|
||||
public const string Checkbox = "[[ ]]";
|
||||
public const string SelectedCheckbox = "[[[blue]X[/]]]";
|
||||
public const string GroupSelectedCheckbox = "[[[grey]X[/]]]";
|
||||
public const string InstructionsMarkup = "[grey](Press <space> to select, <enter> to accept)[/]";
|
||||
public const string MoreChoicesMarkup = "[grey](Move up and down to reveal more choices)[/]";
|
||||
}
|
@ -1,10 +1,9 @@
|
||||
namespace Spectre.Console
|
||||
namespace Spectre.Console;
|
||||
|
||||
internal enum ListPromptInputResult
|
||||
{
|
||||
internal enum ListPromptInputResult
|
||||
{
|
||||
None = 0,
|
||||
Refresh = 1,
|
||||
Submit = 2,
|
||||
Abort = 3,
|
||||
}
|
||||
}
|
||||
None = 0,
|
||||
Refresh = 1,
|
||||
Submit = 2,
|
||||
Abort = 3,
|
||||
}
|
@ -1,80 +1,79 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Spectre.Console
|
||||
namespace Spectre.Console;
|
||||
|
||||
internal sealed class ListPromptItem<T> : IMultiSelectionItem<T>
|
||||
where T : notnull
|
||||
{
|
||||
internal sealed class ListPromptItem<T> : IMultiSelectionItem<T>
|
||||
where T : notnull
|
||||
public T Data { get; }
|
||||
public ListPromptItem<T>? Parent { get; }
|
||||
public List<ListPromptItem<T>> Children { get; }
|
||||
public int Depth { get; }
|
||||
public bool IsSelected { get; set; }
|
||||
|
||||
public bool IsGroup => Children.Count > 0;
|
||||
|
||||
public ListPromptItem(T data, ListPromptItem<T>? parent = null)
|
||||
{
|
||||
public T Data { get; }
|
||||
public ListPromptItem<T>? Parent { get; }
|
||||
public List<ListPromptItem<T>> Children { get; }
|
||||
public int Depth { get; }
|
||||
public bool IsSelected { get; set; }
|
||||
Data = data;
|
||||
Parent = parent;
|
||||
Children = new List<ListPromptItem<T>>();
|
||||
Depth = CalculateDepth(parent);
|
||||
}
|
||||
|
||||
public bool IsGroup => Children.Count > 0;
|
||||
public IMultiSelectionItem<T> Select()
|
||||
{
|
||||
IsSelected = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ListPromptItem(T data, ListPromptItem<T>? parent = null)
|
||||
public ISelectionItem<T> AddChild(T item)
|
||||
{
|
||||
var node = new ListPromptItem<T>(item, this);
|
||||
Children.Add(node);
|
||||
return node;
|
||||
}
|
||||
|
||||
public IEnumerable<ListPromptItem<T>> Traverse(bool includeSelf)
|
||||
{
|
||||
var stack = new Stack<ListPromptItem<T>>();
|
||||
|
||||
if (includeSelf)
|
||||
{
|
||||
Data = data;
|
||||
Parent = parent;
|
||||
Children = new List<ListPromptItem<T>>();
|
||||
Depth = CalculateDepth(parent);
|
||||
stack.Push(this);
|
||||
}
|
||||
|
||||
public IMultiSelectionItem<T> Select()
|
||||
else
|
||||
{
|
||||
IsSelected = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ISelectionItem<T> AddChild(T item)
|
||||
{
|
||||
var node = new ListPromptItem<T>(item, this);
|
||||
Children.Add(node);
|
||||
return node;
|
||||
}
|
||||
|
||||
public IEnumerable<ListPromptItem<T>> Traverse(bool includeSelf)
|
||||
{
|
||||
var stack = new Stack<ListPromptItem<T>>();
|
||||
|
||||
if (includeSelf)
|
||||
foreach (var child in Children)
|
||||
{
|
||||
stack.Push(this);
|
||||
stack.Push(child);
|
||||
}
|
||||
else
|
||||
}
|
||||
|
||||
while (stack.Count > 0)
|
||||
{
|
||||
var current = stack.Pop();
|
||||
yield return current;
|
||||
|
||||
if (current.Children.Count > 0)
|
||||
{
|
||||
foreach (var child in Children)
|
||||
foreach (var child in current.Children.ReverseEnumerable())
|
||||
{
|
||||
stack.Push(child);
|
||||
}
|
||||
}
|
||||
|
||||
while (stack.Count > 0)
|
||||
{
|
||||
var current = stack.Pop();
|
||||
yield return current;
|
||||
|
||||
if (current.Children.Count > 0)
|
||||
{
|
||||
foreach (var child in current.Children.ReverseEnumerable())
|
||||
{
|
||||
stack.Push(child);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static int CalculateDepth(ListPromptItem<T>? parent)
|
||||
{
|
||||
var level = 0;
|
||||
while (parent != null)
|
||||
{
|
||||
level++;
|
||||
parent = parent.Parent;
|
||||
}
|
||||
|
||||
return level;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static int CalculateDepth(ListPromptItem<T>? parent)
|
||||
{
|
||||
var level = 0;
|
||||
while (parent != null)
|
||||
{
|
||||
level++;
|
||||
parent = parent.Parent;
|
||||
}
|
||||
|
||||
return level;
|
||||
}
|
||||
}
|
@ -2,59 +2,58 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using Spectre.Console.Rendering;
|
||||
|
||||
namespace Spectre.Console
|
||||
namespace Spectre.Console;
|
||||
|
||||
internal sealed class ListPromptRenderHook<T> : IRenderHook
|
||||
where T : notnull
|
||||
{
|
||||
internal sealed class ListPromptRenderHook<T> : IRenderHook
|
||||
where T : notnull
|
||||
private readonly IAnsiConsole _console;
|
||||
private readonly Func<IRenderable> _builder;
|
||||
private readonly LiveRenderable _live;
|
||||
private readonly object _lock;
|
||||
private bool _dirty;
|
||||
|
||||
public ListPromptRenderHook(
|
||||
IAnsiConsole console,
|
||||
Func<IRenderable> builder)
|
||||
{
|
||||
private readonly IAnsiConsole _console;
|
||||
private readonly Func<IRenderable> _builder;
|
||||
private readonly LiveRenderable _live;
|
||||
private readonly object _lock;
|
||||
private bool _dirty;
|
||||
_console = console ?? throw new ArgumentNullException(nameof(console));
|
||||
_builder = builder ?? throw new ArgumentNullException(nameof(builder));
|
||||
|
||||
public ListPromptRenderHook(
|
||||
IAnsiConsole console,
|
||||
Func<IRenderable> builder)
|
||||
_live = new LiveRenderable(console);
|
||||
_lock = new object();
|
||||
_dirty = true;
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
_console.Write(_live.RestoreCursor());
|
||||
}
|
||||
|
||||
public void Refresh()
|
||||
{
|
||||
_dirty = true;
|
||||
_console.Write(new ControlCode(string.Empty));
|
||||
}
|
||||
|
||||
public IEnumerable<IRenderable> Process(RenderContext context, IEnumerable<IRenderable> renderables)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
_console = console ?? throw new ArgumentNullException(nameof(console));
|
||||
_builder = builder ?? throw new ArgumentNullException(nameof(builder));
|
||||
|
||||
_live = new LiveRenderable(console);
|
||||
_lock = new object();
|
||||
_dirty = true;
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
_console.Write(_live.RestoreCursor());
|
||||
}
|
||||
|
||||
public void Refresh()
|
||||
{
|
||||
_dirty = true;
|
||||
_console.Write(new ControlCode(string.Empty));
|
||||
}
|
||||
|
||||
public IEnumerable<IRenderable> Process(RenderContext context, IEnumerable<IRenderable> renderables)
|
||||
{
|
||||
lock (_lock)
|
||||
if (!_live.HasRenderable || _dirty)
|
||||
{
|
||||
if (!_live.HasRenderable || _dirty)
|
||||
{
|
||||
_live.SetRenderable(_builder());
|
||||
_dirty = false;
|
||||
}
|
||||
|
||||
yield return _live.PositionCursor();
|
||||
|
||||
foreach (var renderable in renderables)
|
||||
{
|
||||
yield return renderable;
|
||||
}
|
||||
|
||||
yield return _live;
|
||||
_live.SetRenderable(_builder());
|
||||
_dirty = false;
|
||||
}
|
||||
|
||||
yield return _live.PositionCursor();
|
||||
|
||||
foreach (var renderable in renderables)
|
||||
{
|
||||
yield return renderable;
|
||||
}
|
||||
|
||||
yield return _live;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,46 +1,45 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Spectre.Console
|
||||
namespace Spectre.Console;
|
||||
|
||||
internal sealed class ListPromptState<T>
|
||||
where T : notnull
|
||||
{
|
||||
internal sealed class ListPromptState<T>
|
||||
where T : notnull
|
||||
public int Index { get; private set; }
|
||||
public int ItemCount => Items.Count;
|
||||
public int PageSize { get; }
|
||||
public IReadOnlyList<ListPromptItem<T>> Items { get; }
|
||||
|
||||
public ListPromptItem<T> Current => Items[Index];
|
||||
|
||||
public ListPromptState(IReadOnlyList<ListPromptItem<T>> items, int pageSize)
|
||||
{
|
||||
public int Index { get; private set; }
|
||||
public int ItemCount => Items.Count;
|
||||
public int PageSize { get; }
|
||||
public IReadOnlyList<ListPromptItem<T>> Items { get; }
|
||||
|
||||
public ListPromptItem<T> Current => Items[Index];
|
||||
|
||||
public ListPromptState(IReadOnlyList<ListPromptItem<T>> items, int pageSize)
|
||||
{
|
||||
Index = 0;
|
||||
Items = items;
|
||||
PageSize = pageSize;
|
||||
}
|
||||
|
||||
public bool Update(ConsoleKey key)
|
||||
{
|
||||
var index = key switch
|
||||
{
|
||||
ConsoleKey.UpArrow => Index - 1,
|
||||
ConsoleKey.DownArrow => Index + 1,
|
||||
ConsoleKey.Home => 0,
|
||||
ConsoleKey.End => ItemCount - 1,
|
||||
ConsoleKey.PageUp => Index - PageSize,
|
||||
ConsoleKey.PageDown => Index + PageSize,
|
||||
_ => Index,
|
||||
};
|
||||
|
||||
index = index.Clamp(0, ItemCount - 1);
|
||||
if (index != Index)
|
||||
{
|
||||
Index = index;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
Index = 0;
|
||||
Items = items;
|
||||
PageSize = pageSize;
|
||||
}
|
||||
}
|
||||
|
||||
public bool Update(ConsoleKey key)
|
||||
{
|
||||
var index = key switch
|
||||
{
|
||||
ConsoleKey.UpArrow => Index - 1,
|
||||
ConsoleKey.DownArrow => Index + 1,
|
||||
ConsoleKey.Home => 0,
|
||||
ConsoleKey.End => ItemCount - 1,
|
||||
ConsoleKey.PageUp => Index - PageSize,
|
||||
ConsoleKey.PageDown => Index + PageSize,
|
||||
_ => Index,
|
||||
};
|
||||
|
||||
index = index.Clamp(0, ItemCount - 1);
|
||||
if (index != Index)
|
||||
{
|
||||
Index = index;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
@ -1,60 +1,59 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Spectre.Console
|
||||
{
|
||||
internal sealed class ListPromptTree<T>
|
||||
where T : notnull
|
||||
{
|
||||
private readonly List<ListPromptItem<T>> _roots;
|
||||
private readonly IEqualityComparer<T> _comparer;
|
||||
namespace Spectre.Console;
|
||||
|
||||
public ListPromptTree(IEqualityComparer<T> comparer)
|
||||
internal sealed class ListPromptTree<T>
|
||||
where T : notnull
|
||||
{
|
||||
private readonly List<ListPromptItem<T>> _roots;
|
||||
private readonly IEqualityComparer<T> _comparer;
|
||||
|
||||
public ListPromptTree(IEqualityComparer<T> comparer)
|
||||
{
|
||||
_roots = new List<ListPromptItem<T>>();
|
||||
_comparer = comparer ?? throw new ArgumentNullException(nameof(comparer));
|
||||
}
|
||||
|
||||
public ListPromptItem<T>? Find(T item)
|
||||
{
|
||||
var stack = new Stack<ListPromptItem<T>>(_roots);
|
||||
while (stack.Count > 0)
|
||||
{
|
||||
_roots = new List<ListPromptItem<T>>();
|
||||
_comparer = comparer ?? throw new ArgumentNullException(nameof(comparer));
|
||||
var current = stack.Pop();
|
||||
if (_comparer.Equals(item, current.Data))
|
||||
{
|
||||
return current;
|
||||
}
|
||||
|
||||
stack.PushRange(current.Children);
|
||||
}
|
||||
|
||||
public ListPromptItem<T>? Find(T item)
|
||||
return null;
|
||||
}
|
||||
|
||||
public void Add(ListPromptItem<T> node)
|
||||
{
|
||||
_roots.Add(node);
|
||||
}
|
||||
|
||||
public IEnumerable<ListPromptItem<T>> Traverse()
|
||||
{
|
||||
foreach (var root in _roots)
|
||||
{
|
||||
var stack = new Stack<ListPromptItem<T>>(_roots);
|
||||
var stack = new Stack<ListPromptItem<T>>();
|
||||
stack.Push(root);
|
||||
|
||||
while (stack.Count > 0)
|
||||
{
|
||||
var current = stack.Pop();
|
||||
if (_comparer.Equals(item, current.Data))
|
||||
yield return current;
|
||||
|
||||
foreach (var child in current.Children.ReverseEnumerable())
|
||||
{
|
||||
return current;
|
||||
}
|
||||
|
||||
stack.PushRange(current.Children);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public void Add(ListPromptItem<T> node)
|
||||
{
|
||||
_roots.Add(node);
|
||||
}
|
||||
|
||||
public IEnumerable<ListPromptItem<T>> Traverse()
|
||||
{
|
||||
foreach (var root in _roots)
|
||||
{
|
||||
var stack = new Stack<ListPromptItem<T>>();
|
||||
stack.Push(root);
|
||||
|
||||
while (stack.Count > 0)
|
||||
{
|
||||
var current = stack.Pop();
|
||||
yield return current;
|
||||
|
||||
foreach (var child in current.Children.ReverseEnumerable())
|
||||
{
|
||||
stack.Push(child);
|
||||
}
|
||||
stack.Push(child);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -6,278 +6,277 @@ using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Spectre.Console.Rendering;
|
||||
|
||||
namespace Spectre.Console
|
||||
namespace Spectre.Console;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a multi selection list prompt.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
public sealed class MultiSelectionPrompt<T> : IPrompt<List<T>>, IListPromptStrategy<T>
|
||||
where T : notnull
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a multi selection list prompt.
|
||||
/// Gets or sets the title.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
public sealed class MultiSelectionPrompt<T> : IPrompt<List<T>>, IListPromptStrategy<T>
|
||||
where T : notnull
|
||||
public string? Title { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the page size.
|
||||
/// Defaults to <c>10</c>.
|
||||
/// </summary>
|
||||
public int PageSize { get; set; } = 10;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the highlight style of the selected choice.
|
||||
/// </summary>
|
||||
public Style? HighlightStyle { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the converter to get the display string for a choice. By default
|
||||
/// the corresponding <see cref="TypeConverter"/> is used.
|
||||
/// </summary>
|
||||
public Func<T, string>? Converter { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether or not
|
||||
/// at least one selection is required.
|
||||
/// </summary>
|
||||
public bool Required { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the text that will be displayed if there are more choices to show.
|
||||
/// </summary>
|
||||
public string? MoreChoicesText { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the text that instructs the user of how to select items.
|
||||
/// </summary>
|
||||
public string? InstructionsText { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the selection mode.
|
||||
/// Defaults to <see cref="SelectionMode.Leaf"/>.
|
||||
/// </summary>
|
||||
public SelectionMode Mode { get; set; } = SelectionMode.Leaf;
|
||||
|
||||
internal ListPromptTree<T> Tree { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="MultiSelectionPrompt{T}"/> class.
|
||||
/// </summary>
|
||||
/// <param name="comparer">
|
||||
/// The <see cref="IEqualityComparer{T}"/> implementation to use when comparing items,
|
||||
/// or <c>null</c> to use the default <see cref="IEqualityComparer{T}"/> for the type of the item.
|
||||
/// </param>
|
||||
public MultiSelectionPrompt(IEqualityComparer<T>? comparer = null)
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the title.
|
||||
/// </summary>
|
||||
public string? Title { get; set; }
|
||||
Tree = new ListPromptTree<T>(comparer ?? EqualityComparer<T>.Default);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the page size.
|
||||
/// Defaults to <c>10</c>.
|
||||
/// </summary>
|
||||
public int PageSize { get; set; } = 10;
|
||||
/// <summary>
|
||||
/// Adds a choice.
|
||||
/// </summary>
|
||||
/// <param name="item">The item to add.</param>
|
||||
/// <returns>A <see cref="IMultiSelectionItem{T}"/> so that multiple calls can be chained.</returns>
|
||||
public IMultiSelectionItem<T> AddChoice(T item)
|
||||
{
|
||||
var node = new ListPromptItem<T>(item);
|
||||
Tree.Add(node);
|
||||
return node;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the highlight style of the selected choice.
|
||||
/// </summary>
|
||||
public Style? HighlightStyle { get; set; }
|
||||
/// <inheritdoc/>
|
||||
public List<T> Show(IAnsiConsole console)
|
||||
{
|
||||
return ShowAsync(console, CancellationToken.None).GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the converter to get the display string for a choice. By default
|
||||
/// the corresponding <see cref="TypeConverter"/> is used.
|
||||
/// </summary>
|
||||
public Func<T, string>? Converter { get; set; }
|
||||
/// <inheritdoc/>
|
||||
public async Task<List<T>> ShowAsync(IAnsiConsole console, CancellationToken cancellationToken)
|
||||
{
|
||||
// Create the list prompt
|
||||
var prompt = new ListPrompt<T>(console, this);
|
||||
var result = await prompt.Show(Tree, cancellationToken, PageSize).ConfigureAwait(false);
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether or not
|
||||
/// at least one selection is required.
|
||||
/// </summary>
|
||||
public bool Required { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the text that will be displayed if there are more choices to show.
|
||||
/// </summary>
|
||||
public string? MoreChoicesText { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the text that instructs the user of how to select items.
|
||||
/// </summary>
|
||||
public string? InstructionsText { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the selection mode.
|
||||
/// Defaults to <see cref="SelectionMode.Leaf"/>.
|
||||
/// </summary>
|
||||
public SelectionMode Mode { get; set; } = SelectionMode.Leaf;
|
||||
|
||||
internal ListPromptTree<T> Tree { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="MultiSelectionPrompt{T}"/> class.
|
||||
/// </summary>
|
||||
/// <param name="comparer">
|
||||
/// The <see cref="IEqualityComparer{T}"/> implementation to use when comparing items,
|
||||
/// or <c>null</c> to use the default <see cref="IEqualityComparer{T}"/> for the type of the item.
|
||||
/// </param>
|
||||
public MultiSelectionPrompt(IEqualityComparer<T>? comparer = null)
|
||||
if (Mode == SelectionMode.Leaf)
|
||||
{
|
||||
Tree = new ListPromptTree<T>(comparer ?? EqualityComparer<T>.Default);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a choice.
|
||||
/// </summary>
|
||||
/// <param name="item">The item to add.</param>
|
||||
/// <returns>A <see cref="IMultiSelectionItem{T}"/> so that multiple calls can be chained.</returns>
|
||||
public IMultiSelectionItem<T> AddChoice(T item)
|
||||
{
|
||||
var node = new ListPromptItem<T>(item);
|
||||
Tree.Add(node);
|
||||
return node;
|
||||
}
|
||||
|
||||
/// <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 = await prompt.Show(Tree, cancellationToken, PageSize).ConfigureAwait(false);
|
||||
|
||||
if (Mode == SelectionMode.Leaf)
|
||||
{
|
||||
return result.Items
|
||||
.Where(x => x.IsSelected && x.Children.Count == 0)
|
||||
.Select(x => x.Data)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
return result.Items
|
||||
.Where(x => x.IsSelected)
|
||||
.Where(x => x.IsSelected && x.Children.Count == 0)
|
||||
.Select(x => x.Data)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns all parent items of the given <paramref name="item"/>.
|
||||
/// </summary>
|
||||
/// <param name="item">The item for which to find the parents.</param>
|
||||
/// <returns>The parent items, or an empty list, if the given item has no parents.</returns>
|
||||
public IEnumerable<T> GetParents(T item)
|
||||
{
|
||||
var promptItem = Tree.Find(item);
|
||||
if (promptItem == null)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(item), "Item not found in tree.");
|
||||
}
|
||||
|
||||
var parents = new List<ListPromptItem<T>>();
|
||||
while (promptItem.Parent != null)
|
||||
{
|
||||
promptItem = promptItem.Parent;
|
||||
parents.Add(promptItem);
|
||||
}
|
||||
|
||||
return parents
|
||||
.ReverseEnumerable()
|
||||
.Select(x => x.Data);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the parent item of the given <paramref name="item"/>.
|
||||
/// </summary>
|
||||
/// <param name="item">The item for which to find the parent.</param>
|
||||
/// <returns>The parent item, or <c>null</c> if the given item has no parent.</returns>
|
||||
public T? GetParent(T item)
|
||||
{
|
||||
return GetParents(item).LastOrDefault();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
ListPromptInputResult IListPromptStrategy<T>.HandleInput(ConsoleKeyInfo key, ListPromptState<T> state)
|
||||
{
|
||||
if (key.Key == ConsoleKey.Enter)
|
||||
{
|
||||
if (Required && state.Items.None(x => x.IsSelected))
|
||||
{
|
||||
// Selection not permitted
|
||||
return ListPromptInputResult.None;
|
||||
}
|
||||
|
||||
// Submit
|
||||
return ListPromptInputResult.Submit;
|
||||
}
|
||||
|
||||
if (key.Key == ConsoleKey.Spacebar)
|
||||
{
|
||||
var current = state.Items[state.Index];
|
||||
var select = !current.IsSelected;
|
||||
|
||||
if (Mode == SelectionMode.Leaf)
|
||||
{
|
||||
// Select the node and all its children
|
||||
foreach (var item in current.Traverse(includeSelf: true))
|
||||
{
|
||||
item.IsSelected = select;
|
||||
}
|
||||
|
||||
// Visit every parent and evaluate if its selection
|
||||
// status need to be updated
|
||||
var parent = current.Parent;
|
||||
while (parent != null)
|
||||
{
|
||||
parent.IsSelected = parent.Traverse(includeSelf: false).All(x => x.IsSelected);
|
||||
parent = parent.Parent;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
current.IsSelected = !current.IsSelected;
|
||||
}
|
||||
|
||||
// Refresh the list
|
||||
return ListPromptInputResult.Refresh;
|
||||
}
|
||||
|
||||
return ListPromptInputResult.None;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
int IListPromptStrategy<T>.CalculatePageSize(IAnsiConsole console, int totalItemCount, int requestedPageSize)
|
||||
{
|
||||
// The instructions take up two rows including a blank line
|
||||
var extra = 2;
|
||||
if (Title != null)
|
||||
{
|
||||
// Title takes up two rows including a blank line
|
||||
extra += 2;
|
||||
}
|
||||
|
||||
// Scrolling?
|
||||
if (totalItemCount > requestedPageSize)
|
||||
{
|
||||
// The scrolling instructions takes up one row
|
||||
extra++;
|
||||
}
|
||||
|
||||
var pageSize = requestedPageSize;
|
||||
if (pageSize > console.Profile.Height - extra)
|
||||
{
|
||||
pageSize = console.Profile.Height - extra;
|
||||
}
|
||||
|
||||
return pageSize;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
IRenderable IListPromptStrategy<T>.Render(IAnsiConsole console, bool scrollable, int cursorIndex, IEnumerable<(int Index, ListPromptItem<T> Node)> items)
|
||||
{
|
||||
var list = new List<IRenderable>();
|
||||
var highlightStyle = HighlightStyle ?? new Style(foreground: Color.Blue);
|
||||
|
||||
if (Title != null)
|
||||
{
|
||||
list.Add(new Markup(Title));
|
||||
}
|
||||
|
||||
var grid = new Grid();
|
||||
grid.AddColumn(new GridColumn().Padding(0, 0, 1, 0).NoWrap());
|
||||
|
||||
if (Title != null)
|
||||
{
|
||||
grid.AddEmptyRow();
|
||||
}
|
||||
|
||||
foreach (var item in items)
|
||||
{
|
||||
var current = item.Index == cursorIndex;
|
||||
var style = current ? highlightStyle : Style.Plain;
|
||||
|
||||
var indent = new string(' ', item.Node.Depth * 2);
|
||||
var prompt = item.Index == cursorIndex ? ListPromptConstants.Arrow : new string(' ', ListPromptConstants.Arrow.Length);
|
||||
|
||||
var text = (Converter ?? TypeConverterHelper.ConvertToString)?.Invoke(item.Node.Data) ?? item.Node.Data.ToString() ?? "?";
|
||||
if (current)
|
||||
{
|
||||
text = text.RemoveMarkup().EscapeMarkup();
|
||||
}
|
||||
|
||||
var checkbox = item.Node.IsSelected
|
||||
? (item.Node.IsGroup && Mode == SelectionMode.Leaf
|
||||
? ListPromptConstants.GroupSelectedCheckbox : ListPromptConstants.SelectedCheckbox)
|
||||
: ListPromptConstants.Checkbox;
|
||||
|
||||
grid.AddRow(new Markup(indent + prompt + " " + checkbox + " " + text, style));
|
||||
}
|
||||
|
||||
list.Add(grid);
|
||||
list.Add(Text.Empty);
|
||||
|
||||
if (scrollable)
|
||||
{
|
||||
// There are more choices
|
||||
list.Add(new Markup(MoreChoicesText ?? ListPromptConstants.MoreChoicesMarkup));
|
||||
}
|
||||
|
||||
// Instructions
|
||||
list.Add(new Markup(InstructionsText ?? ListPromptConstants.InstructionsMarkup));
|
||||
|
||||
// Combine all items
|
||||
return new Rows(list);
|
||||
}
|
||||
return result.Items
|
||||
.Where(x => x.IsSelected)
|
||||
.Select(x => x.Data)
|
||||
.ToList();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns all parent items of the given <paramref name="item"/>.
|
||||
/// </summary>
|
||||
/// <param name="item">The item for which to find the parents.</param>
|
||||
/// <returns>The parent items, or an empty list, if the given item has no parents.</returns>
|
||||
public IEnumerable<T> GetParents(T item)
|
||||
{
|
||||
var promptItem = Tree.Find(item);
|
||||
if (promptItem == null)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(item), "Item not found in tree.");
|
||||
}
|
||||
|
||||
var parents = new List<ListPromptItem<T>>();
|
||||
while (promptItem.Parent != null)
|
||||
{
|
||||
promptItem = promptItem.Parent;
|
||||
parents.Add(promptItem);
|
||||
}
|
||||
|
||||
return parents
|
||||
.ReverseEnumerable()
|
||||
.Select(x => x.Data);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the parent item of the given <paramref name="item"/>.
|
||||
/// </summary>
|
||||
/// <param name="item">The item for which to find the parent.</param>
|
||||
/// <returns>The parent item, or <c>null</c> if the given item has no parent.</returns>
|
||||
public T? GetParent(T item)
|
||||
{
|
||||
return GetParents(item).LastOrDefault();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
ListPromptInputResult IListPromptStrategy<T>.HandleInput(ConsoleKeyInfo key, ListPromptState<T> state)
|
||||
{
|
||||
if (key.Key == ConsoleKey.Enter)
|
||||
{
|
||||
if (Required && state.Items.None(x => x.IsSelected))
|
||||
{
|
||||
// Selection not permitted
|
||||
return ListPromptInputResult.None;
|
||||
}
|
||||
|
||||
// Submit
|
||||
return ListPromptInputResult.Submit;
|
||||
}
|
||||
|
||||
if (key.Key == ConsoleKey.Spacebar)
|
||||
{
|
||||
var current = state.Items[state.Index];
|
||||
var select = !current.IsSelected;
|
||||
|
||||
if (Mode == SelectionMode.Leaf)
|
||||
{
|
||||
// Select the node and all its children
|
||||
foreach (var item in current.Traverse(includeSelf: true))
|
||||
{
|
||||
item.IsSelected = select;
|
||||
}
|
||||
|
||||
// Visit every parent and evaluate if its selection
|
||||
// status need to be updated
|
||||
var parent = current.Parent;
|
||||
while (parent != null)
|
||||
{
|
||||
parent.IsSelected = parent.Traverse(includeSelf: false).All(x => x.IsSelected);
|
||||
parent = parent.Parent;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
current.IsSelected = !current.IsSelected;
|
||||
}
|
||||
|
||||
// Refresh the list
|
||||
return ListPromptInputResult.Refresh;
|
||||
}
|
||||
|
||||
return ListPromptInputResult.None;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
int IListPromptStrategy<T>.CalculatePageSize(IAnsiConsole console, int totalItemCount, int requestedPageSize)
|
||||
{
|
||||
// The instructions take up two rows including a blank line
|
||||
var extra = 2;
|
||||
if (Title != null)
|
||||
{
|
||||
// Title takes up two rows including a blank line
|
||||
extra += 2;
|
||||
}
|
||||
|
||||
// Scrolling?
|
||||
if (totalItemCount > requestedPageSize)
|
||||
{
|
||||
// The scrolling instructions takes up one row
|
||||
extra++;
|
||||
}
|
||||
|
||||
var pageSize = requestedPageSize;
|
||||
if (pageSize > console.Profile.Height - extra)
|
||||
{
|
||||
pageSize = console.Profile.Height - extra;
|
||||
}
|
||||
|
||||
return pageSize;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
IRenderable IListPromptStrategy<T>.Render(IAnsiConsole console, bool scrollable, int cursorIndex, IEnumerable<(int Index, ListPromptItem<T> Node)> items)
|
||||
{
|
||||
var list = new List<IRenderable>();
|
||||
var highlightStyle = HighlightStyle ?? new Style(foreground: Color.Blue);
|
||||
|
||||
if (Title != null)
|
||||
{
|
||||
list.Add(new Markup(Title));
|
||||
}
|
||||
|
||||
var grid = new Grid();
|
||||
grid.AddColumn(new GridColumn().Padding(0, 0, 1, 0).NoWrap());
|
||||
|
||||
if (Title != null)
|
||||
{
|
||||
grid.AddEmptyRow();
|
||||
}
|
||||
|
||||
foreach (var item in items)
|
||||
{
|
||||
var current = item.Index == cursorIndex;
|
||||
var style = current ? highlightStyle : Style.Plain;
|
||||
|
||||
var indent = new string(' ', item.Node.Depth * 2);
|
||||
var prompt = item.Index == cursorIndex ? ListPromptConstants.Arrow : new string(' ', ListPromptConstants.Arrow.Length);
|
||||
|
||||
var text = (Converter ?? TypeConverterHelper.ConvertToString)?.Invoke(item.Node.Data) ?? item.Node.Data.ToString() ?? "?";
|
||||
if (current)
|
||||
{
|
||||
text = text.RemoveMarkup().EscapeMarkup();
|
||||
}
|
||||
|
||||
var checkbox = item.Node.IsSelected
|
||||
? (item.Node.IsGroup && Mode == SelectionMode.Leaf
|
||||
? ListPromptConstants.GroupSelectedCheckbox : ListPromptConstants.SelectedCheckbox)
|
||||
: ListPromptConstants.Checkbox;
|
||||
|
||||
grid.AddRow(new Markup(indent + prompt + " " + checkbox + " " + text, style));
|
||||
}
|
||||
|
||||
list.Add(grid);
|
||||
list.Add(Text.Empty);
|
||||
|
||||
if (scrollable)
|
||||
{
|
||||
// There are more choices
|
||||
list.Add(new Markup(MoreChoicesText ?? ListPromptConstants.MoreChoicesMarkup));
|
||||
}
|
||||
|
||||
// Instructions
|
||||
list.Add(new Markup(InstructionsText ?? ListPromptConstants.InstructionsMarkup));
|
||||
|
||||
// Combine all items
|
||||
return new Rows(list);
|
||||
}
|
||||
}
|
@ -1,311 +1,310 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Spectre.Console
|
||||
namespace Spectre.Console;
|
||||
|
||||
/// <summary>
|
||||
/// Contains extension methods for <see cref="MultiSelectionPrompt{T}"/>.
|
||||
/// </summary>
|
||||
public static class MultiSelectionPromptExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Contains extension methods for <see cref="MultiSelectionPrompt{T}"/>.
|
||||
/// Sets the selection mode.
|
||||
/// </summary>
|
||||
public static class MultiSelectionPromptExtensions
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="mode">The selection mode.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static MultiSelectionPrompt<T> Mode<T>(this MultiSelectionPrompt<T> obj, SelectionMode mode)
|
||||
where T : notnull
|
||||
{
|
||||
/// <summary>
|
||||
/// Sets the selection mode.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="mode">The selection mode.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static MultiSelectionPrompt<T> Mode<T>(this MultiSelectionPrompt<T> obj, SelectionMode mode)
|
||||
where T : notnull
|
||||
if (obj is null)
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
obj.Mode = mode;
|
||||
return obj;
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a choice.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="choice">The choice to add.</param>
|
||||
/// <param name="configurator">The configurator for the choice.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static MultiSelectionPrompt<T> AddChoices<T>(this MultiSelectionPrompt<T> obj, T choice, Action<IMultiSelectionItem<T>> configurator)
|
||||
where T : notnull
|
||||
obj.Mode = mode;
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a choice.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="choice">The choice to add.</param>
|
||||
/// <param name="configurator">The configurator for the choice.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static MultiSelectionPrompt<T> AddChoices<T>(this MultiSelectionPrompt<T> obj, T choice, Action<IMultiSelectionItem<T>> configurator)
|
||||
where T : notnull
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
if (configurator is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(configurator));
|
||||
}
|
||||
|
||||
var result = obj.AddChoice(choice);
|
||||
configurator(result);
|
||||
|
||||
return obj;
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds multiple choices.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="choices">The choices to add.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static MultiSelectionPrompt<T> AddChoices<T>(this MultiSelectionPrompt<T> obj, params T[] choices)
|
||||
where T : notnull
|
||||
if (configurator is null)
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
foreach (var choice in choices)
|
||||
{
|
||||
obj.AddChoice(choice);
|
||||
}
|
||||
|
||||
return obj;
|
||||
throw new ArgumentNullException(nameof(configurator));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds multiple choices.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="choices">The choices to add.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static MultiSelectionPrompt<T> AddChoices<T>(this MultiSelectionPrompt<T> obj, IEnumerable<T> choices)
|
||||
where T : notnull
|
||||
var result = obj.AddChoice(choice);
|
||||
configurator(result);
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds multiple choices.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="choices">The choices to add.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static MultiSelectionPrompt<T> AddChoices<T>(this MultiSelectionPrompt<T> obj, params T[] choices)
|
||||
where T : notnull
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
foreach (var choice in choices)
|
||||
{
|
||||
obj.AddChoice(choice);
|
||||
}
|
||||
|
||||
return obj;
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds multiple grouped choices.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="group">The group.</param>
|
||||
/// <param name="choices">The choices to add.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static MultiSelectionPrompt<T> AddChoiceGroup<T>(this MultiSelectionPrompt<T> obj, T group, IEnumerable<T> choices)
|
||||
where T : notnull
|
||||
foreach (var choice in choices)
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
var root = obj.AddChoice(group);
|
||||
foreach (var choice in choices)
|
||||
{
|
||||
root.AddChild(choice);
|
||||
}
|
||||
|
||||
return obj;
|
||||
obj.AddChoice(choice);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Marks an item as selected.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="item">The item to select.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static MultiSelectionPrompt<T> Select<T>(this MultiSelectionPrompt<T> obj, T item)
|
||||
where T : notnull
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds multiple choices.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="choices">The choices to add.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static MultiSelectionPrompt<T> AddChoices<T>(this MultiSelectionPrompt<T> obj, IEnumerable<T> choices)
|
||||
where T : notnull
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
var node = obj.Tree.Find(item);
|
||||
node?.Select();
|
||||
|
||||
return obj;
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the title.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="title">The title markup text.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static MultiSelectionPrompt<T> Title<T>(this MultiSelectionPrompt<T> obj, string? title)
|
||||
where T : notnull
|
||||
foreach (var choice in choices)
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
obj.Title = title;
|
||||
return obj;
|
||||
obj.AddChoice(choice);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets how many choices that are displayed to the user.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="pageSize">The number of choices that are displayed to the user.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static MultiSelectionPrompt<T> PageSize<T>(this MultiSelectionPrompt<T> obj, int pageSize)
|
||||
where T : notnull
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds multiple grouped choices.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="group">The group.</param>
|
||||
/// <param name="choices">The choices to add.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static MultiSelectionPrompt<T> AddChoiceGroup<T>(this MultiSelectionPrompt<T> obj, T group, IEnumerable<T> choices)
|
||||
where T : notnull
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
if (pageSize <= 2)
|
||||
{
|
||||
throw new ArgumentException("Page size must be greater or equal to 3.", nameof(pageSize));
|
||||
}
|
||||
|
||||
obj.PageSize = pageSize;
|
||||
return obj;
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the highlight style of the selected choice.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="highlightStyle">The highlight style of the selected choice.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static MultiSelectionPrompt<T> HighlightStyle<T>(this MultiSelectionPrompt<T> obj, Style highlightStyle)
|
||||
where T : notnull
|
||||
var root = obj.AddChoice(group);
|
||||
foreach (var choice in choices)
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
obj.HighlightStyle = highlightStyle;
|
||||
return obj;
|
||||
root.AddChild(choice);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the text that will be displayed if there are more choices to show.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="text">The text to display.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static MultiSelectionPrompt<T> MoreChoicesText<T>(this MultiSelectionPrompt<T> obj, string? text)
|
||||
where T : notnull
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
obj.MoreChoicesText = text;
|
||||
return obj;
|
||||
/// <summary>
|
||||
/// Marks an item as selected.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="item">The item to select.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static MultiSelectionPrompt<T> Select<T>(this MultiSelectionPrompt<T> obj, T item)
|
||||
where T : notnull
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the text that instructs the user of how to select items.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="text">The text to display.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static MultiSelectionPrompt<T> InstructionsText<T>(this MultiSelectionPrompt<T> obj, string? text)
|
||||
where T : notnull
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
var node = obj.Tree.Find(item);
|
||||
node?.Select();
|
||||
|
||||
obj.InstructionsText = text;
|
||||
return obj;
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the title.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="title">The title markup text.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static MultiSelectionPrompt<T> Title<T>(this MultiSelectionPrompt<T> obj, string? title)
|
||||
where T : notnull
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Requires no choice to be selected.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static MultiSelectionPrompt<T> NotRequired<T>(this MultiSelectionPrompt<T> obj)
|
||||
where T : notnull
|
||||
obj.Title = title;
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets how many choices that are displayed to the user.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="pageSize">The number of choices that are displayed to the user.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static MultiSelectionPrompt<T> PageSize<T>(this MultiSelectionPrompt<T> obj, int pageSize)
|
||||
where T : notnull
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
return Required(obj, false);
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Requires a choice to be selected.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static MultiSelectionPrompt<T> Required<T>(this MultiSelectionPrompt<T> obj)
|
||||
where T : notnull
|
||||
if (pageSize <= 2)
|
||||
{
|
||||
return Required(obj, true);
|
||||
throw new ArgumentException("Page size must be greater or equal to 3.", nameof(pageSize));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a value indicating whether or not at least one choice must be selected.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="required">Whether or not at least one choice must be selected.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static MultiSelectionPrompt<T> Required<T>(this MultiSelectionPrompt<T> obj, bool required)
|
||||
where T : notnull
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
obj.PageSize = pageSize;
|
||||
return obj;
|
||||
}
|
||||
|
||||
obj.Required = required;
|
||||
return obj;
|
||||
/// <summary>
|
||||
/// Sets the highlight style of the selected choice.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="highlightStyle">The highlight style of the selected choice.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static MultiSelectionPrompt<T> HighlightStyle<T>(this MultiSelectionPrompt<T> obj, Style highlightStyle)
|
||||
where T : notnull
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the function to create a display string for a given choice.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="displaySelector">The function to get a display string for a given choice.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static MultiSelectionPrompt<T> UseConverter<T>(this MultiSelectionPrompt<T> obj, Func<T, string>? displaySelector)
|
||||
where T : notnull
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
obj.HighlightStyle = highlightStyle;
|
||||
return obj;
|
||||
}
|
||||
|
||||
obj.Converter = displaySelector;
|
||||
return obj;
|
||||
/// <summary>
|
||||
/// Sets the text that will be displayed if there are more choices to show.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="text">The text to display.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static MultiSelectionPrompt<T> MoreChoicesText<T>(this MultiSelectionPrompt<T> obj, string? text)
|
||||
where T : notnull
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
obj.MoreChoicesText = text;
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the text that instructs the user of how to select items.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="text">The text to display.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static MultiSelectionPrompt<T> InstructionsText<T>(this MultiSelectionPrompt<T> obj, string? text)
|
||||
where T : notnull
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
obj.InstructionsText = text;
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Requires no choice to be selected.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static MultiSelectionPrompt<T> NotRequired<T>(this MultiSelectionPrompt<T> obj)
|
||||
where T : notnull
|
||||
{
|
||||
return Required(obj, false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Requires a choice to be selected.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static MultiSelectionPrompt<T> Required<T>(this MultiSelectionPrompt<T> obj)
|
||||
where T : notnull
|
||||
{
|
||||
return Required(obj, true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a value indicating whether or not at least one choice must be selected.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="required">Whether or not at least one choice must be selected.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static MultiSelectionPrompt<T> Required<T>(this MultiSelectionPrompt<T> obj, bool required)
|
||||
where T : notnull
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
obj.Required = required;
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the function to create a display string for a given choice.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="displaySelector">The function to get a display string for a given choice.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static MultiSelectionPrompt<T> UseConverter<T>(this MultiSelectionPrompt<T> obj, Func<T, string>? displaySelector)
|
||||
where T : notnull
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
obj.Converter = displaySelector;
|
||||
return obj;
|
||||
}
|
||||
}
|
@ -5,184 +5,183 @@ using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Spectre.Console.Rendering;
|
||||
|
||||
namespace Spectre.Console
|
||||
namespace Spectre.Console;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a single list prompt.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
public sealed class SelectionPrompt<T> : IPrompt<T>, IListPromptStrategy<T>
|
||||
where T : notnull
|
||||
{
|
||||
private readonly ListPromptTree<T> _tree;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a single list prompt.
|
||||
/// Gets or sets the title.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
public sealed class SelectionPrompt<T> : IPrompt<T>, IListPromptStrategy<T>
|
||||
where T : notnull
|
||||
public string? Title { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the page size.
|
||||
/// Defaults to <c>10</c>.
|
||||
/// </summary>
|
||||
public int PageSize { get; set; } = 10;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the highlight style of the selected choice.
|
||||
/// </summary>
|
||||
public Style? HighlightStyle { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the style of a disabled choice.
|
||||
/// </summary>
|
||||
public Style? DisabledStyle { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the converter to get the display string for a choice. By default
|
||||
/// the corresponding <see cref="TypeConverter"/> is used.
|
||||
/// </summary>
|
||||
public Func<T, string>? Converter { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the text that will be displayed if there are more choices to show.
|
||||
/// </summary>
|
||||
public string? MoreChoicesText { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the selection mode.
|
||||
/// Defaults to <see cref="SelectionMode.Leaf"/>.
|
||||
/// </summary>
|
||||
public SelectionMode Mode { get; set; } = SelectionMode.Leaf;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="SelectionPrompt{T}"/> class.
|
||||
/// </summary>
|
||||
public SelectionPrompt()
|
||||
{
|
||||
private readonly ListPromptTree<T> _tree;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the title.
|
||||
/// </summary>
|
||||
public string? Title { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the page size.
|
||||
/// Defaults to <c>10</c>.
|
||||
/// </summary>
|
||||
public int PageSize { get; set; } = 10;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the highlight style of the selected choice.
|
||||
/// </summary>
|
||||
public Style? HighlightStyle { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the style of a disabled choice.
|
||||
/// </summary>
|
||||
public Style? DisabledStyle { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the converter to get the display string for a choice. By default
|
||||
/// the corresponding <see cref="TypeConverter"/> is used.
|
||||
/// </summary>
|
||||
public Func<T, string>? Converter { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the text that will be displayed if there are more choices to show.
|
||||
/// </summary>
|
||||
public string? MoreChoicesText { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the selection mode.
|
||||
/// Defaults to <see cref="SelectionMode.Leaf"/>.
|
||||
/// </summary>
|
||||
public SelectionMode Mode { get; set; } = SelectionMode.Leaf;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="SelectionPrompt{T}"/> class.
|
||||
/// </summary>
|
||||
public SelectionPrompt()
|
||||
{
|
||||
_tree = new ListPromptTree<T>(EqualityComparer<T>.Default);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a choice.
|
||||
/// </summary>
|
||||
/// <param name="item">The item to add.</param>
|
||||
/// <returns>A <see cref="ISelectionItem{T}"/> so that multiple calls can be chained.</returns>
|
||||
public ISelectionItem<T> AddChoice(T item)
|
||||
{
|
||||
var node = new ListPromptItem<T>(item);
|
||||
_tree.Add(node);
|
||||
return node;
|
||||
}
|
||||
|
||||
/// <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 = await prompt.Show(_tree, cancellationToken, PageSize).ConfigureAwait(false);
|
||||
|
||||
// Return the selected item
|
||||
return result.Items[result.Index].Data;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
ListPromptInputResult IListPromptStrategy<T>.HandleInput(ConsoleKeyInfo key, ListPromptState<T> state)
|
||||
{
|
||||
if (key.Key == ConsoleKey.Enter || key.Key == ConsoleKey.Spacebar)
|
||||
{
|
||||
// Selecting a non leaf in "leaf mode" is not allowed
|
||||
if (state.Current.IsGroup && Mode == SelectionMode.Leaf)
|
||||
{
|
||||
return ListPromptInputResult.None;
|
||||
}
|
||||
|
||||
return ListPromptInputResult.Submit;
|
||||
}
|
||||
|
||||
return ListPromptInputResult.None;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
int IListPromptStrategy<T>.CalculatePageSize(IAnsiConsole console, int totalItemCount, int requestedPageSize)
|
||||
{
|
||||
var extra = 0;
|
||||
|
||||
if (Title != null)
|
||||
{
|
||||
// Title takes up two rows including a blank line
|
||||
extra += 2;
|
||||
}
|
||||
|
||||
// Scrolling?
|
||||
if (totalItemCount > requestedPageSize)
|
||||
{
|
||||
// The scrolling instructions takes up two rows
|
||||
extra += 2;
|
||||
}
|
||||
|
||||
if (requestedPageSize > console.Profile.Height - extra)
|
||||
{
|
||||
return console.Profile.Height - extra;
|
||||
}
|
||||
|
||||
return requestedPageSize;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
IRenderable IListPromptStrategy<T>.Render(IAnsiConsole console, bool scrollable, int cursorIndex, IEnumerable<(int Index, ListPromptItem<T> Node)> items)
|
||||
{
|
||||
var list = new List<IRenderable>();
|
||||
var disabledStyle = DisabledStyle ?? new Style(foreground: Color.Grey);
|
||||
var highlightStyle = HighlightStyle ?? new Style(foreground: Color.Blue);
|
||||
|
||||
if (Title != null)
|
||||
{
|
||||
list.Add(new Markup(Title));
|
||||
}
|
||||
|
||||
var grid = new Grid();
|
||||
grid.AddColumn(new GridColumn().Padding(0, 0, 1, 0).NoWrap());
|
||||
|
||||
if (Title != null)
|
||||
{
|
||||
grid.AddEmptyRow();
|
||||
}
|
||||
|
||||
foreach (var item in items)
|
||||
{
|
||||
var current = item.Index == cursorIndex;
|
||||
var prompt = item.Index == cursorIndex ? ListPromptConstants.Arrow : new string(' ', ListPromptConstants.Arrow.Length);
|
||||
var style = item.Node.IsGroup && Mode == SelectionMode.Leaf
|
||||
? disabledStyle
|
||||
: current ? highlightStyle : Style.Plain;
|
||||
|
||||
var indent = new string(' ', item.Node.Depth * 2);
|
||||
|
||||
var text = (Converter ?? TypeConverterHelper.ConvertToString)?.Invoke(item.Node.Data) ?? item.Node.Data.ToString() ?? "?";
|
||||
if (current)
|
||||
{
|
||||
text = text.RemoveMarkup().EscapeMarkup();
|
||||
}
|
||||
|
||||
grid.AddRow(new Markup(indent + prompt + " " + text, style));
|
||||
}
|
||||
|
||||
list.Add(grid);
|
||||
|
||||
if (scrollable)
|
||||
{
|
||||
// (Move up and down to reveal more choices)
|
||||
list.Add(Text.Empty);
|
||||
list.Add(new Markup(MoreChoicesText ?? ListPromptConstants.MoreChoicesMarkup));
|
||||
}
|
||||
|
||||
return new Rows(list);
|
||||
}
|
||||
_tree = new ListPromptTree<T>(EqualityComparer<T>.Default);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a choice.
|
||||
/// </summary>
|
||||
/// <param name="item">The item to add.</param>
|
||||
/// <returns>A <see cref="ISelectionItem{T}"/> so that multiple calls can be chained.</returns>
|
||||
public ISelectionItem<T> AddChoice(T item)
|
||||
{
|
||||
var node = new ListPromptItem<T>(item);
|
||||
_tree.Add(node);
|
||||
return node;
|
||||
}
|
||||
|
||||
/// <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 = await prompt.Show(_tree, cancellationToken, PageSize).ConfigureAwait(false);
|
||||
|
||||
// Return the selected item
|
||||
return result.Items[result.Index].Data;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
ListPromptInputResult IListPromptStrategy<T>.HandleInput(ConsoleKeyInfo key, ListPromptState<T> state)
|
||||
{
|
||||
if (key.Key == ConsoleKey.Enter || key.Key == ConsoleKey.Spacebar)
|
||||
{
|
||||
// Selecting a non leaf in "leaf mode" is not allowed
|
||||
if (state.Current.IsGroup && Mode == SelectionMode.Leaf)
|
||||
{
|
||||
return ListPromptInputResult.None;
|
||||
}
|
||||
|
||||
return ListPromptInputResult.Submit;
|
||||
}
|
||||
|
||||
return ListPromptInputResult.None;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
int IListPromptStrategy<T>.CalculatePageSize(IAnsiConsole console, int totalItemCount, int requestedPageSize)
|
||||
{
|
||||
var extra = 0;
|
||||
|
||||
if (Title != null)
|
||||
{
|
||||
// Title takes up two rows including a blank line
|
||||
extra += 2;
|
||||
}
|
||||
|
||||
// Scrolling?
|
||||
if (totalItemCount > requestedPageSize)
|
||||
{
|
||||
// The scrolling instructions takes up two rows
|
||||
extra += 2;
|
||||
}
|
||||
|
||||
if (requestedPageSize > console.Profile.Height - extra)
|
||||
{
|
||||
return console.Profile.Height - extra;
|
||||
}
|
||||
|
||||
return requestedPageSize;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
IRenderable IListPromptStrategy<T>.Render(IAnsiConsole console, bool scrollable, int cursorIndex, IEnumerable<(int Index, ListPromptItem<T> Node)> items)
|
||||
{
|
||||
var list = new List<IRenderable>();
|
||||
var disabledStyle = DisabledStyle ?? new Style(foreground: Color.Grey);
|
||||
var highlightStyle = HighlightStyle ?? new Style(foreground: Color.Blue);
|
||||
|
||||
if (Title != null)
|
||||
{
|
||||
list.Add(new Markup(Title));
|
||||
}
|
||||
|
||||
var grid = new Grid();
|
||||
grid.AddColumn(new GridColumn().Padding(0, 0, 1, 0).NoWrap());
|
||||
|
||||
if (Title != null)
|
||||
{
|
||||
grid.AddEmptyRow();
|
||||
}
|
||||
|
||||
foreach (var item in items)
|
||||
{
|
||||
var current = item.Index == cursorIndex;
|
||||
var prompt = item.Index == cursorIndex ? ListPromptConstants.Arrow : new string(' ', ListPromptConstants.Arrow.Length);
|
||||
var style = item.Node.IsGroup && Mode == SelectionMode.Leaf
|
||||
? disabledStyle
|
||||
: current ? highlightStyle : Style.Plain;
|
||||
|
||||
var indent = new string(' ', item.Node.Depth * 2);
|
||||
|
||||
var text = (Converter ?? TypeConverterHelper.ConvertToString)?.Invoke(item.Node.Data) ?? item.Node.Data.ToString() ?? "?";
|
||||
if (current)
|
||||
{
|
||||
text = text.RemoveMarkup().EscapeMarkup();
|
||||
}
|
||||
|
||||
grid.AddRow(new Markup(indent + prompt + " " + text, style));
|
||||
}
|
||||
|
||||
list.Add(grid);
|
||||
|
||||
if (scrollable)
|
||||
{
|
||||
// (Move up and down to reveal more choices)
|
||||
list.Add(Text.Empty);
|
||||
list.Add(new Markup(MoreChoicesText ?? ListPromptConstants.MoreChoicesMarkup));
|
||||
}
|
||||
|
||||
return new Rows(list);
|
||||
}
|
||||
}
|
@ -1,201 +1,200 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Spectre.Console
|
||||
namespace Spectre.Console;
|
||||
|
||||
/// <summary>
|
||||
/// Contains extension methods for <see cref="SelectionPrompt{T}"/>.
|
||||
/// </summary>
|
||||
public static class SelectionPromptExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Contains extension methods for <see cref="SelectionPrompt{T}"/>.
|
||||
/// Sets the selection mode.
|
||||
/// </summary>
|
||||
public static class SelectionPromptExtensions
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="mode">The selection mode.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static SelectionPrompt<T> Mode<T>(this SelectionPrompt<T> obj, SelectionMode mode)
|
||||
where T : notnull
|
||||
{
|
||||
/// <summary>
|
||||
/// Sets the selection mode.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="mode">The selection mode.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static SelectionPrompt<T> Mode<T>(this SelectionPrompt<T> obj, SelectionMode mode)
|
||||
where T : notnull
|
||||
if (obj is null)
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
obj.Mode = mode;
|
||||
return obj;
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds multiple choices.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="choices">The choices to add.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static SelectionPrompt<T> AddChoices<T>(this SelectionPrompt<T> obj, params T[] choices)
|
||||
where T : notnull
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
foreach (var choice in choices)
|
||||
{
|
||||
obj.AddChoice(choice);
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds multiple choices.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="choices">The choices to add.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static SelectionPrompt<T> AddChoices<T>(this SelectionPrompt<T> obj, IEnumerable<T> choices)
|
||||
where T : notnull
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
foreach (var choice in choices)
|
||||
{
|
||||
obj.AddChoice(choice);
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds multiple grouped choices.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="group">The group.</param>
|
||||
/// <param name="choices">The choices to add.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static SelectionPrompt<T> AddChoiceGroup<T>(this SelectionPrompt<T> obj, T group, IEnumerable<T> choices)
|
||||
where T : notnull
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
var root = obj.AddChoice(group);
|
||||
foreach (var choice in choices)
|
||||
{
|
||||
root.AddChild(choice);
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the title.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="title">The title markup text.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static SelectionPrompt<T> Title<T>(this SelectionPrompt<T> obj, string? title)
|
||||
where T : notnull
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
obj.Title = title;
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets how many choices that are displayed to the user.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="pageSize">The number of choices that are displayed to the user.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static SelectionPrompt<T> PageSize<T>(this SelectionPrompt<T> obj, int pageSize)
|
||||
where T : notnull
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
if (pageSize <= 2)
|
||||
{
|
||||
throw new ArgumentException("Page size must be greater or equal to 3.", nameof(pageSize));
|
||||
}
|
||||
|
||||
obj.PageSize = pageSize;
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the highlight style of the selected choice.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="highlightStyle">The highlight style of the selected choice.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static SelectionPrompt<T> HighlightStyle<T>(this SelectionPrompt<T> obj, Style highlightStyle)
|
||||
where T : notnull
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
obj.HighlightStyle = highlightStyle;
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the text that will be displayed if there are more choices to show.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="text">The text to display.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static SelectionPrompt<T> MoreChoicesText<T>(this SelectionPrompt<T> obj, string? text)
|
||||
where T : notnull
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
obj.MoreChoicesText = text;
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the function to create a display string for a given choice.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="displaySelector">The function to get a display string for a given choice.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static SelectionPrompt<T> UseConverter<T>(this SelectionPrompt<T> obj, Func<T, string>? displaySelector)
|
||||
where T : notnull
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
obj.Converter = displaySelector;
|
||||
return obj;
|
||||
}
|
||||
obj.Mode = mode;
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds multiple choices.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="choices">The choices to add.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static SelectionPrompt<T> AddChoices<T>(this SelectionPrompt<T> obj, params T[] choices)
|
||||
where T : notnull
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
foreach (var choice in choices)
|
||||
{
|
||||
obj.AddChoice(choice);
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds multiple choices.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="choices">The choices to add.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static SelectionPrompt<T> AddChoices<T>(this SelectionPrompt<T> obj, IEnumerable<T> choices)
|
||||
where T : notnull
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
foreach (var choice in choices)
|
||||
{
|
||||
obj.AddChoice(choice);
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds multiple grouped choices.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="group">The group.</param>
|
||||
/// <param name="choices">The choices to add.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static SelectionPrompt<T> AddChoiceGroup<T>(this SelectionPrompt<T> obj, T group, IEnumerable<T> choices)
|
||||
where T : notnull
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
var root = obj.AddChoice(group);
|
||||
foreach (var choice in choices)
|
||||
{
|
||||
root.AddChild(choice);
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the title.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="title">The title markup text.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static SelectionPrompt<T> Title<T>(this SelectionPrompt<T> obj, string? title)
|
||||
where T : notnull
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
obj.Title = title;
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets how many choices that are displayed to the user.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="pageSize">The number of choices that are displayed to the user.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static SelectionPrompt<T> PageSize<T>(this SelectionPrompt<T> obj, int pageSize)
|
||||
where T : notnull
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
if (pageSize <= 2)
|
||||
{
|
||||
throw new ArgumentException("Page size must be greater or equal to 3.", nameof(pageSize));
|
||||
}
|
||||
|
||||
obj.PageSize = pageSize;
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the highlight style of the selected choice.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="highlightStyle">The highlight style of the selected choice.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static SelectionPrompt<T> HighlightStyle<T>(this SelectionPrompt<T> obj, Style highlightStyle)
|
||||
where T : notnull
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
obj.HighlightStyle = highlightStyle;
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the text that will be displayed if there are more choices to show.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="text">The text to display.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static SelectionPrompt<T> MoreChoicesText<T>(this SelectionPrompt<T> obj, string? text)
|
||||
where T : notnull
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
obj.MoreChoicesText = text;
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the function to create a display string for a given choice.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="displaySelector">The function to get a display string for a given choice.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static SelectionPrompt<T> UseConverter<T>(this SelectionPrompt<T> obj, Func<T, string>? displaySelector)
|
||||
where T : notnull
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
obj.Converter = displaySelector;
|
||||
return obj;
|
||||
}
|
||||
}
|
@ -1,19 +1,18 @@
|
||||
namespace Spectre.Console
|
||||
namespace Spectre.Console;
|
||||
|
||||
/// <summary>
|
||||
/// Represents how selections are made in a hierarchical prompt.
|
||||
/// </summary>
|
||||
public enum SelectionMode
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents how selections are made in a hierarchical prompt.
|
||||
/// Will only return lead nodes in results.
|
||||
/// </summary>
|
||||
public enum SelectionMode
|
||||
{
|
||||
/// <summary>
|
||||
/// Will only return lead nodes in results.
|
||||
/// </summary>
|
||||
Leaf = 0,
|
||||
Leaf = 0,
|
||||
|
||||
/// <summary>
|
||||
/// Allows selection of parent nodes, but each node
|
||||
/// is independent of its parent and children.
|
||||
/// </summary>
|
||||
Independent = 1,
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Allows selection of parent nodes, but each node
|
||||
/// is independent of its parent and children.
|
||||
/// </summary>
|
||||
Independent = 1,
|
||||
}
|
@ -8,227 +8,226 @@ using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Spectre.Console
|
||||
namespace Spectre.Console;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a prompt.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
public sealed class TextPrompt<T> : IPrompt<T>
|
||||
{
|
||||
private readonly string _prompt;
|
||||
private readonly StringComparer? _comparer;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a prompt.
|
||||
/// Gets or sets the prompt style.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
public sealed class TextPrompt<T> : IPrompt<T>
|
||||
public Style? PromptStyle { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the list of choices.
|
||||
/// </summary>
|
||||
public List<T> Choices { get; } = new List<T>();
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the message for invalid choices.
|
||||
/// </summary>
|
||||
public string InvalidChoiceMessage { get; set; } = "[red]Please select one of the available options[/]";
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether input should
|
||||
/// be hidden in the console.
|
||||
/// </summary>
|
||||
public bool IsSecret { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the validation error message.
|
||||
/// </summary>
|
||||
public string ValidationErrorMessage { get; set; } = "[red]Invalid input[/]";
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether or not
|
||||
/// choices should be shown.
|
||||
/// </summary>
|
||||
public bool ShowChoices { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether or not
|
||||
/// default values should be shown.
|
||||
/// </summary>
|
||||
public bool ShowDefaultValue { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether or not an empty result is valid.
|
||||
/// </summary>
|
||||
public bool AllowEmpty { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the converter to get the display string for a choice. By default
|
||||
/// the corresponding <see cref="TypeConverter"/> is used.
|
||||
/// </summary>
|
||||
public Func<T, string>? Converter { get; set; } = TypeConverterHelper.ConvertToString;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the validator.
|
||||
/// </summary>
|
||||
public Func<T, ValidationResult>? Validator { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the default value.
|
||||
/// </summary>
|
||||
internal DefaultPromptValue<T>? DefaultValue { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="TextPrompt{T}"/> class.
|
||||
/// </summary>
|
||||
/// <param name="prompt">The prompt markup text.</param>
|
||||
/// <param name="comparer">The comparer used for choices.</param>
|
||||
public TextPrompt(string prompt, StringComparer? comparer = null)
|
||||
{
|
||||
private readonly string _prompt;
|
||||
private readonly StringComparer? _comparer;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the prompt style.
|
||||
/// </summary>
|
||||
public Style? PromptStyle { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the list of choices.
|
||||
/// </summary>
|
||||
public List<T> Choices { get; } = new List<T>();
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the message for invalid choices.
|
||||
/// </summary>
|
||||
public string InvalidChoiceMessage { get; set; } = "[red]Please select one of the available options[/]";
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether input should
|
||||
/// be hidden in the console.
|
||||
/// </summary>
|
||||
public bool IsSecret { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the validation error message.
|
||||
/// </summary>
|
||||
public string ValidationErrorMessage { get; set; } = "[red]Invalid input[/]";
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether or not
|
||||
/// choices should be shown.
|
||||
/// </summary>
|
||||
public bool ShowChoices { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether or not
|
||||
/// default values should be shown.
|
||||
/// </summary>
|
||||
public bool ShowDefaultValue { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether or not an empty result is valid.
|
||||
/// </summary>
|
||||
public bool AllowEmpty { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the converter to get the display string for a choice. By default
|
||||
/// the corresponding <see cref="TypeConverter"/> is used.
|
||||
/// </summary>
|
||||
public Func<T, string>? Converter { get; set; } = TypeConverterHelper.ConvertToString;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the validator.
|
||||
/// </summary>
|
||||
public Func<T, ValidationResult>? Validator { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the default value.
|
||||
/// </summary>
|
||||
internal DefaultPromptValue<T>? DefaultValue { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="TextPrompt{T}"/> class.
|
||||
/// </summary>
|
||||
/// <param name="prompt">The prompt markup text.</param>
|
||||
/// <param name="comparer">The comparer used for choices.</param>
|
||||
public TextPrompt(string prompt, StringComparer? comparer = null)
|
||||
{
|
||||
_prompt = prompt;
|
||||
_comparer = comparer;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Shows the prompt and requests input from the user.
|
||||
/// </summary>
|
||||
/// <param name="console">The console to show the prompt in.</param>
|
||||
/// <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 await console.RunExclusive(async () =>
|
||||
{
|
||||
var promptStyle = PromptStyle ?? Style.Plain;
|
||||
var converter = Converter ?? TypeConverterHelper.ConvertToString;
|
||||
var choices = Choices.Select(choice => converter(choice)).ToList();
|
||||
var choiceMap = Choices.ToDictionary(choice => converter(choice), choice => choice, _comparer);
|
||||
|
||||
WritePrompt(console);
|
||||
|
||||
while (true)
|
||||
{
|
||||
var input = await console.ReadLine(promptStyle, IsSecret, choices, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
// Nothing entered?
|
||||
if (string.IsNullOrWhiteSpace(input))
|
||||
{
|
||||
if (DefaultValue != null)
|
||||
{
|
||||
console.Write(IsSecret ? "******" : converter(DefaultValue.Value), promptStyle);
|
||||
console.WriteLine();
|
||||
return DefaultValue.Value;
|
||||
}
|
||||
|
||||
if (!AllowEmpty)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
console.WriteLine();
|
||||
|
||||
T? result;
|
||||
if (Choices.Count > 0)
|
||||
{
|
||||
if (choiceMap.TryGetValue(input, out result) && result != null)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
else
|
||||
{
|
||||
console.MarkupLine(InvalidChoiceMessage);
|
||||
WritePrompt(console);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else if (!TypeConverterHelper.TryConvertFromString<T>(input, out result) || result == null)
|
||||
{
|
||||
console.MarkupLine(ValidationErrorMessage);
|
||||
WritePrompt(console);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Run all validators
|
||||
if (!ValidateResult(result, out var validationMessage))
|
||||
{
|
||||
console.MarkupLine(validationMessage);
|
||||
WritePrompt(console);
|
||||
continue;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the prompt to the console.
|
||||
/// </summary>
|
||||
/// <param name="console">The console to write the prompt to.</param>
|
||||
private void WritePrompt(IAnsiConsole console)
|
||||
{
|
||||
if (console is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(console));
|
||||
}
|
||||
|
||||
var builder = new StringBuilder();
|
||||
builder.Append(_prompt.TrimEnd());
|
||||
|
||||
var appendSuffix = false;
|
||||
if (ShowChoices && Choices.Count > 0)
|
||||
{
|
||||
appendSuffix = true;
|
||||
var converter = Converter ?? TypeConverterHelper.ConvertToString;
|
||||
var choices = string.Join("/", Choices.Select(choice => converter(choice)));
|
||||
builder.AppendFormat(CultureInfo.InvariantCulture, " [blue][[{0}]][/]", choices);
|
||||
}
|
||||
|
||||
if (ShowDefaultValue && DefaultValue != null)
|
||||
{
|
||||
appendSuffix = true;
|
||||
var converter = Converter ?? TypeConverterHelper.ConvertToString;
|
||||
builder.AppendFormat(
|
||||
CultureInfo.InvariantCulture,
|
||||
" [green]({0})[/]",
|
||||
IsSecret ? "******" : converter(DefaultValue.Value));
|
||||
}
|
||||
|
||||
var markup = builder.ToString().Trim();
|
||||
if (appendSuffix)
|
||||
{
|
||||
markup += ":";
|
||||
}
|
||||
|
||||
console.Markup(markup + " ");
|
||||
}
|
||||
|
||||
private bool ValidateResult(T value, [NotNullWhen(false)] out string? message)
|
||||
{
|
||||
if (Validator != null)
|
||||
{
|
||||
var result = Validator(value);
|
||||
if (!result.Successful)
|
||||
{
|
||||
message = result.Message ?? ValidationErrorMessage;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
message = null;
|
||||
return true;
|
||||
}
|
||||
_prompt = prompt;
|
||||
_comparer = comparer;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Shows the prompt and requests input from the user.
|
||||
/// </summary>
|
||||
/// <param name="console">The console to show the prompt in.</param>
|
||||
/// <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 await console.RunExclusive(async () =>
|
||||
{
|
||||
var promptStyle = PromptStyle ?? Style.Plain;
|
||||
var converter = Converter ?? TypeConverterHelper.ConvertToString;
|
||||
var choices = Choices.Select(choice => converter(choice)).ToList();
|
||||
var choiceMap = Choices.ToDictionary(choice => converter(choice), choice => choice, _comparer);
|
||||
|
||||
WritePrompt(console);
|
||||
|
||||
while (true)
|
||||
{
|
||||
var input = await console.ReadLine(promptStyle, IsSecret, choices, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
// Nothing entered?
|
||||
if (string.IsNullOrWhiteSpace(input))
|
||||
{
|
||||
if (DefaultValue != null)
|
||||
{
|
||||
console.Write(IsSecret ? "******" : converter(DefaultValue.Value), promptStyle);
|
||||
console.WriteLine();
|
||||
return DefaultValue.Value;
|
||||
}
|
||||
|
||||
if (!AllowEmpty)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
console.WriteLine();
|
||||
|
||||
T? result;
|
||||
if (Choices.Count > 0)
|
||||
{
|
||||
if (choiceMap.TryGetValue(input, out result) && result != null)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
else
|
||||
{
|
||||
console.MarkupLine(InvalidChoiceMessage);
|
||||
WritePrompt(console);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else if (!TypeConverterHelper.TryConvertFromString<T>(input, out result) || result == null)
|
||||
{
|
||||
console.MarkupLine(ValidationErrorMessage);
|
||||
WritePrompt(console);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Run all validators
|
||||
if (!ValidateResult(result, out var validationMessage))
|
||||
{
|
||||
console.MarkupLine(validationMessage);
|
||||
WritePrompt(console);
|
||||
continue;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the prompt to the console.
|
||||
/// </summary>
|
||||
/// <param name="console">The console to write the prompt to.</param>
|
||||
private void WritePrompt(IAnsiConsole console)
|
||||
{
|
||||
if (console is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(console));
|
||||
}
|
||||
|
||||
var builder = new StringBuilder();
|
||||
builder.Append(_prompt.TrimEnd());
|
||||
|
||||
var appendSuffix = false;
|
||||
if (ShowChoices && Choices.Count > 0)
|
||||
{
|
||||
appendSuffix = true;
|
||||
var converter = Converter ?? TypeConverterHelper.ConvertToString;
|
||||
var choices = string.Join("/", Choices.Select(choice => converter(choice)));
|
||||
builder.AppendFormat(CultureInfo.InvariantCulture, " [blue][[{0}]][/]", choices);
|
||||
}
|
||||
|
||||
if (ShowDefaultValue && DefaultValue != null)
|
||||
{
|
||||
appendSuffix = true;
|
||||
var converter = Converter ?? TypeConverterHelper.ConvertToString;
|
||||
builder.AppendFormat(
|
||||
CultureInfo.InvariantCulture,
|
||||
" [green]({0})[/]",
|
||||
IsSecret ? "******" : converter(DefaultValue.Value));
|
||||
}
|
||||
|
||||
var markup = builder.ToString().Trim();
|
||||
if (appendSuffix)
|
||||
{
|
||||
markup += ":";
|
||||
}
|
||||
|
||||
console.Markup(markup + " ");
|
||||
}
|
||||
|
||||
private bool ValidateResult(T value, [NotNullWhen(false)] out string? message)
|
||||
{
|
||||
if (Validator != null)
|
||||
{
|
||||
var result = Validator(value);
|
||||
if (!result.Successful)
|
||||
{
|
||||
message = result.Message ?? ValidationErrorMessage;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
message = null;
|
||||
return true;
|
||||
}
|
||||
}
|
@ -1,312 +1,311 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Spectre.Console
|
||||
namespace Spectre.Console;
|
||||
|
||||
/// <summary>
|
||||
/// Contains extension methods for <see cref="TextPrompt{T}"/>.
|
||||
/// </summary>
|
||||
public static class TextPromptExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Contains extension methods for <see cref="TextPrompt{T}"/>.
|
||||
/// Allow empty input.
|
||||
/// </summary>
|
||||
public static class TextPromptExtensions
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static TextPrompt<T> AllowEmpty<T>(this TextPrompt<T> obj)
|
||||
{
|
||||
/// <summary>
|
||||
/// Allow empty input.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static TextPrompt<T> AllowEmpty<T>(this TextPrompt<T> obj)
|
||||
if (obj is null)
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
obj.AllowEmpty = true;
|
||||
return obj;
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the prompt style.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="style">The prompt style.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static TextPrompt<T> PromptStyle<T>(this TextPrompt<T> obj, Style style)
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
if (style is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(style));
|
||||
}
|
||||
|
||||
obj.PromptStyle = style;
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Show or hide choices.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="show">Whether or not choices should be visible.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static TextPrompt<T> ShowChoices<T>(this TextPrompt<T> obj, bool show)
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
obj.ShowChoices = show;
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Shows choices.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static TextPrompt<T> ShowChoices<T>(this TextPrompt<T> obj)
|
||||
{
|
||||
return ShowChoices(obj, true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Hides choices.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static TextPrompt<T> HideChoices<T>(this TextPrompt<T> obj)
|
||||
{
|
||||
return ShowChoices(obj, false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Show or hide the default value.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="show">Whether or not the default value should be visible.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static TextPrompt<T> ShowDefaultValue<T>(this TextPrompt<T> obj, bool show)
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
obj.ShowDefaultValue = show;
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Shows the default value.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static TextPrompt<T> ShowDefaultValue<T>(this TextPrompt<T> obj)
|
||||
{
|
||||
return ShowDefaultValue(obj, true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Hides the default value.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static TextPrompt<T> HideDefaultValue<T>(this TextPrompt<T> obj)
|
||||
{
|
||||
return ShowDefaultValue(obj, false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the validation error message for the prompt.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="message">The validation error message.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static TextPrompt<T> ValidationErrorMessage<T>(this TextPrompt<T> obj, string message)
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
obj.ValidationErrorMessage = message;
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the "invalid choice" message for the prompt.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="message">The "invalid choice" message.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static TextPrompt<T> InvalidChoiceMessage<T>(this TextPrompt<T> obj, string message)
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
obj.InvalidChoiceMessage = message;
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the default value of the prompt.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="value">The default value.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static TextPrompt<T> DefaultValue<T>(this TextPrompt<T> obj, T value)
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
obj.DefaultValue = new DefaultPromptValue<T>(value);
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the validation criteria for the prompt.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="validator">The validation criteria.</param>
|
||||
/// <param name="message">The validation error message.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static TextPrompt<T> Validate<T>(this TextPrompt<T> obj, Func<T, bool> validator, string? message = null)
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
obj.Validator = result =>
|
||||
{
|
||||
if (validator(result))
|
||||
{
|
||||
return ValidationResult.Success();
|
||||
}
|
||||
|
||||
return ValidationResult.Error(message);
|
||||
};
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the validation criteria for the prompt.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="validator">The validation criteria.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static TextPrompt<T> Validate<T>(this TextPrompt<T> obj, Func<T, ValidationResult> validator)
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
obj.Validator = validator;
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a choice to the prompt.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="choice">The choice to add.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static TextPrompt<T> AddChoice<T>(this TextPrompt<T> obj, T choice)
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
obj.Choices.Add(choice);
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds multiple choices to the prompt.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="choices">The choices to add.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static TextPrompt<T> AddChoices<T>(this TextPrompt<T> obj, IEnumerable<T> choices)
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
if (choices is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(choices));
|
||||
}
|
||||
|
||||
foreach (var choice in choices)
|
||||
{
|
||||
obj.Choices.Add(choice);
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Replaces prompt user input with asterisks in the console.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static TextPrompt<T> Secret<T>(this TextPrompt<T> obj)
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
obj.IsSecret = true;
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the function to create a display string for a given choice.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="displaySelector">The function to get a display string for a given choice.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static TextPrompt<T> WithConverter<T>(this TextPrompt<T> obj, Func<T, string>? displaySelector)
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
obj.Converter = displaySelector;
|
||||
return obj;
|
||||
}
|
||||
obj.AllowEmpty = true;
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the prompt style.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="style">The prompt style.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static TextPrompt<T> PromptStyle<T>(this TextPrompt<T> obj, Style style)
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
if (style is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(style));
|
||||
}
|
||||
|
||||
obj.PromptStyle = style;
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Show or hide choices.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="show">Whether or not choices should be visible.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static TextPrompt<T> ShowChoices<T>(this TextPrompt<T> obj, bool show)
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
obj.ShowChoices = show;
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Shows choices.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static TextPrompt<T> ShowChoices<T>(this TextPrompt<T> obj)
|
||||
{
|
||||
return ShowChoices(obj, true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Hides choices.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static TextPrompt<T> HideChoices<T>(this TextPrompt<T> obj)
|
||||
{
|
||||
return ShowChoices(obj, false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Show or hide the default value.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="show">Whether or not the default value should be visible.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static TextPrompt<T> ShowDefaultValue<T>(this TextPrompt<T> obj, bool show)
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
obj.ShowDefaultValue = show;
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Shows the default value.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static TextPrompt<T> ShowDefaultValue<T>(this TextPrompt<T> obj)
|
||||
{
|
||||
return ShowDefaultValue(obj, true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Hides the default value.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static TextPrompt<T> HideDefaultValue<T>(this TextPrompt<T> obj)
|
||||
{
|
||||
return ShowDefaultValue(obj, false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the validation error message for the prompt.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="message">The validation error message.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static TextPrompt<T> ValidationErrorMessage<T>(this TextPrompt<T> obj, string message)
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
obj.ValidationErrorMessage = message;
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the "invalid choice" message for the prompt.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="message">The "invalid choice" message.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static TextPrompt<T> InvalidChoiceMessage<T>(this TextPrompt<T> obj, string message)
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
obj.InvalidChoiceMessage = message;
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the default value of the prompt.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="value">The default value.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static TextPrompt<T> DefaultValue<T>(this TextPrompt<T> obj, T value)
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
obj.DefaultValue = new DefaultPromptValue<T>(value);
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the validation criteria for the prompt.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="validator">The validation criteria.</param>
|
||||
/// <param name="message">The validation error message.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static TextPrompt<T> Validate<T>(this TextPrompt<T> obj, Func<T, bool> validator, string? message = null)
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
obj.Validator = result =>
|
||||
{
|
||||
if (validator(result))
|
||||
{
|
||||
return ValidationResult.Success();
|
||||
}
|
||||
|
||||
return ValidationResult.Error(message);
|
||||
};
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the validation criteria for the prompt.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="validator">The validation criteria.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static TextPrompt<T> Validate<T>(this TextPrompt<T> obj, Func<T, ValidationResult> validator)
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
obj.Validator = validator;
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a choice to the prompt.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="choice">The choice to add.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static TextPrompt<T> AddChoice<T>(this TextPrompt<T> obj, T choice)
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
obj.Choices.Add(choice);
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds multiple choices to the prompt.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="choices">The choices to add.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static TextPrompt<T> AddChoices<T>(this TextPrompt<T> obj, IEnumerable<T> choices)
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
if (choices is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(choices));
|
||||
}
|
||||
|
||||
foreach (var choice in choices)
|
||||
{
|
||||
obj.Choices.Add(choice);
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Replaces prompt user input with asterisks in the console.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static TextPrompt<T> Secret<T>(this TextPrompt<T> obj)
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
obj.IsSecret = true;
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the function to create a display string for a given choice.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt type.</typeparam>
|
||||
/// <param name="obj">The prompt.</param>
|
||||
/// <param name="displaySelector">The function to get a display string for a given choice.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static TextPrompt<T> WithConverter<T>(this TextPrompt<T> obj, Func<T, string>? displaySelector)
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
obj.Converter = displaySelector;
|
||||
return obj;
|
||||
}
|
||||
}
|
@ -1,43 +1,42 @@
|
||||
namespace Spectre.Console
|
||||
namespace Spectre.Console;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a prompt validation result.
|
||||
/// </summary>
|
||||
public sealed class ValidationResult
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a prompt validation result.
|
||||
/// Gets a value indicating whether or not validation was successful.
|
||||
/// </summary>
|
||||
public sealed class ValidationResult
|
||||
public bool Successful { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the validation error message.
|
||||
/// </summary>
|
||||
public string? Message { get; }
|
||||
|
||||
private ValidationResult(bool successful, string? message)
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether or not validation was successful.
|
||||
/// </summary>
|
||||
public bool Successful { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the validation error message.
|
||||
/// </summary>
|
||||
public string? Message { get; }
|
||||
|
||||
private ValidationResult(bool successful, string? message)
|
||||
{
|
||||
Successful = successful;
|
||||
Message = message;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a <see cref="ValidationResult"/> representing successful validation.
|
||||
/// </summary>
|
||||
/// <returns>The validation result.</returns>
|
||||
public static ValidationResult Success()
|
||||
{
|
||||
return new ValidationResult(true, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a <see cref="ValidationResult"/> representing a validation error.
|
||||
/// </summary>
|
||||
/// <param name="message">The validation error message, or <c>null</c> to show the default validation error message.</param>
|
||||
/// <returns>The validation result.</returns>
|
||||
public static ValidationResult Error(string? message = null)
|
||||
{
|
||||
return new ValidationResult(false, message);
|
||||
}
|
||||
Successful = successful;
|
||||
Message = message;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a <see cref="ValidationResult"/> representing successful validation.
|
||||
/// </summary>
|
||||
/// <returns>The validation result.</returns>
|
||||
public static ValidationResult Success()
|
||||
{
|
||||
return new ValidationResult(true, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a <see cref="ValidationResult"/> representing a validation error.
|
||||
/// </summary>
|
||||
/// <param name="message">The validation error message, or <c>null</c> to show the default validation error message.</param>
|
||||
/// <returns>The validation result.</returns>
|
||||
public static ValidationResult Error(string? message = null)
|
||||
{
|
||||
return new ValidationResult(false, message);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user