ProgressBar
 
Loading...
Searching...
No Matches
PBar.hpp
Go to the documentation of this file.
1
9#ifndef PROGRESS_BAR_HPP
10#define PROGRESS_BAR_HPP
11
12#include <iostream>
13#include <stdio.h>
14#include <sys/ioctl.h>
15#include <unistd.h>
16#include <cstring>
17#include <sstream>
18#include <iomanip>
19#include <array>
20
21#include "PBarStyle.hpp"
22
34{
35 public:
36
42 ProgressBar(std::string style = "DEFAULT", const std::string& customText = "",
43 const std::string& color = "");
53 ProgressBar(const std::string& customLeftBorder, const char customCompleteSymbol,
54 const char customNextCompleteSymbol, const char customNotCompleteSymbol,
55 const std::string& customRightBorder, const std::string& color,
56 const std::string& customText = "");
60 void SetColor(const std::string& color);
65 void SetStyle(std::string style, const std::string& color);
75 void SetCustomStyle(const std::string& customLeftBorder, const char customCompleteSymbol,
76 const char customNextCompleteSymbol, const char customNotCompleteSymbol,
77 const std::string& customRightBorder, const std::string& color);
81 void SetWidth(const short customWidth);
85 void SetText(const std::string& customText);
91 void Print(const double progress);
96 void Clear();
101 void RePrint();
104 void Fill();
107 void Reset();
112 void SetBarStep(const double step);
117 void SetPrecision(const short precision);
118
125 template <typename... T>
126 void HandleOutput(T... args)
127 {
128 if (printedProgress < 1.) Clear();
129
130 ((std::cout << args << " "), ...);
131 std::cout << std::endl;
132
133 if (printedProgress > 1.) return;
134 RePrint();
135 }
136
138 virtual ~ProgressBar();
139
140 protected:
141
143 short utf8_strlen (const std::string& str);
145 short GetTerminalWidth();
147 std::string DtoStr (const double val, const short precision = 2);
148
150 const short EMPTY_SPACE_WIDTH = 4;
151
153 std::string barText;
154
156 std::string barColor;
158 std::string leftBorder;
160 std::string rightBorder;
162 std::string completeSymbol;
166 std::string notCompleteSymbol;
167
172
174 double barStep = 0.01;
176 double printedProgress = -0.01;
178 short barPrecision = 0;
179
181 struct winsize terminalWindow;
182};
183
184#endif /* PROGRESS_BAR_HPP */
Contains map of styles for ProgressBar class.
void SetStyle(std::string style, const std::string &color)
Set predefined style of progess bar.
Definition PBar.cpp:41
void RePrint()
Prints the progress bar without checking if it should be printed.
Definition PBar.cpp:127
std::string barColor
Color of the progress bar.
Definition PBar.hpp:156
const short EMPTY_SPACE_WIDTH
Number of empty spaces in a progress bar line (2 on very the edges, 1 between text and a bar,...
Definition PBar.hpp:150
short GetTerminalWidth()
Get the width of the current terminal.
Definition PBar.cpp:194
void Print(const double progress)
Prints bar.
Definition PBar.cpp:115
void HandleOutput(T... args)
Rerouts the output in the terminal so the ProgressBar will not be interrupted.
Definition PBar.hpp:126
std::string rightBorder
Right border of a bar.
Definition PBar.hpp:160
short fullWidth
The length of the bar.
Definition PBar.hpp:169
void SetWidth(const short customWidth)
Set the width of the progress bar.
Definition PBar.cpp:101
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.
Definition PBar.cpp:86
std::string completeSymbol
Bar body symbol indicating filled positions in a bar; comes after leftBorder before completeSymbol.
Definition PBar.hpp:162
ProgressBar(std::string style="DEFAULT", const std::string &customText="", const std::string &color="")
Constructor that creates ProgressBar instance with predefined style.
Definition PBar.cpp:14
std::string nextCompleteSymbol
Bar body symbol indicating current progress; comes after the last completeSymbol and before first.
Definition PBar.hpp:164
void SetBarStep(const double step)
Set the step of the progress the ProgressBar should be printed upon reaching.
Definition PBar.cpp:175
std::string barText
Text to be printed on the left of the progress bar.
Definition PBar.hpp:153
void SetColor(const std::string &color)
Set color of progess bar.
Definition PBar.cpp:36
std::string notCompleteSymbol
Bar body symbol indicating not yet filled positions in a bar; comes after completeSymbol and before r...
Definition PBar.hpp:166
struct winsize terminalWindow
Terminal window struct; used to acquire the width of the terminal.
Definition PBar.hpp:181
void SetPrecision(const short precision)
Set the precision of the progress indicator.
Definition PBar.cpp:177
short barPrecision
Bar precision.
Definition PBar.hpp:178
std::string DtoStr(const double val, const short precision=2)
Convert double to std::string with a give precision.
Definition PBar.cpp:200
double printedProgress
Variable that tracks the progress of a last progress bar printed. Used for progress checks to decreas...
Definition PBar.hpp:176
virtual ~ProgressBar()
Default destructor.
Definition PBar.cpp:207
void Clear()
Clears the printed bar.
Definition PBar.cpp:165
void SetText(const std::string &customText)
Set text of a bar.
Definition PBar.cpp:109
void Reset()
Resets the progress of the bar (puts it to 0)
Definition PBar.cpp:173
std::string leftBorder
Left border of a bar.
Definition PBar.hpp:158
short utf8_strlen(const std::string &str)
Get the length of the string containing UTF8 symbols.
Definition PBar.cpp:179
double barStep
Bar step. 1/barStep is the overall number of times ProgressBar will be printed before it is filled.
Definition PBar.hpp:174
short barBodyWidth
The length of the body of the bar (borders + body symbols)
Definition PBar.hpp:171
void Fill()
Completely fills the progress of the bar (puts it to 1)
Definition PBar.cpp:171