Add support for indeterminate progress

This commit also changes the behavior of ProgressContext.IsFinished.
Only tasks that have been started will be taken into consideration,
and not indeterminate tasks.

Closes #329
Closes #331
This commit is contained in:
Patrik Svensson
2021-04-02 18:41:25 +02:00
committed by Phil Scott
parent 6121203fee
commit 6f16081f42
15 changed files with 196 additions and 42 deletions

View File

@ -28,6 +28,11 @@ namespace Spectre.Console
/// </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)
{
@ -39,6 +44,8 @@ namespace Spectre.Console
CompletedStyle = CompletedStyle,
FinishedStyle = FinishedStyle,
RemainingStyle = RemainingStyle,
IndeterminateStyle = IndeterminateStyle,
IsIndeterminate = task.IsIndeterminate,
};
}
}

View File

@ -16,9 +16,9 @@ namespace Spectre.Console
private int _taskId;
/// <summary>
/// Gets a value indicating whether or not all tasks have completed.
/// Gets a value indicating whether or not all started tasks have completed.
/// </summary>
public bool IsFinished => _tasks.All(task => task.IsFinished);
public bool IsFinished => _tasks.Where(x => x.IsStarted).All(task => task.IsFinished);
internal ProgressContext(IAnsiConsole console, ProgressRenderer renderer)
{
@ -28,20 +28,41 @@ namespace Spectre.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 = null)
public ProgressTask AddTask(string description, ProgressTaskSettings settings)
{
if (settings is null)
{
throw new ArgumentNullException(nameof(settings));
}
lock (_taskLock)
{
settings ??= new ProgressTaskSettings();
var task = new ProgressTask(_taskId++, description, settings.MaxValue, settings.AutoStart);
_tasks.Add(task);
return task;
}
}

View File

@ -93,6 +93,12 @@ namespace Spectre.Console
/// </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>

View File

@ -16,5 +16,10 @@ namespace Spectre.Console
/// 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

@ -62,7 +62,7 @@ namespace Spectre.Console
_stopwatch.Start();
}
var renderContext = new RenderContext(_console.Profile.Capabilities);
var renderContext = new RenderContext(_console.Profile.ColorSystem, _console.Profile.Capabilities);
var delta = _stopwatch.Elapsed - _lastUpdate;
_lastUpdate = _stopwatch.Elapsed;