summaryrefslogtreecommitdiffstats
path: root/curs_lib.c
diff options
context:
space:
mode:
authorBrendan Cully <brendan@kublai.com>2007-11-07 14:48:38 -0800
committerBrendan Cully <brendan@kublai.com>2007-11-07 14:48:38 -0800
commit1c514f01bc8e9595d9b8e6cb4b76dfca180bd34d (patch)
tree747938337325bf7331d3bff82a38c5d68e9861a0 /curs_lib.c
parentbcb9c2bbaedba2471633ae9e4f70539fae943313 (diff)
Suppress progress bar updates less than 250ms apart. Closes #2899.
Diffstat (limited to 'curs_lib.c')
-rw-r--r--curs_lib.c76
1 files changed, 47 insertions, 29 deletions
diff --git a/curs_lib.c b/curs_lib.c
index bafb802b..ff1a9431 100644
--- a/curs_lib.c
+++ b/curs_lib.c
@@ -35,11 +35,17 @@
#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,