From 1c514f01bc8e9595d9b8e6cb4b76dfca180bd34d Mon Sep 17 00:00:00 2001 From: Brendan Cully Date: Wed, 7 Nov 2007 14:48:38 -0800 Subject: Suppress progress bar updates less than 250ms apart. Closes #2899. --- curs_lib.c | 76 ++++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 47 insertions(+), 29 deletions(-) (limited to 'curs_lib.c') 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 #include #include +#ifdef HAVE_SYS_TIME_H +# include +#endif +#include #ifdef HAVE_LANGINFO_YESEXPR #include #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, -- cgit v1.2.3