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> /// <summary>
/// Indicates that the progress bars should be hidden. /// Indicates that the progress bars should be hidden.
/// </summary> /// </summary>
Disable, Never,
} }

View file

@ -31,12 +31,20 @@ public class StatusPipelineObserver : IPipelineObserver
/// </summary> /// </summary>
public SpectreDisplayMode DisplayMode { get; set; } 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 /> /// <inheritdoc />
public void OnChangeState( public void OnChangeState(
PipelineRunner runner, PipelineRunner runner,
PipelineRunnerState newState) PipelineRunnerState newState)
{ {
this.queue.Enqueue( this.Enqueue(
new QueueItem(runner) new QueueItem(runner)
{ {
NewState = newState, NewState = newState,
@ -71,7 +79,7 @@ public class StatusPipelineObserver : IPipelineObserver
/// <inheritdoc /> /// <inheritdoc />
public void OnRunStarted(PipelineRunner runner, int inputCount) public void OnRunStarted(PipelineRunner runner, int inputCount)
{ {
this.queue.Enqueue( this.Enqueue(
new QueueItem(runner) new QueueItem(runner)
{ {
Maximum = inputCount, Maximum = inputCount,
@ -81,7 +89,7 @@ public class StatusPipelineObserver : IPipelineObserver
/// <inheritdoc /> /// <inheritdoc />
public void OnRunStep(PipelineRunner runner) public void OnRunStep(PipelineRunner runner)
{ {
this.queue.Enqueue( this.Enqueue(
new QueueItem(runner) new QueueItem(runner)
{ {
Increment = 1, 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() private void Ensure()
{ {
if (this.progress != null) if (!this.IsEnabled || this.progress != null)
{ {
return; return;
} }
@ -124,7 +140,12 @@ public class StatusPipelineObserver : IPipelineObserver
.Progress() .Progress()
.AutoClear(true) .AutoClear(true)
.HideCompleted(true) .HideCompleted(true)
.AutoRefresh(false); .AutoRefresh(false)
.Columns(
new TaskDescriptionColumn(),
new ProgressBarColumn(),
new PercentageColumn(),
new ProgressRemainingColumn());
this.progress.StartAsync(this.Update); this.progress.StartAsync(this.Update);
} }
@ -147,6 +168,12 @@ public class StatusPipelineObserver : IPipelineObserver
private async Task Update(ProgressContext context) 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. // Loop as long as we have at least one task.
while (!this.states.IsEmpty || !this.queue.IsEmpty) while (!this.states.IsEmpty || !this.queue.IsEmpty)
{ {
@ -203,8 +230,7 @@ public class StatusPipelineObserver : IPipelineObserver
case PipelineRunnerState.Finalizing: case PipelineRunnerState.Finalizing:
default: default:
this.GetOrCreateTask(context, runner) this.GetOrCreateTask(context, runner)
.Description = .Description = runner.Pipeline.ToString()!;
runner.Pipeline + " - " + newState;
break; break;
} }
} }

View file

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