CLI.ProgressBar 1.0.4

dotnet add package CLI.ProgressBar --version 1.0.4
                    
NuGet\Install-Package CLI.ProgressBar -Version 1.0.4
                    
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="CLI.ProgressBar" Version="1.0.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CLI.ProgressBar" Version="1.0.4" />
                    
Directory.Packages.props
<PackageReference Include="CLI.ProgressBar" />
                    
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 CLI.ProgressBar --version 1.0.4
                    
#r "nuget: CLI.ProgressBar, 1.0.4"
                    
#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 CLI.ProgressBar@1.0.4
                    
#: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=CLI.ProgressBar&version=1.0.4
                    
Install as a Cake Addin
#tool nuget:?package=CLI.ProgressBar&version=1.0.4
                    
Install as a Cake Tool

CLI.ProgressBar

NuGet Downloads Issues License: MIT

A simple progress bar for Console applications, cross platform ready, built on top of .NET 8.0 without any external dependency.

Setup

Install CLI.ProgressBar package running the following command line:

dotnet add package CLI.ProgressBar

Usage

In order to use the progress bar include the following namespace:

using CliProgressBar;

Default Progress Bar:

Basic Usage

using var progressBar = new ProgressBar();
for (int i = 0; i < 100; i++)
{
	progressBar.AddSteps(1);
	Thread.Sleep(100);
}

Variant Progress Bar:

Variant Usage

using var progressBar = new ProgressBar(Layout.Unix);
for (int i = 0; i < 100; i++)
{
	progressBar.AddSteps(1);
	Thread.Sleep(100);
}

Progress Bar with console output:

Output Usage

using var progressBar = new ProgressBar();
for (int i = 0; i < 100; i++)
{
	progressBar.WriteLine($"Line {i + 1}. Console left: {Console.GetCursorPosition().Left}, Console top: {Console.GetCursorPosition().Top}, Buffer Height: {Console.BufferHeight}, Window Height: {Console.WindowHeight}");
	progressBar.Report(i / 100f, $"step {i} of 100");
	Thread.Sleep(100);
}

The progress bar come with more possibilities of customization, check the sample project or the Customization section to see what is possible.

Update progress:

The ProgressBar class provide two way to update is progression:

  • By using the AddSteps method, which will increment the progress bar by the given number of steps:
	progressBar.AddSteps(1);
  • Or by using the Report method, which will set the progress bar to the given percentage expressed with a float value:
	progressBar.Report(0.5f);

Both methods accepts an optional string parameter used to set a message to display next to the progress bar:

	progressBar.Report(0.5f, "step 50 of 100");
	progressBar.AddSteps(1, "incremented by 1");

Update or define text:

The ProgressBar class provide two methods to update the underlying layout texts:

// Used to configure the main text displayed on the progress bar.
public void SetText(string text)
// Used to configure the additional text displayed on the progress bar, if null is passed the additional text will be hidden.
public void SetAdditionalText(string? text)

Customization

The ProgressBar is highly customizable, you can define the progress bar behaviors and appearance during it's initialization.

ProgressBar:

Property Type Default Value Description
layout Layout Layout.Default The layout object is provided by the package and is used to configure the aspect of the progress bar. 2 default layouts are provided Layout.Default and Layout.Unix.
maxStep int 100 Maximum step number allowed.
start bool True True to start showing immediatly the progress, otherwise False.
redirectConsoleOutput bool False Define if output should be redirected. Enable the progress bar rendering to manage redrawing on Console.WriteLine(string) instructions. It redirect the Console output implementing an internal intermediate TextWriter.

Layout:

The Layout object is used to configure the aspect of the progress bar. It is composed of 3 elements: a bar, a text and an additional text. The bar is the progression bar, the text is the principal text displayed on the progress bar and the additional text is a secondary optional text displayed on the progress bar.

Property Type Default Value Description
Bar BarLayout N/A* Object used to define the bar appearance.
Text Element<string> N/A* Principal text content of the layout.
AdditionalText Element<string>? null Secondary text content of the layout.

* Values are required

BarLayout:

The BarLayout object define the appearance of the progression bar.

Property Type Default Value Description
ProgressIndicator Element<char> N/A* Fullfilled indicator character (can be ASCII character).
PendingIndicator Element<char> N/A* Remaining indicator character (can be ASCII character).
Direction BarDirection BarDirection.Forward Determine the position of the percentage relative to the progression bar. Forward is after the bar and Reverse is before.
Position LayoutPosition LayoutPosition.Center Position of the bar in the parent layout.
BracketOptions BracketLayout? null Define the brackets to display around the bar and/or percentage. If value is null there is no brackets displayed.

* Values are required

You can define the desired appearance of the progress bar defining your own Layout like the following:

var bar = new BarLayout(
	new Element<char>('█') { ForegroundColor = ConsoleColor.Green }, 
	new Element<char>('░') { ForegroundColor = ConsoleColor.DarkGreen }
) {
	Direction = BarDirection.Reverse,
	Position = LayoutPosition.Center,
	BracketOptions = BracketLayout.Percentage | BracketLayout.Bar
}

var layout = new Layout(bar, new Element<string>("Principal text"), new Element<string>("Secondary text"));
using var progressBar = new ProgressBar(layout);
// your logic here

Contributing

Any contributions are welcome. 🙌

if you've found a bug, or a possible improvment you would wish to bring to this project, feel free to open an issue or to create a pull request.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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.
  • net8.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.4 177 12/14/2024
1.0.3 109 11/28/2024