diff options
author | Richard W.M. Jones <rjones@redhat.com> | 2012-10-11 13:51:19 +0000 |
---|---|---|
committer | Richard W.M. Jones <rjones@redhat.com> | 2012-10-30 18:42:33 +0000 |
commit | 1226162d4b1cedc889294a41c84424ff8dceaa42 (patch) | |
tree | 76176570962c95264a1020834145999548c7879b | |
parent | 49a2541872fc48846f091e0c1c4b621117e6310d (diff) | |
download | libguestfs-1226162d4b1cedc889294a41c84424ff8dceaa42.tar.gz libguestfs-1226162d4b1cedc889294a41c84424ff8dceaa42.tar.xz libguestfs-1226162d4b1cedc889294a41c84424ff8dceaa42.zip |
fish: progress bar: Send interactive progress bar output to /dev/tty (RHBZ#859875).
(cherry picked from commit 911a16a9fa965ce8defb3307f6bf338f5d2d5c94)
(cherry picked from commit ea09c6e889b57aab8d2989dba4821a66785ad991)
-rw-r--r-- | fish/progress.c | 54 |
1 files changed, 36 insertions, 18 deletions
diff --git a/fish/progress.c b/fish/progress.c index 6c30e952..1f0c0b43 100644 --- a/fish/progress.c +++ b/fish/progress.c @@ -93,6 +93,7 @@ struct progress_bar { int have_terminfo; int utf8_mode; int machine_readable; + FILE *fp; /* output device, only used when !dumb mode */ }; struct progress_bar * @@ -121,6 +122,8 @@ progress_bar_init (unsigned flags) if (tgetent (NULL, term) == 1) bar->have_terminfo = 1; } + + bar->fp = fopen ("/dev/tty", "w"); /* deliberately ignore errors */ } /* Call this to ensure the other fields are in a reasonable state. @@ -135,6 +138,8 @@ progress_bar_init (unsigned flags) void progress_bar_free (struct progress_bar *bar) { + if (bar->fp) + fclose (bar->fp); free (bar); } @@ -265,17 +270,30 @@ progress_bar_set (struct progress_bar *bar, int i, cols, pulse_mode; double ratio; const char *s_open, *s_dot, *s_dash, *s_close; + FILE *fp; if (bar->machine_readable || bar->have_terminfo == 0) { dumb: printf ("%" PRIu64 "/%" PRIu64 "\n", position, total); + fflush (stdout); } else { cols = tgetnum ((char *) "co"); if (cols < 32) goto dumb; + /* Send progress bar output to /dev/tty if we could open it, else stdout. */ + fp = bar->fp; + if (!fp) + fp = stdout; + /* Update an existing progress bar just printed? */ - if (bar->count > 0) - tputs (UP, 2, putchar); + if (bar->count > 0) { + /* XXX We should call tputs here, but (a) it's unlikely that any + * modern terminal is so slow that it requires padding, and + * (b) it's just not possible to use tputs in a sane way here. + */ + /*tputs (UP, 2, putchar);*/ + fprintf (fp, "%s", UP); + } bar->count++; /* Find out if we're in "pulse mode". */ @@ -285,14 +303,14 @@ progress_bar_set (struct progress_bar *bar, if (ratio < 0) ratio = 0; else if (ratio > 1) ratio = 1; if (pulse_mode) { - printf ("%s --- ", spinner (bar, bar->count)); + fprintf (fp, "%s --- ", spinner (bar, bar->count)); } else if (ratio < 1) { int percent = 100.0 * ratio; - printf ("%s%3d%% ", spinner (bar, bar->count), percent); + fprintf (fp, "%s%3d%% ", spinner (bar, bar->count), percent); } else { - fputs (" 100% ", stdout); + fputs (" 100% ", fp); } if (bar->utf8_mode) { @@ -304,28 +322,28 @@ progress_bar_set (struct progress_bar *bar, s_open = "["; s_dot = "#"; s_dash = "-"; s_close = "]"; } - fputs (s_open, stdout); + fputs (s_open, fp); if (!pulse_mode) { int dots = ratio * (double) (cols - COLS_OVERHEAD); for (i = 0; i < dots; ++i) - fputs (s_dot, stdout); + fputs (s_dot, fp); for (i = dots; i < cols - COLS_OVERHEAD; ++i) - fputs (s_dash, stdout); + fputs (s_dash, fp); } else { /* "Pulse mode": the progress bar just pulses. */ for (i = 0; i < cols - COLS_OVERHEAD; ++i) { int cc = (bar->count * 3 - i) % (cols - COLS_OVERHEAD); if (cc >= 0 && cc <= 3) - fputs (s_dot, stdout); + fputs (s_dot, fp); else - fputs (s_dash, stdout); + fputs (s_dash, fp); } } - fputs (s_close, stdout); - fputc (' ', stdout); + fputs (s_close, fp); + fputc (' ', fp); /* Time estimate. */ double estimate = estimate_remaining_time (bar, ratio); @@ -333,26 +351,26 @@ progress_bar_set (struct progress_bar *bar, /* Display hours<h> */ estimate /= 60. * 60.; int hh = floor (estimate); - printf (">%dh", hh); + fprintf (fp, ">%dh", hh); } else if (estimate >= 100.0 * 60.0 /* >= 100 minutes */) { /* Display hours<h>minutes */ estimate /= 60. * 60.; int hh = floor (estimate); double ignore; int mm = floor (modf (estimate, &ignore) * 60.); - printf ("%02dh%02d", hh, mm); + fprintf (fp, "%02dh%02d", hh, mm); } else if (estimate >= 0.0) { /* Display minutes:seconds */ estimate /= 60.; int mm = floor (estimate); double ignore; int ss = floor (modf (estimate, &ignore) * 60.); - printf ("%02d:%02d", mm, ss); + fprintf (fp, "%02d:%02d", mm, ss); } else /* < 0 means estimate was not meaningful */ - fputs ("--:--", stdout); + fputs ("--:--", fp); - fputc ('\n', stdout); + fputc ('\n', fp); + fflush (fp); } - fflush (stdout); } |