Console_ProgressBar displays a customizable progress bar in the console/terminal.
Example 38-13. Some progress bar examples
Things to setup
Create a new instance of Console_ProgressBar.
Call update() whenever some progress has been made.
(optional) erase(). after you're finished.
Example 38-14. Simple progressbar example
In the example we create the progress bar instance with five parameters: The format string, bar string, empty string, console width and the target number.
The first parameter, format string, defines the layout of the whole progress bar. It may contain any character and has some variables that get replaced:
%bar% is the actual progress bar.
%current% is the current value set via update().
%max% is replaced with the maximum value set by the constructor (target number).
%fraction% - the same as %current%/%max%.
%percent% - status in percent.
%elapsed% - elapsed time.
%estimate% - an estimation of how long the progress will take.
The second argument is the string that is going to fill the progress bar. In the above example, the string "=>" was used. If the string you pass is too short (like "=>" in this example), the leftmost character is used to pad it to the needed size. If the string you pass is too long, excessive characters are stripped from the left.
The third argument is the string that fills the "empty" space in the progress bar. In the above example, that would be "-". If the string you pass is too short (like "-" in this example), the rightmost character is used to pad it to the needed size. If the string you pass is too long, excessive characters are stripped from the right.
The fourth argument specifies the width of the display. A normal console/terminal is 80 chars, so pass 80 here. Currently there is no method to determine the current width of a terminal.
The fifth argument is the target number of the progress bar. For example, if you wanted to display a progress bar for a download of a file that is 115 KB big, you would pass 115 here.
After setting up the progress bar, you can start doing the real work - up- or downloading files, calculating something etc. Whenever you did a step forward, call update() with the current step number.
When you're finished, you may want to remove the progress bar from screen - use erase() for this.