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