2 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software
5 This file is part of GNU Wget.
7 GNU Wget is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 GNU Wget is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Wget. If not, see <http://www.gnu.org/licenses/>.
20 Additional permission under GNU GPL version 3 section 7
22 If you modify this program, or any covered work, by linking or
23 combining it with the OpenSSL project's OpenSSL library (or a
24 modified version of that library), containing parts covered by the
25 terms of the OpenSSL or SSLeay licenses, the Free Software Foundation
26 grants you additional permission to convey the resulting work.
27 Corresponding Source for a non-source form of such a combination
28 shall include the source code for the parts of OpenSSL used as well
29 as that of the covered work. */
47 struct progress_implementation {
50 void *(*create) (wgint, wgint);
51 void (*update) (void *, wgint, double);
52 void (*finish) (void *, double);
53 void (*set_params) (const char *);
56 /* Necessary forward declarations. */
58 static void *dot_create (wgint, wgint);
59 static void dot_update (void *, wgint, double);
60 static void dot_finish (void *, double);
61 static void dot_set_params (const char *);
63 static void *bar_create (wgint, wgint);
64 static void bar_update (void *, wgint, double);
65 static void bar_finish (void *, double);
66 static void bar_set_params (const char *);
68 static struct progress_implementation implementations[] = {
69 { "dot", 0, dot_create, dot_update, dot_finish, dot_set_params },
70 { "bar", 1, bar_create, bar_update, bar_finish, bar_set_params }
72 static struct progress_implementation *current_impl;
73 static int current_impl_locked;
75 /* Progress implementation used by default. Can be overriden in
76 wgetrc or by the fallback one. */
78 #define DEFAULT_PROGRESS_IMPLEMENTATION "bar"
80 /* Fallback progress implementation should be something that works
81 under all display types. If you put something other than "dot"
82 here, remember that bar_set_params tries to switch to this if we're
83 not running on a TTY. So changing this to "bar" could cause
86 #define FALLBACK_PROGRESS_IMPLEMENTATION "dot"
88 /* Return true if NAME names a valid progress bar implementation. The
89 characters after the first : will be ignored. */
92 valid_progress_implementation_p (const char *name)
95 struct progress_implementation *pi = implementations;
96 char *colon = strchr (name, ':');
97 int namelen = colon ? colon - name : strlen (name);
99 for (i = 0; i < countof (implementations); i++, pi++)
100 if (!strncmp (pi->name, name, namelen))
105 /* Set the progress implementation to NAME. */
108 set_progress_implementation (const char *name)
111 struct progress_implementation *pi = implementations;
115 name = DEFAULT_PROGRESS_IMPLEMENTATION;
117 colon = strchr (name, ':');
118 namelen = colon ? colon - name : strlen (name);
120 for (i = 0; i < countof (implementations); i++, pi++)
121 if (!strncmp (pi->name, name, namelen))
124 current_impl_locked = 0;
127 /* We call pi->set_params even if colon is NULL because we
128 want to give the implementation a chance to set up some
129 things it needs to run. */
133 pi->set_params (colon);
139 static int output_redirected;
142 progress_schedule_redirect (void)
144 output_redirected = 1;
147 /* Create a progress gauge. INITIAL is the number of bytes the
148 download starts from (zero if the download starts from scratch).
149 TOTAL is the expected total number of bytes in this download. If
150 TOTAL is zero, it means that the download size is not known in
154 progress_create (wgint initial, wgint total)
156 /* Check if the log status has changed under our feet. */
157 if (output_redirected)
159 if (!current_impl_locked)
160 set_progress_implementation (FALLBACK_PROGRESS_IMPLEMENTATION);
161 output_redirected = 0;
164 return current_impl->create (initial, total);
167 /* Return true if the progress gauge is "interactive", i.e. if it can
168 profit from being called regularly even in absence of data. The
169 progress bar is interactive because it regularly updates the ETA
170 and current update. */
173 progress_interactive_p (void *progress)
175 return current_impl->interactive;
178 /* Inform the progress gauge of newly received bytes. DLTIME is the
179 time since the beginning of the download. */
182 progress_update (void *progress, wgint howmuch, double dltime)
184 current_impl->update (progress, howmuch, dltime);
187 /* Tell the progress gauge to clean up. Calling this will free the
188 PROGRESS object, the further use of which is not allowed. */
191 progress_finish (void *progress, double dltime)
193 current_impl->finish (progress, dltime);
198 struct dot_progress {
199 wgint initial_length; /* how many bytes have been downloaded
201 wgint total_length; /* expected total byte count when the
204 int accumulated; /* number of bytes accumulated after
205 the last printed dot */
207 int rows; /* number of rows printed so far */
208 int dots; /* number of dots printed in this row */
210 double last_timer_value;
213 /* Dot-progress backend for progress_create. */
216 dot_create (wgint initial, wgint total)
218 struct dot_progress *dp = xnew0 (struct dot_progress);
219 dp->initial_length = initial;
220 dp->total_length = total;
222 if (dp->initial_length)
224 int dot_bytes = opt.dot_bytes;
225 const wgint ROW_BYTES = opt.dot_bytes * opt.dots_in_line;
227 int remainder = dp->initial_length % ROW_BYTES;
228 wgint skipped = dp->initial_length - remainder;
232 wgint skipped_k = skipped / 1024; /* skipped amount in K */
233 int skipped_k_len = numdigit (skipped_k);
234 if (skipped_k_len < 6)
237 /* Align the [ skipping ... ] line with the dots. To do
238 that, insert the number of spaces equal to the number of
239 digits in the skipped amount in K. */
240 logprintf (LOG_VERBOSE, _("\n%*s[ skipping %sK ]"),
241 2 + skipped_k_len, "",
242 number_to_static_string (skipped_k));
245 logprintf (LOG_VERBOSE, "\n%6sK",
246 number_to_static_string (skipped / 1024));
247 for (; remainder >= dot_bytes; remainder -= dot_bytes)
249 if (dp->dots % opt.dot_spacing == 0)
250 logputs (LOG_VERBOSE, " ");
251 logputs (LOG_VERBOSE, ",");
254 assert (dp->dots < opt.dots_in_line);
256 dp->accumulated = remainder;
257 dp->rows = skipped / ROW_BYTES;
263 static const char *eta_to_human_short (int, bool);
265 /* Prints the stats (percentage of completion, speed, ETA) for current
266 row. DLTIME is the time spent downloading the data in current
269 #### This function is somewhat uglified by the fact that current
270 row and last row have somewhat different stats requirements. It
271 might be worthwhile to split it to two different functions. */
274 print_row_stats (struct dot_progress *dp, double dltime, bool last)
276 const wgint ROW_BYTES = opt.dot_bytes * opt.dots_in_line;
278 /* bytes_displayed is the number of bytes indicated to the user by
279 dots printed so far, includes the initially "skipped" amount */
280 wgint bytes_displayed = dp->rows * ROW_BYTES + dp->dots * opt.dot_bytes;
283 /* For last row also count bytes accumulated after last dot */
284 bytes_displayed += dp->accumulated;
286 if (dp->total_length)
288 /* Round to floor value to provide gauge how much data *has*
289 been retrieved. 12.8% will round to 12% because the 13% mark
290 has not yet been reached. 100% is only shown when done. */
291 int percentage = 100.0 * bytes_displayed / dp->total_length;
292 logprintf (LOG_VERBOSE, "%3d%%", percentage);
296 static char names[] = {' ', 'K', 'M', 'G'};
299 wgint bytes_this_row;
301 bytes_this_row = ROW_BYTES;
303 /* For last row also include bytes accumulated after last dot. */
304 bytes_this_row = dp->dots * opt.dot_bytes + dp->accumulated;
305 /* Don't count the portion of the row belonging to initial_length */
306 if (dp->rows == dp->initial_length / ROW_BYTES)
307 bytes_this_row -= dp->initial_length % ROW_BYTES;
308 rate = calc_rate (bytes_this_row, dltime - dp->last_timer_value, &units);
309 logprintf (LOG_VERBOSE, " %4.*f%c",
310 rate >= 99.95 ? 0 : rate >= 9.995 ? 1 : 2,
312 dp->last_timer_value = dltime;
317 /* Display ETA based on average speed. Inspired by Vladi
318 Belperchinov-Shabanski's "wget-new-percentage" patch. */
319 if (dp->total_length)
321 wgint bytes_remaining = dp->total_length - bytes_displayed;
322 /* The quantity downloaded in this download run. */
323 wgint bytes_sofar = bytes_displayed - dp->initial_length;
324 double eta = dltime * bytes_remaining / bytes_sofar;
325 if (eta < INT_MAX - 1)
326 logprintf (LOG_VERBOSE, " %s",
327 eta_to_human_short ((int) (eta + 0.5), true));
332 /* When done, print the total download time */
334 logprintf (LOG_VERBOSE, "=%s",
335 eta_to_human_short ((int) (dltime + 0.5), true));
337 logprintf (LOG_VERBOSE, "=%ss", print_decimal (dltime));
341 /* Dot-progress backend for progress_update. */
344 dot_update (void *progress, wgint howmuch, double dltime)
346 struct dot_progress *dp = progress;
347 int dot_bytes = opt.dot_bytes;
348 wgint ROW_BYTES = opt.dot_bytes * opt.dots_in_line;
350 log_set_flush (false);
352 dp->accumulated += howmuch;
353 for (; dp->accumulated >= dot_bytes; dp->accumulated -= dot_bytes)
356 logprintf (LOG_VERBOSE, "\n%6sK",
357 number_to_static_string (dp->rows * ROW_BYTES / 1024));
359 if (dp->dots % opt.dot_spacing == 0)
360 logputs (LOG_VERBOSE, " ");
361 logputs (LOG_VERBOSE, ".");
364 if (dp->dots >= opt.dots_in_line)
369 print_row_stats (dp, dltime, false);
373 log_set_flush (true);
376 /* Dot-progress backend for progress_finish. */
379 dot_finish (void *progress, double dltime)
381 struct dot_progress *dp = progress;
382 wgint ROW_BYTES = opt.dot_bytes * opt.dots_in_line;
385 log_set_flush (false);
388 logprintf (LOG_VERBOSE, "\n%6sK",
389 number_to_static_string (dp->rows * ROW_BYTES / 1024));
390 for (i = dp->dots; i < opt.dots_in_line; i++)
392 if (i % opt.dot_spacing == 0)
393 logputs (LOG_VERBOSE, " ");
394 logputs (LOG_VERBOSE, " ");
397 print_row_stats (dp, dltime, true);
398 logputs (LOG_VERBOSE, "\n\n");
399 log_set_flush (false);
404 /* This function interprets the progress "parameters". For example,
405 if Wget is invoked with --progress=dot:mega, it will set the
406 "dot-style" to "mega". Valid styles are default, binary, mega, and
410 dot_set_params (const char *params)
412 if (!params || !*params)
413 params = opt.dot_style;
418 /* We use this to set the retrieval style. */
419 if (!strcasecmp (params, "default"))
421 /* Default style: 1K dots, 10 dots in a cluster, 50 dots in a
423 opt.dot_bytes = 1024;
424 opt.dot_spacing = 10;
425 opt.dots_in_line = 50;
427 else if (!strcasecmp (params, "binary"))
429 /* "Binary" retrieval: 8K dots, 16 dots in a cluster, 48 dots
431 opt.dot_bytes = 8192;
432 opt.dot_spacing = 16;
433 opt.dots_in_line = 48;
435 else if (!strcasecmp (params, "mega"))
437 /* "Mega" retrieval, for retrieving very long files; each dot is
438 64K, 8 dots in a cluster, 6 clusters (3M) in a line. */
439 opt.dot_bytes = 65536L;
441 opt.dots_in_line = 48;
443 else if (!strcasecmp (params, "giga"))
445 /* "Giga" retrieval, for retrieving very very *very* long files;
446 each dot is 1M, 8 dots in a cluster, 4 clusters (32M) in a
448 opt.dot_bytes = (1L << 20);
450 opt.dots_in_line = 32;
454 _("Invalid dot style specification `%s'; leaving unchanged.\n"),
458 /* "Thermometer" (bar) progress. */
460 /* Assumed screen width if we can't find the real value. */
461 #define DEFAULT_SCREEN_WIDTH 80
463 /* Minimum screen width we'll try to work with. If this is too small,
464 create_image will overflow the buffer. */
465 #define MINIMUM_SCREEN_WIDTH 45
467 /* The last known screen width. This can be updated by the code that
468 detects that SIGWINCH was received (but it's never updated from the
470 static int screen_width;
472 /* A flag that, when set, means SIGWINCH was received. */
473 static volatile sig_atomic_t received_sigwinch;
475 /* Size of the download speed history ring. */
476 #define DLSPEED_HISTORY_SIZE 20
478 /* The minimum time length of a history sample. By default, each
479 sample is at least 150ms long, which means that, over the course of
480 20 samples, "current" download speed spans at least 3s into the
482 #define DLSPEED_SAMPLE_MIN 0.15
484 /* The time after which the download starts to be considered
485 "stalled", i.e. the current bandwidth is not printed and the recent
486 download speeds are scratched. */
487 #define STALL_START_TIME 5
489 /* Time between screen refreshes will not be shorter than this, so
490 that Wget doesn't swamp the TTY with output. */
491 #define REFRESH_INTERVAL 0.2
493 /* Don't refresh the ETA too often to avoid jerkiness in predictions.
494 This allows ETA to change approximately once per second. */
495 #define ETA_REFRESH_INTERVAL 0.99
497 struct bar_progress {
498 wgint initial_length; /* how many bytes have been downloaded
500 wgint total_length; /* expected total byte count when the
502 wgint count; /* bytes downloaded so far */
504 double last_screen_update; /* time of the last screen update,
505 measured since the beginning of
508 int width; /* screen width we're using at the
509 time the progress gauge was
510 created. this is different from
511 the screen_width global variable in
512 that the latter can be changed by a
514 char *buffer; /* buffer where the bar "image" is
516 int tick; /* counter used for drawing the
517 progress bar where the total size
520 /* The following variables (kept in a struct for namespace reasons)
521 keep track of recent download speeds. See bar_update() for
523 struct bar_progress_hist {
525 double times[DLSPEED_HISTORY_SIZE];
526 wgint bytes[DLSPEED_HISTORY_SIZE];
528 /* The sum of times and bytes respectively, maintained for
534 double recent_start; /* timestamp of beginning of current
536 wgint recent_bytes; /* bytes downloaded so far. */
538 bool stalled; /* set when no data arrives for longer
539 than STALL_START_TIME, then reset
540 when new data arrives. */
542 /* create_image() uses these to make sure that ETA information
544 double last_eta_time; /* time of the last update to download
545 speed and ETA, measured since the
546 beginning of download. */
550 static void create_image (struct bar_progress *, double, bool);
551 static void display_image (char *);
554 bar_create (wgint initial, wgint total)
556 struct bar_progress *bp = xnew0 (struct bar_progress);
558 /* In theory, our callers should take care of this pathological
559 case, but it can sometimes happen. */
563 bp->initial_length = initial;
564 bp->total_length = total;
566 /* Initialize screen_width if this hasn't been done or if it might
567 have changed, as indicated by receiving SIGWINCH. */
568 if (!screen_width || received_sigwinch)
570 screen_width = determine_screen_width ();
572 screen_width = DEFAULT_SCREEN_WIDTH;
573 else if (screen_width < MINIMUM_SCREEN_WIDTH)
574 screen_width = MINIMUM_SCREEN_WIDTH;
575 received_sigwinch = 0;
578 /* - 1 because we don't want to use the last screen column. */
579 bp->width = screen_width - 1;
580 /* + 1 for the terminating zero. */
581 bp->buffer = xmalloc (bp->width + 1);
583 logputs (LOG_VERBOSE, "\n");
585 create_image (bp, 0, false);
586 display_image (bp->buffer);
591 static void update_speed_ring (struct bar_progress *, wgint, double);
594 bar_update (void *progress, wgint howmuch, double dltime)
596 struct bar_progress *bp = progress;
597 bool force_screen_update = false;
599 bp->count += howmuch;
600 if (bp->total_length > 0
601 && bp->count + bp->initial_length > bp->total_length)
602 /* We could be downloading more than total_length, e.g. when the
603 server sends an incorrect Content-Length header. In that case,
604 adjust bp->total_length to the new reality, so that the code in
605 create_image() that depends on total size being smaller or
606 equal to the expected size doesn't abort. */
607 bp->total_length = bp->initial_length + bp->count;
609 update_speed_ring (bp, howmuch, dltime);
611 /* If SIGWINCH (the window size change signal) been received,
612 determine the new screen size and update the screen. */
613 if (received_sigwinch)
615 int old_width = screen_width;
616 screen_width = determine_screen_width ();
618 screen_width = DEFAULT_SCREEN_WIDTH;
619 else if (screen_width < MINIMUM_SCREEN_WIDTH)
620 screen_width = MINIMUM_SCREEN_WIDTH;
621 if (screen_width != old_width)
623 bp->width = screen_width - 1;
624 bp->buffer = xrealloc (bp->buffer, bp->width + 1);
625 force_screen_update = true;
627 received_sigwinch = 0;
630 if (dltime - bp->last_screen_update < REFRESH_INTERVAL && !force_screen_update)
631 /* Don't update more often than five times per second. */
634 create_image (bp, dltime, false);
635 display_image (bp->buffer);
636 bp->last_screen_update = dltime;
640 bar_finish (void *progress, double dltime)
642 struct bar_progress *bp = progress;
644 if (bp->total_length > 0
645 && bp->count + bp->initial_length > bp->total_length)
646 /* See bar_update() for explanation. */
647 bp->total_length = bp->initial_length + bp->count;
649 create_image (bp, dltime, true);
650 display_image (bp->buffer);
652 logputs (LOG_VERBOSE, "\n\n");
658 /* This code attempts to maintain the notion of a "current" download
659 speed, over the course of no less than 3s. (Shorter intervals
660 produce very erratic results.)
662 To do so, it samples the speed in 150ms intervals and stores the
663 recorded samples in a FIFO history ring. The ring stores no more
664 than 20 intervals, hence the history covers the period of at least
665 three seconds and at most 20 reads into the past. This method
666 should produce reasonable results for downloads ranging from very
669 The idea is that for fast downloads, we get the speed over exactly
670 the last three seconds. For slow downloads (where a network read
671 takes more than 150ms to complete), we get the speed over a larger
672 time period, as large as it takes to complete thirty reads. This
673 is good because slow downloads tend to fluctuate more and a
674 3-second average would be too erratic. */
677 update_speed_ring (struct bar_progress *bp, wgint howmuch, double dltime)
679 struct bar_progress_hist *hist = &bp->hist;
680 double recent_age = dltime - bp->recent_start;
682 /* Update the download count. */
683 bp->recent_bytes += howmuch;
685 /* For very small time intervals, we return after having updated the
686 "recent" download count. When its age reaches or exceeds minimum
687 sample time, it will be recorded in the history ring. */
688 if (recent_age < DLSPEED_SAMPLE_MIN)
693 /* If we're not downloading anything, we might be stalling,
694 i.e. not downloading anything for an extended period of time.
695 Since 0-reads do not enter the history ring, recent_age
696 effectively measures the time since last read. */
697 if (recent_age >= STALL_START_TIME)
699 /* If we're stalling, reset the ring contents because it's
700 stale and because it will make bar_update stop printing
701 the (bogus) current bandwidth. */
704 bp->recent_bytes = 0;
709 /* We now have a non-zero amount of to store to the speed ring. */
711 /* If the stall status was acquired, reset it. */
715 /* "recent_age" includes the the entired stalled period, which
716 could be very long. Don't update the speed ring with that
717 value because the current bandwidth would start too small.
718 Start with an arbitrary (but more reasonable) time value and
723 /* Store "recent" bytes and download time to history ring at the
726 /* To correctly maintain the totals, first invalidate existing data
727 (least recent in time) at this position. */
728 hist->total_time -= hist->times[hist->pos];
729 hist->total_bytes -= hist->bytes[hist->pos];
731 /* Now store the new data and update the totals. */
732 hist->times[hist->pos] = recent_age;
733 hist->bytes[hist->pos] = bp->recent_bytes;
734 hist->total_time += recent_age;
735 hist->total_bytes += bp->recent_bytes;
737 /* Start a new "recent" period. */
738 bp->recent_start = dltime;
739 bp->recent_bytes = 0;
741 /* Advance the current ring position. */
742 if (++hist->pos == DLSPEED_HISTORY_SIZE)
746 /* Sledgehammer check to verify that the totals are accurate. */
749 double sumt = 0, sumb = 0;
750 for (i = 0; i < DLSPEED_HISTORY_SIZE; i++)
752 sumt += hist->times[i];
753 sumb += hist->bytes[i];
755 assert (sumb == hist->total_bytes);
756 /* We can't use assert(sumt==hist->total_time) because some
757 precision is lost by adding and subtracting floating-point
758 numbers. But during a download this precision should not be
759 detectable, i.e. no larger than 1ns. */
760 double diff = sumt - hist->total_time;
761 if (diff < 0) diff = -diff;
762 assert (diff < 1e-9);
767 #define APPEND_LITERAL(s) do { \
768 memcpy (p, s, sizeof (s) - 1); \
769 p += sizeof (s) - 1; \
772 /* Use move_to_end (s) to get S to point the end of the string (the
773 terminating \0). This is faster than s+=strlen(s), but some people
774 are confused when they see strchr (s, '\0') in the code. */
775 #define move_to_end(s) s = strchr (s, '\0');
778 # define MAX(a, b) ((a) >= (b) ? (a) : (b))
782 create_image (struct bar_progress *bp, double dl_total_time, bool done)
784 char *p = bp->buffer;
785 wgint size = bp->initial_length + bp->count;
787 const char *size_grouped = with_thousand_seps (size);
788 int size_grouped_len = strlen (size_grouped);
790 struct bar_progress_hist *hist = &bp->hist;
792 /* The progress bar should look like this:
793 xx% [=======> ] nn,nnn 12.34K/s eta 36m 51s
795 Calculate the geometry. The idea is to assign as much room as
796 possible to the progress bar. The other idea is to never let
797 things "jitter", i.e. pad elements that vary in size so that
798 their variance does not affect the placement of other elements.
799 It would be especially bad for the progress bar to be resized
802 "xx% " or "100%" - percentage - 4 chars
803 "[]" - progress bar decorations - 2 chars
804 " nnn,nnn,nnn" - downloaded bytes - 12 chars or very rarely more
805 " 12.5K/s" - download rate - 8 chars
806 " eta 36m 51s" - ETA - 13 chars
808 "=====>..." - progress bar - the rest
810 int dlbytes_size = 1 + MAX (size_grouped_len, 11);
811 int progress_size = bp->width - (4 + 2 + dlbytes_size + 8 + 13);
813 if (progress_size < 5)
817 if (bp->total_length > 0)
819 int percentage = 100.0 * size / bp->total_length;
820 assert (percentage <= 100);
822 if (percentage < 100)
823 sprintf (p, "%2d%% ", percentage);
829 APPEND_LITERAL (" ");
831 /* The progress bar: "[====> ]" or "[++==> ]". */
832 if (progress_size && bp->total_length > 0)
834 /* Size of the initial portion. */
835 int insz = (double)bp->initial_length / bp->total_length * progress_size;
837 /* Size of the downloaded portion. */
838 int dlsz = (double)size / bp->total_length * progress_size;
843 assert (dlsz <= progress_size);
844 assert (insz <= dlsz);
849 /* Print the initial portion of the download with '+' chars, the
850 rest with '=' and one '>'. */
851 for (i = 0; i < insz; i++)
856 for (i = 0; i < dlsz - 1; i++)
861 while (p - begin < progress_size)
865 else if (progress_size)
867 /* If we can't draw a real progress bar, then at least show
868 *something* to the user. */
869 int ind = bp->tick % (progress_size * 2 - 6);
872 /* Make the star move in two directions. */
873 if (ind < progress_size - 2)
876 pos = progress_size - (ind - progress_size + 5);
879 for (i = 0; i < progress_size; i++)
881 if (i == pos - 1) *p++ = '<';
882 else if (i == pos ) *p++ = '=';
883 else if (i == pos + 1) *p++ = '>';
893 sprintf (p, " %-11s", size_grouped);
897 if (hist->total_time > 0 && hist->total_bytes)
899 static const char *short_units[] = { "B/s", "K/s", "M/s", "G/s" };
901 /* Calculate the download speed using the history ring and
902 recent data that hasn't made it to the ring yet. */
903 wgint dlquant = hist->total_bytes + bp->recent_bytes;
904 double dltime = hist->total_time + (dl_total_time - bp->recent_start);
905 double dlspeed = calc_rate (dlquant, dltime, &units);
906 sprintf (p, " %4.*f%s", dlspeed >= 99.95 ? 0 : dlspeed >= 9.995 ? 1 : 2,
907 dlspeed, short_units[units]);
911 APPEND_LITERAL (" --.-K/s");
915 /* " eta ..m ..s"; wait for three seconds before displaying the ETA.
916 That's because the ETA value needs a while to become
918 if (bp->total_length > 0 && bp->count > 0 && dl_total_time > 3)
922 /* Don't change the value of ETA more than approximately once
923 per second; doing so would cause flashing without providing
924 any value to the user. */
925 if (bp->total_length != size
926 && bp->last_eta_value != 0
927 && dl_total_time - bp->last_eta_time < ETA_REFRESH_INTERVAL)
928 eta = bp->last_eta_value;
931 /* Calculate ETA using the average download speed to predict
932 the future speed. If you want to use a speed averaged
933 over a more recent period, replace dl_total_time with
934 hist->total_time and bp->count with hist->total_bytes.
935 I found that doing that results in a very jerky and
936 ultimately unreliable ETA. */
937 wgint bytes_remaining = bp->total_length - size;
938 double eta_ = dl_total_time * bytes_remaining / bp->count;
939 if (eta_ >= INT_MAX - 1)
941 eta = (int) (eta_ + 0.5);
942 bp->last_eta_value = eta;
943 bp->last_eta_time = dl_total_time;
946 /* Translation note: "ETA" is English-centric, but this must
947 be short, ideally 3 chars. Abbreviate if necessary. */
948 sprintf (p, _(" eta %s"), eta_to_human_short (eta, false));
951 else if (bp->total_length > 0)
954 APPEND_LITERAL (" ");
959 /* When the download is done, print the elapsed time. */
961 /* Note to translators: this should not take up more room than
962 available here. Abbreviate if necessary. */
963 strcpy (p, _(" in "));
964 move_to_end (p); /* not p+=6, think translations! */
965 if (dl_total_time >= 10)
966 strcpy (p, eta_to_human_short ((int) (dl_total_time + 0.5), false));
968 sprintf (p, "%ss", print_decimal (dl_total_time));
972 assert (p - bp->buffer <= bp->width);
974 while (p < bp->buffer + bp->width)
979 /* Print the contents of the buffer as a one-line ASCII "image" so
980 that it can be overwritten next time. */
983 display_image (char *buf)
985 bool old = log_set_save_context (false);
986 logputs (LOG_VERBOSE, "\r");
987 logputs (LOG_VERBOSE, buf);
988 log_set_save_context (old);
992 bar_set_params (const char *params)
994 char *term = getenv ("TERM");
997 && 0 == strcmp (params, "force"))
998 current_impl_locked = 1;
1002 /* The progress bar doesn't make sense if the output is not a
1003 TTY -- when logging to file, it is better to review the
1005 || !isatty (fileno (stderr))
1007 /* Normally we don't depend on terminal type because the
1008 progress bar only uses ^M to move the cursor to the
1009 beginning of line, which works even on dumb terminals. But
1010 Jamie Zawinski reports that ^M and ^H tricks don't work in
1011 Emacs shell buffers, and only make a mess. */
1012 || (term && 0 == strcmp (term, "emacs"))
1014 && !current_impl_locked)
1016 /* We're not printing to a TTY, so revert to the fallback
1017 display. #### We're recursively calling
1018 set_progress_implementation here, which is slightly kludgy.
1019 It would be nicer if we provided that function a return value
1020 indicating a failure of some sort. */
1021 set_progress_implementation (FALLBACK_PROGRESS_IMPLEMENTATION);
1028 progress_handle_sigwinch (int sig)
1030 received_sigwinch = 1;
1031 signal (SIGWINCH, progress_handle_sigwinch);
1035 /* Provide a short human-readable rendition of the ETA. This is like
1036 secs_to_human_time in main.c, except the output doesn't include
1037 fractions (which would look silly in by nature imprecise ETA) and
1038 takes less room. If the time is measured in hours, hours and
1039 minutes (but not seconds) are shown; if measured in days, then days
1040 and hours are shown. This ensures brevity while still displaying
1041 as much as possible.
1043 If CONDENSED is true, the separator between minutes and seconds
1044 (and hours and minutes, etc.) is not included, shortening the
1045 display by one additional character. This is used for dot
1048 The display never occupies more than 7 characters of screen
1052 eta_to_human_short (int secs, bool condensed)
1054 static char buf[10]; /* 8 should be enough, but just in case */
1055 static int last = -1;
1056 const char *space = condensed ? "" : " ";
1058 /* Trivial optimization. create_image can call us every 200 msecs
1059 (see bar_update) for fast downloads, but ETA will only change
1060 once per 900 msecs. */
1066 sprintf (buf, "%ds", secs);
1067 else if (secs < 100 * 60)
1068 sprintf (buf, "%dm%s%ds", secs / 60, space, secs % 60);
1069 else if (secs < 48 * 3600)
1070 sprintf (buf, "%dh%s%dm", secs / 3600, space, (secs / 60) % 60);
1071 else if (secs < 100 * 86400)
1072 sprintf (buf, "%dd%s%dh", secs / 86400, space, (secs / 3600) % 60);
1074 /* even (2^31-1)/86400 doesn't overflow BUF. */
1075 sprintf (buf, "%dd", secs / 86400);