17 #ifndef OSMANIP_PROGRESSBAR_PROGRESSBAR_HPP
18 #define OSMANIP_PROGRESSBAR_PROGRESSBAR_HPP
40 #include <unordered_map>
41 #include <unordered_set>
49 using string_set_map = std::unordered_map<std::string, std::unordered_set<std::string>>;
51 using duration = std::chrono::duration<float, steady_clock::period>;
63 template <
typename bar_type>
84 time_count_(
duration::zero().count()),
109 time_count_(
duration::zero().count()),
129 void setMax(bar_type max) { max_ = max; }
138 void setMin(bar_type min) { min_ = min; }
149 void setStyle(
const std::string &type,
const std::string &style) {
151 if (styles_map_.at(type).find(style) != styles_map_.at(type).end()) {
154 }
else if (styles_map_.at(type).find(style) == styles_map_.at(type).end()) {
172 }
catch (std::out_of_range
const &exception) {
174 "Inserted ProgressBar "
176 type,
"is not supported!");
190 void setStyle(
const std::string &type,
const std::string &style_p,
const std::string &style_l) {
191 if (styles_map_.at(
"indicator").find(style_p) != styles_map_.at(
"indicator").end() &&
192 styles_map_.at(
"loader").find(style_l) != styles_map_.at(
"loader").end() && type ==
"complete") {
193 style_ = style_p + style_l;
197 }
else if (styles_map_.at(
"indicator").find(style_p) == styles_map_.at(
"indicator").end()) {
199 "Inserted indicator "
202 "is not supported for "
204 }
else if (styles_map_.at(
"loader").find(style_l) == styles_map_.at(
"loader").end()) {
206 "is not supported for "
210 "Inserted ProgressBar "
212 type,
"is not supported!");
223 void setMessage(std::string_view message) { message_ = message; }
240 end = steady_clock::now();
241 time_count_ += std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count();
252 void setBrackets(std::string_view brackets_open, std::string_view brackets_close) {
253 brackets_open_ = brackets_open, brackets_close_ = brackets_close;
288 max_ =
static_cast<bar_type
>(0), min_ =
static_cast<bar_type
>(0), style_ =
"", type_ =
"",
289 message_ =
"", time_count_ = 0, ticks_occurred = 0, begin_timer = steady_clock::now(),
290 brackets_open_ =
"", brackets_close_ =
"", color_ =
feat(
rst,
"color");
301 void resetMax() { max_ =
static_cast<bar_type
>(0); }
309 void resetMin() { min_ =
static_cast<bar_type
>(0); }
336 void resetTime() { time_count_ = duration::zero().count(); }
346 begin_timer = steady_clock::now();
397 long long getTime()
const {
return time_count_; }
425 if (type_ ==
"complete") {
426 return "Percentage: \"" + style_p_ +
"\"\n" +
"Loader: \"" + style_l_ +
"\"\n";
506 std::lock_guard<std::mutex> lock{mutex_};
508 iterating_var_ = 100 * (iterating_var - min_) / (max_ - min_ -
osm::one(iterating_var)),
509 iterating_var_spin_ =
511 width_ = (iterating_var_ + 1) / 4;
514 if (styles_map_.at(
"indicator").find(style_) != styles_map_.at(
"indicator").end()) {
516 std::to_string(
static_cast<int32_t
>(round(iterating_var_++))) +
feat(
rst,
"color") +
519 update_output(output_);
523 else if (styles_map_.at(
"loader").find(style_) != styles_map_.at(
"loader").end()) {
529 update_output(output_);
533 else if (style_.find(style_p_) != std::string::npos && style_.find(style_l_) != std::string::npos &&
534 type_ ==
"complete") {
539 std::to_string(
static_cast<int32_t
>(round(iterating_var_++))) +
feat(
rst,
"color") + style_p_;
541 update_output(output_);
545 else if (styles_map_.at(
"spinner").find(style_) != styles_map_.at(
"spinner").end()) {
547 getStyle()[
static_cast<uint64_t
>(iterating_var_spin_) & 3] +
feat(
col,
"green") +
549 ? (
static_cast<std::string
>(
feat(
crs,
"left", 100) +
"0"))
553 update_output(output_);
557 throw std::runtime_error(
558 "ProgressBar style has "
571 <<
"Min: " << min_ <<
"\n"
572 <<
"Time counter: " << time_count_ <<
"\n"
573 <<
"Style: " << style_ <<
"\n"
574 <<
"Type: " << type_ <<
"\n"
575 <<
"Message: " << message_ <<
"\n"
576 <<
"Brackets style: " << brackets_open_ << brackets_close_ <<
"\n"
577 <<
"Color: " << color_name_ <<
"\n"
578 <<
"Show remaining time: " << time_flag_ <<
"\n";
589 void addStyle(
const std::string &type,
const std::string &style) { styles_map_.at(type).insert(style); }
604 void remaining_time() {
607 duration time_taken = steady_clock::now() - begin_timer;
608 float percentage_done =
static_cast<float>(ticks_occurred) / (max_spin_);
609 duration time_left = time_taken * (1 / percentage_done - 1);
610 std::chrono::minutes minutes_left = std::chrono::duration_cast<std::chrono::minutes>(time_left);
611 std::chrono::seconds seconds_left =
612 std::chrono::duration_cast<std::chrono::seconds>(time_left - minutes_left);
619 <<
feat(
col,
"green") << minutes_left.count() <<
feat(
rst,
"color") <<
"m "
620 <<
feat(
col,
"green") << seconds_left.count() <<
feat(
rst,
"color") <<
"s"
631 void update_output(std::string_view output) {
633 << ((message_ != osm::null_str<std::string>)
634 ? (osm::empty_space<std::string> + message_ + osm::empty_space<std::string>)
635 : osm::empty_space<std::string>)
638 if (time_flag_ ==
"on") {
650 static std::mutex mutex_;
655 long long time_count_;
656 std::uint64_t ticks_occurred;
657 bar_type max_, max_spin_, min_, iterating_var_, iterating_var_spin_, width_;
658 std::string style_, style_p_, style_l_, type_, message_, brackets_open_, brackets_close_, output_, color_,
659 time_flag_, color_name_;
660 steady_clock::time_point begin, end, begin_timer;
674 template <
typename bar_type>
676 os <<
"Max: " << pb.
getMax() <<
"\n"
677 <<
"Min: " << pb.
getMin() <<
"\n"
678 <<
"Time counter: " << pb.
getTime() <<
"\n"
679 <<
"Style: " << pb.
getStyle() <<
"\n"
680 <<
"Type: " << pb.
getType() <<
"\n"
692 template <
typename bar_type>
694 {
"indicator", {
"%",
"/100"}},
695 {
"loader", {
"#",
"■"}},
696 {
"spinner", {
"/-\\|"}},
699 template <
typename bar_type>
700 std::mutex ProgressBar<bar_type>::mutex_;
Template class used to create customized progress bars.
Definition: progress_bar.hpp:64
void resetTime()
Reset the ProgressBar time count.
Definition: progress_bar.hpp:336
void update(bar_type iterating_var)
Update the progress bar indicator.
Definition: progress_bar.hpp:505
void setMax(bar_type max)
Set the maximum value of the ProgressBar.
Definition: progress_bar.hpp:129
bar_type getIteratingVar() const
Get the ProgressBar iterating variable value.
Definition: progress_bar.hpp:406
void setMessage(std::string_view message)
Set the message of the ProgressBar.
Definition: progress_bar.hpp:223
void resetColor()
Reset the ProgressBar color.
Definition: progress_bar.hpp:363
void resetAll()
Reset the ProgressBar variables.
Definition: progress_bar.hpp:287
std::string getRemainingTimeFlag() const
Get the ProgressBar time remaining variable.
Definition: progress_bar.hpp:492
std::string getColorName() const
Get the ProgressBar color name variable.
Definition: progress_bar.hpp:483
std::string getMessage() const
Get the ProgressBar Message variable.
Definition: progress_bar.hpp:447
void setRemainingTimeFlag(std::string_view time_flag)
Set the remaining time of the ProgressBar.
Definition: progress_bar.hpp:275
void resetStyle()
Reset the ProgressBar Style variable.
Definition: progress_bar.hpp:317
void print() const
Prints on the screen the progress bar variable values.
Definition: progress_bar.hpp:569
std::string getBrackets_open() const
Get the ProgressBar brackets_open variable.
Definition: progress_bar.hpp:456
ProgressBar()
Construct a new ProgressBar <bar_type>::ProgressBar object. Default constructor which set to null val...
Definition: progress_bar.hpp:78
std::string getStyle() const
Get the ProgressBar Style variable.
Definition: progress_bar.hpp:415
ProgressBar(const bar_type &min, const bar_type &max)
Construct a new ProgressBar <bar_type>::ProgressBar object. Parametric constructor which set to null ...
Definition: progress_bar.hpp:103
void resetBrackets()
Reset the ProgressBar brackets.
Definition: progress_bar.hpp:355
long long getTime() const
Get the ProgressBar current time value.
Definition: progress_bar.hpp:397
void setBrackets(std::string_view brackets_open, std::string_view brackets_close)
Set brackets of the ProgressBar.
Definition: progress_bar.hpp:252
void setBegin()
Set begin time count.
Definition: progress_bar.hpp:231
std::string getBrackets_close() const
Get the ProgressBar brackets_close variable.
Definition: progress_bar.hpp:465
void addStyle(const std::string &type, const std::string &style)
Add customized styles to the ProgressBar.
Definition: progress_bar.hpp:589
void resetMin()
Reset the ProgressBar Min variable.
Definition: progress_bar.hpp:309
std::string getStyleComplete() const
Get the ProgressBar Style variable for "complete" variable type.
Definition: progress_bar.hpp:424
bar_type getMin() const
Get the ProgressBar Min variable.
Definition: progress_bar.hpp:388
void setMin(bar_type min)
Set the minimum value of the ProgressBar.
Definition: progress_bar.hpp:138
void setEnd()
Set end time count.
Definition: progress_bar.hpp:239
std::string getType() const
Get the ProgressBar Type variable.
Definition: progress_bar.hpp:438
void setColor(const std::string &color)
Set the color of the ProgressBar.
Definition: progress_bar.hpp:263
void resetRemainingTime()
Reset the ProgressBar time remaining count.
Definition: progress_bar.hpp:344
void resetMessage()
Reset the ProgressBar Message variable.
Definition: progress_bar.hpp:328
bar_type getMax() const
Get the ProgressBar Max variable.
Definition: progress_bar.hpp:379
void setStyle(const std::string &type, const std::string &style)
Set the type and style of the ProgressBar. Available: "indicator" ("%", "/100"), "loader" ("#",...
Definition: progress_bar.hpp:149
void setStyle(const std::string &type, const std::string &style_p, const std::string &style_l)
Set the style of the complete ProgressBar. Available: "indicator" ("%", "/100"), "loader" ("#",...
Definition: progress_bar.hpp:190
std::string getColor() const
Get the ProgressBar color variable.
Definition: progress_bar.hpp:474
void resetMax()
Reset the ProgressBar Max variable.
Definition: progress_bar.hpp:301
Definition: canvas.cpp:30
std::ostream & operator<<(Decorator my_shell, const T &elem)
Operator overload to output a modified ostream object which properties are set thanks to the Decorato...
Definition: decorator.hpp:98
std::chrono::steady_clock steady_clock
Definition: progress_bar.hpp:50
bool isFloatingPoint(const T &)
Function to check if an expression (not a type) is a floating point or not. I know this function is a...
Definition: generic.hpp:140
std::unordered_map< std::string, std::unordered_set< std::string > > string_set_map
Definition: progress_bar.hpp:49
std::chrono::duration< float, steady_clock::period > duration
Definition: progress_bar.hpp:51
const std::unordered_map< std::string, std::string > rst
It is used to store the reset features commands.
Definition: colsty.cpp:114
const std::unordered_map< std::string, std::string > sty
It is used to store the styles.
Definition: colsty.cpp:95
T_err except_error_func(const std::string &beg="", std::string var=nullptr, const std::string &end="")
Function used to throw customized stdexception error. This function is extremely specific to my purpo...
Definition: generic.hpp:68
T one(const T &iterating_var)
Function to find the incremented unit of a loop. Not easy to understand its purpose without context,...
Definition: generic.hpp:156
const std::unordered_map< std::string, std::string > col
It is used to store the colors. Note: "bg" is the prefix of the background color features and "bd" is...
Definition: colsty.cpp:39
const string_pair_map tcsc
It is used to store the terminal control sequences for clear line / screen.
Definition: cursor.cpp:80
T roundoff(T value, unsigned char prec)
Function to round a floating point to n-th decimal place after comma.
Definition: generic.hpp:123
const string_pair_map crs
It is used to store the cursor commands.
Definition: cursor.cpp:42
const std::string & feat(const std::unordered_map< std::string, std::string > &generic_map, const std::string &feat_string)
It takes an std::map object as the first argument and an std::string object (map key) as the second a...
Definition: common.cpp:41