source: trunk/minix/commands/simple/progressbar.c@ 10

Last change on this file since 10 was 9, checked in by Mattia Monga, 13 years ago

Minix 3.1.2a

File size: 1.6 KB
Line 
1
2#include <stdio.h>
3#include <stdlib.h>
4#include <time.h>
5#include <unistd.h>
6#include <string.h>
7
8_PROTOTYPE(int main, (int argc, char **argv));
9
10void
11prettyprogress(long b, long maxb, time_t starttime)
12{
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);
26
27#if 0
28 len += printf("ETA: %d:%02d:%02d ",
29 hremain, minremain, secremain % 60);
30#endif
31
32 len += printf(" [");
33
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("][K\n");
42 } else printf("\n");
43
44 return;
45}
46
47int main(argc, argv)
48int argc;
49char *argv[];
50{
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("[A");
63 while(fgets(line, sizeof(line), stdin)) {
64 char *nl;
65 i++;
66 for(l = 0; l <= LINES; l++) printf("[A");
67 if(i <= count) prettyprogress(i, count, start);
68 else printf("\n");
69 printf("[M");
70 for(l = 0; l < LINES; l++) printf("[B");
71 if((nl = strchr(line, '\n'))) *nl = '\0';
72 line[78] = '\0';
73 printf("\r%s\r", line);
74 }
75
76 printf("\nDone.[K\n");
77
78 return 0;
79}
Note: See TracBrowser for help on using the repository browser.