chiark / gitweb /
better progress indication
[ypp-sc-tools.db-test.git] / pctb / convert.c
1
2 #include "convert.h"
3
4
5 void debug_flush(void) {
6   eassert(!fflush(debug));
7   eassert(!ferror(debug));
8 }
9
10
11 const char *get_vardir(void) { return "."; }
12
13 static enum {
14   mf_findwindow=    01,
15   mf_screenshot=    02,
16   mf_analyse=       04,
17   
18   mode_findwindow=  01,
19   mode_screenshot=  03,
20   mode_analyse=     04,
21
22   mode_all=         07,
23 } o_mode= mode_all;
24
25 static char *o_screenshots_fn;
26 static int o_single_page, o_quiet;
27
28 FILE *screenshots_file;
29
30 int main(int argc, char **argv) {
31   const char *arg;
32   int r;
33
34   while ((arg=*++argv)) {
35     if (!strcmp(arg,"--find-window-only"))
36       o_mode= mode_findwindow;
37     else if (!strcmp(arg,"--screenshot-only"))
38       o_mode= mode_screenshot;
39     else if (!strcmp(arg,"--analyse-only"))
40       o_mode= mode_analyse;
41     else if (!strcmp(arg,"--single-page"))
42       o_single_page= 1;
43     else if (!strcmp(arg,"--quiet"))
44       o_quiet= 1;
45     else if (!strcmp(arg,"--screenshots-file"))
46       eassert( o_screenshots_fn= *++argv );
47 #define DF(f)                                   \
48     else if (!strcmp(arg,"-D" #f))              \
49       debug_flags |= dbg_##f;
50     DEBUG_FLAG_LIST
51 #undef DF
52     else if (!strcmp(arg,"--window-id")) {
53       char *ep;
54       eassert((arg=*++argv));
55       unsigned long windowid= strtoul(arg,&ep,0);
56       eassert(!*ep);
57       set_yppclient_window(windowid);
58     } else
59       eassert(!"bad option");
60   }
61   
62   if (!o_screenshots_fn) {
63     r= asprintf(&o_screenshots_fn,"%s/#pages#.ppm",get_vardir());
64     eassert(r>=0);  eassert(o_screenshots_fn);
65   }
66
67   if (o_mode & mf_findwindow) {
68     screenshot_startup();
69     find_yppclient_window();
70   }
71   if (o_mode & mf_screenshot) {
72     screenshots_file= fopen(o_screenshots_fn, "w"); eassert(screenshots_file);
73     if (o_single_page) take_one_screenshot();
74     else take_screenshots();
75   } else {
76     screenshots_file= fopen(o_screenshots_fn, "r"); eassert(screenshots_file);
77     if (o_single_page) read_one_screenshot();
78     else read_screenshots();
79   }
80   if (o_mode & mf_analyse) {
81     analyse();
82     //output_tsv();
83   }
84   return 0;
85 }
86
87
88
89
90 DEFINE_VWRAPPERF(, progress)
91 DEFINE_VWRAPPERF(, progress_log)
92 DEFINE_VWRAPPERF(, progress_spinner)
93
94 static int last_progress_len;
95      
96 static void vprogress_core(int spinner, const char *fmt, va_list al) {
97   int r;
98   
99   if (o_quiet) return;
100   if (!isatty(2)) return;
101   
102   if (last_progress_len)
103     putc('\r',stderr);
104
105   r= vfprintf(stderr,fmt,al);
106   eassert(r>=0);
107
108   if (spinner) {
109     putc(spinner,stderr);
110     r++;
111   }
112
113   if (r < last_progress_len) {
114     fprintf(stderr,"%*s", last_progress_len - r, "");
115     if (!r) putc('\r', stderr);
116     else while (last_progress_len-- > r) putc('\b',stderr);
117   }
118   last_progress_len= r;
119   fflush(stderr);
120 }
121    
122 void vprogress(const char *fmt, va_list al) { vprogress_core(0,fmt,al); }
123 void vprogress_spinner(const char *fmt, va_list al) {
124   static const char spinchars[]="/-\\";
125   static int spinner;
126
127   vprogress_core(spinchars[spinner],fmt,al);
128   spinner++;
129   spinner %= (sizeof(spinchars)-1);
130 }
131
132 void vprogress_log(const char *fmt, va_list al) {
133   if (o_quiet) return;
134   
135   progress("");
136   vfprintf(stderr,fmt,al);
137   putc('\n',stderr);
138   fflush(stderr);
139 }