A simple .NET console spinner thingie. Targets .NET 10.
Different ways of entertaining the user while progress is progressing in console applications.
using(var spinner = new Spinner())
{
// (...)
// do stuff and periodically:
spinner.SetProgress(progressPercentage);
// (...)
}
which will look something like this:
Spinning on its own line...
/ 22 %
Spinning on its own line...
- 35 %
Spinning on its own line...
\ 86 %
Spinning on its own line...
| 96 %
A Spinner can be customized by overriding its animation characters like this:
var myAnimationChars = "0123456789";
using(var spinner = new Spinner(animationCharacters: myAnimationChars))
{
// (...)
}
using(var bar = new ProgressBar())
{
// (...)
// do stuff and periodically:
bar.SetProgress(progressPercentage);
// (...)
}
which will look something like this:
|===================----------------| 25 % |-----------------------------------|
|===================================| 68 % |==========-------------------------|
|==================================| 100 % |===================================|
The ProgressBar can have its characters customized like this:
using(var bar = new ProgressBar(completedChar: '#'))
{
// (...)
}
which will turn it into this:
|###################----------------| 25 % |-----------------------------------|
Console.Write("Doing something: ")
using(var bar = new InlineProgressBar())
{
// (...)
// do stuff and periodically:
bar.SetProgress(progressPercentage);
// (...)
}
which will look something like this:
Doing something: [##--------] 29 %
Doing something: [######----] 63 %
Doing something: [##########] 100 %
The InlineProgressBar can have its characters customized like this:
Console.Write("See plus plus: ")
using(var bar = new InlineProgressBar(completedChar: '+', notCompletedChar: ' ', width: 20))
{
// (...)
}
which will turn it into this:
See plus plus: [+++++++++++++++++ ] 85 %
Console.WriteLine("Processing data..");
using (var bar = new IndefiniteProgressBar())
{
// (...)
// do stuff and from time to time:
bar.SetAction($"Processed {dataProcessedMb:0.00} MB");
// (...)
}
which will look something like this:
|-------------------=========| Processed 0,94 MB |==========-------------------|
|===================---------| Processed 2,62 MB |--------=====================|
|-----=======================| Processed 3,83 MB |-----------------------------|
The IndefiniteProgressBar can have its characters customized too, and also the direction of the mover. This
can be used to neat effect to e.g. "download" something:
var direction = IndefiniteProgressBar.ProgressDirection.RightToLeft;
using (var bar = new IndefiniteProgressBar(moverChar: '<', backgroundChar: ' ', moverWidth: 5, direction: direction))
{
// (...)
// do stuff and from time to time:
bar.SetAction($"Downloading {downloadedDataMb:0.00} MB");
// (...)
}
which will look something like this:
| | Downloading 0,50 MB | <<<<< |
| | Downloading 0,81 MB | <<<<< |
| | Downloading 1,11 MB |<<<<< |
A lock object can be optionally passed into each spinner thingie's constructor. This allows you to lock around spinner updates when there are console writes occurring on other threads.
When console output is redirected (e.g. piped to a file or captured by a CI system), nothing is animated - only the
final state (e.g. 100 %) is written, so your logs stay clean.