RUSH
Reusable Utilities in Single Headers is a collection of header-only utilities for C++
Loading...
Searching...
No Matches
progress-bar.hpp
1
8#ifndef RUSH_PROGRESS_BAR_HPP
9#define RUSH_PROGRESS_BAR_HPP
10
11#include "rush/string.hpp"
12#include <algorithm>
13#include <array>
14#include <iostream>
15#include <string>
16#include <sys/ioctl.h>
17#include <unistd.h>
18#include <utility>
19
20namespace rush::progress {
21
26 std::string name;
27 std::string complete{"="};
28 std::string uncomplete{" "};
29 std::array<std::string, 2> decorator{{"[", "]"}};
30 bool percentage{true};
31 double decimals{0};
32};
33
34class Bar {
35private:
36 int width_{0};
37 double max_{0};
38 double current_{0};
39 Configuration config_;
40
41 double update(double p) {
42 p = std::clamp(p, 0.0, 1.0);
43
44 std::string s = std::string("\033[0m") + config_.name;
45 if(!config_.name.empty()) {
46 s += " ";
47 }
48 s += config_.decorator[0];
49 s += static_cast<rush::string>(config_.complete) * static_cast<int>(p * width_);
50 s += static_cast<rush::string>(config_.uncomplete) * (width_ - static_cast<int>(p * width_));
51 s += config_.decorator[1];
52
53 if(config_.percentage) {
54 s += " " + std::to_string(p * 100).substr(0, std::to_string(p * 100).find('.') + (config_.decimals > 0) + config_.decimals) + "%";
55 }
56
57 std::cout << "\u001b[1000D\033[31m\033[41m" << s << std::flush;
58 return p;
59 }
60
61public:
67 explicit Bar(const double max, Configuration cfg = Configuration()) : max_{max}, config_{std::move(cfg)} {
68 struct winsize w{};
69 ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
70 width_ = w.ws_col - config_.decorator.size() - config_.name.size() - config_.decimals - 7;
71 }
72
74 ~Bar() {
75 std::cout << '\n';
76 }
84 double operator()(const double x) {
85 return update((current_ = x) / max_);
86 }
87
92 double operator++() {
93 return update(++current_ / max_);
94 }
95
100 double operator++(int) {
101 return update(++current_ / max_);
102 }
103
109 double operator+=(const int n) {
110 return update((current_ += n) / max_);
111 }
112
117 double operator--() {
118 return update(--current_ / max_);
119 }
120
125 double operator--(int) {
126 return update(--current_ / max_);
127 }
128
134 double operator-=(const int n) {
135 return update((current_ -= n) / max_);
136 }
137};
138
139} // namespace rush::progress
140
141#endif // RUSH_PROGRESS_BAR_HPP
Definition progress-bar.hpp:34
double operator++(int)
Increment the progress bar by 1 (postfix).
Definition progress-bar.hpp:100
Bar(const double max, Configuration cfg=Configuration())
Construct a new Bar object.
Definition progress-bar.hpp:67
double operator-=(const int n)
Decrement the progress bar by a specified value.
Definition progress-bar.hpp:134
double operator+=(const int n)
Increment the progress bar by a specified value.
Definition progress-bar.hpp:109
double operator++()
Increment the progress bar by 1.
Definition progress-bar.hpp:92
double operator()(const double x)
Update the progress bar with a specific value.
Definition progress-bar.hpp:84
double operator--(int)
Decrement the progress bar by 1 (postfix).
Definition progress-bar.hpp:125
double operator--()
Decrement the progress bar by 1.
Definition progress-bar.hpp:117
This class extends std::string.
Definition string.hpp:22
This library provides string-related utilities.
Configuration structure for the progress bar.
Definition progress-bar.hpp:25
std::string uncomplete
Character used to represent uncompleted progress.
Definition progress-bar.hpp:28
std::string name
Name displayed alongside the progress bar.
Definition progress-bar.hpp:26
std::string complete
Character used to represent completed progress.
Definition progress-bar.hpp:27
bool percentage
Flag indicating whether to display percentage completion.
Definition progress-bar.hpp:30
std::array< std::string, 2 > decorator
Decorator strings surrounding the progress bar.
Definition progress-bar.hpp:29
double decimals
Number of decimal places for the percentage display.
Definition progress-bar.hpp:31