-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogress_bar.h
73 lines (58 loc) · 2.06 KB
/
progress_bar.h
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
// progress_bar.h
//
// a simple progress bar for terminal output
#ifndef PROGRESS_BAR_H
#define PROGRESS_BAR_H
#include <unistd.h>
#include <stdio.h>
class progress_bar {
FILE *output = nullptr;
static constexpr const char *non = "............................................................";
static constexpr const char *bar = "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||";
size_t pbwidth = strlen(bar);
public:
progress_bar() : output{nullptr} { }
progress_bar(FILE *f) {
set_output(f);
pbwidth = strlen(bar); // note: could find terminal width from tty_ioctl(TIOCGWINSZ)
}
void set_output(FILE *f) {
if (isatty(fileno(f))) { // TODO: conditional compilation around POSIX functions
output = f;
}
}
void print_bar(size_t iteration, size_t num_trials) {
if (output) {
// avoid division by zero
if (num_trials == 0) {
return;
}
// we only need to update the progress bar at most
// num_trials/100 times; for most iterations, we can just
// return and avoid the extra computation needed to print
// out the bar
//
if (iteration % (num_trials/100) != 0 && iteration != num_trials-1) {
return;
}
// compute length of left and right parts of bar
//
double fraction = (double) (iteration+1) / num_trials;
int percent = (int) (fraction * 100);
int lpad = (int) (fraction * pbwidth);
int rpad = pbwidth - lpad;
// display progress bar
//
fprintf(output, "\033[;32m");
fprintf(output, "\r%3d%% [%.*s%.*s]", percent, lpad, bar, rpad, non);
fprintf(output, "\033[0m");
fflush(output);
// advance to next line, if we are finished
//
if (iteration == num_trials-1) {
fputc('\n', output);
}
}
}
};
#endif // PROGRESS_BAR_H