GithubHelp home page GithubHelp logo

anthrax3 / shellprogressbar Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mpdreamz/shellprogressbar

1.0 0.0 0.0 3.48 MB

ShellProgressBar - display progress in your console application

License: MIT License

C# 99.73% Batchfile 0.27%

shellprogressbar's Introduction

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"); 
}

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 👍

shellprogressbar's People

Contributors

0xced avatar dlech avatar gakera avatar hakakou avatar jstallm avatar kjaleshire avatar klemmchr avatar mandreko avatar mpdreamz avatar sckelemen avatar thoemmi avatar toyz avatar

Stargazers

 avatar

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.