ProgressBar
Loading...
Searching...
No Matches
PBar.hpp
Go to the documentation of this file.
1
9#ifndef PROGRESS_BAR_PBAR_HPP
10#define PROGRESS_BAR_PBAR_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);
93 void Finish();
98 void Clear();
103 void RePrint();
106 void Reset();
111 void SetBarStep(const double step);
116 void SetPrecision(const short precision);
117
124 template <typename... T>
125 void HandleOutput(T... args)
126 {
127 Clear();
128
129 ((std::cout << args << " "), ...);
130 std::cout << std::endl;
131
132 if (printedProgress > 1.) return;
133 RePrint();
134 }
135
137 virtual ~ProgressBar();
138
139 protected:
140
142 short utf8_strlen (const std::string& str);
144 short GetTerminalWidth();
146 std::string DtoStr (const double val, const short precision = 2);
147
149 const short EMPTY_SPACE_WIDTH = 4;
150
152 std::string barText;
153
155 std::string barColor;
157 std::string leftBorder;
159 std::string rightBorder;
161 std::string completeSymbol;
165 std::string notCompleteSymbol;
166
171
173 double barStep = 0.01;
175 double printedProgress = -0.01;
177 short barPrecision = 0;
178
180 struct winsize terminalWindow;
181};
182
183#endif /* PROGRESS_BAR__PBAR_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:132
std::string barColor
Color of the progress bar.
Definition PBar.hpp:155
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:149
short GetTerminalWidth()
Get the width of the current terminal.
Definition PBar.cpp:197
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:125
std::string rightBorder
Right border of a bar.
Definition PBar.hpp:159
short fullWidth
The length of the bar.
Definition PBar.hpp:168
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:161
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:163
void SetBarStep(const double step)
Set the step of the progress the ProgressBar should be printed upon reaching.
Definition PBar.cpp:178
std::string barText
Text to be printed on the left of the progress bar.
Definition PBar.hpp:152
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:165
struct winsize terminalWindow
Terminal window struct; used to acquire the width of the terminal.
Definition PBar.hpp:180
void SetPrecision(const short precision)
Set the precision of the progress indicator.
Definition PBar.cpp:180
short barPrecision
Bar precision.
Definition PBar.hpp:177
std::string DtoStr(const double val, const short precision=2)
Convert double to std::string with a give precision.
Definition PBar.cpp:203
double printedProgress
Variable that tracks the progress of a last progress bar printed. Used for progress checks to decreas...
Definition PBar.hpp:175
virtual ~ProgressBar()
Default destructor.
Definition PBar.cpp:210
void Clear()
Clears the printed bar.
Definition PBar.cpp:170
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:176
std::string leftBorder
Left border of a bar.
Definition PBar.hpp:157
void Finish()
Finishes the bar and clears it.
Definition PBar.cpp:127
short utf8_strlen(const std::string &str)
Get the length of the string containing UTF8 symbols.
Definition PBar.cpp:182
double barStep
Bar step. 1/barStep is the overall number of times ProgressBar will be printed before it is filled.
Definition PBar.hpp:173
short barBodyWidth
The length of the body of the bar (borders + body symbols).
Definition PBar.hpp:170