Clean up Widgets

* Move /Widgets/Live/* to /Live/*
* Move /Widgets/Prompt/* to /Prompts/*
* Move tests and expectations to match the new locations
This commit is contained in:
Patrik Svensson
2021-07-12 09:39:20 +02:00
committed by Phil Scott
parent d532e1011f
commit fa5a1e88ec
114 changed files with 5 additions and 5 deletions

View File

@ -1,126 +0,0 @@
using System;
using System.Threading.Tasks;
using Spectre.Console.Rendering;
namespace Spectre.Console
{
/// <summary>
/// Represents a live display.
/// </summary>
public sealed class LiveDisplay
{
private readonly IAnsiConsole _console;
private readonly IRenderable _target;
/// <summary>
/// Gets or sets a value indicating whether or not the live display should
/// be cleared when it's done.
/// Defaults to <c>false</c>.
/// </summary>
public bool AutoClear { get; set; }
/// <summary>
/// Gets or sets the vertical overflow strategy.
/// </summary>
public VerticalOverflow Overflow { get; set; } = VerticalOverflow.Ellipsis;
/// <summary>
/// Gets or sets the vertical overflow cropping strategy.
/// </summary>
public VerticalOverflowCropping Cropping { get; set; } = VerticalOverflowCropping.Top;
/// <summary>
/// Initializes a new instance of the <see cref="LiveDisplay"/> class.
/// </summary>
/// <param name="console">The console.</param>
/// <param name="target">The target renderable to update.</param>
public LiveDisplay(IAnsiConsole console, IRenderable target)
{
_console = console ?? throw new ArgumentNullException(nameof(console));
_target = target ?? throw new ArgumentNullException(nameof(target));
}
/// <summary>
/// Starts the live display.
/// </summary>
/// <param name="action">The action to execute.</param>
public void Start(Action<LiveDisplayContext> action)
{
var task = StartAsync(ctx =>
{
action(ctx);
return Task.CompletedTask;
});
task.GetAwaiter().GetResult();
}
/// <summary>
/// Starts the live display.
/// </summary>
/// <typeparam name="T">The result type.</typeparam>
/// <param name="func">The action to execute.</param>
/// <returns>The result.</returns>
public T Start<T>(Func<LiveDisplayContext, T> func)
{
var task = StartAsync(ctx => Task.FromResult(func(ctx)));
return task.GetAwaiter().GetResult();
}
/// <summary>
/// Starts the live display.
/// </summary>
/// <param name="func">The action to execute.</param>
/// <returns>The result.</returns>
public async Task StartAsync(Func<LiveDisplayContext, Task> func)
{
if (func is null)
{
throw new ArgumentNullException(nameof(func));
}
_ = await StartAsync<object?>(async ctx =>
{
await func(ctx).ConfigureAwait(false);
return default;
}).ConfigureAwait(false);
}
/// <summary>
/// Starts the live display.
/// </summary>
/// <typeparam name="T">The result type.</typeparam>
/// <param name="func">The action to execute.</param>
/// <returns>The result.</returns>
public async Task<T> StartAsync<T>(Func<LiveDisplayContext, Task<T>> func)
{
if (func is null)
{
throw new ArgumentNullException(nameof(func));
}
return await _console.RunExclusive(async () =>
{
var context = new LiveDisplayContext(_console, _target);
context.SetOverflow(Overflow, Cropping);
var renderer = new LiveDisplayRenderer(_console, context);
renderer.Started();
try
{
using (new RenderHookScope(_console, renderer))
{
var result = await func(context).ConfigureAwait(false);
context.Refresh();
return result;
}
}
finally
{
renderer.Completed(AutoClear);
}
}).ConfigureAwait(false);
}
}
}

View File

@ -1,54 +0,0 @@
using System;
using Spectre.Console.Rendering;
namespace Spectre.Console
{
/// <summary>
/// Represents a context that can be used to interact with a <see cref="LiveDisplay"/>.
/// </summary>
public sealed class LiveDisplayContext
{
private readonly IAnsiConsole _console;
internal object Lock { get; }
internal LiveRenderable Live { get; }
internal LiveDisplayContext(IAnsiConsole console, IRenderable target)
{
_console = console ?? throw new ArgumentNullException(nameof(console));
Live = new LiveRenderable(_console, target);
Lock = new object();
}
/// <summary>
/// Updates the live display target.
/// </summary>
/// <param name="target">The new live display target.</param>
public void UpdateTarget(IRenderable? target)
{
lock (Lock)
{
Live.SetRenderable(target);
Refresh();
}
}
/// <summary>
/// Refreshes the live display.
/// </summary>
public void Refresh()
{
lock (Lock)
{
_console.Write(new ControlCode(string.Empty));
}
}
internal void SetOverflow(VerticalOverflow overflow, VerticalOverflowCropping cropping)
{
Live.Overflow = overflow;
Live.OverflowCropping = cropping;
}
}
}

View File

@ -1,62 +0,0 @@
using System.Collections.Generic;
using Spectre.Console.Rendering;
namespace Spectre.Console
{
internal sealed class LiveDisplayRenderer : IRenderHook
{
private readonly IAnsiConsole _console;
private readonly LiveDisplayContext _context;
public LiveDisplayRenderer(IAnsiConsole console, LiveDisplayContext context)
{
_console = console;
_context = context;
}
public void Started()
{
_console.Cursor.Hide();
}
public void Completed(bool autoclear)
{
lock (_context.Lock)
{
if (autoclear)
{
_console.Write(_context.Live.RestoreCursor());
}
else
{
if (_context.Live.HasRenderable && _context.Live.DidOverflow)
{
// Redraw the whole live renderable
_console.Write(_context.Live.RestoreCursor());
_context.Live.Overflow = VerticalOverflow.Visible;
_console.Write(_context.Live.Target);
}
_console.WriteLine();
}
_console.Cursor.Show();
}
}
public IEnumerable<IRenderable> Process(RenderContext context, IEnumerable<IRenderable> renderables)
{
lock (_context.Lock)
{
yield return _context.Live.PositionCursor();
foreach (var renderable in renderables)
{
yield return renderable;
}
yield return _context.Live;
}
}
}
}

View File

@ -1,41 +0,0 @@
using System;
using System.Globalization;
using Spectre.Console.Rendering;
namespace Spectre.Console
{
/// <summary>
/// A column showing download progress.
/// </summary>
public sealed class DownloadedColumn : ProgressColumn
{
/// <summary>
/// Gets or sets the <see cref="CultureInfo"/> to use.
/// </summary>
public CultureInfo? Culture { get; set; }
/// <inheritdoc/>
public override IRenderable Render(RenderContext context, ProgressTask task, TimeSpan deltaTime)
{
var total = new FileSize(task.MaxValue);
if (task.IsFinished)
{
return new Markup(string.Format(
"[green]{0} {1}[/]",
total.Format(Culture),
total.Suffix));
}
else
{
var downloaded = new FileSize(task.Value, total.Unit);
return new Markup(string.Format(
"{0}[grey]/[/]{1} [grey]{2}[/]",
downloaded.Format(Culture),
total.Format(Culture),
total.Suffix));
}
}
}
}

View File

@ -1,42 +0,0 @@
using System;
using Spectre.Console.Rendering;
namespace Spectre.Console
{
/// <summary>
/// A column showing the elapsed time of a task.
/// </summary>
public sealed class ElapsedTimeColumn : ProgressColumn
{
/// <inheritdoc/>
protected internal override bool NoWrap => true;
/// <summary>
/// Gets or sets the style of the remaining time text.
/// </summary>
public Style Style { get; set; } = new Style(foreground: Color.Blue);
/// <inheritdoc/>
public override IRenderable Render(RenderContext context, ProgressTask task, TimeSpan deltaTime)
{
var elapsed = task.ElapsedTime;
if (elapsed == null)
{
return new Markup("--:--:--");
}
if (elapsed.Value.TotalHours > 99)
{
return new Markup("**:**:**");
}
return new Text($"{elapsed.Value:hh\\:mm\\:ss}", Style ?? Style.Plain);
}
/// <inheritdoc/>
public override int? GetColumnWidth(RenderContext context)
{
return 8;
}
}
}

View File

@ -1,35 +0,0 @@
using System;
using Spectre.Console.Rendering;
namespace Spectre.Console
{
/// <summary>
/// A column showing task progress in percentage.
/// </summary>
public sealed class PercentageColumn : ProgressColumn
{
/// <summary>
/// Gets or sets the style for a non-complete task.
/// </summary>
public Style Style { get; set; } = Style.Plain;
/// <summary>
/// Gets or sets the style for a completed task.
/// </summary>
public Style CompletedStyle { get; set; } = new Style(foreground: Color.Green);
/// <inheritdoc/>
public override IRenderable Render(RenderContext context, ProgressTask task, TimeSpan deltaTime)
{
var percentage = (int)task.Percentage;
var style = percentage == 100 ? CompletedStyle : Style ?? Style.Plain;
return new Text($"{percentage}%", style).RightAligned();
}
/// <inheritdoc/>
public override int? GetColumnWidth(RenderContext context)
{
return 4;
}
}
}

View File

@ -1,52 +0,0 @@
using System;
using Spectre.Console.Rendering;
namespace Spectre.Console
{
/// <summary>
/// A column showing task progress as a progress bar.
/// </summary>
public sealed class ProgressBarColumn : ProgressColumn
{
/// <summary>
/// Gets or sets the width of the column.
/// </summary>
public int? Width { get; set; } = 40;
/// <summary>
/// Gets or sets the style of completed portions of the progress bar.
/// </summary>
public Style CompletedStyle { get; set; } = new Style(foreground: Color.Yellow);
/// <summary>
/// Gets or sets the style of a finished progress bar.
/// </summary>
public Style FinishedStyle { get; set; } = new Style(foreground: Color.Green);
/// <summary>
/// Gets or sets the style of remaining portions of the progress bar.
/// </summary>
public Style RemainingStyle { get; set; } = new Style(foreground: Color.Grey);
/// <summary>
/// Gets or sets the style of an indeterminate progress bar.
/// </summary>
public Style IndeterminateStyle { get; set; } = ProgressBar.DefaultPulseStyle;
/// <inheritdoc/>
public override IRenderable Render(RenderContext context, ProgressTask task, TimeSpan deltaTime)
{
return new ProgressBar
{
MaxValue = task.MaxValue,
Value = task.Value,
Width = Width,
CompletedStyle = CompletedStyle,
FinishedStyle = FinishedStyle,
RemainingStyle = RemainingStyle,
IndeterminateStyle = IndeterminateStyle,
IsIndeterminate = task.IsIndeterminate,
};
}
}
}

View File

@ -1,42 +0,0 @@
using System;
using Spectre.Console.Rendering;
namespace Spectre.Console
{
/// <summary>
/// A column showing the remaining time of a task.
/// </summary>
public sealed class RemainingTimeColumn : ProgressColumn
{
/// <inheritdoc/>
protected internal override bool NoWrap => true;
/// <summary>
/// Gets or sets the style of the remaining time text.
/// </summary>
public Style Style { get; set; } = new Style(foreground: Color.Blue);
/// <inheritdoc/>
public override IRenderable Render(RenderContext context, ProgressTask task, TimeSpan deltaTime)
{
var remaining = task.RemainingTime;
if (remaining == null)
{
return new Markup("--:--:--");
}
if (remaining.Value.TotalHours > 99)
{
return new Markup("**:**:**");
}
return new Text($"{remaining.Value:hh\\:mm\\:ss}", Style ?? Style.Plain);
}
/// <inheritdoc/>
public override int? GetColumnWidth(RenderContext context)
{
return 8;
}
}
}

View File

@ -1,155 +0,0 @@
using System;
using System.Linq;
using Spectre.Console.Rendering;
namespace Spectre.Console
{
/// <summary>
/// A column showing a spinner.
/// </summary>
public sealed class SpinnerColumn : ProgressColumn
{
private const string ACCUMULATED = "SPINNER_ACCUMULATED";
private const string INDEX = "SPINNER_INDEX";
private readonly object _lock;
private Spinner _spinner;
private int? _maxWidth;
private string? _completed;
private string? _pending;
/// <inheritdoc/>
protected internal override bool NoWrap => true;
/// <summary>
/// Gets or sets the <see cref="Console.Spinner"/>.
/// </summary>
public Spinner Spinner
{
get => _spinner;
set
{
lock (_lock)
{
_spinner = value ?? Spinner.Known.Default;
_maxWidth = null;
}
}
}
/// <summary>
/// Gets or sets the text that should be shown instead
/// of the spinner once a task completes.
/// </summary>
public string? CompletedText
{
get => _completed;
set
{
_completed = value;
_maxWidth = null;
}
}
/// <summary>
/// Gets or sets the text that should be shown instead
/// of the spinner before a task begins.
/// </summary>
public string? PendingText
{
get => _pending;
set
{
_pending = value;
_maxWidth = null;
}
}
/// <summary>
/// Gets or sets the completed style.
/// </summary>
public Style? CompletedStyle { get; set; }
/// <summary>
/// Gets or sets the pending style.
/// </summary>
public Style? PendingStyle { get; set; }
/// <summary>
/// Gets or sets the style of the spinner.
/// </summary>
public Style? Style { get; set; } = new Style(foreground: Color.Yellow);
/// <summary>
/// Initializes a new instance of the <see cref="SpinnerColumn"/> class.
/// </summary>
public SpinnerColumn()
: this(Spinner.Known.Default)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="SpinnerColumn"/> class.
/// </summary>
/// <param name="spinner">The spinner to use.</param>
public SpinnerColumn(Spinner spinner)
{
_spinner = spinner ?? throw new ArgumentNullException(nameof(spinner));
_lock = new object();
}
/// <inheritdoc/>
public override IRenderable Render(RenderContext context, ProgressTask task, TimeSpan deltaTime)
{
var useAscii = !context.Unicode && _spinner.IsUnicode;
var spinner = useAscii ? Spinner.Known.Ascii : _spinner ?? Spinner.Known.Default;
if (!task.IsStarted)
{
return new Markup(PendingText ?? " ", PendingStyle ?? Style.Plain);
}
if (task.IsFinished)
{
return new Markup(CompletedText ?? " ", CompletedStyle ?? Style.Plain);
}
var accumulated = task.State.Update<double>(ACCUMULATED, acc => acc + deltaTime.TotalMilliseconds);
if (accumulated >= spinner.Interval.TotalMilliseconds)
{
task.State.Update<double>(ACCUMULATED, _ => 0);
task.State.Update<int>(INDEX, index => index + 1);
}
var index = task.State.Get<int>(INDEX);
var frame = spinner.Frames[index % spinner.Frames.Count];
return new Markup(frame.EscapeMarkup(), Style ?? Style.Plain);
}
/// <inheritdoc/>
public override int? GetColumnWidth(RenderContext context)
{
return GetMaxWidth(context);
}
private int GetMaxWidth(RenderContext context)
{
lock (_lock)
{
if (_maxWidth == null)
{
var useAscii = !context.Unicode && _spinner.IsUnicode;
var spinner = useAscii ? Spinner.Known.Ascii : _spinner ?? Spinner.Known.Default;
_maxWidth = Math.Max(
Math.Max(
((IRenderable)new Markup(PendingText ?? " ")).Measure(context, int.MaxValue).Max,
((IRenderable)new Markup(CompletedText ?? " ")).Measure(context, int.MaxValue).Max),
spinner.Frames.Max(frame => Cell.GetCellLength(frame)));
}
return _maxWidth.Value;
}
}
}
}

View File

@ -1,26 +0,0 @@
using System;
using Spectre.Console.Rendering;
namespace Spectre.Console
{
/// <summary>
/// A column showing the task description.
/// </summary>
public sealed class TaskDescriptionColumn : ProgressColumn
{
/// <inheritdoc/>
protected internal override bool NoWrap => true;
/// <summary>
/// Gets or sets the alignment of the task description.
/// </summary>
public Justify Alignment { get; set; } = Justify.Right;
/// <inheritdoc/>
public override IRenderable Render(RenderContext context, ProgressTask task, TimeSpan deltaTime)
{
var text = task.Description?.RemoveNewLines()?.Trim();
return new Markup(text ?? string.Empty).Overflow(Overflow.Ellipsis).Alignment(Alignment);
}
}
}

View File

@ -1,29 +0,0 @@
using System;
using System.Globalization;
using Spectre.Console.Rendering;
namespace Spectre.Console
{
/// <summary>
/// A column showing transfer speed.
/// </summary>
public sealed class TransferSpeedColumn : ProgressColumn
{
/// <summary>
/// Gets or sets the <see cref="CultureInfo"/> to use.
/// </summary>
public CultureInfo? Culture { get; set; }
/// <inheritdoc/>
public override IRenderable Render(RenderContext context, ProgressTask task, TimeSpan deltaTime)
{
if (task.Speed == null)
{
return new Text("?/s");
}
var size = new FileSize(task.Speed.Value);
return new Markup(string.Format("{0}/s", size.ToString(suffix: true, Culture)));
}
}
}

View File

@ -1,174 +0,0 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Spectre.Console.Rendering;
namespace Spectre.Console
{
/// <summary>
/// Represents a task list.
/// </summary>
public sealed class Progress
{
private readonly IAnsiConsole _console;
/// <summary>
/// Gets or sets a value indicating whether or not task list should auto refresh.
/// Defaults to <c>true</c>.
/// </summary>
public bool AutoRefresh { get; set; } = true;
/// <summary>
/// Gets or sets a value indicating whether or not the task list should
/// be cleared once it completes.
/// Defaults to <c>false</c>.
/// </summary>
public bool AutoClear { get; set; }
/// <summary>
/// Gets or sets a value indicating whether or not the task list should
/// only include tasks not completed
/// Defaults to <c>false</c>.
/// </summary>
public bool HideCompleted { get; set; }
/// <summary>
/// Gets or sets the refresh rate if <c>AutoRefresh</c> is enabled.
/// Defaults to 10 times/second.
/// </summary>
public TimeSpan RefreshRate { get; set; } = TimeSpan.FromMilliseconds(100);
internal List<ProgressColumn> Columns { get; }
internal ProgressRenderer? FallbackRenderer { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="Progress"/> class.
/// </summary>
/// <param name="console">The console to render to.</param>
public Progress(IAnsiConsole console)
{
_console = console ?? throw new ArgumentNullException(nameof(console));
// Initialize with default columns
Columns = new List<ProgressColumn>
{
new TaskDescriptionColumn(),
new ProgressBarColumn(),
new PercentageColumn(),
};
}
/// <summary>
/// Starts the progress task list.
/// </summary>
/// <param name="action">The action to execute.</param>
public void Start(Action<ProgressContext> action)
{
var task = StartAsync(ctx =>
{
action(ctx);
return Task.CompletedTask;
});
task.GetAwaiter().GetResult();
}
/// <summary>
/// Starts the progress task list and returns a result.
/// </summary>
/// <typeparam name="T">The result type.</typeparam>
/// <param name="func">he action to execute.</param>
/// <returns>The result.</returns>
public T Start<T>(Func<ProgressContext, T> func)
{
var task = StartAsync(ctx => Task.FromResult(func(ctx)));
return task.GetAwaiter().GetResult();
}
/// <summary>
/// Starts the progress task list.
/// </summary>
/// <param name="action">The action to execute.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public async Task StartAsync(Func<ProgressContext, Task> action)
{
if (action is null)
{
throw new ArgumentNullException(nameof(action));
}
_ = await StartAsync<object?>(async progressContext =>
{
await action(progressContext).ConfigureAwait(false);
return default;
}).ConfigureAwait(false);
}
/// <summary>
/// Starts the progress task list and returns a result.
/// </summary>
/// <param name="action">The action to execute.</param>
/// <typeparam name="T">The result type of task.</typeparam>
/// <returns>A <see cref="Task{T}"/> representing the asynchronous operation.</returns>
public async Task<T> StartAsync<T>(Func<ProgressContext, Task<T>> action)
{
if (action is null)
{
throw new ArgumentNullException(nameof(action));
}
return await _console.RunExclusive(async () =>
{
var renderer = CreateRenderer();
renderer.Started();
T result;
try
{
using (new RenderHookScope(_console, renderer))
{
var context = new ProgressContext(_console, renderer);
if (AutoRefresh)
{
using (var thread = new ProgressRefreshThread(context, renderer.RefreshRate))
{
result = await action(context).ConfigureAwait(false);
}
}
else
{
result = await action(context).ConfigureAwait(false);
}
context.Refresh();
}
}
finally
{
renderer.Completed(AutoClear);
}
return result;
}).ConfigureAwait(false);
}
private ProgressRenderer CreateRenderer()
{
var caps = _console.Profile.Capabilities;
var interactive = caps.Interactive && caps.Ansi;
if (interactive)
{
var columns = new List<ProgressColumn>(Columns);
return new DefaultProgressRenderer(_console, columns, RefreshRate, HideCompleted);
}
else
{
return FallbackRenderer ?? new FallbackProgressRenderer();
}
}
}
}

View File

@ -1,35 +0,0 @@
using System;
using Spectre.Console.Rendering;
namespace Spectre.Console
{
/// <summary>
/// Represents a progress column.
/// </summary>
public abstract class ProgressColumn
{
/// <summary>
/// Gets a value indicating whether or not content should not wrap.
/// </summary>
protected internal virtual bool NoWrap { get; }
/// <summary>
/// Gets a renderable representing the column.
/// </summary>
/// <param name="context">The render context.</param>
/// <param name="task">The task.</param>
/// <param name="deltaTime">The elapsed time since last call.</param>
/// <returns>A renderable representing the column.</returns>
public abstract IRenderable Render(RenderContext context, ProgressTask task, TimeSpan deltaTime);
/// <summary>
/// Gets the width of the column.
/// </summary>
/// <param name="context">The context.</param>
/// <returns>The width of the column, or <c>null</c> to calculate.</returns>
public virtual int? GetColumnWidth(RenderContext context)
{
return null;
}
}
}

View File

@ -1,87 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace Spectre.Console
{
/// <summary>
/// Represents a context that can be used to interact with a <see cref="Progress"/>.
/// </summary>
public sealed class ProgressContext
{
private readonly List<ProgressTask> _tasks;
private readonly object _taskLock;
private readonly IAnsiConsole _console;
private readonly ProgressRenderer _renderer;
private int _taskId;
/// <summary>
/// Gets a value indicating whether or not all started tasks have completed.
/// </summary>
public bool IsFinished => _tasks.Where(x => x.IsStarted).All(task => task.IsFinished);
internal ProgressContext(IAnsiConsole console, ProgressRenderer renderer)
{
_tasks = new List<ProgressTask>();
_taskLock = new object();
_console = console ?? throw new ArgumentNullException(nameof(console));
_renderer = renderer ?? throw new ArgumentNullException(nameof(renderer));
}
/// <summary>
/// Adds a task.
/// </summary>
/// <param name="description">The task description.</param>
/// <param name="autoStart">Whether or not the task should start immediately.</param>
/// <param name="maxValue">The task's max value.</param>
/// <returns>The newly created task.</returns>
public ProgressTask AddTask(string description, bool autoStart = true, double maxValue = 100)
{
return AddTask(description, new ProgressTaskSettings
{
AutoStart = autoStart,
MaxValue = maxValue,
});
}
/// <summary>
/// Adds a task.
/// </summary>
/// <param name="description">The task description.</param>
/// <param name="settings">The task settings.</param>
/// <returns>The newly created task.</returns>
public ProgressTask AddTask(string description, ProgressTaskSettings settings)
{
if (settings is null)
{
throw new ArgumentNullException(nameof(settings));
}
lock (_taskLock)
{
var task = new ProgressTask(_taskId++, description, settings.MaxValue, settings.AutoStart);
_tasks.Add(task);
return task;
}
}
/// <summary>
/// Refreshes the current progress.
/// </summary>
public void Refresh()
{
_renderer.Update(this);
_console.Write(new ControlCode(string.Empty));
}
internal IReadOnlyList<ProgressTask> GetTasks()
{
lock (_taskLock)
{
return new List<ProgressTask>(_tasks);
}
}
}
}

View File

@ -1,58 +0,0 @@
using System;
using System.Threading;
namespace Spectre.Console
{
internal sealed class ProgressRefreshThread : IDisposable
{
private readonly ProgressContext _context;
private readonly TimeSpan _refreshRate;
private readonly ManualResetEvent _running;
private readonly ManualResetEvent _stopped;
private readonly Thread? _thread;
public ProgressRefreshThread(ProgressContext context, TimeSpan refreshRate)
{
_context = context ?? throw new ArgumentNullException(nameof(context));
_refreshRate = refreshRate;
_running = new ManualResetEvent(false);
_stopped = new ManualResetEvent(false);
_thread = new Thread(Run);
_thread.IsBackground = true;
_thread.Start();
}
public void Dispose()
{
if (_thread == null || !_running.WaitOne(0))
{
return;
}
_stopped.Set();
_thread.Join();
_stopped.Dispose();
_running.Dispose();
}
private void Run()
{
_running.Set();
try
{
while (!_stopped.WaitOne(_refreshRate))
{
_context.Refresh();
}
}
finally
{
_stopped.Reset();
_running.Reset();
}
}
}
}

View File

@ -1,22 +0,0 @@
using System;
using System.Collections.Generic;
using Spectre.Console.Rendering;
namespace Spectre.Console
{
internal abstract class ProgressRenderer : IRenderHook
{
public abstract TimeSpan RefreshRate { get; }
public virtual void Started()
{
}
public virtual void Completed(bool clear)
{
}
public abstract void Update(ProgressContext context);
public abstract IEnumerable<IRenderable> Process(RenderContext context, IEnumerable<IRenderable> renderables);
}
}

View File

@ -1,16 +0,0 @@
using System;
namespace Spectre.Console
{
internal readonly struct ProgressSample
{
public double Value { get; }
public DateTime Timestamp { get; }
public ProgressSample(DateTime timestamp, double value)
{
Timestamp = timestamp;
Value = value;
}
}
}

View File

@ -1,313 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace Spectre.Console
{
/// <summary>
/// Represents a progress task.
/// </summary>
public sealed class ProgressTask : IProgress<double>
{
private readonly List<ProgressSample> _samples;
private readonly object _lock;
private double _maxValue;
private string _description;
private double _value;
/// <summary>
/// Gets the task ID.
/// </summary>
public int Id { get; }
/// <summary>
/// Gets or sets the task description.
/// </summary>
public string Description
{
get => _description;
set => Update(description: value);
}
/// <summary>
/// Gets or sets the max value of the task.
/// </summary>
public double MaxValue
{
get => _maxValue;
set => Update(maxValue: value);
}
/// <summary>
/// Gets or sets the value of the task.
/// </summary>
public double Value
{
get => _value;
set => Update(value: value);
}
/// <summary>
/// Gets the start time of the task.
/// </summary>
public DateTime? StartTime { get; private set; }
/// <summary>
/// Gets the stop time of the task.
/// </summary>
public DateTime? StopTime { get; private set; }
/// <summary>
/// Gets the task state.
/// </summary>
public ProgressTaskState State { get; }
/// <summary>
/// Gets a value indicating whether or not the task has started.
/// </summary>
public bool IsStarted => StartTime != null;
/// <summary>
/// Gets a value indicating whether or not the task has finished.
/// </summary>
public bool IsFinished => StopTime != null || Value >= MaxValue;
/// <summary>
/// Gets the percentage done of the task.
/// </summary>
public double Percentage => GetPercentage();
/// <summary>
/// Gets the speed measured in steps/second.
/// </summary>
public double? Speed => GetSpeed();
/// <summary>
/// Gets the elapsed time.
/// </summary>
public TimeSpan? ElapsedTime => GetElapsedTime();
/// <summary>
/// Gets the remaining time.
/// </summary>
public TimeSpan? RemainingTime => GetRemainingTime();
/// <summary>
/// Gets or sets a value indicating whether the ProgressBar shows
/// actual values or generic, continuous progress feedback.
/// </summary>
public bool IsIndeterminate { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="ProgressTask"/> class.
/// </summary>
/// <param name="id">The task ID.</param>
/// <param name="description">The task description.</param>
/// <param name="maxValue">The task max value.</param>
/// <param name="autoStart">Whether or not the task should start automatically.</param>
public ProgressTask(int id, string description, double maxValue, bool autoStart = true)
{
_samples = new List<ProgressSample>();
_lock = new object();
_maxValue = maxValue;
_value = 0;
_description = description?.RemoveNewLines()?.Trim() ??
throw new ArgumentNullException(nameof(description));
if (string.IsNullOrWhiteSpace(_description))
{
throw new ArgumentException("Task name cannot be empty", nameof(description));
}
Id = id;
State = new ProgressTaskState();
StartTime = autoStart ? DateTime.Now : null;
}
/// <summary>
/// Starts the task.
/// </summary>
public void StartTask()
{
lock (_lock)
{
if (StopTime != null)
{
throw new InvalidOperationException("Stopped tasks cannot be restarted");
}
StartTime = DateTime.Now;
StopTime = null;
}
}
/// <summary>
/// Stops and marks the task as finished.
/// </summary>
public void StopTask()
{
lock (_lock)
{
var now = DateTime.Now;
StartTime ??= now;
StopTime = now;
}
}
/// <summary>
/// Increments the task's value.
/// </summary>
/// <param name="value">The value to increment with.</param>
public void Increment(double value)
{
Update(increment: value);
}
private void Update(
string? description = null,
double? maxValue = null,
double? increment = null,
double? value = null)
{
lock (_lock)
{
var startValue = Value;
if (description != null)
{
description = description?.RemoveNewLines()?.Trim();
if (string.IsNullOrWhiteSpace(description))
{
throw new InvalidOperationException("Task name cannot be empty.");
}
_description = description;
}
if (maxValue != null)
{
_maxValue = maxValue.Value;
}
if (increment != null)
{
_value += increment.Value;
}
if (value != null)
{
_value = value.Value;
}
// Need to cap the max value?
if (_value > _maxValue)
{
_value = _maxValue;
}
var timestamp = DateTime.Now;
var threshold = timestamp - TimeSpan.FromSeconds(30);
// Remove samples that's too old
while (_samples.Count > 0 && _samples[0].Timestamp < threshold)
{
_samples.RemoveAt(0);
}
// Keep maximum of 1000 samples
while (_samples.Count > 1000)
{
_samples.RemoveAt(0);
}
_samples.Add(new ProgressSample(timestamp, Value - startValue));
}
}
private double GetPercentage()
{
var percentage = (Value / MaxValue) * 100;
percentage = Math.Min(100, Math.Max(0, percentage));
return percentage;
}
private double? GetSpeed()
{
lock (_lock)
{
if (StartTime == null)
{
return null;
}
if (_samples.Count == 0)
{
return null;
}
var totalTime = _samples.Last().Timestamp - _samples[0].Timestamp;
if (totalTime == TimeSpan.Zero)
{
return null;
}
var totalCompleted = _samples.Sum(x => x.Value);
return totalCompleted / totalTime.TotalSeconds;
}
}
private TimeSpan? GetElapsedTime()
{
lock (_lock)
{
if (StartTime == null)
{
return null;
}
if (StopTime != null)
{
return StopTime - StartTime;
}
return DateTime.Now - StartTime;
}
}
private TimeSpan? GetRemainingTime()
{
lock (_lock)
{
if (IsFinished)
{
return TimeSpan.Zero;
}
var speed = GetSpeed();
if (speed == null || speed == 0)
{
return null;
}
// If the speed is near zero, the estimate below causes the
// TimeSpan creation to throw an OverflowException. Just return
// the maximum possible remaining time instead of overflowing.
var estimate = (MaxValue - Value) / speed.Value;
if (estimate > TimeSpan.MaxValue.TotalSeconds)
{
return TimeSpan.MaxValue;
}
return TimeSpan.FromSeconds(estimate);
}
}
/// <inheritdoc />
void IProgress<double>.Report(double value)
{
Update(increment: value - Value);
}
}
}

View File

@ -1,25 +0,0 @@
namespace Spectre.Console
{
/// <summary>
/// Represents settings for a progress task.
/// </summary>
public sealed class ProgressTaskSettings
{
/// <summary>
/// Gets or sets the task's max value.
/// Defaults to <c>100</c>.
/// </summary>
public double MaxValue { get; set; } = 100;
/// <summary>
/// Gets or sets a value indicating whether or not the task
/// will be auto started. Defaults to <c>true</c>.
/// </summary>
public bool AutoStart { get; set; } = true;
/// <summary>
/// Gets the default progress task settings.
/// </summary>
internal static ProgressTaskSettings Default { get; } = new ProgressTaskSettings();
}
}

View File

@ -1,81 +0,0 @@
using System;
using System.Collections.Generic;
namespace Spectre.Console
{
/// <summary>
/// Represents progress task state.
/// </summary>
public sealed class ProgressTaskState
{
private readonly Dictionary<string, object> _state;
private readonly object _lock;
/// <summary>
/// Initializes a new instance of the <see cref="ProgressTaskState"/> class.
/// </summary>
public ProgressTaskState()
{
_state = new Dictionary<string, object>();
_lock = new object();
}
/// <summary>
/// Gets the state value for the specified key.
/// </summary>
/// <typeparam name="T">The state value type.</typeparam>
/// <param name="key">The state key.</param>
/// <returns>The value for the specified key.</returns>
public T Get<T>(string key)
where T : struct
{
lock (_lock)
{
if (!_state.TryGetValue(key, out var value))
{
return default;
}
if (!(value is T))
{
throw new InvalidOperationException("State value is of the wrong type.");
}
return (T)value;
}
}
/// <summary>
/// Updates a task state value.
/// </summary>
/// <typeparam name="T">The state value type.</typeparam>
/// <param name="key">The key.</param>
/// <param name="func">The transformation function.</param>
/// <returns>The updated value.</returns>
public T Update<T>(string key, Func<T, T> func)
where T : struct
{
lock (_lock)
{
if (func is null)
{
throw new ArgumentNullException(nameof(func));
}
var old = default(T);
if (_state.TryGetValue(key, out var value))
{
if (!(value is T))
{
throw new InvalidOperationException("State value is of the wrong type.");
}
old = (T)value;
}
_state[key] = func(old);
return (T)_state[key];
}
}
}
}

View File

@ -1,129 +0,0 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Spectre.Console.Rendering;
namespace Spectre.Console
{
internal sealed class DefaultProgressRenderer : ProgressRenderer
{
private readonly IAnsiConsole _console;
private readonly List<ProgressColumn> _columns;
private readonly LiveRenderable _live;
private readonly object _lock;
private readonly Stopwatch _stopwatch;
private readonly bool _hideCompleted;
private TimeSpan _lastUpdate;
public override TimeSpan RefreshRate { get; }
public DefaultProgressRenderer(IAnsiConsole console, List<ProgressColumn> columns, TimeSpan refreshRate, bool hideCompleted)
{
_console = console ?? throw new ArgumentNullException(nameof(console));
_columns = columns ?? throw new ArgumentNullException(nameof(columns));
_live = new LiveRenderable(console);
_lock = new object();
_stopwatch = new Stopwatch();
_lastUpdate = TimeSpan.Zero;
_hideCompleted = hideCompleted;
RefreshRate = refreshRate;
}
public override void Started()
{
_console.Cursor.Hide();
}
public override void Completed(bool clear)
{
lock (_lock)
{
if (clear)
{
_console.Write(_live.RestoreCursor());
}
else
{
if (_live.HasRenderable && _live.DidOverflow)
{
// Redraw the whole live renderable
_console.Write(_live.RestoreCursor());
_live.Overflow = VerticalOverflow.Visible;
_console.Write(_live.Target);
}
_console.WriteLine();
}
_console.Cursor.Show();
}
}
public override void Update(ProgressContext context)
{
lock (_lock)
{
if (!_stopwatch.IsRunning)
{
_stopwatch.Start();
}
var renderContext = new RenderContext(_console.Profile.Capabilities);
var delta = _stopwatch.Elapsed - _lastUpdate;
_lastUpdate = _stopwatch.Elapsed;
var grid = new Grid();
for (var columnIndex = 0; columnIndex < _columns.Count; columnIndex++)
{
var column = new GridColumn().PadRight(1);
var columnWidth = _columns[columnIndex].GetColumnWidth(renderContext);
if (columnWidth != null)
{
column.Width = columnWidth;
}
if (_columns[columnIndex].NoWrap)
{
column.NoWrap();
}
// Last column?
if (columnIndex == _columns.Count - 1)
{
column.PadRight(0);
}
grid.AddColumn(column);
}
// Add rows
foreach (var task in context.GetTasks().Where(tsk => !(_hideCompleted && tsk.IsFinished)))
{
var columns = _columns.Select(column => column.Render(renderContext, task, delta));
grid.AddRow(columns.ToArray());
}
_live.SetRenderable(new Padder(grid, new Padding(0, 1)));
}
}
public override IEnumerable<IRenderable> Process(RenderContext context, IEnumerable<IRenderable> renderables)
{
lock (_lock)
{
yield return _live.PositionCursor();
foreach (var renderable in renderables)
{
yield return renderable;
}
yield return _live;
}
}
}
}

View File

@ -1,125 +0,0 @@
using System;
using System.Collections.Generic;
using Spectre.Console.Rendering;
namespace Spectre.Console
{
internal sealed class FallbackProgressRenderer : ProgressRenderer
{
private const double FirstMilestone = 25;
private static readonly double?[] _milestones = new double?[] { FirstMilestone, 50, 75, 95, 96, 97, 98, 99, 100 };
private readonly Dictionary<int, double> _taskMilestones;
private readonly object _lock;
private IRenderable? _renderable;
private DateTime _lastUpdate;
public override TimeSpan RefreshRate => TimeSpan.FromSeconds(1);
public FallbackProgressRenderer()
{
_taskMilestones = new Dictionary<int, double>();
_lock = new object();
}
public override void Update(ProgressContext context)
{
lock (_lock)
{
var hasStartedTasks = false;
var updates = new List<(string, double)>();
foreach (var task in context.GetTasks())
{
if (!task.IsStarted || task.IsFinished)
{
continue;
}
hasStartedTasks = true;
if (TryAdvance(task.Id, task.Percentage))
{
updates.Add((task.Description, task.Percentage));
}
}
// Got started tasks but no updates for 30 seconds?
if (hasStartedTasks && updates.Count == 0 && (DateTime.Now - _lastUpdate) > TimeSpan.FromSeconds(30))
{
foreach (var task in context.GetTasks())
{
updates.Add((task.Description, task.Percentage));
}
}
if (updates.Count > 0)
{
_lastUpdate = DateTime.Now;
}
_renderable = BuildTaskGrid(updates);
}
}
public override IEnumerable<IRenderable> Process(RenderContext context, IEnumerable<IRenderable> renderables)
{
lock (_lock)
{
var result = new List<IRenderable>();
result.AddRange(renderables);
if (_renderable != null)
{
result.Add(_renderable);
}
_renderable = null;
return result;
}
}
private bool TryAdvance(int task, double percentage)
{
if (!_taskMilestones.TryGetValue(task, out var milestone))
{
_taskMilestones.Add(task, FirstMilestone);
return true;
}
if (percentage > milestone)
{
var nextMilestone = GetNextMilestone(percentage);
if (nextMilestone != null && _taskMilestones[task] != nextMilestone)
{
_taskMilestones[task] = nextMilestone.Value;
return true;
}
}
return false;
}
private static double? GetNextMilestone(double percentage)
{
return Array.Find(_milestones, p => p > percentage);
}
private static IRenderable? BuildTaskGrid(List<(string Name, double Percentage)> updates)
{
if (updates.Count > 0)
{
var renderables = new List<IRenderable>();
foreach (var (name, percentage) in updates)
{
renderables.Add(new Markup($"[blue]{name}[/]: {(int)percentage}%"));
}
return new Rows(renderables);
}
return null;
}
}
}

View File

@ -1,60 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Spectre.Console.Rendering;
namespace Spectre.Console
{
internal sealed class FallbackStatusRenderer : ProgressRenderer
{
private readonly object _lock;
private IRenderable? _renderable;
private string? _lastStatus;
public override TimeSpan RefreshRate => TimeSpan.FromMilliseconds(100);
public FallbackStatusRenderer()
{
_lock = new object();
}
public override void Update(ProgressContext context)
{
lock (_lock)
{
var task = context.GetTasks().SingleOrDefault();
if (task != null)
{
// Not same description?
if (_lastStatus != task.Description)
{
_lastStatus = task.Description;
_renderable = new Markup(task.Description + Environment.NewLine);
return;
}
}
_renderable = null;
return;
}
}
public override IEnumerable<IRenderable> Process(RenderContext context, IEnumerable<IRenderable> renderables)
{
lock (_lock)
{
var result = new List<IRenderable>();
result.AddRange(renderables);
if (_renderable != null)
{
result.Add(_renderable);
}
_renderable = null;
return result;
}
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -1,27 +0,0 @@
using System;
using System.Collections.Generic;
namespace Spectre.Console
{
/// <summary>
/// Represents a spinner used in a <see cref="SpinnerColumn"/>.
/// </summary>
public abstract partial class Spinner
{
/// <summary>
/// Gets the update interval for the spinner.
/// </summary>
public abstract TimeSpan Interval { get; }
/// <summary>
/// Gets a value indicating whether or not the spinner
/// uses Unicode characters.
/// </summary>
public abstract bool IsUnicode { get; }
/// <summary>
/// Gets the spinner frames.
/// </summary>
public abstract IReadOnlyList<string> Frames { get; }
}
}

View File

@ -1,127 +0,0 @@
using System;
using System.Threading.Tasks;
namespace Spectre.Console
{
/// <summary>
/// Represents a status display.
/// </summary>
public sealed class Status
{
private readonly IAnsiConsole _console;
/// <summary>
/// Gets or sets the spinner.
/// </summary>
public Spinner? Spinner { get; set; }
/// <summary>
/// Gets or sets the spinner style.
/// </summary>
public Style? SpinnerStyle { get; set; } = new Style(foreground: Color.Yellow);
/// <summary>
/// Gets or sets a value indicating whether or not status
/// should auto refresh. Defaults to <c>true</c>.
/// </summary>
public bool AutoRefresh { get; set; } = true;
/// <summary>
/// Initializes a new instance of the <see cref="Status"/> class.
/// </summary>
/// <param name="console">The console.</param>
public Status(IAnsiConsole console)
{
_console = console ?? throw new ArgumentNullException(nameof(console));
}
/// <summary>
/// Starts a new status display.
/// </summary>
/// <param name="status">The status to display.</param>
/// <param name="action">The action to execute.</param>
public void Start(string status, Action<StatusContext> action)
{
var task = StartAsync(status, ctx =>
{
action(ctx);
return Task.CompletedTask;
});
task.GetAwaiter().GetResult();
}
/// <summary>
/// Starts a new status display.
/// </summary>
/// <typeparam name="T">The result type.</typeparam>
/// <param name="status">The status to display.</param>
/// <param name="func">The action to execute.</param>
/// <returns>The result.</returns>
public T Start<T>(string status, Func<StatusContext, T> func)
{
var task = StartAsync(status, ctx => Task.FromResult(func(ctx)));
return task.GetAwaiter().GetResult();
}
/// <summary>
/// Starts a new status display.
/// </summary>
/// <param name="status">The status to display.</param>
/// <param name="action">The action to execute.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public async Task StartAsync(string status, Func<StatusContext, Task> action)
{
if (action is null)
{
throw new ArgumentNullException(nameof(action));
}
_ = await StartAsync<object?>(status, async statusContext =>
{
await action(statusContext).ConfigureAwait(false);
return default;
}).ConfigureAwait(false);
}
/// <summary>
/// Starts a new status display and returns a result.
/// </summary>
/// <typeparam name="T">The result type of task.</typeparam>
/// <param name="status">The status to display.</param>
/// <param name="func">The action to execute.</param>
/// <returns>A <see cref="Task{T}"/> representing the asynchronous operation.</returns>
public async Task<T> StartAsync<T>(string status, Func<StatusContext, Task<T>> func)
{
if (func is null)
{
throw new ArgumentNullException(nameof(func));
}
// Set the progress columns
var spinnerColumn = new SpinnerColumn(Spinner ?? Spinner.Known.Default)
{
Style = SpinnerStyle ?? Style.Plain,
};
var progress = new Progress(_console)
{
FallbackRenderer = new FallbackStatusRenderer(),
AutoClear = true,
AutoRefresh = AutoRefresh,
};
progress.Columns(new ProgressColumn[]
{
spinnerColumn,
new TaskDescriptionColumn(),
});
return await progress.StartAsync(async ctx =>
{
var statusContext = new StatusContext(ctx, ctx.AddTask(status), spinnerColumn);
return await func(statusContext).ConfigureAwait(false);
}).ConfigureAwait(false);
}
}
}

View File

@ -1,76 +0,0 @@
using System;
namespace Spectre.Console
{
/// <summary>
/// Represents a context that can be used to interact with a <see cref="Status"/>.
/// </summary>
public sealed class StatusContext
{
private readonly ProgressContext _context;
private readonly ProgressTask _task;
private readonly SpinnerColumn _spinnerColumn;
/// <summary>
/// Gets or sets the current status.
/// </summary>
public string Status
{
get => _task.Description;
set => SetStatus(value);
}
/// <summary>
/// Gets or sets the current spinner.
/// </summary>
public Spinner Spinner
{
get => _spinnerColumn.Spinner;
set => SetSpinner(value);
}
/// <summary>
/// Gets or sets the current spinner style.
/// </summary>
public Style? SpinnerStyle
{
get => _spinnerColumn.Style;
set => _spinnerColumn.Style = value;
}
internal StatusContext(ProgressContext context, ProgressTask task, SpinnerColumn spinnerColumn)
{
_context = context ?? throw new ArgumentNullException(nameof(context));
_task = task ?? throw new ArgumentNullException(nameof(task));
_spinnerColumn = spinnerColumn ?? throw new ArgumentNullException(nameof(spinnerColumn));
}
/// <summary>
/// Refreshes the status.
/// </summary>
public void Refresh()
{
_context.Refresh();
}
private void SetStatus(string status)
{
if (status is null)
{
throw new ArgumentNullException(nameof(status));
}
_task.Description = status;
}
private void SetSpinner(Spinner spinner)
{
if (spinner is null)
{
throw new ArgumentNullException(nameof(spinner));
}
_spinnerColumn.Spinner = spinner;
}
}
}

View File

@ -1,76 +0,0 @@
using System.Threading;
using System.Threading.Tasks;
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>
/// 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;
}
}
}

View File

@ -1,12 +0,0 @@
namespace Spectre.Console
{
internal sealed class DefaultPromptValue<T>
{
public T Value { get; }
public DefaultPromptValue(T value)
{
Value = value;
}
}
}

View File

@ -1,21 +0,0 @@
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>
/// Gets a value indicating whether or not this item is selected.
/// </summary>
bool IsSelected { get; }
/// <summary>
/// Selects the item.
/// </summary>
/// <returns>The same instance so that multiple calls can be chained.</returns>
IMultiSelectionItem<T> Select();
}
}

View File

@ -1,27 +0,0 @@
using System.Threading;
using System.Threading.Tasks;
namespace Spectre.Console
{
/// <summary>
/// Represents a 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);
/// <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);
}
}

View File

@ -1,17 +0,0 @@
namespace Spectre.Console
{
/// <summary>
/// Represent a selection 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);
}
}

View File

@ -1,41 +0,0 @@
using System;
using System.Collections.Generic;
using Spectre.Console.Rendering;
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>
/// 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);
/// <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);
}
}

View File

@ -1,120 +0,0 @@
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Spectre.Console.Rendering;
namespace Spectre.Console
{
internal sealed class ListPrompt<T>
where T : notnull
{
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)));
}
}
}

View File

@ -1,12 +0,0 @@
namespace Spectre.Console
{
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)[/]";
}
}

View File

@ -1,10 +0,0 @@
namespace Spectre.Console
{
internal enum ListPromptInputResult
{
None = 0,
Refresh = 1,
Submit = 2,
Abort = 3,
}
}

View File

@ -1,80 +0,0 @@
using System.Collections.Generic;
namespace Spectre.Console
{
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)
{
Data = data;
Parent = parent;
Children = new List<ListPromptItem<T>>();
Depth = CalculateDepth(parent);
}
public IMultiSelectionItem<T> Select()
{
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)
{
stack.Push(this);
}
else
{
foreach (var child in Children)
{
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;
}
}
}

View File

@ -1,60 +0,0 @@
using System;
using System.Collections.Generic;
using Spectre.Console.Rendering;
namespace Spectre.Console
{
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)
{
_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)
{
_live.SetRenderable(_builder());
_dirty = false;
}
yield return _live.PositionCursor();
foreach (var renderable in renderables)
{
yield return renderable;
}
yield return _live;
}
}
}
}

View File

@ -1,46 +0,0 @@
using System;
using System.Collections.Generic;
namespace Spectre.Console
{
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)
{
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;
}
}
}

View File

@ -1,60 +0,0 @@
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;
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)
{
var current = stack.Pop();
if (_comparer.Equals(item, current.Data))
{
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);
}
}
}
}
}
}

View File

@ -1,248 +0,0 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Spectre.Console.Rendering;
namespace Spectre.Console
{
/// <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>
/// 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 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)
{
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)
.Select(x => x.Data)
.ToList();
}
/// <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 it's children
foreach (var item in current.Traverse(includeSelf: true))
{
item.IsSelected = select;
}
// Visit every parent and evaluate if it's 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();
}
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);
}
}
}

View File

@ -1,311 +0,0 @@
using System;
using System.Collections.Generic;
namespace Spectre.Console
{
/// <summary>
/// Contains extension methods for <see cref="MultiSelectionPrompt{T}"/>.
/// </summary>
public static class MultiSelectionPromptExtensions
{
/// <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)
{
throw new ArgumentNullException(nameof(obj));
}
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)
{
throw new ArgumentNullException(nameof(obj));
}
if (configurator is null)
{
throw new ArgumentNullException(nameof(configurator));
}
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)
{
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 MultiSelectionPrompt<T> AddChoices<T>(this MultiSelectionPrompt<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 MultiSelectionPrompt<T> AddChoiceGroup<T>(this MultiSelectionPrompt<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>
/// 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));
}
var node = obj.Tree.Find(item);
node?.Select();
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));
}
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)
{
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 MultiSelectionPrompt<T> HighlightStyle<T>(this MultiSelectionPrompt<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 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;
}
}
}

View File

@ -1,188 +0,0 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading;
using System.Threading.Tasks;
using Spectre.Console.Rendering;
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>
/// 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();
}
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);
}
}
}

View File

@ -1,201 +0,0 @@
using System;
using System.Collections.Generic;
namespace Spectre.Console
{
/// <summary>
/// Contains extension methods for <see cref="SelectionPrompt{T}"/>.
/// </summary>
public static class SelectionPromptExtensions
{
/// <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)
{
throw new ArgumentNullException(nameof(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;
}
}
}

View File

@ -1,19 +0,0 @@
namespace Spectre.Console
{
/// <summary>
/// Represents how selections are made in a hierarchical prompt.
/// </summary>
public enum SelectionMode
{
/// <summary>
/// Will only return lead nodes in results.
/// </summary>
Leaf = 0,
/// <summary>
/// Allows selection of parent nodes, but each node
/// is independent of its parent and children.
/// </summary>
Independent = 1,
}
}

View File

@ -1,234 +0,0 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
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>
/// 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;
}
}
}

View File

@ -1,312 +0,0 @@
using System;
using System.Collections.Generic;
namespace Spectre.Console
{
/// <summary>
/// Contains extension methods for <see cref="TextPrompt{T}"/>.
/// </summary>
public static class TextPromptExtensions
{
/// <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)
{
throw new ArgumentNullException(nameof(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 asterixes 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;
}
}
}

View File

@ -1,43 +0,0 @@
namespace Spectre.Console
{
/// <summary>
/// Represents a prompt validation result.
/// </summary>
public sealed class ValidationResult
{
/// <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);
}
}
}