-
Notifications
You must be signed in to change notification settings - Fork 33
/
progressbar.c
66 lines (55 loc) · 1.86 KB
/
progressbar.c
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
#include <stdio.h>
#include "progressbar.h"
// see progressbar.h
static const unsigned progressbar_width = 70;
static unsigned get_to_print(unsigned current, unsigned max) {
return progressbar_width * current / max;
}
static unsigned get_percentage(unsigned current, unsigned max) {
return 100 * current / max;
}
static void progressbar_show(struct progressbar *pb) {
unsigned to_print = get_to_print(pb->current, pb->max);
unsigned percentage = get_percentage(pb->current, pb->max);
printf("\r[");
for(unsigned i = 0; i < to_print; i++) {
printf("#");
}
for(unsigned i = 0; i < progressbar_width - to_print; i++) {
printf(" ");
}
printf("] %3u%%", percentage);
fflush(stdout);
}
void progressbar_start(struct progressbar *pb, unsigned max) {
pb->max = max;
pb->current = 0;
progressbar_show(pb);
}
void progressbar_set(struct progressbar *pb, unsigned current) {
unsigned old_to_print = get_to_print(pb->current, pb->max);
unsigned old_percentage = get_percentage(pb->current, pb->max);
unsigned to_print = get_to_print(current, pb->max);
unsigned percentage = get_percentage(current, pb->max);
pb->current = current;
if(old_to_print == to_print && old_percentage == percentage) {
return;
}
progressbar_show(pb);
}
void progressbar_add(struct progressbar *pb, unsigned n) {
progressbar_set(pb, pb->current + n);
}
void progressbar_inc(struct progressbar *pb) {
progressbar_add(pb, 1);
}
void progressbar_clear(struct progressbar *pb) {
(void)pb;
printf("\r");
for(unsigned i = 0; i < progressbar_width + 7; i++) {
printf(" ");
}
fflush(stdout);
printf("\r");
fflush(stdout);
}