opendir change: refinement
[minix.git] / commands / progressbar / progressbar.c
blob4065fa9e5d22c020f2f1de92194f62a02b4f7a77
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <time.h>
5 #include <unistd.h>
6 #include <string.h>
8 int main(int argc, char **argv);
10 void
11 prettyprogress(long b, long maxb, time_t starttime)
13 /* print progress indication */
14 time_t spent, now;
15 double bpsec;
16 time(&now);
17 spent = now - starttime;
18 printf("\r"); /* Make sure progress bar starts at beginning of line */
19 if(spent > 0 && (bpsec = (double)b / spent) > 0) {
20 int len, i;
21 long secremain, minremain, hremain;
22 secremain = (maxb - b) / bpsec;
23 minremain = (secremain / 60) % 60;
24 hremain = secremain / 3600;
25 len = printf("Remaining: %ld files. ", maxb-b);
27 #if 0
28 len += printf("ETA: %d:%02d:%02d ",
29 hremain, minremain, secremain % 60);
30 #endif
32 len += printf(" [");
34 #define WIDTH 77
35 len = WIDTH - len;
36 for(i = 0; i < (b * (len-1) / maxb); i++)
37 printf("=");
38 printf("|");
39 for(; i < len-2; i++)
40 printf("-");
41 printf("]\e[K\n");
42 } else printf("\n");
44 return;
47 int main(argc, argv)
48 int argc;
49 char *argv[];
51 long i = 0, count = 0;
52 int l;
53 char line[2000];
54 time_t start;
55 if(argc < 2) return 1;
56 count = atol(argv[1]);
57 if(count < 0) return 1;
58 time(&start);
59 printf("\n");
60 #define LINES 5
61 for(l = 1; l <= LINES+1; l++) printf("\n");
62 printf("\e[A");
63 while(fgets(line, sizeof(line), stdin)) {
64 char *nl;
65 i++;
66 for(l = 0; l <= LINES; l++) printf("\e[A");
67 if(i <= count) prettyprogress(i, count, start);
68 else printf("\n");
69 printf("\e[M");
70 for(l = 0; l < LINES; l++) printf("\e[B");
71 if((nl = strchr(line, '\n'))) *nl = '\0';
72 line[78] = '\0';
73 printf("\r%s\r", line);
76 printf("\nDone.\e[K\n");
78 return 0;