Scott Hanselman

Accelerated 3D VR, sure, but impress me with a nice ASCII progress bar or spinner

December 4, '17 Comments [10] Posted in DotNetCore
Sponsored By

I'm glad you have a 1080p 60fps accelerated graphics setup, but I'm old school. Impress me with a really nice polished ASCII progress bar or spinner!

I received two tips this week about cool .NET Core ready progress bars so I thought I'd try them out.

ShellProgressBar by Martijn Laarman

This one is super cool. It even supports child progress bars for async stuff happening in parallel! It's very easy to use. I was able to get a nice looking progress bar going in minutes.

static void Main(string[] args)
{
const int totalTicks = 100;
var options = new ProgressBarOptions
{
ForegroundColor = ConsoleColor.Yellow,
ForegroundColorDone = ConsoleColor.DarkGreen,
BackgroundColor = ConsoleColor.DarkGray,
BackgroundCharacter = '\u2593'
};
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");
TickToCompletion(pbar, totalTicks, sleep: 50);
}
}

Boom.

Cool ASCII Progress Bars in .NET Core

Be sure to check out the examples for ShellProgressBar, specifically ExampleBase.cs where he has some helper stuff like TickToCompletion() that isn't initially obvious.

Kurukuru by Mayuki Sawatari

Another nice progress system that is in active development for .NET Core (like super active...I can see they updated code an hour ago!) is called Kurukuru. This code is less about progress bars and more about spinners. It's smart about Unicode vs. non-Unicode as there's a lot of cool characters you could use in a Unicode-aware console that make for attractive spinners.

What a lovely ASCII Spinner in .NET Core!

Kurukuru is also super easy to use and integrated into your code. It also uses the "using" disposable pattern in a clever way. Wrap your work and if you throw an exception, it will show a failed spinner.

Spinner.Start("Processing...", () =>
{
Thread.Sleep(1000 * 3);

// MEMO: If you want to show as failed, throw a exception here.
// throw new Exception("Something went wrong!");
});

Spinner.Start("Stage 1...", spinner =>
{
Thread.Sleep(1000 * 3);
spinner.Text = "Stage 2...";
Thread.Sleep(1000 * 3);
spinner.Fail("Something went wrong!");
});

TIP: If your .NET Core console app wants to use an async Main (like I did) and call Kurukuru's async methods, you'll want to indicate you want to use the latest C# 7.1 features by adding this to your project's *.csproj file:

<PropertyGroup>
    <LangVersion>latest</LangVersion>
</PropertyGroup>

This allowed me to do this:

public static async Task Main(string[] args)
{
Console.WriteLine("Hello World!");
await Spinner.StartAsync("Stage 1...", async spinner =>
{
await Task.Delay(1000 * 3);
spinner.Text = "Stage 2...";
await Task.Delay(1000 * 3);
spinner.Fail("Something went wrong!");
});
}

Did I miss some? I'm sure I did. What nice ASCII progress bars and spinners make YOU happy?

And again, as with all Open Source, I encourage you to HELP OUT! I know the authors would appreciate it.


Sponsor: Check out JetBrains Rider: a new cross-platform .NET IDE. Edit, refactor, test and debug ASP.NET, .NET Framework, .NET Core, Xamarin or Unity applications. Learn more and download a 30-day trial!

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb
Monday, 04 December 2017 08:25:25 UTC
Nice jobs both! Blog10
Ralph
Monday, 04 December 2017 08:37:43 UTC
Maybe someone should try to port Ora to dotnet. Beautiful, but node.
Christian Wattengård
Monday, 04 December 2017 08:47:14 UTC
Shout out about Colorful.Console which lets you add some colour to your console apps and use cool ASCII art using Figlet fonts (ASCII fonts).
Monday, 04 December 2017 14:48:03 UTC
Jason Argot
Monday, 04 December 2017 14:52:41 UTC
If you use something like ConEmu instead of the Windows shell the results will be much nicer.
Ronald
Monday, 04 December 2017 17:34:19 UTC
I had to write a command-line utility to perform a long-running but variable-length task, and needed to add some sort of asynchronous progress spinner to let the user know the command was still actively working and the machine wasn't frozen. I came up with a pattern that I figured was hypnotic enough to keep the user distracted, but while I originally called it a "Hypnotic sparkle-bob", it ended up being called the "throbber":
°º¤ø,¸,ø¤º°`


Next time, though, I'd probably turn to Kurukuru.
Mike C
Monday, 04 December 2017 19:15:22 UTC
I wrote a one-line Powershell countdown timer for inserting a short pause without requiring a keystroke, for when something is about to happen and you want to briefly show output from the last commmand:

for([int] $i=3;$i -gt 0;$i--){Write-Host "$i" -NoNewline ;Start-Sleep 1; Write-Host ("`r" * ([string] $i).Length) -NoNewline }


It's not fancy ascii but it acts like the spinner example - overwriting the previously displayed number.
Rich Moss
Tuesday, 05 December 2017 01:41:56 UTC
border-radius: 2px; doesn't work on the progress bar. Fail!
RobertG
Wednesday, 06 December 2017 09:39:02 UTC
Hello hanselman.com, Thanks for This article . Best wishes for you . Advance Happy New Year 2018
Thursday, 07 December 2017 23:39:20 UTC
ASCII spinners really take me back to my childhood with the family Apple //c!
Name
E-mail
(will show your gravatar icon)
Home page

Comment (Some html is allowed: a@href@title, b, blockquote@cite, em, i, li, ol, pre, strike, strong, sub, super, u, ul) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Live Comment Preview

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.