minix/commands/progressbar/progressbar.c
2010-05-12 16:28:54 +00:00

79 lines
1.6 KiB
C
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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