2 Copyright (C) 2001, 2002 Free Software Foundation, Inc.
4 This file is part of GNU Wget.
6 GNU Wget is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 GNU Wget is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Wget; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 In addition, as a special exception, the Free Software Foundation
21 gives permission to link the code of its release of Wget with the
22 OpenSSL project's "OpenSSL" library (or with modified versions of it
23 that use the same license as the "OpenSSL" library), and distribute
24 the linked executables. You must obey the GNU General Public License
25 in all respects for all of the code used other than "OpenSSL". If you
26 modify this file, you may extend this exception to your version of the
27 file, but you are not obligated to do so. If you do not wish to do
28 so, delete this exception statement from your version. */
46 struct progress_implementation {
49 void *(*create) (wgint, wgint);
50 void (*update) (void *, wgint, double);
51 void (*finish) (void *, double);
52 void (*set_params) (const char *);
55 /* Necessary forward declarations. */
57 static void *dot_create (wgint, wgint);
58 static void dot_update (void *, wgint, double);
59 static void dot_finish (void *, double);
60 static void dot_set_params (const char *);
62 static void *bar_create (wgint, wgint);
63 static void bar_update (void *, wgint, double);
64 static void bar_finish (void *, double);
65 static void bar_set_params (const char *);
67 static struct progress_implementation implementations[] = {
68 { "dot", 0, dot_create, dot_update, dot_finish, dot_set_params },
69 { "bar", 1, bar_create, bar_update, bar_finish, bar_set_params }
71 static struct progress_implementation *current_impl;
72 static int current_impl_locked;
74 /* Progress implementation used by default. Can be overriden in
75 wgetrc or by the fallback one. */
77 #define DEFAULT_PROGRESS_IMPLEMENTATION "bar"
79 /* Fallback progress implementation should be something that works
80 under all display types. If you put something other than "dot"
81 here, remember that bar_set_params tries to switch to this if we're
82 not running on a TTY. So changing this to "bar" could cause
85 #define FALLBACK_PROGRESS_IMPLEMENTATION "dot"
87 /* Return true if NAME names a valid progress bar implementation. The
88 characters after the first : will be ignored. */
91 valid_progress_implementation_p (const char *name)
94 struct progress_implementation *pi = implementations;
95 char *colon = strchr (name, ':');
96 int namelen = colon ? colon - name : strlen (name);
98 for (i = 0; i < countof (implementations); i++, pi++)
99 if (!strncmp (pi->name, name, namelen))
104 /* Set the progress implementation to NAME. */
107 set_progress_implementation (const char *name)
110 struct progress_implementation *pi = implementations;
114 name = DEFAULT_PROGRESS_IMPLEMENTATION;
116 colon = strchr (name, ':');
117 namelen = colon ? colon - name : strlen (name);
119 for (i = 0; i < countof (implementations); i++, pi++)
120 if (!strncmp (pi->name, name, namelen))
123 current_impl_locked = 0;
126 /* We call pi->set_params even if colon is NULL because we
127 want to give the implementation a chance to set up some
128 things it needs to run. */
132 pi->set_params (colon);
138 static int output_redirected;
141 progress_schedule_redirect (void)
143 output_redirected = 1;
146 /* Create a progress gauge. INITIAL is the number of bytes the
147 download starts from (zero if the download starts from scratch).
148 TOTAL is the expected total number of bytes in this download. If
149 TOTAL is zero, it means that the download size is not known in
153 progress_create (wgint initial, wgint total)
155 /* Check if the log status has changed under our feet. */
156 if (output_redirected)
158 if (!current_impl_locked)
159 set_progress_implementation (FALLBACK_PROGRESS_IMPLEMENTATION);
160 output_redirected = 0;
163 return current_impl->create (initial, total);
166 /* Return true if the progress gauge is "interactive", i.e. if it can
167 profit from being called regularly even in absence of data. The
168 progress bar is interactive because it regularly updates the ETA
169 and current update. */
172 progress_interactive_p (void *progress)
174 return current_impl->interactive;
177 /* Inform the progress gauge of newly received bytes. DLTIME is the
178 time in milliseconds since the beginning of the download. */
181 progress_update (void *progress, wgint howmuch, double dltime)
183 current_impl->update (progress, howmuch, dltime);
186 /* Tell the progress gauge to clean up. Calling this will free the
187 PROGRESS object, the further use of which is not allowed. */
190 progress_finish (void *progress, double dltime)
192 current_impl->finish (progress, dltime);
197 struct dot_progress {
198 wgint initial_length; /* how many bytes have been downloaded
200 wgint total_length; /* expected total byte count when the
205 int rows; /* number of rows printed so far */
206 int dots; /* number of dots printed in this row */
207 double last_timer_value;
210 /* Dot-progress backend for progress_create. */
213 dot_create (wgint initial, wgint total)
215 struct dot_progress *dp = xnew0 (struct dot_progress);
216 dp->initial_length = initial;
217 dp->total_length = total;
219 if (dp->initial_length)
221 int dot_bytes = opt.dot_bytes;
222 wgint row_bytes = opt.dot_bytes * opt.dots_in_line;
224 int remainder = (int) (dp->initial_length % row_bytes);
225 wgint skipped = dp->initial_length - remainder;
229 int skipped_k = (int) (skipped / 1024); /* skipped amount in K */
230 int skipped_k_len = numdigit (skipped_k);
231 if (skipped_k_len < 5)
234 /* Align the [ skipping ... ] line with the dots. To do
235 that, insert the number of spaces equal to the number of
236 digits in the skipped amount in K. */
237 logprintf (LOG_VERBOSE, _("\n%*s[ skipping %dK ]"),
238 2 + skipped_k_len, "", skipped_k);
241 logprintf (LOG_VERBOSE, "\n%5ldK", (long) (skipped / 1024));
242 for (; remainder >= dot_bytes; remainder -= dot_bytes)
244 if (dp->dots % opt.dot_spacing == 0)
245 logputs (LOG_VERBOSE, " ");
246 logputs (LOG_VERBOSE, ",");
249 assert (dp->dots < opt.dots_in_line);
251 dp->accumulated = remainder;
252 dp->rows = skipped / row_bytes;
259 print_percentage (wgint bytes, wgint expected)
261 /* This intentionally rounds to the floor value because it is a
262 measure of how much data *has* been retrieved. Therefore 12.8%
263 rounds to 12% because the 13% mark has not yet been reached.
264 Likewise, 100% is only shown when all data has been retrieved,
267 int percentage = 100.0 * bytes / expected;
268 logprintf (LOG_VERBOSE, "%3d%%", percentage);
272 print_download_speed (struct dot_progress *dp, wgint bytes, double dltime)
274 logprintf (LOG_VERBOSE, " %7s",
275 retr_rate (bytes, dltime - dp->last_timer_value));
276 dp->last_timer_value = dltime;
279 /* Dot-progress backend for progress_update. */
282 dot_update (void *progress, wgint howmuch, double dltime)
284 struct dot_progress *dp = progress;
285 int dot_bytes = opt.dot_bytes;
286 wgint row_bytes = opt.dot_bytes * opt.dots_in_line;
288 log_set_flush (false);
290 dp->accumulated += howmuch;
291 for (; dp->accumulated >= dot_bytes; dp->accumulated -= dot_bytes)
294 logprintf (LOG_VERBOSE, "\n%5ldK", (long) (dp->rows * row_bytes / 1024));
296 if (dp->dots % opt.dot_spacing == 0)
297 logputs (LOG_VERBOSE, " ");
298 logputs (LOG_VERBOSE, ".");
301 if (dp->dots >= opt.dots_in_line)
303 wgint row_qty = row_bytes;
304 if (dp->rows == dp->initial_length / row_bytes)
305 row_qty -= dp->initial_length % row_bytes;
310 if (dp->total_length)
311 print_percentage (dp->rows * row_bytes, dp->total_length);
312 print_download_speed (dp, row_qty, dltime);
316 log_set_flush (true);
319 /* Dot-progress backend for progress_finish. */
322 dot_finish (void *progress, double dltime)
324 struct dot_progress *dp = progress;
325 int dot_bytes = opt.dot_bytes;
326 wgint row_bytes = opt.dot_bytes * opt.dots_in_line;
329 log_set_flush (false);
332 logprintf (LOG_VERBOSE, "\n%5ldK", (long) (dp->rows * row_bytes / 1024));
333 for (i = dp->dots; i < opt.dots_in_line; i++)
335 if (i % opt.dot_spacing == 0)
336 logputs (LOG_VERBOSE, " ");
337 logputs (LOG_VERBOSE, " ");
339 if (dp->total_length)
341 print_percentage (dp->rows * row_bytes
342 + dp->dots * dot_bytes
348 wgint row_qty = dp->dots * dot_bytes + dp->accumulated;
349 if (dp->rows == dp->initial_length / row_bytes)
350 row_qty -= dp->initial_length % row_bytes;
351 print_download_speed (dp, row_qty, dltime);
354 logputs (LOG_VERBOSE, "\n\n");
355 log_set_flush (false);
360 /* This function interprets the progress "parameters". For example,
361 if Wget is invoked with --progress=dot:mega, it will set the
362 "dot-style" to "mega". Valid styles are default, binary, mega, and
366 dot_set_params (const char *params)
368 if (!params || !*params)
369 params = opt.dot_style;
374 /* We use this to set the retrieval style. */
375 if (!strcasecmp (params, "default"))
377 /* Default style: 1K dots, 10 dots in a cluster, 50 dots in a
379 opt.dot_bytes = 1024;
380 opt.dot_spacing = 10;
381 opt.dots_in_line = 50;
383 else if (!strcasecmp (params, "binary"))
385 /* "Binary" retrieval: 8K dots, 16 dots in a cluster, 48 dots
387 opt.dot_bytes = 8192;
388 opt.dot_spacing = 16;
389 opt.dots_in_line = 48;
391 else if (!strcasecmp (params, "mega"))
393 /* "Mega" retrieval, for retrieving very long files; each dot is
394 64K, 8 dots in a cluster, 6 clusters (3M) in a line. */
395 opt.dot_bytes = 65536L;
397 opt.dots_in_line = 48;
399 else if (!strcasecmp (params, "giga"))
401 /* "Giga" retrieval, for retrieving very very *very* long files;
402 each dot is 1M, 8 dots in a cluster, 4 clusters (32M) in a
404 opt.dot_bytes = (1L << 20);
406 opt.dots_in_line = 32;
410 _("Invalid dot style specification `%s'; leaving unchanged.\n"),
414 /* "Thermometer" (bar) progress. */
416 /* Assumed screen width if we can't find the real value. */
417 #define DEFAULT_SCREEN_WIDTH 80
419 /* Minimum screen width we'll try to work with. If this is too small,
420 create_image will overflow the buffer. */
421 #define MINIMUM_SCREEN_WIDTH 45
423 /* The last known screen width. This can be updated by the code that
424 detects that SIGWINCH was received (but it's never updated from the
426 static int screen_width;
428 /* A flag that, when set, means SIGWINCH was received. */
429 static volatile sig_atomic_t received_sigwinch;
431 /* Size of the download speed history ring. */
432 #define DLSPEED_HISTORY_SIZE 20
434 /* The minimum time length of a history sample. By default, each
435 sample is at least 150ms long, which means that, over the course of
436 20 samples, "current" download speed spans at least 3s into the
438 #define DLSPEED_SAMPLE_MIN 150
440 /* The time after which the download starts to be considered
441 "stalled", i.e. the current bandwidth is not printed and the recent
442 download speeds are scratched. */
443 #define STALL_START_TIME 5000
445 struct bar_progress {
446 wgint initial_length; /* how many bytes have been downloaded
448 wgint total_length; /* expected total byte count when the
450 wgint count; /* bytes downloaded so far */
452 double last_screen_update; /* time of the last screen update,
453 measured since the beginning of
456 int width; /* screen width we're using at the
457 time the progress gauge was
458 created. this is different from
459 the screen_width global variable in
460 that the latter can be changed by a
462 char *buffer; /* buffer where the bar "image" is
464 int tick; /* counter used for drawing the
465 progress bar where the total size
468 /* The following variables (kept in a struct for namespace reasons)
469 keep track of recent download speeds. See bar_update() for
471 struct bar_progress_hist {
473 wgint times[DLSPEED_HISTORY_SIZE];
474 wgint bytes[DLSPEED_HISTORY_SIZE];
476 /* The sum of times and bytes respectively, maintained for
482 double recent_start; /* timestamp of beginning of current
484 wgint recent_bytes; /* bytes downloaded so far. */
486 bool stalled; /* set when no data arrives for longer
487 than STALL_START_TIME, then reset
488 when new data arrives. */
490 /* create_image() uses these to make sure that ETA information
492 double last_eta_time; /* time of the last update to download
493 speed and ETA, measured since the
494 beginning of download. */
498 static void create_image (struct bar_progress *, double, bool);
499 static void display_image (char *);
502 bar_create (wgint initial, wgint total)
504 struct bar_progress *bp = xnew0 (struct bar_progress);
506 /* In theory, our callers should take care of this pathological
507 case, but it can sometimes happen. */
511 bp->initial_length = initial;
512 bp->total_length = total;
514 /* Initialize screen_width if this hasn't been done or if it might
515 have changed, as indicated by receiving SIGWINCH. */
516 if (!screen_width || received_sigwinch)
518 screen_width = determine_screen_width ();
520 screen_width = DEFAULT_SCREEN_WIDTH;
521 else if (screen_width < MINIMUM_SCREEN_WIDTH)
522 screen_width = MINIMUM_SCREEN_WIDTH;
523 received_sigwinch = 0;
526 /* - 1 because we don't want to use the last screen column. */
527 bp->width = screen_width - 1;
528 /* + 1 for the terminating zero. */
529 bp->buffer = xmalloc (bp->width + 1);
531 logputs (LOG_VERBOSE, "\n");
533 create_image (bp, 0, false);
534 display_image (bp->buffer);
539 static void update_speed_ring (struct bar_progress *, wgint, double);
542 bar_update (void *progress, wgint howmuch, double dltime)
544 struct bar_progress *bp = progress;
545 bool force_screen_update = false;
547 bp->count += howmuch;
548 if (bp->total_length > 0
549 && bp->count + bp->initial_length > bp->total_length)
550 /* We could be downloading more than total_length, e.g. when the
551 server sends an incorrect Content-Length header. In that case,
552 adjust bp->total_length to the new reality, so that the code in
553 create_image() that depends on total size being smaller or
554 equal to the expected size doesn't abort. */
555 bp->total_length = bp->initial_length + bp->count;
557 update_speed_ring (bp, howmuch, dltime);
559 /* If SIGWINCH (the window size change signal) been received,
560 determine the new screen size and update the screen. */
561 if (received_sigwinch)
563 int old_width = screen_width;
564 screen_width = determine_screen_width ();
566 screen_width = DEFAULT_SCREEN_WIDTH;
567 else if (screen_width < MINIMUM_SCREEN_WIDTH)
568 screen_width = MINIMUM_SCREEN_WIDTH;
569 if (screen_width != old_width)
571 bp->width = screen_width - 1;
572 bp->buffer = xrealloc (bp->buffer, bp->width + 1);
573 force_screen_update = true;
575 received_sigwinch = 0;
578 if (dltime - bp->last_screen_update < 200 && !force_screen_update)
579 /* Don't update more often than five times per second. */
582 create_image (bp, dltime, false);
583 display_image (bp->buffer);
584 bp->last_screen_update = dltime;
588 bar_finish (void *progress, double dltime)
590 struct bar_progress *bp = progress;
592 if (bp->total_length > 0
593 && bp->count + bp->initial_length > bp->total_length)
594 /* See bar_update() for explanation. */
595 bp->total_length = bp->initial_length + bp->count;
597 create_image (bp, dltime, true);
598 display_image (bp->buffer);
600 logputs (LOG_VERBOSE, "\n\n");
606 /* This code attempts to maintain the notion of a "current" download
607 speed, over the course of no less than 3s. (Shorter intervals
608 produce very erratic results.)
610 To do so, it samples the speed in 150ms intervals and stores the
611 recorded samples in a FIFO history ring. The ring stores no more
612 than 20 intervals, hence the history covers the period of at least
613 three seconds and at most 20 reads into the past. This method
614 should produce reasonable results for downloads ranging from very
617 The idea is that for fast downloads, we get the speed over exactly
618 the last three seconds. For slow downloads (where a network read
619 takes more than 150ms to complete), we get the speed over a larger
620 time period, as large as it takes to complete thirty reads. This
621 is good because slow downloads tend to fluctuate more and a
622 3-second average would be too erratic. */
625 update_speed_ring (struct bar_progress *bp, wgint howmuch, double dltime)
627 struct bar_progress_hist *hist = &bp->hist;
628 double recent_age = dltime - bp->recent_start;
630 /* Update the download count. */
631 bp->recent_bytes += howmuch;
633 /* For very small time intervals, we return after having updated the
634 "recent" download count. When its age reaches or exceeds minimum
635 sample time, it will be recorded in the history ring. */
636 if (recent_age < DLSPEED_SAMPLE_MIN)
641 /* If we're not downloading anything, we might be stalling,
642 i.e. not downloading anything for an extended period of time.
643 Since 0-reads do not enter the history ring, recent_age
644 effectively measures the time since last read. */
645 if (recent_age >= STALL_START_TIME)
647 /* If we're stalling, reset the ring contents because it's
648 stale and because it will make bar_update stop printing
649 the (bogus) current bandwidth. */
652 bp->recent_bytes = 0;
657 /* We now have a non-zero amount of to store to the speed ring. */
659 /* If the stall status was acquired, reset it. */
663 /* "recent_age" includes the the entired stalled period, which
664 could be very long. Don't update the speed ring with that
665 value because the current bandwidth would start too small.
666 Start with an arbitrary (but more reasonable) time value and
671 /* Store "recent" bytes and download time to history ring at the
674 /* To correctly maintain the totals, first invalidate existing data
675 (least recent in time) at this position. */
676 hist->total_time -= hist->times[hist->pos];
677 hist->total_bytes -= hist->bytes[hist->pos];
679 /* Now store the new data and update the totals. */
680 hist->times[hist->pos] = recent_age;
681 hist->bytes[hist->pos] = bp->recent_bytes;
682 hist->total_time += recent_age;
683 hist->total_bytes += bp->recent_bytes;
685 /* Start a new "recent" period. */
686 bp->recent_start = dltime;
687 bp->recent_bytes = 0;
689 /* Advance the current ring position. */
690 if (++hist->pos == DLSPEED_HISTORY_SIZE)
694 /* Sledgehammer check to verify that the totals are accurate. */
697 double sumt = 0, sumb = 0;
698 for (i = 0; i < DLSPEED_HISTORY_SIZE; i++)
700 sumt += hist->times[i];
701 sumb += hist->bytes[i];
703 assert (sumt == hist->total_time);
704 assert (sumb == hist->total_bytes);
709 static const char *eta_to_human_short (int);
711 #define APPEND_LITERAL(s) do { \
712 memcpy (p, s, sizeof (s) - 1); \
713 p += sizeof (s) - 1; \
716 /* Use move_to_end (s) to get S to point the end of the string (the
717 terminating \0). This is faster than s+=strlen(s), but some people
718 are confused when they see strchr (s, '\0') in the code. */
719 #define move_to_end(s) s = strchr (s, '\0');
722 # define MAX(a, b) ((a) >= (b) ? (a) : (b))
726 create_image (struct bar_progress *bp, double dl_total_time, bool done)
728 char *p = bp->buffer;
729 wgint size = bp->initial_length + bp->count;
731 const char *size_grouped = with_thousand_seps (size);
732 int size_grouped_len = strlen (size_grouped);
734 struct bar_progress_hist *hist = &bp->hist;
736 /* The progress bar should look like this:
737 xx% [=======> ] nn,nnn 12.34K/s eta 36m 51s
739 Calculate the geometry. The idea is to assign as much room as
740 possible to the progress bar. The other idea is to never let
741 things "jitter", i.e. pad elements that vary in size so that
742 their variance does not affect the placement of other elements.
743 It would be especially bad for the progress bar to be resized
746 "xx% " or "100%" - percentage - 4 chars
747 "[]" - progress bar decorations - 2 chars
748 " nnn,nnn,nnn" - downloaded bytes - 12 chars or very rarely more
749 " 1012.56K/s" - dl rate - 11 chars
750 " eta 36m 51s" - ETA - 13 chars
752 "=====>..." - progress bar - the rest
754 int dlbytes_size = 1 + MAX (size_grouped_len, 11);
755 int progress_size = bp->width - (4 + 2 + dlbytes_size + 11 + 13);
757 if (progress_size < 5)
761 if (bp->total_length > 0)
763 int percentage = 100.0 * size / bp->total_length;
764 assert (percentage <= 100);
766 if (percentage < 100)
767 sprintf (p, "%2d%% ", percentage);
773 APPEND_LITERAL (" ");
775 /* The progress bar: "[====> ]" or "[++==> ]". */
776 if (progress_size && bp->total_length > 0)
778 /* Size of the initial portion. */
779 int insz = (double)bp->initial_length / bp->total_length * progress_size;
781 /* Size of the downloaded portion. */
782 int dlsz = (double)size / bp->total_length * progress_size;
787 assert (dlsz <= progress_size);
788 assert (insz <= dlsz);
793 /* Print the initial portion of the download with '+' chars, the
794 rest with '=' and one '>'. */
795 for (i = 0; i < insz; i++)
800 for (i = 0; i < dlsz - 1; i++)
805 while (p - begin < progress_size)
809 else if (progress_size)
811 /* If we can't draw a real progress bar, then at least show
812 *something* to the user. */
813 int ind = bp->tick % (progress_size * 2 - 6);
816 /* Make the star move in two directions. */
817 if (ind < progress_size - 2)
820 pos = progress_size - (ind - progress_size + 5);
823 for (i = 0; i < progress_size; i++)
825 if (i == pos - 1) *p++ = '<';
826 else if (i == pos ) *p++ = '=';
827 else if (i == pos + 1) *p++ = '>';
837 sprintf (p, " %-11s", size_grouped);
841 if (hist->total_time && hist->total_bytes)
843 static const char *short_units[] = { "B/s", "K/s", "M/s", "G/s" };
845 /* Calculate the download speed using the history ring and
846 recent data that hasn't made it to the ring yet. */
847 wgint dlquant = hist->total_bytes + bp->recent_bytes;
848 double dltime = hist->total_time + (dl_total_time - bp->recent_start);
849 double dlspeed = calc_rate (dlquant, dltime, &units);
850 sprintf (p, " %7.2f%s", dlspeed, short_units[units]);
854 APPEND_LITERAL (" --.--K/s");
858 /* " eta ..m ..s"; wait for three seconds before displaying the ETA.
859 That's because the ETA value needs a while to become
861 if (bp->total_length > 0 && bp->count > 0 && dl_total_time > 3000)
865 /* Don't change the value of ETA more than approximately once
866 per second; doing so would cause flashing without providing
867 any value to the user. */
868 if (bp->total_length != size
869 && bp->last_eta_value != 0
870 && dl_total_time - bp->last_eta_time < 900)
871 eta = bp->last_eta_value;
874 /* Calculate ETA using the average download speed to predict
875 the future speed. If you want to use a speed averaged
876 over a more recent period, replace dl_total_time with
877 hist->total_time and bp->count with hist->total_bytes.
878 I found that doing that results in a very jerky and
879 ultimately unreliable ETA. */
880 double time_sofar = (double) dl_total_time / 1000;
881 wgint bytes_remaining = bp->total_length - size;
882 eta = (int) (time_sofar * bytes_remaining / bp->count + 0.5);
883 bp->last_eta_value = eta;
884 bp->last_eta_time = dl_total_time;
887 /* Translation note: "ETA" is English-centric, but this must
888 be short, ideally 3 chars. Abbreviate if necessary. */
889 sprintf (p, _(" eta %s"), eta_to_human_short (eta));
892 else if (bp->total_length > 0)
894 APPEND_LITERAL (" ");
899 /* When the download is done, print the elapsed time. */
900 double secs = dl_total_time / 1000;
901 /* Note to translators: this should not take up more room than
902 available here. Abbreviate if necessary. */
903 strcpy (p, _(" in "));
904 move_to_end (p); /* not p+=6, think translations! */
906 strcpy (p, eta_to_human_short ((int) (secs + 0.5)));
908 /* For very quick downloads show more exact timing information. */
910 secs < 0.001 ? 0 : /* 0s instead of 0.000s */
911 secs < 0.01 ? 3 : /* 0.00x */
912 secs < 0.1 ? 2 : /* 0.0x */
913 1, /* 0.x, 1.x, ..., 9.x */
918 assert (p - bp->buffer <= bp->width);
920 while (p < bp->buffer + bp->width)
925 /* Print the contents of the buffer as a one-line ASCII "image" so
926 that it can be overwritten next time. */
929 display_image (char *buf)
931 bool old = log_set_save_context (false);
932 logputs (LOG_VERBOSE, "\r");
933 logputs (LOG_VERBOSE, buf);
934 log_set_save_context (old);
938 bar_set_params (const char *params)
940 char *term = getenv ("TERM");
943 && 0 == strcmp (params, "force"))
944 current_impl_locked = 1;
948 /* The progress bar doesn't make sense if the output is not a
949 TTY -- when logging to file, it is better to review the
951 || !isatty (fileno (stderr))
953 /* Normally we don't depend on terminal type because the
954 progress bar only uses ^M to move the cursor to the
955 beginning of line, which works even on dumb terminals. But
956 Jamie Zawinski reports that ^M and ^H tricks don't work in
957 Emacs shell buffers, and only make a mess. */
958 || (term && 0 == strcmp (term, "emacs"))
960 && !current_impl_locked)
962 /* We're not printing to a TTY, so revert to the fallback
963 display. #### We're recursively calling
964 set_progress_implementation here, which is slightly kludgy.
965 It would be nicer if we provided that function a return value
966 indicating a failure of some sort. */
967 set_progress_implementation (FALLBACK_PROGRESS_IMPLEMENTATION);
974 progress_handle_sigwinch (int sig)
976 received_sigwinch = 1;
977 signal (SIGWINCH, progress_handle_sigwinch);
981 /* Provide a short human-readable rendition of the ETA. This is like
982 secs_to_human_time in main.c, except the output doesn't include
983 fractions (which would look silly in by nature imprecise ETA) and
984 takes less room. If the time is measured in hours, hours and
985 minutes (but not seconds) are shown; if measured in days, then days
986 and hours are shown. This ensures brevity while still displaying
989 If SEP is false, the separator between minutes and seconds (and
990 hours and minutes, etc.) is not included, shortening the display by
991 one additional character. This is used for dot progress.
993 The display never occupies more than 7 characters of screen
997 eta_to_human_short (int secs)
999 static char buf[10]; /* 8 should be enough, but just in case */
1000 static int last = -1;
1002 /* Trivial optimization. create_image can call us every 200 msecs
1003 (see bar_update) for fast downloads, but ETA will only change
1004 once per 900 msecs. */
1010 sprintf (buf, "%ds", secs);
1011 else if (secs < 100 * 60)
1012 sprintf (buf, "%dm %ds", secs / 60, secs % 60);
1013 else if (secs < 100 * 3600)
1014 sprintf (buf, "%dh %dm", secs / 3600, (secs / 60) % 60);
1015 else if (secs < 100 * 86400)
1016 sprintf (buf, "%dd %dh", secs / 86400, (secs / 3600) % 60);
1018 /* even (2^31-1)/86400 doesn't overflow BUF. */
1019 sprintf (buf, "%dd", secs / 86400);