GithubHelp home page GithubHelp logo

nikiforovall / spectre.console.extensions Goto Github PK

View Code? Open in Web Editor NEW
20.0 3.0 3.0 115 KB

A library that extends Spectre.Console to build beautiful console apps.

Home Page: https://nikiforovall.github.io/dotnet/2022/01/22/building-console-application-with-spectre-console.html

License: MIT License

C# 100.00%
console dotnet dotnet-core extension spectre-console good-first-issue

spectre.console.extensions's Introduction

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.

spectre.console.extensions's People

Contributors

nikiforovall avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

spectre.console.extensions's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.