Skip to content

NikiforovAll/Spectre.Console.Extensions

Repository files navigation

Spectre.Console.Extensions

GitHub Actions Status PRs Welcome

GitHub Actions Build History

The goal of this project is to extend Spectre.Console plugins with some niche functionality.

  • Progress with IProgress Adapter.
  • Progress with automatic reporting for HttpClient.
  • Table with DataTable.
Package Version Description
Spectre.Console.Extensions.Progress Nuget IProgress adapter and HttpClient reporting.
Spectre.Console.Extensions.Table Nuget DataTable and DataSet support.

Spectre.Console.Extensions.Progress NuGet Badge

Extensions for AnsiConsole.Progress

IProgress Adapter

Use spectre spinner with standard IProgress interface.

Motivation: To plug methods that accept IProgress so that reporting is declarative, familiar, convenient.

private static async Task RunSimpleExampleAsync()
{
    await BuildProgress().StartAsync(
        GenerateProgressTasks,
        (reporter) => RunSpinnerWithIProgress(reporter, TimeSpan.FromMilliseconds(500)),
        (reporter) => RunSpinnerWithIProgress(reporter, TimeSpan.FromSeconds(1)));

    // Collection of tasks to execute,
    // every task corresponds to following delegates sequentially.
    static IEnumerable<ProgressTask> GenerateProgressTasks(ProgressContext ctx)
    {
        yield return ctx.AddTask("Task1");
        yield return ctx.AddTask("Task2");
    }

    static async Task RunSpinnerWithIProgress(
        IProgress<double> reporter,
        TimeSpan delay)
    {
        var capacity = 100;
        var step = 10;
        while (capacity > 0)
        {
            reporter.Report(step);
            capacity -= step;
            await Task.Delay(delay);
        }
    }
}

Reporting for HttpClient

Run progress for a given HttpClient and HttpRequestMessage. Result is provided as System.IO.Stream.

Motivation: It is quite a common task to download something and have a spinner for that. Basically, you don't even wanna to bother with reporting in this case.

var message = new HttpRequestMessage(HttpMethod.Get, url);
var http = new HttpClient();
var description = "Downloading cats images";
await BuildProgress().StartAsync(http, message, taskDescription: description, DownloadCallback);

static async Task DownloadCallback(Stream stream) => {};

await BuildProgress()
    .WithHttp(http, request, description, DownloadCallback1)
    .WithHttp(http, request, description, DownloadCallback2)
    .StartAsync();

Spectre.Console.Extensions.Table NuGet Badge

Display System.Data.DataTable.

System.Data.DataTable dataTable = DataTableFactory();
var table = dataTable.FromDataTable().Border(TableBorder.Rounded);
AnsiConsole.Render();

TODO

  • Add xml-doc for existing public APIs.
  • Add statiq docs
  • Add unit tests.
  • Fix warnings; consider to TreatWarningsAsErrors

Examples

To see Spectre.Console in action, install the dotnet-example global tool.

> dotnet tool restore

Now you can list available examples in this repository:

> dotnet example

And to run an example:

> dotnet example iprogress

For more details, please see:

.
|-- Samples
|   |-- DataSet
|   |-- DataTable
|   |-- Directory.Build.props
|   |-- http-progress
|   |-- iprogress
|   `-- iprogress-http-client-multiple-calls
...

License

Copyright © Alexey Nikiforov.

Provided as-is under the MIT license. For more information see LICENSE.md.