57 lines
1.7 KiB
C#
57 lines
1.7 KiB
C#
using Spectre.Console;
|
|
using System.Linq;
|
|
|
|
namespace BadBuilder.Utilities
|
|
{
|
|
internal class ActionQueue
|
|
{
|
|
private readonly SortedDictionary<int, Queue<Func<Task>>> priorityQueue = new();
|
|
|
|
internal void EnqueueAction(Func<Task> action, int priority)
|
|
{
|
|
if (!priorityQueue.ContainsKey(priority))
|
|
{
|
|
priorityQueue[priority] = new Queue<Func<Task>>();
|
|
}
|
|
priorityQueue[priority].Enqueue(action);
|
|
}
|
|
|
|
internal async Task ExecuteActionsAsync()
|
|
{
|
|
int totalActions = priorityQueue.Values.Sum(queue => queue.Count);
|
|
if (totalActions == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
await AnsiConsole.Progress()
|
|
.Columns(
|
|
new TaskDescriptionColumn(),
|
|
new ProgressBarColumn(),
|
|
new PercentageColumn()
|
|
)
|
|
.StartAsync(async ctx =>
|
|
{
|
|
var copyTask = ctx.AddTask("Dateien auf das Laufwerk kopieren", new ProgressTaskSettings
|
|
{
|
|
AutoStart = true,
|
|
MaxValue = totalActions
|
|
});
|
|
|
|
foreach (var priority in priorityQueue.Keys.OrderByDescending(p => p))
|
|
{
|
|
var actions = priorityQueue[priority];
|
|
while (actions.Count > 0)
|
|
{
|
|
var action = actions.Dequeue();
|
|
await action();
|
|
copyTask.Increment(1);
|
|
}
|
|
}
|
|
});
|
|
|
|
priorityQueue.Clear();
|
|
}
|
|
}
|
|
}
|