minix/commands/simple/progressbar.c

80 lines
1.6 KiB
C
Raw Normal View History

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
2005-08-11 18:18:53 +02:00
#include <unistd.h>
2005-08-12 13:23:33 +02:00
#include <string.h>
_PROTOTYPE(int main, (int argc, char **argv));
void
prettyprogress(long b, long maxb, time_t starttime)
{
/* print progress indication */
time_t spent, now;
2005-08-11 18:18:53 +02:00
double bpsec;
time(&now);
spent = now - starttime;
2005-08-12 13:23:33 +02:00
printf("\r"); /* Make sure progress bar starts at beginning of line */
2005-08-11 18:18:53 +02:00
if(spent > 0 && (bpsec = (double)b / spent) > 0) {
int len, i;
long secremain, minremain, hremain;
secremain = (maxb - b) / bpsec;
minremain = (secremain / 60) % 60;
hremain = secremain / 3600;
2005-08-12 14:35:17 +02:00
len = printf("Remaining: %ld files. ", maxb-b);
2005-08-11 18:18:53 +02:00
#if 0
2005-08-12 13:23:33 +02:00
len += printf("ETA: %d:%02d:%02d ",
hremain, minremain, secremain % 60);
2005-08-11 18:18:53 +02:00
#endif
2005-08-12 13:23:33 +02:00
len += printf(" [");
2005-08-11 18:18:53 +02:00
#define WIDTH 77
len = WIDTH - len;
for(i = 0; i < (b * (len-1) / maxb); i++)
2005-08-12 13:23:33 +02:00
printf("=");
printf("|");
for(; i < len-2; i++)
2005-08-12 13:23:33 +02:00
printf("-");
2005-08-12 14:35:17 +02:00
printf("]\n");
2005-08-12 13:23:33 +02:00
} else printf("\n");
return;
}
int main(argc, argv)
int argc;
char *argv[];
{
long i = 0, count = 0;
2005-08-12 13:23:33 +02:00
int l;
char line[2000];
time_t start;
if(argc < 2) return 1;
count = atol(argv[1]);
if(count < 1) return 1;
time(&start);
printf("\n");
2005-08-12 13:23:33 +02:00
#define LINES 5
for(l = 1; l <= LINES+1; l++) printf("\n");
printf("");
while(fgets(line, sizeof(line), stdin)) {
2005-08-12 14:35:17 +02:00
char *nl;
i++;
2005-08-12 13:23:33 +02:00
for(l = 0; l <= LINES; l++) printf("");
if(i <= count) prettyprogress(i, count, start);
2005-08-12 13:23:33 +02:00
else printf("\n");
printf("");
for(l = 0; l < LINES; l++) printf("");
2005-08-12 14:35:17 +02:00
if((nl = strchr(line, '\n'))) *nl = '\0';
line[78] = '\0';
printf("\r%s\r", line);
}
2005-08-12 13:23:33 +02:00
printf("\nDone.\n");
return 0;
}