mfgames-cil/examples/SampleTool/Commands/SpectreCommand.cs

88 lines
2.6 KiB
C#

/*
* 2023-09-02 DREM: The SpectreConsole command didn't give much benefit for the
* cost of occasionally trashing the console window due to colors, the extra
* space after progress bars went away, and a few other visual quirks. So it was
* removed but I'm leaving the code here in case things change.
*/
#if REMOVED
using System;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.Threading;
using System.Threading.Tasks;
using MfGames.ToolBuilder;
using Microsoft.Extensions.Logging;
using Spectre.Console;
namespace SampleTool.Commands;
public class SpectreCommand : Command, ICommandHandler, ITopCommand
{
private readonly ILogger<LogCommand> logger;
/// <inheritdoc />
public SpectreCommand(ILoggerFactory loggerFactory)
: base("spectre", "Shows various SpectreConsole features")
{
this.logger = loggerFactory.CreateLogger<LogCommand>();
this.Handler = this;
}
/// <inheritdoc />
public int Invoke(InvocationContext context)
{
return this.InvokeAsync(context).Result;
}
/// <inheritdoc />
public async Task<int> InvokeAsync(InvocationContext context)
{
// Display a message ahead of this.
this.logger.LogInformation("Before things happened.");
// Show a progress bar.
CancellationToken cancellationToken = context.GetCancellationToken();
DateTime last = DateTime.UtcNow;
await AnsiConsole
.Progress()
.AutoClear(true)
.StartAsync(async ctx =>
{
ProgressTask task1 = ctx.AddTask("[green]Fast Task[/]");
ProgressTask task2 = ctx.AddTask("[green]Slow Task[/]");
while (!ctx.IsFinished)
{
// See if we're cancelled.
if (cancellationToken.IsCancellationRequested)
{
break;
}
// Simulate some work
await Task.Delay(25, cancellationToken);
if (DateTime.UtcNow - last > TimeSpan.FromMilliseconds(500))
{
this.logger.LogWarning("Working...");
last = DateTime.UtcNow;
}
await Task.Delay(25, cancellationToken);
// Increment
task1.Increment(2.0);
task2.Increment(1.0);
}
});
// Report we're done.
this.logger.LogInformation("Done");
return 0;
}
}
#endif