Skip to content

Instantly share code, notes, and snippets.

@t3knoid
Created February 11, 2022 14:32
Show Gist options
  • Save t3knoid/f49de1d5cde1cc042003d88aa49474a3 to your computer and use it in GitHub Desktop.
Save t3knoid/f49de1d5cde1cc042003d88aa49474a3 to your computer and use it in GitHub Desktop.
An example of emulating a progress bar in console application using c#
# Copied from https://www.codeproject.com/Tips/5255878/A-Console-Progress-Bar-in-Csharp
using System;
namespace CU
{
static class ConsoleUtility
{
const char _block = '■';
const string _back = "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b";
const string _twirl = "-\\|/";
public static void WriteProgressBar(int percent, bool update = false)
{
if(update)
Console.Write(_back);
Console.Write("[");
var p = (int)((percent / 10f)+.5f);
for (var i = 0;i<10;++i)
{
if (i >= p)
Console.Write(' ');
else
Console.Write(_block);
}
Console.Write("] {0,3:##0}%", percent);
}
public static void WriteProgress(int progress, bool update = false)
{
if (update)
Console.Write("\b");
Console.Write(_twirl[progress % _twirl.Length]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment