summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--curs_lib.c76
-rw-r--r--mutt_curses.h1
2 files changed, 48 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,
diff --git a/mutt_curses.h b/mutt_curses.h
index 901c1df2..854e3d5c 100644
--- a/mutt_curses.h
+++ b/mutt_curses.h
@@ -151,6 +151,7 @@ typedef struct
const char* msg;
long pos;
long size;
+ unsigned int timestamp;
char sizestr[SHORT_STRING];
} progress_t;