-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprogress.c
More file actions
50 lines (37 loc) · 1.32 KB
/
progress.c
File metadata and controls
50 lines (37 loc) · 1.32 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 <stdio.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <string.h>
#include <math.h>
#include "progress.h"
void print_bars(ProgressInfo * info) {
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
printf("\033[%dF", info->num_progressbars);
for (Progressbar * bar = info->progressbars; bar < info->progressbars + info->num_progressbars; ++bar) {
const unsigned limitstrlen = log10(bar->limit) + 1;
const unsigned barlen = w.ws_col - strlen(bar->title) - (bar->show_percentage ? 3 : 2 * limitstrlen) - 10;
printf(" %s [", bar->title);
unsigned progress_rel_to_bar = (float)bar->progress / bar->limit * barlen;
for (int i = 0; i < progress_rel_to_bar; ++i)
printf("%c", info->fill_char);
if (bar->progress < bar->limit)
printf("%c", info->last_char);
else
printf("%c", info->fill_char);
printf("%*s] [", barlen - progress_rel_to_bar, "");
if (bar->show_percentage) {
const unsigned p = (unsigned)((float)bar->progress / bar->limit * 100);
printf("%*s", 2 - (unsigned)log10(p), "");
printf("%d%%", p);
} else {
printf("%*s", limitstrlen - 1 - (unsigned)(bar->progress ? log10(bar->progress) : 0), "");
printf("%d/%d", bar->progress, bar->limit);
}
printf("]\n");
}
}
void init_bars(ProgressInfo * info) {
for (int i = 0; i < info->num_progressbars; ++i)
printf("\n");
}