fish: Use a perfect hash for faster command lookups.
[libguestfs.git] / fish / progress.c
1 /* guestfish - the filesystem interactive shell
2  * Copyright (C) 2010 Red Hat Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <inttypes.h>
24 #include <math.h>
25
26 #include <guestfs.h>
27
28 #include "fish.h"
29 #include "rmsd.h"
30
31 /* Include these last since they redefine symbols such as 'lines'
32  * which seriously breaks other headers.
33  */
34 #include <term.h>
35 #include <curses.h>
36
37 /* Provided by termcap or terminfo emulation, but not defined
38  * in any header file.
39  */
40 extern const char *UP;
41
42 static const char *
43 spinner (int count)
44 {
45   /* Choice of unicode spinners.
46    *
47    * For basic dingbats, see:
48    * http://www.fileformat.info/info/unicode/block/geometric_shapes/utf8test.htm
49    * http://www.fileformat.info/info/unicode/block/dingbats/utf8test.htm
50    *
51    * Arrows are a mess in unicode.  This page helps a lot:
52    * http://xahlee.org/comp/unicode_arrows.html
53    *
54    * I prefer something which doesn't point, just spins.
55    */
56   /* Black pointing triangle. */
57   //static const char *us[] = { "\u25b2", "\u25b6", "\u25bc", "\u25c0" };
58   /* White pointing triangle. */
59   //static const char *us[] = { "\u25b3", "\u25b7", "\u25bd", "\u25c1" };
60   /* Circle with half black. */
61   static const char *us[] = { "\u25d0", "\u25d3", "\u25d1", "\u25d2" };
62   /* White square white quadrant. */
63   //static const char *us[] = { "\u25f0", "\u25f3", "\u25f2", "\u25f1" };
64   /* White circle white quadrant. */
65   //static const char *us[] = { "\u25f4", "\u25f7", "\u25f6", "\u25f5" };
66   /* Black triangle. */
67   //static const char *us[] = { "\u25e2", "\u25e3", "\u25e4", "\u25e5" };
68   /* Spinning arrow in 8 directions. */
69   //static const char *us[] = { "\u2190", "\u2196", "\u2191", "\u2197",
70   //                            "\u2192", "\u2198", "\u2193", "\u2199" };
71
72   /* ASCII spinner. */
73   static const char *as[] = { "/", "-", "\\", "|" };
74
75   const char **s;
76   size_t n;
77
78   if (utf8_mode) {
79     s = us;
80     n = sizeof us / sizeof us[0];
81   }
82   else {
83     s = as;
84     n = sizeof as / sizeof as[0];
85   }
86
87   return s[count % n];
88 }
89
90 static double start;         /* start time of command */
91 static int count;            /* number of progress notifications per cmd */
92 static struct rmsd rmsd;     /* running mean and standard deviation */
93
94 /* This function is called just before we issue any command. */
95 void
96 reset_progress_bar (void)
97 {
98   /* The time at which this command was issued. */
99   struct timeval start_t;
100   gettimeofday (&start_t, NULL);
101
102   start = start_t.tv_sec + start_t.tv_usec / 1000000.;
103
104   count = 0;
105
106   rmsd_init (&rmsd);
107 }
108
109 /* Return remaining time estimate (in seconds) for current call.
110  *
111  * This returns the running mean estimate of remaining time, but if
112  * the latest estimate of total time is greater than two s.d.'s from
113  * the running mean then we don't print anything because we're not
114  * confident that the estimate is meaningful.  (Returned value is <0.0
115  * when nothing should be printed).
116  */
117 static double
118 estimate_remaining_time (double ratio)
119 {
120   if (ratio <= 0.)
121     return -1.0;
122
123   struct timeval now_t;
124   gettimeofday (&now_t, NULL);
125
126   double now = now_t.tv_sec + now_t.tv_usec / 1000000.;
127   /* We've done 'ratio' of the work in 'now - start' seconds. */
128   double time_passed = now - start;
129
130   double total_time = time_passed / ratio;
131
132   /* Add total_time to running mean and s.d. and then see if our
133    * estimate of total time is meaningful.
134    */
135   rmsd_add_sample (&rmsd, total_time);
136
137   double mean = rmsd_get_mean (&rmsd);
138   double sd = rmsd_get_standard_deviation (&rmsd);
139   if (fabs (total_time - mean) >= 2.0*sd)
140     return -1.0;
141
142   /* Don't return early estimates. */
143   if (time_passed < 3.0)
144     return -1.0;
145
146   return total_time - time_passed;
147 }
148
149 /* The overhead is how much we subtract before we get to the progress
150  * bar itself.
151  *
152  * / 100% [########---------------] xx:xx
153  * | |    |                       | |
154  * | |    |                       | time (5 cols)
155  * | |    |                       |
156  * | |    open paren + close paren + space (3 cols)
157  * | |
158  * | percentage and space (5 cols)
159  * |
160  * spinner and space (2 cols)
161  *
162  * Total = 2 + 5 + 3 + 5 = 15
163  */
164 #define COLS_OVERHEAD 15
165
166 /* Callback which displays a progress bar. */
167 void
168 progress_callback (guestfs_h *g, void *data,
169                    int proc_nr, int serial,
170                    uint64_t position, uint64_t total)
171 {
172   if (have_terminfo == 0) {
173   dumb:
174     printf ("%" PRIu64 "/%" PRIu64 "\n", position, total);
175   } else {
176     int cols = tgetnum ((char *) "co");
177     if (cols < 32) goto dumb;
178
179     /* Update an existing progress bar just printed? */
180     if (count > 0)
181       tputs (UP, 2, putchar);
182     count++;
183
184     double ratio = (double) position / total;
185     if (ratio < 0) ratio = 0; else if (ratio > 1) ratio = 1;
186
187     if (ratio < 1) {
188       int percent = 100.0 * ratio;
189       printf ("%s%3d%% ", spinner (count), percent);
190     } else {
191       fputs (" 100% ", stdout);
192     }
193
194     int dots = ratio * (double) (cols - COLS_OVERHEAD);
195
196     const char *s_open, *s_dot, *s_dash, *s_close;
197     if (utf8_mode) {
198       s_open = "\u27e6"; s_dot = "\u2589"; s_dash = "\u2550"; s_close = "\u27e7";
199     } else {
200       s_open = "["; s_dot = "#"; s_dash = "-"; s_close = "]";
201     }
202
203     fputs (s_open, stdout);
204     int i;
205     for (i = 0; i < dots; ++i)
206       fputs (s_dot, stdout);
207     for (i = dots; i < cols - COLS_OVERHEAD; ++i)
208       fputs (s_dash, stdout);
209     fputs (s_close, stdout);
210     fputc (' ', stdout);
211
212     /* Time estimate. */
213     double estimate = estimate_remaining_time (ratio);
214     if (estimate >= 100.0 * 60.0 * 60.0 /* >= 100 hours */) {
215       /* Display hours<h> */
216       estimate /= 60. * 60.;
217       int hh = floor (estimate);
218       printf (">%dh", hh);
219     } else if (estimate >= 100.0 * 60.0 /* >= 100 minutes */) {
220       /* Display hours<h>minutes */
221       estimate /= 60. * 60.;
222       int hh = floor (estimate);
223       double ignore;
224       int mm = floor (modf (estimate, &ignore) * 60.);
225       printf ("%02dh%02d", hh, mm);
226     } else if (estimate >= 0.0) {
227       /* Display minutes:seconds */
228       estimate /= 60.;
229       int mm = floor (estimate);
230       double ignore;
231       int ss = floor (modf (estimate, &ignore) * 60.);
232       printf ("%02d:%02d", mm, ss);
233     }
234     else /* < 0 means estimate was not meaningful */
235       fputs ("--:--", stdout);
236
237     fputc ('\n', stdout);
238   }
239 }