mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-06-19 13:28:16 +08:00
Add enhancements to progress widget
* Adds TransferSpeedColumn * Adds DownloadedColumn * Adds ElapsedTimeColumn * Minor enhancements to existing columns
This commit is contained in:

committed by
Patrik Svensson

parent
d87d8e4422
commit
07db28bb6f
@ -0,0 +1,42 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using Spectre.Console.Internal;
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
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("-:--:--");
|
||||
}
|
||||
|
||||
return new Text($"{elapsed.Value:h\\:mm\\:ss}", Style ?? Style.Plain);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override int? GetColumnWidth(RenderContext context)
|
||||
{
|
||||
return 7;
|
||||
}
|
||||
}
|
||||
}
|
@ -16,6 +16,7 @@ namespace Spectre.Console
|
||||
private readonly object _lock;
|
||||
private Spinner _spinner;
|
||||
private int? _maxWidth;
|
||||
private string? _completed;
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected internal override bool NoWrap => true;
|
||||
@ -36,6 +37,25 @@ namespace Spectre.Console
|
||||
}
|
||||
}
|
||||
|
||||
/// <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 completed style.
|
||||
/// </summary>
|
||||
public Style? CompletedStyle { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the style of the spinner.
|
||||
/// </summary>
|
||||
@ -67,7 +87,7 @@ namespace Spectre.Console
|
||||
|
||||
if (!task.IsStarted || task.IsFinished)
|
||||
{
|
||||
return new Markup(new string(' ', GetMaxWidth(context)));
|
||||
return new Markup(CompletedText ?? " ", CompletedStyle ?? Style.Plain);
|
||||
}
|
||||
|
||||
var accumulated = task.State.Update<double>(ACCUMULATED, acc => acc + deltaTime.TotalMilliseconds);
|
||||
@ -97,7 +117,9 @@ namespace Spectre.Console
|
||||
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));
|
||||
_maxWidth = Math.Max(
|
||||
((IRenderable)new Markup(CompletedText ?? " ")).Measure(context, int.MaxValue).Max,
|
||||
spinner.Frames.Max(frame => Cell.GetCellLength(context, frame)));
|
||||
}
|
||||
|
||||
return _maxWidth.Value;
|
||||
|
@ -0,0 +1,30 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using Spectre.Console.Internal;
|
||||
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)));
|
||||
}
|
||||
}
|
||||
}
|
@ -89,7 +89,14 @@ namespace Spectre.Console
|
||||
/// </summary>
|
||||
public TimeSpan? RemainingTime => GetRemainingTime();
|
||||
|
||||
internal ProgressTask(int id, string description, double maxValue, bool autoStart)
|
||||
/// <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();
|
||||
|
Reference in New Issue
Block a user