Page 1 of 1

Progress Bars in Command Prompt - Part A A thorough look into what goes into a command line progress bar

#1 SwiftStriker00   User is offline

  • No idea why my code works
  • member icon

Reputation: 439
  • View blog
  • Posts: 1,617
  • Joined: 25-December 08

Posted 29 December 2009 - 08:16 AM

Progress Bars 101, A Tutorial
This is a short tutorial to making some progress bars in the command prompt. I will be using C# because I like to enhance my progress bars with color, however baring that you can easily take these tactics int C++, Java, etc...

First lets take a look at some components of a progress bar. You will have 3 independent variables, and by independent I mean that they do not rely on another variable to be derived.
  • total - The total will be your goal (i.e. if I was counting regular season games in the NFL my total will be 17 )
  • progress - The progress will be how far along you are in reaching your goal (i.e. Week 5 will be progress = 5)
  • size - The size is how long your progress bar will be in the prompt (Using standards, 80 characters is good)
with those there are many dependent variables with can acquire!
  • percent done
  • how far along
  • percent left
  • etc...
However this is going require a little math to get some of these, so without further delay

Lets start simple and derive our first and quite important variable of percent done! percentDone = progress / total
Since we are doing math like this percentDone will be between 0 and 1, if you want percentDone*100 will give you your actual percent, but for now im going to keep the decimal form.

Why are these important, because the save you work if you understand them!


A small anecdote: My roommate and each had to make a left to right speaker slider bar for a music player in flash for our homework. Because I understood proportions I was able to quickly evaluate what value the volume was at and where to display the slider (slider bars very similar to progress bars in nature), my roommate didn't and he had to spend half the night trying to find the best parabolic equation to evaluate where his sliders were. Once I heard the head banging against the desk I was able to help him and after a few choice words at the programming gods we both finished our projects successfully

My point is proportions are helpful! Lets take your room, and a football field , and you walk across 10% of this distance of each the distance traveled will be different, but they will proportional!

So lets take your room, and its 20 feet and someone tells you to walk the same percentage that the football player ran from the endzone to the first 35 yard line marker (Ugghh word problems). We can use a proportion to solve it:
Let the variable X be the distance I need to travel across my room
x out of 20 feet will be the same 35 yards ran out of 100 yards
x/ 20 =35 /100
x =(35 /100 )20
x = 7 feet

However take not that you can use cross multiplication as well
x/ 20 =35 /100
x ( 100 ) =  20 ( 35 )
x = ( 20 ( 35 ) ) / 100

Now you may be saying, hey the second way took longer. True that time it did, however wouldn't it be convenient if it was simply just variables, and we never had to think about it again?
x / size = progress / total
Congrats! you have found X, or the number of marks to fill in the bar of any given size, with any given progress out of any given total!

The Code
So go ahead and make a new Console Project in Visual Studio, and add a class ProgressBar to the solution (or however you want to follow along, by no means do you actually need visual studio, just need a compiler really). And I am going to do this following OOP practices as its easier to demonstrate, however all the tricks can and are often done sequentiality as well.

The Variables
I was talking about components of progress bars earlier, and that sounds a lot like variables of a class for ProgressBars (go figure, right?). So lets go ahead and place are variables in the class like so:

public class ProgressBar{
  public const int DEFAULT_SIZE = 80;
  private int size;
  private double progress, total;

Now your first question may be why use doubles and ints, why not just pick one? Well I used ints for the size because there is no way only displaying a portion of a character in the command prompt so we only really a whole number value. The progress and total however we want a bit more precision to make it more versatile for the user and so when we divide the progress from the total, we can maintain that precision (integer division will truncate too much). Also I just have a default number there to remind myself the standard prompt is 80 characters wide.

Notice i didn't write down percentageDone. That is because it is a derived variable and every time something changes i will have to call a method to update that variable, so I'm just going to leave it as that, a method (well technically a Property, but we'll get to that).

Well in a default constructor we are going to need to set some of the other variables to default values, however we can overload th constructor to add some more ways to build a ProgressBar in the future. A good default for progress is at 0, since a process is usually started at the beginning. The total is unspecified and should probably be 100, since its the easiest number to work with, and we will make the default size 80 because of the command prompt's size.

I've taken the liberty to write a few, constructors to make the ProgressBar easier to initially set up depending on the situation

  public ProgressBar() {
   this.size = DEFAULT_SIZE-2;
   progress = 0;
   total = 100;

  public ProgressBar( double total ) {
   this.size = DEFAULT_SIZE-2;
   progress = 0; = total;

  public ProgressBar( double total, int size ) {
   this.size = size -2;
   progress = 0; = total;

  public ProgressBar( double progress, double total, int size ) {
   this.size = size - 2;
   this.progress = progress; = total;
   this.progress = progress;

When I create a Progress bar I like to make it look better by putting caps on the ends, this is why the size is set to inputted size -2. For example a basic progress bar of size 10 and then my progress bar of size 12:

Basic: ***-------
Mine: [***-------]

Is This A Good Question/Topic? 0
  • +

Replies To: Progress Bars in Command Prompt - Part A

#2 SwiftStriker00   User is offline

  • No idea why my code works
  • member icon

Reputation: 439
  • View blog
  • Posts: 1,617
  • Joined: 25-December 08

Posted 29 December 2009 - 07:05 PM

The tutorial continues HERE

Sorry about the split, I had some technical difficulties upload the tutorial. (thanks for the help Core)!

This post has been edited by SwiftStriker00: 29 December 2009 - 07:06 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1