Loic.Mpdreamz.shellprogressbar 1.0.0

dotnet add package Loic.Mpdreamz.shellprogressbar --version 1.0.0
                    
NuGet\Install-Package Loic.Mpdreamz.shellprogressbar -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Loic.Mpdreamz.shellprogressbar" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Loic.Mpdreamz.shellprogressbar" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Loic.Mpdreamz.shellprogressbar" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Loic.Mpdreamz.shellprogressbar --version 1.0.0
                    
#r "nuget: Loic.Mpdreamz.shellprogressbar, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Loic.Mpdreamz.shellprogressbar@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Loic.Mpdreamz.shellprogressbar&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Loic.Mpdreamz.shellprogressbar&version=1.0.0
                    
Install as a Cake Tool

ShellProgressBar

visualize (concurrent) progress in your console application

This is a great little library to visualize long running command line tasks.

.NET Core ready!

It also supports spawning child progress bars which allows you to visualize dependencies and concurrency rather nicely.

Tested on OSX

example osx

and Windows

example win cmd

(Powershell works too, see example further down)

Install

Get it on nuget: http://www.nuget.org/packages/ShellProgressBar/

Usage

Usage is really straightforward

const int totalTicks = 10;
var options = new ProgressBarOptions
{
    ProgressCharacter = '─',
    ProgressBarOnBottom = true
};
using (var pbar = new ProgressBar(totalTicks, "Initial message", options))
{
    pbar.Tick(); //will advance pbar to 1 out of 10.
    //we can also advance and update the progressbar text
    pbar.Tick("Step 2 of 10"); 
}

Reporting progression

There are two ways to report progression. You can use the Tick() function as described above. Alternatively you can report progression through an IProgress<T> instance that you obtain by calling AsProgress<T>() on the progress bar object.

For a simple case where the progress type is a float value between 0.0 and 1.0 that represents the completion percentage, use progressBar.AsProgress<float>():

using ProgressBar progressBar = new ProgressBar(10000, "My Progress Message");
IProgress progress = progressBar.AsProgress<float>();
progress.Report(0.25); // Advances the progress bar to 25%

See IntegrationWithIProgressExample.cs and IntegrationWithIProgressPercentageExample.cs in the src/ShellProgressBar.Example/Examples directory for full examples.

Options

Progress bar position

const int totalTicks = 10;
var options = new ProgressBarOptions
{
	ProgressCharacter = '─',
	ProgressBarOnBottom = true
};
using (var pbar = new ProgressBar(totalTicks, "progress bar is on the bottom now", options))
{
	TickToCompletion(pbar, totalTicks, sleep: 500);
}

By default the progress bar is at the top and the message at the bottom. This can be flipped around if so desired.

bar_on_bottom

Styling changes

const int totalTicks = 10;
var options = new ProgressBarOptions
{
	ForegroundColor = ConsoleColor.Yellow,
	ForegroundColorDone = ConsoleColor.DarkGreen,
	BackgroundColor = ConsoleColor.DarkGray,
	BackgroundCharacter = '\u2593'
};
using (var pbar = new ProgressBar(totalTicks, "showing off styling", options))
{
	TickToCompletion(pbar, totalTicks, sleep: 500);
}

Many aspects can be styled including foreground color, background (inactive portion) and changing the color on completion.

styling

No real time update

By default a timer will draw the screen every 500ms. You can configure the progressbar to only be drawn when .Tick() is called.

const int totalTicks = 5;
var options = new ProgressBarOptions
{
	DisplayTimeInRealTime = false
};
using (var pbar = new ProgressBar(totalTicks, "only draw progress on tick", options))
{
	TickToCompletion(pbar, totalTicks, sleep:1750);
}

If you look at the time passed you will see it skips 02:00

update_on_tick

Descendant progressbars

A progressbar can spawn child progress bars and each child can spawn its own progressbars. Each child can have its own styling options.

This is great to visualize concurrent running tasks.

const int totalTicks = 10;
var options = new ProgressBarOptions
{
	ForegroundColor = ConsoleColor.Yellow,
	BackgroundColor = ConsoleColor.DarkYellow,
	ProgressCharacter = '─'
};
var childOptions = new ProgressBarOptions
{
	ForegroundColor = ConsoleColor.Green,
	BackgroundColor = ConsoleColor.DarkGreen,
	ProgressCharacter = '─'
};
using (var pbar = new ProgressBar(totalTicks, "main progressbar", options))
{
	TickToCompletion(pbar, totalTicks, sleep: 10, childAction: () =>
	{
		using (var child = pbar.Spawn(totalTicks, "child actions", childOptions))
		{
			TickToCompletion(child, totalTicks, sleep: 100);
		}
	});
}

children

By default children will collapse when done, making room for new/concurrent progressbars.

You can keep them around by specifying CollapseWhenFinished = false

var childOptions = new ProgressBarOptions
{
	ForegroundColor = ConsoleColor.Green,
	BackgroundColor = ConsoleColor.DarkGreen,
	ProgressCharacter = '─',
	CollapseWhenFinished = false
};

children_no_collapse

FixedDurationBar

ProgressBar is great for visualizing tasks with an unknown runtime. If you have a task that you know takes a fixed amount of time there is also a FixedDurationBar subclass. FixedDurationBar will Tick() automatically but other then that all the options and usage are the same. Except it relies on the real time update feature so disabling that will throw.

FixedDurationBar exposes an IsCompleted and CompletedHandle

Credits

The initial implementation was inspired by this article. http://www.bytechaser.com/en/articles/ckcwh8nsyt/display-progress-bar-in-console-application-in-c.aspx

And obviously anyone who sends a PR to this repository 👍

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net5.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 0 7/31/2021