using System; using System.Collections.Generic; using System.Linq; namespace Spectre.Console; /// /// Represents a context that can be used to interact with a . /// public sealed class ProgressContext { private readonly List _tasks; private readonly object _taskLock; private readonly IAnsiConsole _console; private readonly ProgressRenderer _renderer; private int _taskId; /// /// Gets a value indicating whether or not all started tasks have completed. /// public bool IsFinished => _tasks.Where(x => x.IsStarted).All(task => task.IsFinished); internal ProgressContext(IAnsiConsole console, ProgressRenderer renderer) { _tasks = new List(); _taskLock = new object(); _console = console ?? throw new ArgumentNullException(nameof(console)); _renderer = renderer ?? throw new ArgumentNullException(nameof(renderer)); } /// /// Adds a task. /// /// The task description. /// Whether or not the task should start immediately. /// The task's max value. /// The newly created task. public ProgressTask AddTask(string description, bool autoStart = true, double maxValue = 100) { return AddTask(description, new ProgressTaskSettings { AutoStart = autoStart, MaxValue = maxValue, }); } /// /// Adds a task. /// /// The task description. /// The task settings. /// The newly created task. public ProgressTask AddTask(string description, ProgressTaskSettings settings) { if (settings is null) { throw new ArgumentNullException(nameof(settings)); } lock (_taskLock) { var task = new ProgressTask(_taskId++, description, settings.MaxValue, settings.AutoStart); _tasks.Add(task); return task; } } /// /// Refreshes the current progress. /// public void Refresh() { _renderer.Update(this); _console.Write(new ControlCode(string.Empty)); } internal IReadOnlyList GetTasks() { lock (_taskLock) { return new List(_tasks); } } }