forked from gipert/progressbar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
progressbar.hpp
308 lines (264 loc) · 8 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
#pragma once
// Copyright (c) 2021 Gerp Muller <[email protected]>
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// 3. Neither the name of mosquitto nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
#include <cmath>
#include <iomanip>
#include <iostream>
#include <set>
#include <stdexcept>
#include <string>
// #include <chrono>
// ETA
#if defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#define VC_EXTRALEAN
#include <Windows.h>
#elif defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
#include <sys/ioctl.h>
#endif // Windows/Linux
class Progress
{
public:
Progress() : nTotalSteps(0), nFinishedSteps(0)
{
}
Progress(size_t totalSteps, size_t nFinishedSteps = 0) : nTotalSteps(totalSteps), nFinishedSteps(nFinishedSteps)
{
}
~Progress() = default;
void advance(size_t nSteps = 1)
{
nFinishedSteps += nSteps;
onAdvance();
}
void setTotalSteps(size_t totalSteps)
{
nTotalSteps = totalSteps;
}
size_t getTotalSteps()
{
return nTotalSteps;
}
void setFinishedSteps(size_t finishedSteps)
{
nFinishedSteps = finishedSteps;
}
size_t getFinishedSteps()
{
return nFinishedSteps;
}
float getFraction()
{
return float(nFinishedSteps) / float(nTotalSteps);
}
void reset(size_t nInitialSteps = 0)
{
nFinishedSteps = std::min(nTotalSteps, nInitialSteps);
onReset();
}
virtual void onReset()
{
}
virtual void onAdvance()
{
}
protected:
size_t nTotalSteps;
size_t nFinishedSteps;
};
class SingleLineProgress : public Progress
{
public:
SingleLineProgress() : Progress()
{
}
SingleLineProgress(size_t totalSteps, size_t nFinishedSteps = 0) : Progress(totalSteps, nFinishedSteps)
{
}
void onAdvance()
{
std::cout << "\r" << std::setw(3) << int((100.0f * nFinishedSteps) / nTotalSteps) << "% ("
<< std::setw(1 + ::log10(nTotalSteps)) << nFinishedSteps << "/" << nTotalSteps << ")";
if (nFinishedSteps == nTotalSteps)
std::cout << ", done\n";
std::cout.flush();
}
};
// Updating files: 100% (808/808), done.
class ProgressBar : public Progress
{
public:
ProgressBar();
ProgressBar(size_t totalSteps, size_t nFinishedSteps = 0);
~ProgressBar() = default;
// chose your style
void set_done_char(const std::string &sym)
{
done_char = sym;
}
void set_todo_char(const std::string &sym)
{
todo_char = sym;
}
void set_opening_bracket_char(const std::string &sym)
{
opening_bracket_char = sym;
}
void set_closing_bracket_char(const std::string &sym)
{
closing_bracket_char = sym;
}
// to show only the percentage
void show_bar(bool flag = true)
{
do_show_bar = flag;
}
// main function
void drfaw(Progress &progress);
void draw_percentage(float fraction);
void draw_bar(float fraction);
void draw();
void reset();
// get window_width
int get_window_width() const;
virtual void onReset()
{
reset();
}
virtual void onAdvance()
{
draw();
}
// virtual void onFinish(){};
private:
int last_promille;
int last_done_chars;
int last_todo_chars;
bool do_show_bar;
bool draw_was_called;
std::string done_char;
std::string todo_char;
std::string opening_bracket_char;
std::string closing_bracket_char;
};
ProgressBar::ProgressBar()
: Progress(), last_promille(0), last_done_chars(0), last_todo_chars(0), do_show_bar(true), draw_was_called(false),
done_char("#"), todo_char(" "), opening_bracket_char("["), closing_bracket_char("]")
{
}
ProgressBar::ProgressBar(size_t totalSteps, size_t nFinishedSteps)
: Progress(totalSteps, nFinishedSteps), last_promille(0), last_done_chars(0), last_todo_chars(0), do_show_bar(true),
draw_was_called(false), done_char("#"), todo_char(" "), opening_bracket_char("["), closing_bracket_char("]")
{
}
void ProgressBar::reset()
{
draw_was_called = false;
last_promille = 0;
last_done_chars = 0;
last_todo_chars = 0;
return;
}
void ProgressBar::draw()
{
float fraction = getFraction();
int promille = int(fraction * 1000.0);
if (promille > last_promille)
{
if (do_show_bar)
draw_bar(fraction);
else
draw_percentage(fraction);
std::cout << std::flush;
}
last_promille = promille;
}
void ProgressBar::draw_percentage(float fraction)
{
int percentage = int(fraction * 100);
if (percentage < 10)
std::cout << "\b\b" << percentage << '%';
else if (percentage >= 10 && percentage < 100)
std::cout << "\b\b\b" << percentage << '%';
else if (percentage == 100)
std::cout << "\b\b\b\b" << percentage << '%';
}
void ProgressBar::draw_bar(float fraction)
{
int width = get_window_width() - 7;
if (!draw_was_called)
{
std::cout << opening_bracket_char;
for (int _ = 0; _ < width; _++)
std::cout << todo_char;
std::cout << closing_bracket_char << " 0%";
last_done_chars = 0;
last_todo_chars = width;
}
draw_was_called = true;
int done_chars = std::min(width, int(fraction * float(width)));
int todo_chars = width - done_chars;
// erase percentage
std::cout << "\b\b\b\b";
// erase closing bracket
std::cout << std::string(closing_bracket_char.size(), '\b');
// erase 'todo_char'
for (int j = 0; j < last_todo_chars; ++j)
{
std::cout << std::string(todo_char.size(), '\b');
}
for (int j = 0; j < last_done_chars; ++j)
{
std::cout << std::string(todo_char.size(), '\b');
}
// refill with 'todo_char'
for (int j = 0; j < done_chars; ++j)
std::cout << done_char;
for (int j = 0; j < todo_chars; ++j)
std::cout << todo_char;
// readd trailing percentage characters
std::cout << closing_bracket_char;
int percentage = int(fraction * 100);
if (percentage < 10)
std::cout << " " << percentage << '%';
else if (percentage >= 10 && percentage < 100)
std::cout << " " << percentage << '%';
else if (percentage == 100)
std::cout << " " << percentage << '%';
}
int ProgressBar::get_window_width() const
{
#if defined(_WIN32)
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
return (int)(csbi.dwSize.X);
#elif defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
struct winsize w;
ioctl(fileno(stdout), TIOCGWINSZ, &w);
return (int)(w.ws_col);
#endif // Windows/Linux
}