]> sjero.net Git - wget/blobdiff - src/progress.c
[svn] Fall back to dot progress if the terminal type is "emacs".
[wget] / src / progress.c
index 82e40c7a0cb33b0a58eb386fe326bf29e635e17a..0288355d58efdf4fd151c6f27c6cdbf9b3ba2c3d 100644 (file)
@@ -1,5 +1,5 @@
 /* Download progress.
-   Copyright (C) 2001 Free Software Foundation, Inc.
+   Copyright (C) 2001, 2002 Free Software Foundation, Inc.
 
 This file is part of GNU Wget.
 
@@ -15,7 +15,17 @@ GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with Wget; if not, write to the Free Software
-Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
+Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+In addition, as a special exception, the Free Software Foundation
+gives permission to link the code of its release of Wget with the
+OpenSSL project's "OpenSSL" library (or with modified versions of it
+that use the same license as the "OpenSSL" library), and distribute
+the linked executables.  You must obey the GNU General Public License
+in all respects for all of the code used other than "OpenSSL".  If you
+modify this file, you may extend this exception to your version of the
+file, but you are not obligated to do so.  If you do not wish to do
+so, delete this exception statement from your version.  */
 
 #include <config.h>
 
@@ -41,10 +51,10 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
 
 struct progress_implementation {
   char *name;
-  void *(*create) (long, long);
-  void (*update) (void *, long, long);
-  void (*finish) (void *, long);
-  void (*set_params) (const char *);
+  void *(*create) PARAMS ((long, long));
+  void (*update) PARAMS ((void *, long, long));
+  void (*finish) PARAMS ((void *, long));
+  void (*set_params) PARAMS ((const char *));
 };
 
 /* Necessary forward declarations. */
@@ -461,6 +471,11 @@ bar_create (long initial, long total)
 
   memset (bp, 0, sizeof (*bp));
 
+  /* In theory, our callers should take care of this pathological
+     case, but it can sometimes happen. */
+  if (initial > total)
+    total = initial;
+
   bp->initial_length = initial;
   bp->total_length   = total;
 
@@ -493,7 +508,7 @@ bar_update (void *progress, long howmuch, long dltime)
        adjust bp->total_length to the new reality, so that the code in
        create_image() that depends on total size being smaller or
        equal to the expected size doesn't abort.  */
-    bp->total_length = bp->count + bp->initial_length;
+    bp->total_length = bp->initial_length + bp->count;
 
   /* This code attempts to determine the current download speed.  We
      measure the speed over the interval of approximately three
@@ -565,13 +580,10 @@ bar_finish (void *progress, long dltime)
 {
   struct bar_progress *bp = progress;
 
-  /* If the download was faster than the granularity of the timer,
-     fake some output so that we don't get the ugly "----.--" rate at
-     the download finish.  */
-  if (bp->hist.summed_times == 0)
-    bp->hist.summed_times = 1;
-  if (dltime == 0)
-    dltime = 1;
+  if (bp->total_length > 0
+      && bp->count + bp->initial_length > bp->total_length)
+    /* See bar_update() for explanation. */
+    bp->total_length = bp->initial_length + bp->count;
 
   create_image (bp, dltime);
   display_image (bp->buffer);
@@ -600,8 +612,7 @@ create_image (struct bar_progress *bp, long dl_total_time)
   char *size_legible = legible (size);
   int size_legible_len = strlen (size_legible);
 
-  long recent_time = bp->hist.summed_times;
-  long recent_bytes = bp->hist.summed_bytes;
+  struct bar_progress_hist *hist = &bp->hist;
 
   /* The progress bar should look like this:
      xx% [=======>             ] nn,nnn 12.34K/s ETA 00:00
@@ -700,11 +711,12 @@ create_image (struct bar_progress *bp, long dl_total_time)
   p += strlen (p);
 
   /* " 1012.45K/s" */
-  if (recent_time && recent_bytes)
+  if (hist->summed_times && hist->summed_bytes)
     {
       static char *short_units[] = { "B/s", "K/s", "M/s", "G/s" };
       int units = 0;
-      double dlrate = calc_rate (recent_bytes, recent_time, &units);
+      double dlrate;
+      dlrate = calc_rate (hist->summed_bytes, hist->summed_times, &units);
       sprintf (p, " %7.2f%s", dlrate, short_units[units]);
       p += strlen (p);
     }
