Al's window feature

This commit is contained in:
Ben Gras 2005-08-12 11:23:33 +00:00
parent fb2fc31f8e
commit ea5c8d0318

View file

@ -3,6 +3,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
#include <string.h>
_PROTOTYPE(int main, (int argc, char **argv)); _PROTOTYPE(int main, (int argc, char **argv));
@ -14,31 +15,31 @@ prettyprogress(long b, long maxb, time_t starttime)
double bpsec; double bpsec;
time(&now); time(&now);
spent = now - starttime; spent = now - starttime;
printf("\r"); /* Make sure progress bar starts at beginning of line */
if(spent > 0 && (bpsec = (double)b / spent) > 0) { if(spent > 0 && (bpsec = (double)b / spent) > 0) {
int len, i; int len, i;
long secremain, minremain, hremain; long secremain, minremain, hremain;
secremain = (maxb - b) / bpsec; secremain = (maxb - b) / bpsec;
minremain = (secremain / 60) % 60; minremain = (secremain / 60) % 60;
hremain = secremain / 3600; hremain = secremain / 3600;
len = fprintf(stderr, "Remain %ld files. ", maxb-b); len = printf("Remainining: %ld files. ", maxb-b);
#if 0 #if 0
len += fprintf(stderr, "ETA: %d:%02d:%02d ", len += printf("ETA: %d:%02d:%02d ",
hremain, minremain, secremain % 60); hremain, minremain, secremain % 60);
#endif #endif
len += fprintf(stderr, " ["); len += printf(" [");
#define WIDTH 77 #define WIDTH 77
len = WIDTH - len; len = WIDTH - len;
for(i = 0; i < (b * (len-1) / maxb); i++) for(i = 0; i < (b * (len-1) / maxb); i++)
fprintf(stderr, "="); printf("=");
fprintf(stderr, "|"); printf("|");
for(; i < len-2; i++) for(; i < len-2; i++)
fprintf(stderr, "-"); printf("-");
fprintf(stderr, "]\r"); printf("]\n");
fflush(stderr); } else printf("\n");
}
return; return;
} }
@ -48,24 +49,29 @@ int argc;
char *argv[]; char *argv[];
{ {
long i = 0, count = 0; long i = 0, count = 0;
int l;
char line[2000]; char line[2000];
time_t start; time_t start;
if(argc < 2) return 1; if(argc < 2) return 1;
count = atol(argv[1]); count = atol(argv[1]);
if(count < 1) return 1; if(count < 1) return 1;
sync();
time(&start); time(&start);
printf("\n"); printf("\n");
#define LINES 5
for(l = 1; l <= LINES+1; l++) printf("\n");
printf("");
sleep(1);
while(fgets(line, sizeof(line), stdin)) { while(fgets(line, sizeof(line), stdin)) {
i++; i++;
printf("%s", line); for(l = 0; l <= LINES; l++) printf("");
if(i <= count) prettyprogress(i, count, start); if(i <= count) prettyprogress(i, count, start);
else printf("\r"); else printf("\n");
printf(""); printf("");
fflush(stdout); for(l = 0; l < LINES; l++) printf("");
printf("\r%s", line);
} }
fprintf(stderr, "\nDone.\n"); printf("\nDone.\n");
return 0; return 0;
} }