-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogressbar.hpp
340 lines (304 loc) · 11.2 KB
/
progressbar.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <sys/ioctl.h>
#include <thread>
#include <unistd.h>
#include <utility>
#include "windowhistory.hpp"
enum class DisplayComponent {
None,
ElapsedTime,
RawProgress,
ProgressBar,
Percentage,
EstimatedTime,
Speed,
All
};
template <DisplayComponent dc> struct ComponentFormatter;
template <DisplayComponent... dcs> struct Printer;
template <typename T, DisplayComponent... dcs> class ProgressBarImpl;
template <typename T, DisplayComponent... dcs>
struct ProgressBar : public ProgressBarImpl<T, dcs...> {
ProgressBar(const T &progress, const T &max_progress)
: ProgressBarImpl<T, dcs...>(progress, max_progress) {}
};
template <typename T>
struct ProgressBar<T> : public ProgressBarImpl<T, DisplayComponent::All> {
ProgressBar(const T &progress, const T &max_progress)
: ProgressBarImpl<T, DisplayComponent::All>(progress, max_progress) {}
};
template <typename T, DisplayComponent... dcs> class ProgressBarImpl {
public:
ProgressBarImpl(const T &progress, const T &max_progress)
: progress(progress), max_progress(max_progress), speed_history() {}
void init() {
start = std::chrono::steady_clock::now();
history.emplace(start, progress);
while (true) {
print_progressbar();
std::this_thread::sleep_for(
std::chrono::milliseconds(sampling_freq_millis));
if (progress == max_progress) {
print_progressbar();
// plot_speed_history();
std::cout << '\n';
break;
}
}
}
private:
template <DisplayComponent dc> friend struct ComponentFormatter;
typedef std::chrono::time_point<std::chrono::steady_clock> time_point;
/* configurations */
static constexpr size_t bar_width = 50;
static constexpr size_t sampling_freq_millis = 100;
static constexpr const char *fillchar = "█";
static constexpr const char *emptychar = "―";
static constexpr size_t history_window_size = 20;
static constexpr size_t speed_history_size = 20;
static constexpr size_t speed_history_plot_height = 15;
/* end configurations */
/* members */
const T &progress;
const T &max_progress;
WindowHistory<std::pair<time_point, T>, history_window_size> history;
std::array<double, speed_history_size> speed_history;
double current_speed;
time_point start;
time_point current;
/* end members */
double current_progress() const {
return static_cast<double>(progress) / max_progress;
}
size_t map_progress(size_t upper) const { return current_progress() * upper; }
double calculate_speed() const {
T window_progress = history.end().second - history.beg().second;
double time_taken = duration_sec(history.beg().first, history.end().first);
return window_progress / time_taken;
}
static double duration_sec(const time_point &t1, const time_point &t2) {
return std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1)
.count() /
1000.0;
}
static std::string fmt_time(double tot_seconds) {
std::stringstream ss;
ss << std::fixed << std::setprecision(2);
ss << std::setw(2);
size_t days = tot_seconds / (60 * 60 * 24);
size_t hours = std::fmod(tot_seconds, 60 * 60 * 24) / (60 * 60);
size_t minutes = std::fmod(tot_seconds, 60 * 60) / 60;
double seconds = std::fmod(tot_seconds, 60);
bool started = false;
if (days > 0) {
ss << days << 'd' << std::setfill('0');
started = true;
}
if (started || hours > 0) {
ss << hours << 'h' << std::setfill('0');
started = true;
}
if (started || minutes > 0) {
ss << minutes << 'm' << std::setfill('0');
}
ss << seconds << 's';
std::string out = ss.str();
if (out.size() > 15) {
out = "-";
}
return out;
}
void store_speeds() {
current_speed = calculate_speed();
speed_history[map_progress(speed_history_size)] = current_speed;
}
void print_progressbar() {
current = std::chrono::steady_clock::now();
history.emplace(current, progress);
store_speeds();
std::ostringstream ss;
ss << std::fixed << std::setprecision(2);
Printer<dcs...>::print(this, ss);
std::string output = ss.str();
#if 0
struct winsize size;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &size);
size_t columns = size.ws_col;
size_t output_lines = (output.size() + columns - 1)/ columns;
// for(size_t i = 0; i < output_lines - 1; i++) {
// std::cout << "\033[F";
// }
#endif
std::cout << "\33[2K\r" << output << std::flush;
}
void plot_speed_history() const {
std::array<std::array<std::string, speed_history_size>,
speed_history_plot_height>
plot;
for (auto &s : plot)
for (auto &c : s)
c = "·";
double max_speed = 0;
for (const auto &speed : speed_history) {
max_speed = (max_speed < speed &&
speed != std::numeric_limits<double>::infinity())
? speed
: max_speed;
}
for (size_t i = 0; i < speed_history_size; i++) {
if (speed_history[i] != std::numeric_limits<double>::infinity()) {
plot[(size_t)((speed_history[i] / max_speed) *
(speed_history_plot_height - 1))][i] = "⏺";
}
}
std::cout << '\n';
for (size_t i = 0; i < speed_history_plot_height; i++) {
std::cout << "│ ";
for (size_t j = 0; j < speed_history_size; j++) {
std::cout << plot[speed_history_plot_height - 1 - i][j] << " ";
}
std::cout << '\n';
}
std::cout << "└─";
for (size_t i = 0; i < speed_history_size; i++) {
std::cout << "───";
}
std::cout << "\n";
for (size_t i = 1; i <= speed_history_size; i++) {
std::cout << std::setw(3) << ((i * 100) / speed_history_size)
<< std::flush;
}
std::cout << std::endl;
}
};
template <typename T, DisplayComponent... dcs>
constexpr size_t ProgressBarImpl<T, dcs...>::bar_width;
template <typename T, DisplayComponent... dcs>
constexpr size_t ProgressBarImpl<T, dcs...>::sampling_freq_millis;
template <typename T, DisplayComponent... dcs>
constexpr const char *ProgressBarImpl<T, dcs...>::fillchar;
template <typename T, DisplayComponent... dcs>
constexpr const char *ProgressBarImpl<T, dcs...>::emptychar;
template <typename T, DisplayComponent... dcs>
constexpr size_t ProgressBarImpl<T, dcs...>::history_window_size;
template <typename T, DisplayComponent... dcs>
constexpr size_t ProgressBarImpl<T, dcs...>::speed_history_size;
template <typename T, DisplayComponent... dcs>
constexpr size_t ProgressBarImpl<T, dcs...>::speed_history_plot_height;
template <DisplayComponent dc> struct ComponentFormatter {
template <typename T, DisplayComponent... dcs>
static void print(const ProgressBarImpl<T, dcs...> *pb,
std::ostringstream &ss) {}
};
template <> struct ComponentFormatter<DisplayComponent::ElapsedTime> {
template <typename T, DisplayComponent... dcs>
static void print(const ProgressBarImpl<T, dcs...> *pb,
std::ostringstream &ss) {
double current_time =
ProgressBarImpl<T, dcs...>::duration_sec(pb->start, pb->current);
ss << "[Elapsed: " << ProgressBarImpl<T, dcs...>::fmt_time(current_time)
<< "]";
}
};
template <> struct ComponentFormatter<DisplayComponent::RawProgress> {
template <typename T, DisplayComponent... dcs>
static void print(const ProgressBarImpl<T, dcs...> *pb,
std::ostringstream &ss) {
ss << "[Progress: " << pb->progress << '/' << pb->max_progress << " Ticks]";
}
};
template <> struct ComponentFormatter<DisplayComponent::ProgressBar> {
template <typename T, DisplayComponent... dcs>
static void print(const ProgressBarImpl<T, dcs...> *pb,
std::ostringstream &ss) {
ss << "|";
for (size_t i = 0; i < pb->bar_width; i++) {
ss << ((i <= (pb->progress * ProgressBarImpl<T, dcs...>::bar_width) /
pb->max_progress)
? ProgressBarImpl<T, dcs...>::fillchar
: ProgressBarImpl<T, dcs...>::emptychar);
}
ss << "|";
}
};
template <> struct ComponentFormatter<DisplayComponent::Percentage> {
template <typename T, DisplayComponent... dcs>
static void print(const ProgressBarImpl<T, dcs...> *pb,
std::ostringstream &ss) {
double current_progress_percentage =
(pb->progress * 100.0) / pb->max_progress;
ss << '[' << current_progress_percentage << "%]";
}
};
template <> struct ComponentFormatter<DisplayComponent::EstimatedTime> {
template <typename T, DisplayComponent... dcs>
static void print(const ProgressBarImpl<T, dcs...> *pb,
std::ostringstream &ss) {
double estimated_remaining_time =
(pb->max_progress - pb->progress) / pb->current_speed;
ss << "[Est.Remaining: "
<< ProgressBarImpl<T, dcs...>::fmt_time(estimated_remaining_time) << "]";
}
};
template <> struct ComponentFormatter<DisplayComponent::Speed> {
template <typename T, DisplayComponent... dcs>
static void print(const ProgressBarImpl<T, dcs...> *pb,
std::ostringstream &ss) {
ss << "[Speed: " << pb->current_speed << " Tick/s]";
}
};
template <DisplayComponent dc> struct Enum {
static constexpr DisplayComponent previous =
static_cast<DisplayComponent>(static_cast<size_t>(dc) - 1);
};
template <DisplayComponent dc> struct FormatAllImpl {
template <typename T, DisplayComponent... dcs>
static void print(const ProgressBarImpl<T, dcs...> *pb,
std::ostringstream &ss) {
FormatAllImpl<Enum<dc>::previous>::print(pb, ss);
ComponentFormatter<Enum<dc>::previous>::print(pb, ss);
}
};
template <> struct FormatAllImpl<DisplayComponent::None> {
template <typename T, DisplayComponent... dcs>
static void print(const ProgressBarImpl<T, dcs...> *pb,
std::ostringstream &ss) {}
};
template <> struct ComponentFormatter<DisplayComponent::All> {
template <typename T, DisplayComponent... dcs>
static void print(const ProgressBarImpl<T, dcs...> *pb,
std::ostringstream &ss) {
FormatAllImpl<DisplayComponent::All>::print(pb, ss);
}
};
template <DisplayComponent... dcs> struct Container {
static constexpr DisplayComponent dcsa[sizeof...(dcs)] = {dcs...};
};
template <size_t idx, DisplayComponent... dcs> struct Selector {
static constexpr DisplayComponent dc = Container<dcs...>::dcsa[idx];
};
template <size_t idx, DisplayComponent... dcs> struct PrinterImpl {
template <typename T>
static void print(const ProgressBarImpl<T, dcs...> *pb,
std::ostringstream &ss) {
PrinterImpl<idx - 1, dcs...>::print(pb, ss);
ComponentFormatter<Selector<idx - 1, dcs...>::dc>::print(pb, ss);
}
};
template <DisplayComponent... dcs> struct PrinterImpl<0, dcs...> {
template <typename T>
static void print(const ProgressBarImpl<T, dcs...> *pb,
std::ostringstream &ss) {}
};
template <DisplayComponent... dcs> struct Printer {
template <typename T>
static void print(const ProgressBarImpl<T, dcs...> *pb,
std::ostringstream &ss) {
PrinterImpl<sizeof...(dcs), dcs...>::print(pb, ss);
}
};