24 #ifndef JODA_PROGRESSBAR_H
25 #define JODA_PROGRESSBAR_H
34 template <
typename IntType>
39 const IntType total_ticks;
40 const IntType bar_width;
41 const char complete_char =
'=';
42 const char incomplete_char =
' ';
43 std::chrono::steady_clock::time_point start_time =
44 std::chrono::steady_clock::now();
50 static void output_time(std::stringstream& ss,
double seconds) {
51 std::size_t s_in_h(60 * 60), s_in_m(60) ;
53 IntType hours_rem = seconds / s_in_h;
55 IntType minutes_rem = (seconds - (hours_rem * s_in_h)) / s_in_m;
58 seconds - ((hours_rem * s_in_h) + (minutes_rem * s_in_m));
60 std::ios init(
nullptr), time_fmt(
nullptr);
62 ss << std::setfill(
'0') << std::setw(2);
82 ProgressBar(IntType total, IntType width,
char complete,
char incomplete)
83 : max_limit(std::numeric_limits<IntType>().max()),
86 complete_char{complete},
87 incomplete_char{incomplete} {}
90 : max_limit(std::numeric_limits<IntType>().max()),
95 if (ticks != max_limit) {
120 template <
typename NumberType>
123 ticks = std::min(prog, max_limit);
127 template <
typename NumberType>
132 IntType dist_from_limit = max_limit - ticks;
133 if (prog <= dist_from_limit) {
134 ticks = ticks + prog;
139 template <
typename NumberType>
144 IntType dist_from_limit = ticks - 0;
145 if (prog <= dist_from_limit) {
146 ticks = ticks - prog;
153 static_cast<double>(ticks) /
static_cast<double>(total_ticks);
154 IntType pos = (IntType)(bar_width * progress);
156 std::chrono::steady_clock::time_point now =
157 std::chrono::steady_clock::now();
159 std::chrono::duration_cast<std::chrono::milliseconds>(now - start_time)
162 double seconds =
static_cast<double>(time_elapsed) / 1000.0;
167 std::stringstream ss;
172 for (IntType i = 0; i < bar_width; ++i) {
178 ss << incomplete_char;
181 ss <<
"] " <<
static_cast<IntType
>(progress * 100.0) <<
"% (";
183 ss << ticks <<
"/" << total_ticks <<
") ";
185 ProgressBar::output_time(ss, seconds);
199 static_cast<double>(ticks) /
static_cast<double>(total_ticks);
200 IntType pos = (IntType)(bar_width * progress);
202 std::chrono::steady_clock::time_point now =
203 std::chrono::steady_clock::now();
205 std::chrono::duration_cast<std::chrono::milliseconds>(now - start_time)
208 double seconds =
static_cast<double>(time_elapsed) / 1000.0;
209 double ticks_per_second =
static_cast<double>(ticks) / seconds;
210 double remaining_ticks = total_ticks - ticks;
211 double total_seconds_rem = remaining_ticks / ticks_per_second;
213 std::stringstream ss;
218 for (IntType i = 0; i < bar_width; ++i) {
224 ss << incomplete_char;
227 ss <<
"] " <<
static_cast<IntType
>(progress * 100.0) <<
"% ";
229 ss << ticks <<
" / " << total_ticks <<
" ticks ";
231 ProgressBar::output_time(ss, seconds);
235 ProgressBar::output_time(ss, total_seconds_rem);
239 ss <<
" (" <<
static_cast<std::size_t
>(ticks_per_second) <<
" t/s)";
241 std::cout << ss.str() << std::flush;
246 std::cout << std::endl;
250 start_time = std::chrono::steady_clock::now();
Definition: ProgressBar.h:35
void remove_progress(NumberType prog)
Definition: ProgressBar.h:140
ProgressBar & operator--()
Definition: ProgressBar.h:107
void add_progress(NumberType prog)
Definition: ProgressBar.h:128
const ProgressBar operator--(int)
Definition: ProgressBar.h:114
std::string toString() const
Definition: ProgressBar.h:151
const ProgressBar operator++(int)
Definition: ProgressBar.h:101
void reset()
Definition: ProgressBar.h:249
ProgressBar & operator++()
Definition: ProgressBar.h:94
ProgressBar(IntType total, IntType width, char complete, char incomplete)
Definition: ProgressBar.h:82
void display() const
Definition: ProgressBar.h:197
void done() const
Definition: ProgressBar.h:244
const IntType getBar_width() const
Definition: ProgressBar.h:47
void set_progress(NumberType prog)
Definition: ProgressBar.h:121
ProgressBar(IntType total, IntType width)
Definition: ProgressBar.h:89