1 /* guestfish - the filesystem interactive shell
2 * Copyright (C) 2010 Red Hat Inc.
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.
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.
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.
31 /* Include these last since they redefine symbols such as 'lines'
32 * which seriously breaks other headers.
37 /* Provided by termcap or terminfo emulation, but not defined
40 extern const char *UP;
45 /* Choice of unicode spinners.
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
51 * Arrows are a mess in unicode. This page helps a lot:
52 * http://xahlee.org/comp/unicode_arrows.html
54 * I prefer something which doesn't point, just spins.
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" };
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" };
73 static const char *as[] = { "/", "-", "\\", "|" };
80 n = sizeof us / sizeof us[0];
84 n = sizeof as / sizeof as[0];
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 */
94 /* This function is called just before we issue any command. */
96 reset_progress_bar (void)
98 /* The time at which this command was issued. */
99 struct timeval start_t;
100 gettimeofday (&start_t, NULL);
102 start = start_t.tv_sec + start_t.tv_usec / 1000000.;
109 /* Return remaining time estimate (in seconds) for current call.
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).
118 estimate_remaining_time (double ratio)
123 struct timeval now_t;
124 gettimeofday (&now_t, NULL);
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;
130 double total_time = time_passed / ratio;
132 /* Add total_time to running mean and s.d. and then see if our
133 * estimate of total time is meaningful.
135 rmsd_add_sample (&rmsd, total_time);
137 double mean = rmsd_get_mean (&rmsd);
138 double sd = rmsd_get_standard_deviation (&rmsd);
139 if (fabs (total_time - mean) >= 2.0*sd)
142 /* Don't return early estimates. */
143 if (time_passed < 3.0)
146 return total_time - time_passed;
149 /* The overhead is how much we subtract before we get to the progress
152 * / 100% [########---------------] xx:xx
154 * | | | | time (5 cols)
156 * | | open paren + close paren + space (3 cols)
158 * | percentage and space (5 cols)
160 * spinner and space (2 cols)
162 * Total = 2 + 5 + 3 + 5 = 15
164 #define COLS_OVERHEAD 15
166 /* Callback which displays a progress bar. */
168 progress_callback (guestfs_h *g, void *data,
169 int proc_nr, int serial,
170 uint64_t position, uint64_t total)
172 if (have_terminfo == 0) {
174 printf ("%" PRIu64 "/%" PRIu64 "\n", position, total);
176 int cols = tgetnum ((char *) "co");
177 if (cols < 32) goto dumb;
179 /* Update an existing progress bar just printed? */
181 tputs (UP, 2, putchar);
184 double ratio = (double) position / total;
185 if (ratio < 0) ratio = 0; else if (ratio > 1) ratio = 1;
188 int percent = 100.0 * ratio;
189 printf ("%s%3d%% ", spinner (count), percent);
191 fputs (" 100% ", stdout);
194 int dots = ratio * (double) (cols - COLS_OVERHEAD);
196 const char *s_open, *s_dot, *s_dash, *s_close;
198 s_open = "\u27e6"; s_dot = "\u2589"; s_dash = "\u2550"; s_close = "\u27e7";
200 s_open = "["; s_dot = "#"; s_dash = "-"; s_close = "]";
203 fputs (s_open, stdout);
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);
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);
219 } else if (estimate >= 100.0 * 60.0 /* >= 100 minutes */) {
220 /* Display hours<h>minutes */
221 estimate /= 60. * 60.;
222 int hh = floor (estimate);
224 int mm = floor (modf (estimate, &ignore) * 60.);
225 printf ("%02dh%02d", hh, mm);
226 } else if (estimate >= 0.0) {
227 /* Display minutes:seconds */
229 int mm = floor (estimate);
231 int ss = floor (modf (estimate, &ignore) * 60.);
232 printf ("%02d:%02d", mm, ss);
234 else /* < 0 means estimate was not meaningful */
235 fputs ("--:--", stdout);
237 fputc ('\n', stdout);