]> granicus.if.org Git - mutt/commitdiff
Suppress progress bar updates less than 250ms apart. Closes #2899.
authorBrendan Cully <brendan@kublai.com>
Wed, 7 Nov 2007 22:48:38 +0000 (14:48 -0800)
committerBrendan Cully <brendan@kublai.com>
Wed, 7 Nov 2007 22:48:38 +0000 (14:48 -0800)
curs_lib.c
mutt_curses.h

index bafb802b79a451a635acc9402d01ae5ddd48e4a7..ff1a9431345652732fd60ccf3853129397ecdd3a 100644 (file)
 #include <string.h>
 #include <errno.h>
 #include <ctype.h>
+#ifdef HAVE_SYS_TIME_H
+# include <sys/time.h>
+#endif
+#include <time.h>
 
 #ifdef HAVE_LANGINFO_YESEXPR
 #include <langinfo.h>
 #endif
 
+#define PROGRESS_REFRESH_MILLIS 250
+
 /* not possible to unget more than one char under some curses libs, and it
  * is impossible to unget function keys in SLang, so roll our own input
  * buffering routines.
@@ -343,6 +349,8 @@ void mutt_progress_init (progress_t* progress, const char *msg,
                         unsigned short flags, unsigned short inc,
                         long size)
 {
+  struct timeval tv = { 0, 0 };
+
   if (!progress)
     return;
   memset (progress, 0, sizeof (progress_t));
@@ -350,6 +358,17 @@ void mutt_progress_init (progress_t* progress, const char *msg,
   progress->flags = flags;
   progress->msg = msg;
   progress->size = size;
+  if (progress->size) {
+    if (progress->flags & M_PROGRESS_SIZE)
+      mutt_pretty_size (progress->sizestr, sizeof (progress->sizestr),
+                       progress->size);
+    else
+      snprintf (progress->sizestr, sizeof (progress->sizestr), "%ld",
+               progress->size);
+  }
+  if (gettimeofday (&tv, NULL) < 0)
+    dprint (1, (debugfile, "gettimeofday failed: %d\n", errno));
+  progress->timestamp = tv.tv_sec * 1000 + tv.tv_usec / 1000;
   mutt_progress_update (progress, 0, 0);
 }
 
@@ -357,45 +376,44 @@ void mutt_progress_update (progress_t* progress, long pos, int percent)
 {
   char posstr[SHORT_STRING];
   short update = 0;
+  struct timeval tv = { 0, 0 };
+  unsigned int now = 0;
 
-  if (!pos)
-  {
-    if (!progress->inc)
-      mutt_message (progress->msg);
-    else
-    {
-      if (progress->size)
-      {
-       if (progress->flags & M_PROGRESS_SIZE)
-         mutt_pretty_size (progress->sizestr, sizeof (progress->sizestr), progress->size);
-       else
-         snprintf (progress->sizestr, sizeof (progress->sizestr), "%ld", progress->size);
-      }
-      progress->pos = 0;
-    }
-  }
-
-  if (!progress->inc)
+  if (pos && !progress->inc)
     return;
 
-  if (progress->flags & M_PROGRESS_SIZE)
-  {
-    if (pos >= progress->pos + (progress->inc << 10))
-    {
-      pos = pos / (progress->inc << 10) * (progress->inc << 10);
-      mutt_pretty_size (posstr, sizeof (posstr), pos);
-      update = 1;
-    }
-  }
+  /* refresh if size > inc */
+  if (progress->flags & M_PROGRESS_SIZE &&
+      (pos >= progress->pos + (progress->inc << 10)))
+    update = 1;
   else if (pos >= progress->pos + progress->inc)
-  {
-    snprintf (posstr, sizeof (posstr), "%ld", pos);
     update = 1;
+
+  /* skip refresh if not enough time has passed */
+  if (update && progress->timestamp && !gettimeofday (&tv, NULL)) {
+    now = tv.tv_sec * 1000 + tv.tv_usec / 1000;
+    if (now && now - progress->timestamp < PROGRESS_REFRESH_MILLIS)
+      update = 0;
   }
 
+  /* always show the first update */
+  if (!pos)
+    update = 1;
+
   if (update)
   {
+    if (progress->flags & M_PROGRESS_SIZE)
+    {
+      pos = pos / (progress->inc << 10) * (progress->inc << 10);
+      mutt_pretty_size (posstr, sizeof (posstr), pos);
+    }
+    else
+      snprintf (posstr, sizeof (posstr), "%ld", pos);
+    
     progress->pos = pos;
+    if (now)
+      progress->timestamp = now;
+
     if (progress->size > 0)
     {
       mutt_message ("%s %s/%s (%d%%)", progress->msg, posstr, progress->sizestr,
index 901c1df28f8294d591eaa2883760a23fc65e83eb..854e3d5cf6a00a28ee15969477618761a4f66651 100644 (file)
@@ -151,6 +151,7 @@ typedef struct
   const char* msg;
   long pos;
   long size;
+  unsigned int timestamp;
   char sizestr[SHORT_STRING];
 } progress_t;