Introduction
It is nice to place progress bars in console apps, but sometimes there is no time to create them. So I wrote some utility classes to be used in all console apps.
About the code
The project has three classes; one abstract (AbstractBar) that define the generic behaviour of the progress bars and two implementation,
which are: AnimatedBar and SwayBar. feel free to add your own implemetantion.

Abstract class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ProgressBarDemo
{
abstract class AbstractBar
{
public AbstractBar()
{
}
public void PrintMessage(string msg)
{
Console.WriteLine(msg);
}
public abstract void Step();
}
}
Animated class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ProgressBarDemo
{
class AnimatedBar : AbstractBar
{
List<string> animation;
int counter;
public AnimatedBar() : base()
{
this.animation = new List<string> { "/", "-", @"\", "|" };
this.counter = 0;
}
public override void Step() {
Console.Write(this.animation[this.counter]+"\b");
this.counter++;
if (this.counter == this.animation.Count)
this.counter = 0;
}
}
}
Output:

Sway class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ProgressBarDemo
{
class SwayBar : AbstractBar
{
string bar;
string pointer;
string blankPointer;
int counter;
direction currdir;
enum direction { right, left };
public SwayBar() : base()
{
this.bar = "| |";
this.pointer = "***";
this.blankPointer = this.BlankPointer();
this.currdir = direction.right;
this.counter = 1;
}
private string BlankPointer()
{
StringBuilder blank = new StringBuilder();
for (int cont = 0; cont < this.pointer.Length; cont++)
blank.Append(" ");
return blank.ToString();
}
private void ClearBar()
{
this.bar = this.bar.Replace(this.pointer, this.blankPointer);
}
private void PlacePointer(int start, int end)
{
this.ClearBar();
this.bar = this.bar.Remove(start, end);
this.bar = this.bar.Insert(start, this.pointer);
}
public override void Step()
{
if (this.currdir == direction.right)
{
this.PlacePointer(counter, this.pointer.Length);
this.counter++;
if (this.counter+this.pointer.Length == this.bar.Length)
this.currdir = direction.left;
}
else
{
this.PlacePointer(counter - this.pointer.Length, this.pointer.Length);
this.counter--;
if (this.counter == this.pointer.Length)
this.currdir = direction.right;
}
Console.Write(this.bar + "\r");
}
}
}
Output:

Using the code
In order to test our progress bars we are going to write a simple console app and write the following code in its program.cs file.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace ProgressBarDemo
{
class Program
{
static void Main()
{
AbstractBar bar;
bar = new AnimatedBar();
int wait = 100;
int end = 50;
Test(bar, wait, end);
bar = new SwayBar();
Test(bar, wait, end);
Console.ReadLine();
}
public static void Test(AbstractBar bar, int wait, int end)
{
bar.PrintMessage("Loading...");
for (int cont = 0; cont < end; cont++)
{
bar.Step();
Thread.Sleep(wait);
}
bar.PrintMessage("Finished");
}
}
}
Conclusion
I had fun writing those classes, I hope you find them useful.
CTO and Co-founder of Golabs, a software development company located in Costa Rica. I am very passionate about my job and I really enjoy building things with software and make things happening. I am also a professor at Universidad Técnica Nacional because I believe that knowledge should be shared, I really enjoy seeing people grow from students at the university to great professionals and entrepreneurs.