X-Git-Url: http://git.annexia.org/?p=libguestfs.git;a=blobdiff_plain;f=fish%2Fprogress.c;h=fea93845fb313f8dab6237333806892778351746;hp=27dfbecef2a3bf23d33a6223adffd59926c7983e;hb=1fdd0193fd63af71359748915a0326d623a3d6ad;hpb=096f341714ee7d1575c93d437a8085821aa88d23 diff --git a/fish/progress.c b/fish/progress.c index 27dfbec..fea9384 100644 --- a/fish/progress.c +++ b/fish/progress.c @@ -1,5 +1,5 @@ /* guestfish - the filesystem interactive shell - * Copyright (C) 2010 Red Hat Inc. + * Copyright (C) 2010-2011 Red Hat Inc. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -167,14 +167,33 @@ estimate_remaining_time (double ratio) /* Callback which displays a progress bar. */ void progress_callback (guestfs_h *g, void *data, - int proc_nr, int serial, - uint64_t position, uint64_t total) + uint64_t event, int event_handle, int flags, + const char *buf, size_t buf_len, + const uint64_t *array, size_t array_len) { + int i, cols, pulse_mode; + double ratio; + const char *s_open, *s_dot, *s_dash, *s_close; + + if (utf8_mode) { + s_open = "\u27e6"; s_dot = "\u2589"; s_dash = "\u2550"; s_close = "\u27e7"; + } else { + s_open = "["; s_dot = "#"; s_dash = "-"; s_close = "]"; + } + + if (array_len < 4) + return; + + /*uint64_t proc_nr = array[0];*/ + /*uint64_t serial = array[1];*/ + uint64_t position = array[2]; + uint64_t total = array[3]; + if (have_terminfo == 0) { dumb: printf ("%" PRIu64 "/%" PRIu64 "\n", position, total); } else { - int cols = tgetnum ((char *) "co"); + cols = tgetnum ((char *) "co"); if (cols < 32) goto dumb; /* Update an existing progress bar just printed? */ @@ -182,31 +201,43 @@ progress_callback (guestfs_h *g, void *data, tputs (UP, 2, putchar); count++; - double ratio = (double) position / total; + /* Find out if we're in "pulse mode". */ + pulse_mode = position == 0 && total == 1; + + ratio = (double) position / total; if (ratio < 0) ratio = 0; else if (ratio > 1) ratio = 1; - if (ratio < 1) { + if (pulse_mode) { + printf ("%s --- ", spinner (count)); + } + else if (ratio < 1) { int percent = 100.0 * ratio; printf ("%s%3d%% ", spinner (count), percent); - } else { + } + else { fputs (" 100% ", stdout); } - int dots = ratio * (double) (cols - COLS_OVERHEAD); + fputs (s_open, stdout); - const char *s_open, *s_dot, *s_dash, *s_close; - if (utf8_mode) { - s_open = "\u27e6"; s_dot = "\u2589"; s_dash = "\u2550"; s_close = "\u27e7"; - } else { - s_open = "["; s_dot = "#"; s_dash = "-"; s_close = "]"; + if (!pulse_mode) { + int dots = ratio * (double) (cols - COLS_OVERHEAD); + + for (i = 0; i < dots; ++i) + fputs (s_dot, stdout); + for (i = dots; i < cols - COLS_OVERHEAD; ++i) + fputs (s_dash, stdout); + } + else { /* "Pulse mode": the progress bar just pulses. */ + for (i = 0; i < cols - COLS_OVERHEAD; ++i) { + int cc = (count * 3 - i) % (cols - COLS_OVERHEAD); + if (cc >= 0 && cc <= 3) + fputs (s_dot, stdout); + else + fputs (s_dash, stdout); + } } - fputs (s_open, stdout); - int i; - for (i = 0; i < dots; ++i) - fputs (s_dot, stdout); - for (i = dots; i < cols - COLS_OVERHEAD; ++i) - fputs (s_dash, stdout); fputs (s_close, stdout); fputc (' ', stdout);