ProgressBar
 
Loading...
Searching...
No Matches
ProgressBar Class Reference

Class ProgressBar can be used used to print the progress in an ascii horizontal bar. See example on it's implementation below. More...

#include <PBar.hpp>

Public Member Functions

 ProgressBar (std::string style="DEFAULT", const std::string &customText="", const std::string &color="")
 Constructor that creates ProgressBar instance with predefined style.
 
 ProgressBar (const std::string &customLeftBorder, const char customCompleteSymbol, const char customNextCompleteSymbol, const char customNotCompleteSymbol, const std::string &customRightBorder, const std::string &color, const std::string &customText="")
 Constructor that creates ProgressBar instance with custom style.
 
void SetColor (const std::string &color)
 Set color of progess bar.
 
void SetStyle (std::string style, const std::string &color)
 Set predefined style of progess bar.
 
void SetCustomStyle (const std::string &customLeftBorder, const char customCompleteSymbol, const char customNextCompleteSymbol, const char customNotCompleteSymbol, const std::string &customRightBorder, const std::string &color)
 Set custom style of progress bar.
 
void SetWidth (const short customWidth)
 Set the width of the progress bar.
 
void SetText (const std::string &customText)
 Set text of a bar.
 
void Print (const double progress)
 Prints bar.
 
void Clear ()
 Clears the printed bar.
 
void RePrint ()
 Prints the progress bar without checking if it should be printed.
 
void Fill ()
 Completely fills the progress of the bar (puts it to 1)
 
void Reset ()
 Resets the progress of the bar (puts it to 0)
 
void SetBarStep (const double step)
 Set the step of the progress the ProgressBar should be printed upon reaching.
 
void SetPrecision (const short precision)
 Set the precision of the progress indicator.
 
template<typename... T>
void HandleOutput (T... args)
 Rerouts the output in the terminal so the ProgressBar will not be interrupted.
 
virtual ~ProgressBar ()
 Default destructor.
 

Protected Member Functions

short utf8_strlen (const std::string &str)
 Get the length of the string containing UTF8 symbols.
 
short GetTerminalWidth ()
 Get the width of the current terminal.
 
std::string DtoStr (const double val, const short precision=2)
 Convert double to std::string with a give precision.
 

Protected Attributes

const short EMPTY_SPACE_WIDTH = 4
 Number of empty spaces in a progress bar line (2 on very the edges, 1 between text and a bar, and 1 between bar and progress indicator)
 
std::string barText
 Text to be printed on the left of the progress bar.
 
std::string barColor
 Color of the progress bar.
 
std::string leftBorder
 Left border of a bar.
 
std::string rightBorder
 Right border of a bar.
 
std::string completeSymbol
 Bar body symbol indicating filled positions in a bar; comes after leftBorder before completeSymbol.
 
std::string nextCompleteSymbol
 Bar body symbol indicating current progress; comes after the last completeSymbol and before first.
 
std::string notCompleteSymbol
 Bar body symbol indicating not yet filled positions in a bar; comes after completeSymbol and before rightBorder.
 
short fullWidth
 The length of the bar.
 
short barBodyWidth
 The length of the body of the bar (borders + body symbols)
 
double barStep = 0.01
 Bar step. 1/barStep is the overall number of times ProgressBar will be printed before it is filled.
 
double printedProgress = -0.01
 Variable that tracks the progress of a last progress bar printed. Used for progress checks to decrease the number of times ProgressBar is printed.
 
short barPrecision = 0
 Bar precision.
 
struct winsize terminalWindow
 Terminal window struct; used to acquire the width of the terminal.
 

Detailed Description

Class ProgressBar can be used used to print the progress in an ascii horizontal bar. See example on it's implementation below.

Examples
handle_output.cpp, mt.cpp, possibilities.cpp, speed.cpp, style.cpp, and test.cpp.

Constructor & Destructor Documentation

◆ ProgressBar() [1/2]

ProgressBar::ProgressBar ( std::string style = "DEFAULT",
const std::string & customText = "",
const std::string & color = "" )

Constructor that creates ProgressBar instance with predefined style.

Parameters
[in]stylepredefined style of the bar. Currently there are 9 predefined styles ("DEFAULT", "IMPROVED", "FANCY", "FANCY1", "FANCY2", "WAVE", "BLOCK", "BLOCK1", "BLOCK2" (for more info see examples) and can be passed as a string in upper and/or lower case )
[in]customTexttext to be printed on the left of the bar
[in]colorcolor of the bar (see PBColor namespace). By default each predefined style has it's own default color, which will be used if the default arguments is passed

◆ ProgressBar() [2/2]

