From 89014502837f7763c8c9dca1b13db79b664daaa8 Mon Sep 17 00:00:00 2001 From: Thomas Freudenberg Date: Wed, 6 Jan 2021 08:08:38 +0100 Subject: [PATCH] Allow returning a result from Progress.StartAsync/Status.StartAsync --- .../Widgets/Progress/Progress.cs | 28 +++++++++++++++-- .../Widgets/Progress/Status.cs | 30 +++++++++++++++++-- 2 files changed, 54 insertions(+), 4 deletions(-) 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(async progressContext => + { + await action(progressContext); + return default; + }); + } + + /// + /// Starts the progress task list. + /// + /// The action to execute. + /// The result type of task. + /// A representing the asynchronous operation. + public async Task StartAsync(Func> 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 /// 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) { @@ -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); } }