@@ -712,22 +724,28 @@ create_image (struct bar_progress *bp, long dl_total_time)
     APPEND_LITERAL ("   --.--K/s");
 
   /* " ETA xx:xx:xx" */
-  if (bp->total_length > 0 && recent_bytes > 0)
+  if (bp->total_length > 0 && dl_total_time > 3000)
     {
       long eta;
       int eta_hrs, eta_min, eta_sec;
 
       /* Don't change the value of ETA more than approximately once
         per second; doing so would cause flashing without providing
-        any value to the user.  */
+        any value to the user. */
       if (dl_total_time - bp->last_eta_time < 900
          && bp->last_eta_value != 0)
        eta = bp->last_eta_value;
       else
        {
-         double tm_sofar = (double)recent_time / 1000;
+         /* Calculate ETA using the average download speed to predict
+            the future speed.  If you want to use the current speed
+            instead, replace dl_total_time with hist->summed_times
+            and bp->count with hist->summed_bytes.  I found that
+            doing that results in a very jerky and ultimately
+            unreliable ETA.  */
+         double time_sofar = (double)dl_total_time / 1000;
          long bytes_remaining = bp->total_length - size;
-         eta = (long) (tm_sofar * bytes_remaining / recent_bytes);
+         eta = (long) (time_sofar * bytes_remaining / bp->count);
          bp->last_eta_value = eta;
          bp->last_eta_time = dl_total_time;
        }
@@ -736,34 +754,29 @@ create_image (struct bar_progress *bp, long dl_total_time)
       eta_min = eta / 60,   eta %= 60;
       eta_sec = eta;
 
-      /* Pad until the end of screen.  The padding is dependent on the
-        hour value.  */
-      if (eta_hrs == 0 || eta_hrs > 99)
-       /* Hours not printed: pad with three spaces (two digits and
-          colon). */
-       APPEND_LITERAL ("   ");
-      else if (eta_hrs < 10)
-       /* Hours printed with one digit: pad with one space. */
-       *p++ = ' ';
-      else
-       /* Hours printed with two digits: we're using maximum width,
-          don't pad. */
-       ;
-
-      APPEND_LITERAL (" ETA ");
-
       if (eta_hrs > 99)
-       /* Bogus value, probably due to a calculation overflow.  Print
-          something safe to avoid overstepping the buffer bounds. */
-       sprintf (p, "--:--");
-      else if (eta_hrs > 0)
-       sprintf (p, "%d:%02d:%02d", eta_hrs, eta_min, eta_sec);
+       goto no_eta;
+
+      if (eta_hrs == 0)
+       {
+         /* Hours not printed: pad with three spaces. */
+         APPEND_LITERAL ("   ");
+         sprintf (p, " ETA %02d:%02d", eta_min, eta_sec);
+       }
       else
-       sprintf (p, "%02d:%02d", eta_min, eta_sec);
+       {
+         if (eta_hrs < 10)
+           /* Hours printed with one digit: pad with one space. */
+           *p++ = ' ';
+         sprintf (p, " ETA %d:%02d:%02d", eta_hrs, eta_min, eta_sec);
+       }
       p += strlen (p);
     }
   else if (bp->total_length > 0)
-    APPEND_LITERAL ("    ETA --:--");
+    {
+    no_eta:
+      APPEND_LITERAL ("             ");
+    }
 
   assert (p - bp->buffer <= bp->width);
 
@@ -788,6 +801,7 @@ static void
 bar_set_params (const char *params)
 {
   int sw;
+  char *term = getenv ("TERM");
 
   if (params
       && 0 == strcmp (params, "force"))
@@ -795,10 +809,19 @@ bar_set_params (const char *params)
 
   if ((opt.lfilename
 #ifdef HAVE_ISATTY
+       /* The progress bar doesn't make sense if the output is not a
+         TTY -- when logging to file, it is better to review the
+         dots.  */
        || !isatty (fileno (stderr))
 #else
        1
 #endif
+       /* Normally we don't depend on terminal type because the
+         progress bar only uses ^M to move the cursor to the
+         beginning of line, which works even on dumb terminals.  But
+         Jamie Zawinski reports that ^M and ^H tricks don't work in
+         Emacs shell buffers, and only make a mess.  */
+       || (term && 0 == strcmp (term, "emacs"))
        )
       && !current_impl_locked)
     {