Files
aeqw89.tools.Publish/aeqw89.tools.Publish/Staging.cs
T
2026-06-04 16:45:35 +10:00

47 lines
1.8 KiB
C#

using System.Diagnostics;
using Spectre.Console;
using static aeqw89.tools.Publish.Program;
namespace aeqw89.tools.Publish;
internal record Stager(RunContext RunContext) {
public async Task<T?> Spinner<T>(string name, Func<RunContext, StatusContext, Task<Result<T, ReadableError>>> factory) {
try {
var result = AnsiConsole.Status()
.Spinner(Spectre.Console.Spinner.Known.Dots)
.Start(name, async ctx => await factory(RunContext, ctx));
return (await result).Unwrap(RunContext);
} catch (Exception e) {
ShowError(string.Format(Exceptions.generic_error.EscapeMarkup(), e.ToString().EscapeMarkup()));
RunContext.Restore();
return default;
}
throw new UnreachableException();
}
public ProgressStager Progress() {
return new ProgressStager(RunContext, [], AnsiConsole.Progress()
.AutoClear(true)
.HideCompleted(false)
.Columns([
new TaskDescriptionColumn(),
new ProgressBarColumn()
.RemainingStyle(Style.Parse("dim gray slowblink"))
.CompletedStyle(Style.Parse("green strikethrough"))
.FinishedStyle("green strikethrough"),
new DownloadedColumn(),
new RemainingTimeColumn(),
new TransferSpeedColumn(),
]));
}
}
internal record TaskWithProgress<T>(string Name, Task<T> Value);
internal record ProgressStager(RunContext RunContext, Task[] Tasks, Progress Progress) {
public TaskWithProgress<T> Run<T>(string Name, Func<RunContext, ProgressContext, Task<T>> factory) {
return new(Name, Progress.StartAsync(async ctx => {
return await factory(RunContext, ctx);
}));
}
}