ProgressBar::ProgressBar ( const std::string & customLeftBorder,
const char customCompleteSymbol,
const char customNextCompleteSymbol,
const char customNotCompleteSymbol,
const std::string & customRightBorder,
const std::string & color,
const std::string & customText = "" )

Constructor that creates ProgressBar instance with custom style.

Parameters
[in]customLeftBorderleft border of the bar
[in]customCompleteSymbolsymbol that denotes filled positions in a bar
[in]customNextCompleteSymbolsymbol that denotes current position of a progress in a bar
[in]customNotCompleteSymbolsymbol that denotes positions to be filled in a bar
[in]customRightBorderright border of the bar
[in]colorcolor of the bar (see PBColor namespace)
[in]customTexttext to be printed on the left of the bar

Member Function Documentation

◆ Clear()

void ProgressBar::Clear ( )

Clears the printed bar.

This function should only be used when using ProgressBar::RePrint. It will clear the last line the cursor is on. So if the user is not paying attention data from terminal will be erased.

◆ HandleOutput()

template<typename... T>
void ProgressBar::HandleOutput ( T... args)
inline

Rerouts the output in the terminal so the ProgressBar will not be interrupted.

Parameters
[in]argsparameter pack which will be printed.

Functions similarly to python print function. But current function will also erase the last bar and reprint it after it has printed the passed output. So this function should not be used too often since reprinting the body of the progress bar a lot of times can take significant resources. On how to use see handle_output.cpp in examples directory.

◆ Print()

void ProgressBar::Print ( const double progress)

Prints bar.

Parameters
[in]progressprogress value ranging from 0. to 1. (complete)

This functions automaticaly checks if it should be printed so there is no worry that printing the bar will take all resources. The check is also very fast which allows for the ProgressBar to be very fast (see speed.cpp in examples directory)

Examples
mt.cpp, possibilities.cpp, speed.cpp, and test.cpp.

◆ RePrint()

void ProgressBar::RePrint ( )

Prints the progress bar without checking if it should be printed.

This function should not be used to often since no checking means that it can take a lot of resources to print the bar a lot of times in terminal. Use only when you cannot redirect outputs to ProgressBar::HandleOutput (see handle_output.cpp in examples directory)

◆ SetBarStep()

void ProgressBar::SetBarStep ( const double step)

Set the step of the progress the ProgressBar should be printed upon reaching.

Parameters
[in]stepthis value determines the frequency of printing the progress bar into the terminal. Do not set the value too low otherwise it will take a lot of resources to print the bar many times. The default value is 0.01 (progress bar will be printed 100 times with this value)

◆ SetColor()

void ProgressBar::SetColor ( const std::string & color)

Set color of progess bar.

Parameters
[in]colorcolor of the bar (see PBColor namespace)

◆ SetCustomStyle()

void ProgressBar::SetCustomStyle ( const std::string & customLeftBorder,
const char customCompleteSymbol,
const char customNextCompleteSymbol,
const char customNotCompleteSymbol,
const std::string & customRightBorder,
const std::string & color )

Set custom style of progress bar.

Parameters
[in]customLeftBorderleft border of the bar
[in]customCompleteSymbolsymbol that denotes filled positions in a bar
[in]customNextCompleteSymbolsymbol that denotes current position of a progress in a bar
[in]customNotCompleteSymbolsymbol that denotes positions to be filled in a bar
[in]customRightBorderright border of the bar
[in]colorcolor of the bar (see PBColor namespace)
[in]customTexttext to be printed on the left of the bar

◆ SetPrecision()

void ProgressBar::SetPrecision ( const short precision)

Set the precision of the progress indicator.

Parameters
[in]precisionfloating point precision of the progress indicator (default is 0)

◆ SetStyle()

void ProgressBar::SetStyle ( std::string style,
const std::string & color )

Set predefined style of progess bar.

Parameters
[in]stylepredefined style of the bar. Currently there are 9 predefined styles ("DEFAULT", "IMPROVED", "FANCY", "FANCY1", "FANCY2", "WAVE", "BLOCK", "BLOCK1", "BLOCK2" (for more info see examples) and can be passed as a string in upper and/or lower case )
[in]colorcolor of the bar (see PBColor namespace). By default each predefined style has it's own default color, which will be used if the default arguments is passed

◆ SetText()

void ProgressBar::SetText ( const std::string & customText)

Set text of a bar.

Parameters
[in]customTexttext to be printed on the left of the bar

◆ SetWidth()

void ProgressBar::SetWidth ( const short customWidth)

Set the width of the progress bar.

Parameters
[in]customWidthwidth of the progress bar. By defaul the width of the progress bar is the width of terminal

The documentation for this class was generated from the following files: