mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-06-19 13:28:16 +08:00

committed by
Patrik Svensson

parent
e280b82679
commit
1cf30f62fc
30
src/Spectre.Console/Widgets/Figlet/FigletCharacter.cs
Normal file
30
src/Spectre.Console/Widgets/Figlet/FigletCharacter.cs
Normal file
@ -0,0 +1,30 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Spectre.Console
|
||||
{
|
||||
internal sealed class FigletCharacter
|
||||
{
|
||||
public int Code { get; }
|
||||
public int Width { get; }
|
||||
public int Height { get; }
|
||||
public IReadOnlyList<string> Lines { get; }
|
||||
|
||||
public FigletCharacter(int code, IEnumerable<string> lines)
|
||||
{
|
||||
Code = code;
|
||||
Lines = new List<string>(lines ?? throw new ArgumentNullException(nameof(lines)));
|
||||
|
||||
var min = Lines.Min(x => x.Length);
|
||||
var max = Lines.Max(x => x.Length);
|
||||
if (min != max)
|
||||
{
|
||||
throw new InvalidOperationException($"Figlet character #{code} has varying width");
|
||||
}
|
||||
|
||||
Width = max;
|
||||
Height = Lines.Count;
|
||||
}
|
||||
}
|
||||
}
|
137
src/Spectre.Console/Widgets/Figlet/FigletFont.cs
Normal file
137
src/Spectre.Console/Widgets/Figlet/FigletFont.cs
Normal file
@ -0,0 +1,137 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using Spectre.Console.Internal;
|
||||
|
||||
namespace Spectre.Console
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a FIGlet font.
|
||||
/// </summary>
|
||||
public sealed class FigletFont
|
||||
{
|
||||
private const string StandardFont = "Spectre.Console/Widgets/Figlet/Fonts/Standard.flf";
|
||||
|
||||
private readonly Dictionary<int, FigletCharacter> _characters;
|
||||
private static readonly Lazy<FigletFont> _standard;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the number of characters in the font.
|
||||
/// </summary>
|
||||
public int Count => _characters.Count;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the height of the font.
|
||||
/// </summary>
|
||||
public int Height { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the font's baseline.
|
||||
/// </summary>
|
||||
public int Baseline { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the font's maximum width.
|
||||
/// </summary>
|
||||
public int MaxWidth { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the default FIGlet font.
|
||||
/// </summary>
|
||||
public static FigletFont Default => _standard.Value;
|
||||
|
||||
static FigletFont()
|
||||
{
|
||||
_standard = new Lazy<FigletFont>(() => Parse(
|
||||
ResourceReader.ReadManifestData(StandardFont)));
|
||||
}
|
||||
|
||||
internal FigletFont(IEnumerable<FigletCharacter> characters, FigletHeader header)
|
||||
{
|
||||
_characters = new Dictionary<int, FigletCharacter>();
|
||||
|
||||
foreach (var character in characters)
|
||||
{
|
||||
if (_characters.ContainsKey(character.Code))
|
||||
{
|
||||
throw new InvalidOperationException("Character already exist");
|
||||
}
|
||||
|
||||
_characters[character.Code] = character;
|
||||
}
|
||||
|
||||
Height = header.Height;
|
||||
Baseline = header.Baseline;
|
||||
MaxWidth = header.MaxLength;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads a FIGlet font from the specified stream.
|
||||
/// </summary>
|
||||
/// <param name="stream">The stream to load the FIGlet font from.</param>
|
||||
/// <returns>The loaded FIGlet font.</returns>
|
||||
public static FigletFont Load(Stream stream)
|
||||
{
|
||||
using (var reader = new StreamReader(stream))
|
||||
{
|
||||
return Parse(reader.ReadToEnd());
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads a FIGlet font from disk.
|
||||
/// </summary>
|
||||
/// <param name="path">The path of the FIGlet font to load.</param>
|
||||
/// <returns>The loaded FIGlet font.</returns>
|
||||
public static FigletFont Load(string path)
|
||||
{
|
||||
return Parse(File.ReadAllText(path));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parses a FIGlet font from the specified <see cref="string"/>.
|
||||
/// </summary>
|
||||
/// <param name="source">The FIGlet font source.</param>
|
||||
/// <returns>The parsed FIGlet font.</returns>
|
||||
public static FigletFont Parse(string source)
|
||||
{
|
||||
return FigletFontParser.Parse(source);
|
||||
}
|
||||
|
||||
internal int GetWidth(string text)
|
||||
{
|
||||
var width = 0;
|
||||
foreach (var character in text)
|
||||
{
|
||||
width += GetCharacter(character)?.Width ?? 0;
|
||||
}
|
||||
|
||||
return width;
|
||||
}
|
||||
|
||||
internal FigletCharacter? GetCharacter(char character)
|
||||
{
|
||||
_characters.TryGetValue(character, out var result);
|
||||
return result;
|
||||
}
|
||||
|
||||
internal IEnumerable<FigletCharacter> GetCharacters(string text)
|
||||
{
|
||||
if (text is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(text));
|
||||
}
|
||||
|
||||
var result = new List<FigletCharacter>();
|
||||
foreach (var character in text)
|
||||
{
|
||||
if (_characters.TryGetValue(character, out var figletCharacter))
|
||||
{
|
||||
result.Add(figletCharacter);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
114
src/Spectre.Console/Widgets/Figlet/FigletFontParser.cs
Normal file
114
src/Spectre.Console/Widgets/Figlet/FigletFontParser.cs
Normal file
@ -0,0 +1,114 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
|
||||
namespace Spectre.Console
|
||||
{
|
||||
internal static class FigletFontParser
|
||||
{
|
||||
public static FigletFont Parse(string source)
|
||||
{
|
||||
var lines = source.SplitLines();
|
||||
var header = ParseHeader(lines.FirstOrDefault());
|
||||
|
||||
var characters = new List<FigletCharacter>();
|
||||
|
||||
var index = 32;
|
||||
var indexOverridden = false;
|
||||
var hasOverriddenIndex = false;
|
||||
var buffer = new List<string>();
|
||||
|
||||
foreach (var line in lines.Skip(header.CommentLines + 1))
|
||||
{
|
||||
if (!line.EndsWith("@", StringComparison.Ordinal))
|
||||
{
|
||||
var words = line.SplitWords();
|
||||
if (words.Length > 0 && TryParseIndex(words[0], out var newIndex))
|
||||
{
|
||||
index = newIndex;
|
||||
indexOverridden = true;
|
||||
hasOverriddenIndex = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (hasOverriddenIndex && !indexOverridden)
|
||||
{
|
||||
throw new InvalidOperationException("Unknown index for FIGlet character");
|
||||
}
|
||||
|
||||
buffer.Add(line.Replace('$', ' ').ReplaceExact("@", string.Empty));
|
||||
|
||||
if (line.EndsWith("@@", StringComparison.Ordinal))
|
||||
{
|
||||
characters.Add(new FigletCharacter(index, buffer));
|
||||
buffer.Clear();
|
||||
|
||||
if (!hasOverriddenIndex)
|
||||
{
|
||||
index++;
|
||||
}
|
||||
|
||||
// Reset the flag so we know if we're trying to parse
|
||||
// a character that wasn't prefixed with an ASCII index.
|
||||
indexOverridden = false;
|
||||
}
|
||||
}
|
||||
|
||||
return new FigletFont(characters, header);
|
||||
}
|
||||
|
||||
private static bool TryParseIndex(string index, out int result)
|
||||
{
|
||||
var style = NumberStyles.Integer;
|
||||
if (index.StartsWith("0x", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
// TODO: ReplaceExact should not be used
|
||||
index = index.ReplaceExact("0x", string.Empty).ReplaceExact("0x", string.Empty);
|
||||
style = NumberStyles.HexNumber;
|
||||
}
|
||||
|
||||
return int.TryParse(index, style, CultureInfo.InvariantCulture, out result);
|
||||
}
|
||||
|
||||
private static FigletHeader ParseHeader(string text)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(text))
|
||||
{
|
||||
throw new InvalidOperationException("Invalid Figlet font");
|
||||
}
|
||||
|
||||
var parts = text.SplitWords(StringSplitOptions.RemoveEmptyEntries);
|
||||
if (parts.Length < 6)
|
||||
{
|
||||
throw new InvalidOperationException("Invalid Figlet font header");
|
||||
}
|
||||
|
||||
if (!IsValidSignature(parts[0]))
|
||||
{
|
||||
throw new InvalidOperationException("Invalid Figlet font header signature");
|
||||
}
|
||||
|
||||
return new FigletHeader
|
||||
{
|
||||
Hardblank = parts[0][5],
|
||||
Height = int.Parse(parts[1], CultureInfo.InvariantCulture),
|
||||
Baseline = int.Parse(parts[2], CultureInfo.InvariantCulture),
|
||||
MaxLength = int.Parse(parts[3], CultureInfo.InvariantCulture),
|
||||
OldLayout = int.Parse(parts[4], CultureInfo.InvariantCulture),
|
||||
CommentLines = int.Parse(parts[5], CultureInfo.InvariantCulture),
|
||||
};
|
||||
}
|
||||
|
||||
private static bool IsValidSignature(string signature)
|
||||
{
|
||||
return signature.Length == 6
|
||||
&& signature[0] == 'f' && signature[1] == 'l'
|
||||
&& signature[2] == 'f' && signature[3] == '2'
|
||||
&& signature[4] == 'a';
|
||||
}
|
||||
}
|
||||
}
|
12
src/Spectre.Console/Widgets/Figlet/FigletHeader.cs
Normal file
12
src/Spectre.Console/Widgets/Figlet/FigletHeader.cs
Normal file
@ -0,0 +1,12 @@
|
||||
namespace Spectre.Console
|
||||
{
|
||||
internal sealed class FigletHeader
|
||||
{
|
||||
public char Hardblank { get; set; }
|
||||
public int Height { get; set; }
|
||||
public int Baseline { get; set; }
|
||||
public int MaxLength { get; set; }
|
||||
public int OldLayout { get; set; }
|
||||
public int CommentLines { get; set; }
|
||||
}
|
||||
}
|
151
src/Spectre.Console/Widgets/Figlet/FigletText.cs
Normal file
151
src/Spectre.Console/Widgets/Figlet/FigletText.cs
Normal file
@ -0,0 +1,151 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Spectre.Console.Rendering;
|
||||
|
||||
namespace Spectre.Console
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents text rendered with a FIGlet font.
|
||||
/// </summary>
|
||||
public sealed class FigletText : Renderable, IAlignable
|
||||
{
|
||||
private readonly FigletFont _font;
|
||||
private readonly string _text;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the color of the text.
|
||||
/// </summary>
|
||||
public Color? Color { get; set; }
|
||||
|
||||
/// <inheritdoc/>
|
||||
public Justify? Alignment { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="FigletText"/> class.
|
||||
/// </summary>
|
||||
/// <param name="text">The text.</param>
|
||||
public FigletText(string text)
|
||||
: this(FigletFont.Default, text)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="FigletText"/> class.
|
||||
/// </summary>
|
||||
/// <param name="font">The FIGlet font to use.</param>
|
||||
/// <param name="text">The text.</param>
|
||||
public FigletText(FigletFont font, string text)
|
||||
{
|
||||
_font = font ?? throw new ArgumentNullException(nameof(font));
|
||||
_text = text ?? throw new ArgumentNullException(nameof(text));
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override IEnumerable<Segment> Render(RenderContext context, int maxWidth)
|
||||
{
|
||||
var style = new Style(Color ?? Console.Color.Default);
|
||||
var alignment = Alignment ?? Justify.Left;
|
||||
|
||||
foreach (var row in GetRows(maxWidth))
|
||||
{
|
||||
for (var index = 0; index < _font.Height; index++)
|
||||
{
|
||||
var line = new Segment(string.Concat(row.Select(x => x.Lines[index])), style);
|
||||
|
||||
var lineWidth = line.CellCount(context);
|
||||
if (alignment == Justify.Left)
|
||||
{
|
||||
yield return line;
|
||||
|
||||
if (lineWidth < maxWidth)
|
||||
{
|
||||
yield return new Segment(new string(' ', maxWidth - lineWidth));
|
||||
}
|
||||
}
|
||||
else if (alignment == Justify.Center)
|
||||
{
|
||||
var left = (maxWidth - lineWidth) / 2;
|
||||
var right = left + ((maxWidth - lineWidth) % 2);
|
||||
|
||||
yield return new Segment(new string(' ', left));
|
||||
yield return line;
|
||||
yield return new Segment(new string(' ', right));
|
||||
}
|
||||
else if (alignment == Justify.Right)
|
||||
{
|
||||
if (lineWidth < maxWidth)
|
||||
{
|
||||
yield return new Segment(new string(' ', maxWidth - lineWidth));
|
||||
}
|
||||
|
||||
yield return line;
|
||||
}
|
||||
|
||||
yield return Segment.LineBreak;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private List<List<FigletCharacter>> GetRows(int maxWidth)
|
||||
{
|
||||
var result = new List<List<FigletCharacter>>();
|
||||
var words = _text.SplitWords(StringSplitOptions.None);
|
||||
|
||||
var totalWidth = 0;
|
||||
var line = new List<FigletCharacter>();
|
||||
|
||||
foreach (var word in words)
|
||||
{
|
||||
// Does the whole word fit?
|
||||
var width = _font.GetWidth(word);
|
||||
if (width + totalWidth < maxWidth)
|
||||
{
|
||||
// Add it to the line
|
||||
line.AddRange(_font.GetCharacters(word));
|
||||
totalWidth += width;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Does it fit on it's own line?
|
||||
if (width < maxWidth)
|
||||
{
|
||||
// Flush the line
|
||||
result.Add(line);
|
||||
line = new List<FigletCharacter>();
|
||||
totalWidth = 0;
|
||||
|
||||
line.AddRange(_font.GetCharacters(word));
|
||||
totalWidth += width;
|
||||
}
|
||||
else
|
||||
{
|
||||
// We need to split it up.
|
||||
var queue = new Queue<FigletCharacter>(_font.GetCharacters(word));
|
||||
while (queue.Count > 0)
|
||||
{
|
||||
var current = queue.Dequeue();
|
||||
if (totalWidth + current.Width > maxWidth)
|
||||
{
|
||||
// Flush the line
|
||||
result.Add(line);
|
||||
line = new List<FigletCharacter>();
|
||||
totalWidth = 0;
|
||||
}
|
||||
|
||||
line.Add(current);
|
||||
totalWidth += current.Width;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (line.Count > 0)
|
||||
{
|
||||
result.Add(line);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
2227
src/Spectre.Console/Widgets/Figlet/Fonts/Standard.flf
Normal file
2227
src/Spectre.Console/Widgets/Figlet/Fonts/Standard.flf
Normal file
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,35 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
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);
|
||||
|
||||
/// <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,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
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("-:--:--");
|
||||
}
|
||||
|
||||
return new Text($"{remaining.Value:h\\:mm\\:ss}", Style ?? Style.Plain);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override int? GetColumnWidth(RenderContext context)
|
||||
{
|
||||
return 7;
|
||||
}
|
||||
}
|
||||
}
|
107
src/Spectre.Console/Widgets/Progress/Columns/SpinnerColumn.cs
Normal file
107
src/Spectre.Console/Widgets/Progress/Columns/SpinnerColumn.cs
Normal file
@ -0,0 +1,107 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Spectre.Console.Internal;
|
||||
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;
|
||||
|
||||
/// <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 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.LegacyConsole || !context.Unicode) && _spinner.IsUnicode;
|
||||
var spinner = useAscii ? Spinner.Known.Ascii : _spinner ?? Spinner.Known.Default;
|
||||
|
||||
if (!task.IsStarted || task.IsFinished)
|
||||
{
|
||||
return new Markup(new string(' ', GetMaxWidth(context)));
|
||||
}
|
||||
|
||||
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.LegacyConsole || !context.Unicode) && _spinner.IsUnicode;
|
||||
var spinner = useAscii ? Spinner.Known.Ascii : _spinner ?? Spinner.Known.Default;
|
||||
|
||||
_maxWidth = spinner.Frames.Max(frame => Cell.GetCellLength(context, frame));
|
||||
}
|
||||
|
||||
return _maxWidth.Value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
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;
|
||||
|
||||
/// <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).RightAligned();
|
||||
}
|
||||
}
|
||||
}
|
129
src/Spectre.Console/Widgets/Progress/Progress.cs
Normal file
129
src/Spectre.Console/Widgets/Progress/Progress.cs
Normal file
@ -0,0 +1,129 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Spectre.Console.Internal;
|
||||
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 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.
|
||||
/// </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));
|
||||
}
|
||||
|
||||
var renderer = CreateRenderer();
|
||||
renderer.Started();
|
||||
|
||||
try
|
||||
{
|
||||
using (new RenderHookScope(_console, renderer))
|
||||
{
|
||||
var context = new ProgressContext(_console, renderer);
|
||||
|
||||
if (AutoRefresh)
|
||||
{
|
||||
using (var thread = new ProgressRefreshThread(context, renderer.RefreshRate))
|
||||
{
|
||||
await action(context).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
await action(context).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
context.Refresh();
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
renderer.Completed(AutoClear);
|
||||
}
|
||||
}
|
||||
|
||||
private ProgressRenderer CreateRenderer()
|
||||
{
|
||||
var caps = _console.Capabilities;
|
||||
var interactive = caps.SupportsInteraction && caps.SupportsAnsi;
|
||||
|
||||
if (interactive)
|
||||
{
|
||||
var columns = new List<ProgressColumn>(Columns);
|
||||
return new DefaultProgressRenderer(_console, columns, RefreshRate);
|
||||
}
|
||||
else
|
||||
{
|
||||
return FallbackRenderer ?? new FallbackProgressRenderer();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
35
src/Spectre.Console/Widgets/Progress/ProgressColumn.cs
Normal file
35
src/Spectre.Console/Widgets/Progress/ProgressColumn.cs
Normal file
@ -0,0 +1,35 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
70
src/Spectre.Console/Widgets/Progress/ProgressContext.cs
Normal file
70
src/Spectre.Console/Widgets/Progress/ProgressContext.cs
Normal file
@ -0,0 +1,70 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Spectre.Console.Internal;
|
||||
|
||||
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 tasks have completed.
|
||||
/// </summary>
|
||||
public bool IsFinished => _tasks.All(task => task.IsFinished);
|
||||
|
||||
internal Encoding Encoding => _console.Encoding;
|
||||
|
||||
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="settings">The task settings.</param>
|
||||
/// <returns>The task's ID.</returns>
|
||||
public ProgressTask AddTask(string description, ProgressTaskSettings? settings = null)
|
||||
{
|
||||
lock (_taskLock)
|
||||
{
|
||||
settings ??= new ProgressTaskSettings();
|
||||
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.Render(new ControlSequence(string.Empty));
|
||||
}
|
||||
|
||||
internal IReadOnlyList<ProgressTask> GetTasks()
|
||||
{
|
||||
lock (_taskLock)
|
||||
{
|
||||
return new List<ProgressTask>(_tasks);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
|
||||
namespace Spectre.Console.Internal
|
||||
{
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
22
src/Spectre.Console/Widgets/Progress/ProgressRenderer.cs
Normal file
22
src/Spectre.Console/Widgets/Progress/ProgressRenderer.cs
Normal file
@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Spectre.Console.Rendering;
|
||||
|
||||
namespace Spectre.Console.Internal
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
16
src/Spectre.Console/Widgets/Progress/ProgressSample.cs
Normal file
16
src/Spectre.Console/Widgets/Progress/ProgressSample.cs
Normal file
@ -0,0 +1,16 @@
|
||||
using System;
|
||||
|
||||
namespace Spectre.Console.Internal
|
||||
{
|
||||
internal readonly struct ProgressSample
|
||||
{
|
||||
public double Value { get; }
|
||||
public DateTime Timestamp { get; }
|
||||
|
||||
public ProgressSample(DateTime timestamp, double value)
|
||||
{
|
||||
Timestamp = timestamp;
|
||||
Value = value;
|
||||
}
|
||||
}
|
||||
}
|
281
src/Spectre.Console/Widgets/Progress/ProgressTask.cs
Normal file
281
src/Spectre.Console/Widgets/Progress/ProgressTask.cs
Normal file
@ -0,0 +1,281 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Spectre.Console.Internal;
|
||||
|
||||
namespace Spectre.Console
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a progress task.
|
||||
/// </summary>
|
||||
public sealed class ProgressTask
|
||||
{
|
||||
private readonly List<ProgressSample> _samples;
|
||||
private readonly object _lock;
|
||||
|
||||
private double _maxValue;
|
||||
private string _description;
|
||||
|
||||
/// <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 the value of the task.
|
||||
/// </summary>
|
||||
public double Value { get; private set; }
|
||||
|
||||
/// <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 => 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();
|
||||
|
||||
internal ProgressTask(int id, string description, double maxValue, bool autoStart)
|
||||
{
|
||||
_samples = new List<ProgressSample>();
|
||||
_lock = new object();
|
||||
_maxValue = maxValue;
|
||||
|
||||
_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();
|
||||
Value = 0;
|
||||
StartTime = autoStart ? DateTime.Now : null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Starts the task.
|
||||
/// </summary>
|
||||
public void StartTask()
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
StartTime = DateTime.Now;
|
||||
StopTime = null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stops the task.
|
||||
/// </summary>
|
||||
public void StopTask()
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
var now = DateTime.Now;
|
||||
if (StartTime == null)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
// 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)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// If the speed is zero, the estimate below
|
||||
// will return infinity (since it's a double),
|
||||
// so let's set the speed to 1 in that case.
|
||||
if (speed == 0)
|
||||
{
|
||||
speed = 1;
|
||||
}
|
||||
|
||||
var estimate = (MaxValue - Value) / speed.Value;
|
||||
return TimeSpan.FromSeconds(estimate);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
20
src/Spectre.Console/Widgets/Progress/ProgressTaskSettings.cs
Normal file
20
src/Spectre.Console/Widgets/Progress/ProgressTaskSettings.cs
Normal file
@ -0,0 +1,20 @@
|
||||
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;
|
||||
}
|
||||
}
|
81
src/Spectre.Console/Widgets/Progress/ProgressTaskState.cs
Normal file
81
src/Spectre.Console/Widgets/Progress/ProgressTaskState.cs
Normal file
@ -0,0 +1,81 @@
|
||||
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];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,119 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using Spectre.Console.Rendering;
|
||||
|
||||
namespace Spectre.Console.Internal
|
||||
{
|
||||
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 TimeSpan _lastUpdate;
|
||||
|
||||
public override TimeSpan RefreshRate { get; }
|
||||
|
||||
public DefaultProgressRenderer(IAnsiConsole console, List<ProgressColumn> columns, TimeSpan refreshRate)
|
||||
{
|
||||
_console = console ?? throw new ArgumentNullException(nameof(console));
|
||||
_columns = columns ?? throw new ArgumentNullException(nameof(columns));
|
||||
_live = new LiveRenderable();
|
||||
_lock = new object();
|
||||
_stopwatch = new Stopwatch();
|
||||
_lastUpdate = TimeSpan.Zero;
|
||||
|
||||
RefreshRate = refreshRate;
|
||||
}
|
||||
|
||||
public override void Started()
|
||||
{
|
||||
_console.Cursor.Hide();
|
||||
}
|
||||
|
||||
public override void Completed(bool clear)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
if (clear)
|
||||
{
|
||||
_console.Render(_live.RestoreCursor());
|
||||
}
|
||||
else
|
||||
{
|
||||
_console.WriteLine();
|
||||
}
|
||||
|
||||
_console.Cursor.Show();
|
||||
}
|
||||
}
|
||||
|
||||
public override void Update(ProgressContext context)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
if (!_stopwatch.IsRunning)
|
||||
{
|
||||
_stopwatch.Start();
|
||||
}
|
||||
|
||||
var renderContext = new RenderContext(_console.Encoding, _console.Capabilities.LegacyConsole);
|
||||
|
||||
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())
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,125 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Spectre.Console.Rendering;
|
||||
|
||||
namespace Spectre.Console.Internal
|
||||
{
|
||||
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)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Spectre.Console.Rendering;
|
||||
|
||||
namespace Spectre.Console.Internal
|
||||
{
|
||||
internal sealed class StatusFallbackRenderer : ProgressRenderer
|
||||
{
|
||||
private readonly object _lock;
|
||||
private IRenderable? _renderable;
|
||||
private string? _lastStatus;
|
||||
|
||||
public override TimeSpan RefreshRate => TimeSpan.FromMilliseconds(100);
|
||||
|
||||
public StatusFallbackRenderer()
|
||||
{
|
||||
_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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
1873
src/Spectre.Console/Widgets/Progress/Spinner.Generated.cs
Normal file
1873
src/Spectre.Console/Widgets/Progress/Spinner.Generated.cs
Normal file
File diff suppressed because it is too large
Load Diff
27
src/Spectre.Console/Widgets/Progress/Spinner.cs
Normal file
27
src/Spectre.Console/Widgets/Progress/Spinner.cs
Normal file
@ -0,0 +1,27 @@
|
||||
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; }
|
||||
}
|
||||
}
|
89
src/Spectre.Console/Widgets/Progress/Status.cs
Normal file
89
src/Spectre.Console/Widgets/Progress/Status.cs
Normal file
@ -0,0 +1,89 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Spectre.Console.Internal;
|
||||
|
||||
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">he 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>
|
||||
/// <param name="status">The status to display.</param>
|
||||
/// <param name="action">he action to execute.</param>
|
||||
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
|
||||
public async Task StartAsync(string status, Func<StatusContext, Task> action)
|
||||
{
|
||||
// Set the progress columns
|
||||
var spinnerColumn = new SpinnerColumn(Spinner ?? Spinner.Known.Default)
|
||||
{
|
||||
Style = SpinnerStyle ?? Style.Plain,
|
||||
};
|
||||
|
||||
var progress = new Progress(_console)
|
||||
{
|
||||
FallbackRenderer = new StatusFallbackRenderer(),
|
||||
AutoClear = true,
|
||||
AutoRefresh = AutoRefresh,
|
||||
};
|
||||
|
||||
progress.Columns(new ProgressColumn[]
|
||||
{
|
||||
spinnerColumn,
|
||||
new TaskDescriptionColumn(),
|
||||
});
|
||||
|
||||
await progress.StartAsync(async ctx =>
|
||||
{
|
||||
var statusContext = new StatusContext(ctx, ctx.AddTask(status), spinnerColumn);
|
||||
await action(statusContext).ConfigureAwait(false);
|
||||
}).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
76
src/Spectre.Console/Widgets/Progress/StatusContext.cs
Normal file
76
src/Spectre.Console/Widgets/Progress/StatusContext.cs
Normal file
@ -0,0 +1,76 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
12
src/Spectre.Console/Widgets/Prompt/DefaultPromptValue.cs
Normal file
12
src/Spectre.Console/Widgets/Prompt/DefaultPromptValue.cs
Normal file
@ -0,0 +1,12 @@
|
||||
namespace Spectre.Console
|
||||
{
|
||||
internal sealed class DefaultPromptValue<T>
|
||||
{
|
||||
public T Value { get; }
|
||||
|
||||
public DefaultPromptValue(T value)
|
||||
{
|
||||
Value = value;
|
||||
}
|
||||
}
|
||||
}
|
16
src/Spectre.Console/Widgets/Prompt/IPrompt.cs
Normal file
16
src/Spectre.Console/Widgets/Prompt/IPrompt.cs
Normal file
@ -0,0 +1,16 @@
|
||||
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);
|
||||
}
|
||||
}
|
212
src/Spectre.Console/Widgets/Prompt/TextPrompt.cs
Normal file
212
src/Spectre.Console/Widgets/Prompt/TextPrompt.cs
Normal file
@ -0,0 +1,212 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Spectre.Console.Internal;
|
||||
|
||||
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;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the prompt style.
|
||||
/// </summary>
|
||||
public Style? PromptStyle { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the list of choices.
|
||||
/// </summary>
|
||||
public HashSet<T> Choices { get; }
|
||||
|
||||
/// <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 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, IEqualityComparer<T>? comparer = null)
|
||||
{
|
||||
_prompt = prompt;
|
||||
|
||||
Choices = new HashSet<T>(comparer ?? EqualityComparer<T>.Default);
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
if (console is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(console));
|
||||
}
|
||||
|
||||
var promptStyle = PromptStyle ?? Style.Plain;
|
||||
var choices = Choices.Select(choice => TypeConverterHelper.ConvertToString(choice));
|
||||
|
||||
WritePrompt(console);
|
||||
|
||||
while (true)
|
||||
{
|
||||
var input = console.ReadLine(promptStyle, IsSecret, choices);
|
||||
|
||||
// Nothing entered?
|
||||
if (string.IsNullOrWhiteSpace(input))
|
||||
{
|
||||
if (DefaultValue != null)
|
||||
{
|
||||
console.Write(TypeConverterHelper.ConvertToString(DefaultValue.Value), promptStyle);
|
||||
console.WriteLine();
|
||||
return DefaultValue.Value;
|
||||
}
|
||||
|
||||
if (!AllowEmpty)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
console.WriteLine();
|
||||
|
||||
// Try convert the value to the expected type.
|
||||
if (!TypeConverterHelper.TryConvertFromString<T>(input, out var result) || result == null)
|
||||
{
|
||||
console.MarkupLine(ValidationErrorMessage);
|
||||
WritePrompt(console);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (Choices.Count > 0)
|
||||
{
|
||||
if (Choices.Contains(result))
|
||||
{
|
||||
return result;
|
||||
}
|
||||
else
|
||||
{
|
||||
console.MarkupLine(InvalidChoiceMessage);
|
||||
WritePrompt(console);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Run all validators
|
||||
if (!ValidateResult(result, out var validationMessage))
|
||||
{
|
||||
console.MarkupLine(validationMessage);
|
||||
WritePrompt(console);
|
||||
continue;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <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());
|
||||
|
||||
if (ShowChoices && Choices.Count > 0)
|
||||
{
|
||||
var choices = string.Join("/", Choices.Select(choice => TypeConverterHelper.ConvertToString(choice)));
|
||||
builder.AppendFormat(CultureInfo.InvariantCulture, " [blue][[{0}]][/]", choices);
|
||||
}
|
||||
|
||||
if (ShowDefaultValue && DefaultValue != null)
|
||||
{
|
||||
builder.AppendFormat(
|
||||
CultureInfo.InvariantCulture,
|
||||
" [green]({0})[/]",
|
||||
TypeConverterHelper.ConvertToString(DefaultValue.Value));
|
||||
}
|
||||
|
||||
var markup = builder.ToString().Trim();
|
||||
if (!markup.EndsWith("?", StringComparison.OrdinalIgnoreCase) &&
|
||||
!markup.EndsWith(":", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
266
src/Spectre.Console/Widgets/Prompt/TextPromptExtensions.cs
Normal file
266
src/Spectre.Console/Widgets/Prompt/TextPromptExtensions.cs
Normal file
@ -0,0 +1,266 @@
|
||||
using System;
|
||||
|
||||
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>
|
||||
/// 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;
|
||||
}
|
||||
}
|
||||
}
|
43
src/Spectre.Console/Widgets/Prompt/ValidationResult.cs
Normal file
43
src/Spectre.Console/Widgets/Prompt/ValidationResult.cs
Normal file
@ -0,0 +1,43 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user