fix: added the ability to disable progress bars

This commit is contained in:
D. Moonfire 2023-08-02 11:42:38 -05:00
parent 5c58b181e6
commit a2fd6a9f89
4 changed files with 80 additions and 8 deletions

View file

@ -0,0 +1,40 @@
using Spectre.Console;
using Spectre.Console.Rendering;
namespace MfGames.Nitride.Pipelines.Observers;
/// <summary>
/// A column showing item count.
/// </summary>
public sealed class ProgressRemainingColumn : ProgressColumn
{
/// <inheritdoc />
public override IRenderable Render(
RenderOptions options,
ProgressTask task,
TimeSpan deltaTime)
{
double total = task.MaxValue;
if (task.IsIndeterminate)
{
return new Markup("");
}
if (task.IsFinished)
{
return new Markup(
string.Format(
"[green]{0:N0}[/]",
total));
}
double value = task.Value;
return new Markup(
string.Format(
"{0:N0}[grey]/[/]{1:N0} [grey][/]",
value,
total));
}
}

View file

@ -16,5 +16,5 @@ public enum SpectreDisplayMode
/// <summary>
/// Indicates that the progress bars should be hidden.
/// </summary>
Disable,
Never,
}

View file

@ -31,12 +31,20 @@ public class StatusPipelineObserver : IPipelineObserver
/// </summary>
public SpectreDisplayMode DisplayMode { get; set; }
private bool IsEnabled => this.DisplayMode switch
{
SpectreDisplayMode.Auto => AnsiConsole.Profile.Capabilities.Interactive,
SpectreDisplayMode.Always => true,
SpectreDisplayMode.Never => false,
_ => throw new ArgumentOutOfRangeException()
};
/// <inheritdoc />
public void OnChangeState(
PipelineRunner runner,
PipelineRunnerState newState)
{
this.queue.Enqueue(
this.Enqueue(
new QueueItem(runner)
{
NewState = newState,
@ -71,7 +79,7 @@ public class StatusPipelineObserver : IPipelineObserver
/// <inheritdoc />
public void OnRunStarted(PipelineRunner runner, int inputCount)
{
this.queue.Enqueue(
this.Enqueue(
new QueueItem(runner)
{
Maximum = inputCount,
@ -81,7 +89,7 @@ public class StatusPipelineObserver : IPipelineObserver
/// <inheritdoc />
public void OnRunStep(PipelineRunner runner)
{
this.queue.Enqueue(
this.Enqueue(
new QueueItem(runner)
{
Increment = 1,
@ -113,9 +121,17 @@ public class StatusPipelineObserver : IPipelineObserver
{
}
private void Enqueue(QueueItem item)
{
if (this.IsEnabled)
{
this.queue.Enqueue(item);
}
}
private void Ensure()
{
if (this.progress != null)
if (!this.IsEnabled || this.progress != null)
{
return;
}
@ -124,7 +140,12 @@ public class StatusPipelineObserver : IPipelineObserver
.Progress()
.AutoClear(true)
.HideCompleted(true)
.AutoRefresh(false);
.AutoRefresh(false)
.Columns(
new TaskDescriptionColumn(),
new ProgressBarColumn(),
new PercentageColumn(),
new ProgressRemainingColumn());
this.progress.StartAsync(this.Update);
}
@ -147,6 +168,12 @@ public class StatusPipelineObserver : IPipelineObserver
private async Task Update(ProgressContext context)
{
// If we are not enabled, there is nothing to do.
if (!this.IsEnabled)
{
return;
}
// Loop as long as we have at least one task.
while (!this.states.IsEmpty || !this.queue.IsEmpty)
{
@ -203,8 +230,7 @@ public class StatusPipelineObserver : IPipelineObserver
case PipelineRunnerState.Finalizing:
default:
this.GetOrCreateTask(context, runner)
.Description =
runner.Pipeline + " - " + newState;
.Description = runner.Pipeline.ToString()!;
break;
}
}

View file

@ -2,6 +2,8 @@ using System.CommandLine.Parsing;
using Serilog;
using Spectre.Console;
namespace MfGames.ToolBuilder;
/// <summary>
@ -42,5 +44,9 @@ public class ToolBox
return Environment.ExitCode == 0 ? 1 : Environment.ExitCode;
}
finally
{
AnsiConsole.Reset();
}
}
}