-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogress.h
More file actions
50 lines (41 loc) · 1.08 KB
/
progress.h
File metadata and controls
50 lines (41 loc) · 1.08 KB
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
#include <iostream>
#include <string>
#ifdef _WIN32
// #include <unistd.h>
#else
#include <unistd.h>
#endif
class ProgressBar {
public:
ProgressBar(int total) : total(total), current(0) {}
void update(int amount) {
current += amount;
int percent = (int)(((float)current / (float)total) * 100);
// calculate the length of the progress bar
int length = (int)((float)percent / 100.0f * 20.0f);
// output the progress bar to the terminal
std::cout << "\r[";
for (int i = 0; i < length; i++) {
std::cout << "#";
}
for (int i = 0; i < 20 - length; i++) {
std::cout << " ";
}
std::cout << "] " << percent << "%" << std::flush;
}
//************
// call close to force 100% if needed
//
////************
void close() {
int diff;
if ( current != total ) {
// std::cout << "status below 100%\n";
diff = total - current;
update(diff);
}
}
private:
int total;
int current;
};