using System; using System.Threading.Tasks; using Spectre.Console.Internal; namespace Spectre.Console { /// /// Represents a status display. /// public sealed class Status { private readonly IAnsiConsole _console; /// /// Gets or sets the spinner. /// public Spinner? Spinner { get; set; } /// /// Gets or sets the spinner style. /// public Style? SpinnerStyle { get; set; } = new Style(foreground: Color.Yellow); /// /// Gets or sets a value indicating whether or not status /// should auto refresh. Defaults to true. /// public bool AutoRefresh { get; set; } = true; /// /// Initializes a new instance of the class. /// /// The console. public Status(IAnsiConsole console) { _console = console ?? throw new ArgumentNullException(nameof(console)); } /// /// Starts a new status display. /// /// The status to display. /// he action to execute. public void Start(string status, Action action) { var task = StartAsync(status, ctx => { action(ctx); return Task.CompletedTask; }); task.GetAwaiter().GetResult(); } /// /// Starts a new status display. /// /// The status to display. /// he action to execute. /// A representing the asynchronous operation. public async Task StartAsync(string status, Func action) { if (action is null) { throw new ArgumentNullException(nameof(action)); } _ = await StartAsync(status, async statusContext => { await action(statusContext); return default; }); } /// /// Starts a new status display. /// /// The status to display. /// he action to execute. /// The result type of task. /// A representing the asynchronous operation. public async Task StartAsync(string status, Func> action) { if (action is null) { throw new ArgumentNullException(nameof(action)); } // Set the progress columns var spinnerColumn = new SpinnerColumn(Spinner ?? Spinner.Known.Default) { Style = SpinnerStyle ?? Style.Plain, }; var progress = new Progress(_console) { FallbackRenderer = new StatusFallbackRenderer(), AutoClear = true, AutoRefresh = AutoRefresh, }; progress.Columns(new ProgressColumn[] { spinnerColumn, new TaskDescriptionColumn(), }); return await progress.StartAsync(async ctx => { var statusContext = new StatusContext(ctx, ctx.AddTask(status), spinnerColumn); return await action(statusContext).ConfigureAwait(false); }).ConfigureAwait(false); } } }