diff --git a/src/Spectre.Console/Widgets/Progress/Progress.cs b/src/Spectre.Console/Widgets/Progress/Progress.cs index 76d6d0e..0e34b6d 100644 --- a/src/Spectre.Console/Widgets/Progress/Progress.cs +++ b/src/Spectre.Console/Widgets/Progress/Progress.cs @@ -80,9 +80,31 @@ namespace Spectre.Console throw new ArgumentNullException(nameof(action)); } + _ = await StartAsync<object?>(async progressContext => + { + await action(progressContext); + return default; + }); + } + + /// <summary> + /// Starts the progress task list. + /// </summary> + /// <param name="action">The action to execute.</param> + /// <typeparam name="T">The result type of task.</typeparam> + /// <returns>A <see cref="Task{T}"/> representing the asynchronous operation.</returns> + public async Task<T> StartAsync<T>(Func<ProgressContext, Task<T>> action) + { + if (action is null) + { + throw new ArgumentNullException(nameof(action)); + } + var renderer = CreateRenderer(); renderer.Started(); + T result; + try { using (new RenderHookScope(_console, renderer)) @@ -93,12 +115,12 @@ namespace Spectre.Console { using (var thread = new ProgressRefreshThread(context, renderer.RefreshRate)) { - await action(context).ConfigureAwait(false); + result = await action(context).ConfigureAwait(false); } } else { - await action(context).ConfigureAwait(false); + result = await action(context).ConfigureAwait(false); } context.Refresh(); @@ -108,6 +130,8 @@ namespace Spectre.Console { renderer.Completed(AutoClear); } + + return result; } private ProgressRenderer CreateRenderer() diff --git a/src/Spectre.Console/Widgets/Progress/Status.cs b/src/Spectre.Console/Widgets/Progress/Status.cs index 0a98bcb..94634fb 100644 --- a/src/Spectre.Console/Widgets/Progress/Status.cs +++ b/src/Spectre.Console/Widgets/Progress/Status.cs @@ -60,6 +60,32 @@ namespace Spectre.Console /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> public async Task StartAsync(string status, Func<StatusContext, Task> action) { + if (action is null) + { + throw new ArgumentNullException(nameof(action)); + } + + _ = await StartAsync<object?>(status, async statusContext => + { + await action(statusContext); + return default; + }); + } + + /// <summary> + /// Starts a new status display. + /// </summary> + /// <param name="status">The status to display.</param> + /// <param name="action">he action to execute.</param> + /// <typeparam name="T">The result type of task.</typeparam> + /// <returns>A <see cref="Task{T}"/> representing the asynchronous operation.</returns> + public async Task<T> StartAsync<T>(string status, Func<StatusContext, Task<T>> action) + { + if (action is null) + { + throw new ArgumentNullException(nameof(action)); + } + // Set the progress columns var spinnerColumn = new SpinnerColumn(Spinner ?? Spinner.Known.Default) { @@ -79,10 +105,10 @@ namespace Spectre.Console new TaskDescriptionColumn(), }); - await progress.StartAsync(async ctx => + return await progress.StartAsync(async ctx => { var statusContext = new StatusContext(ctx, ctx.AddTask(status), spinnerColumn); - await action(statusContext).ConfigureAwait(false); + return await action(statusContext).ConfigureAwait(false); }).ConfigureAwait(false); } }