]> granicus.if.org Git - neomutt/commitdiff
feature: progress
authorStefan Kuhn <wuodan@hispeed.ch>
Sat, 30 Jan 2016 16:13:53 +0000 (16:13 +0000)
committerRichard Russon <rich@flatcap.org>
Thu, 18 Aug 2016 15:15:30 +0000 (16:15 +0100)
Show a visual progress bar on slow operations

color.c
curs_lib.c
mutt_curses.h

diff --git a/color.c b/color.c
index c33d41ebc2250cb42219911a87a0b79ed59c4eae..d8ed4e3e4a97e4177708f72b11eaa4046a3c19ec 100644 (file)
--- a/color.c
+++ b/color.c
@@ -93,6 +93,7 @@ static const struct mapping_t Fields[] =
   { "bold",            MT_COLOR_BOLD },
   { "underline",       MT_COLOR_UNDERLINE },
   { "index",           MT_COLOR_INDEX },
+  { "progress",                MT_COLOR_PROGRESS },
   { "prompt",          MT_COLOR_PROMPT },
 #ifdef USE_SIDEBAR
   { "sidebar_divider", MT_COLOR_DIVIDER },
index 8b21c43fb08cea5d3221d7b2c031d7373eabc279..eb1b363d05bd8b0f3fded479f7e32a44ba068288 100644 (file)
@@ -419,6 +419,61 @@ void mutt_progress_init (progress_t* progress, const char *msg,
   mutt_progress_update (progress, 0, 0);
 }
 
+/**
+ * message_bar - Draw a colourful progress bar
+ * @percent: %age complete
+ * @fmt:     printf(1)-like formatting string
+ * @...:     Arguments to formatting string
+ *
+ */
+static void
+message_bar (int percent, const char *fmt, ...)
+{
+       va_list ap;
+       char buf[STRING], buf2[STRING];
+       int w = percent * COLS / 100;
+       size_t l;
+
+       va_start (ap, fmt);
+       vsnprintf (buf, sizeof (buf), fmt, ap);
+       l = mutt_strwidth (buf);
+       va_end (ap);
+
+       mutt_format_string (buf2, sizeof (buf2), 0, COLS-2, FMT_LEFT, 0, buf, sizeof (buf), 0);
+
+       move (LINES - 1, 0);
+
+       if (ColorDefs[MT_COLOR_PROGRESS] == 0) {
+               addstr (buf2);
+       } else {
+               if (l < w) {
+                       /* The string fits within the colour bar */
+                       SETCOLOR(MT_COLOR_PROGRESS);
+                       addstr (buf2);
+                       w -= l;
+                       while (w--) {
+                               addch (' ');
+                       }
+                       SETCOLOR(MT_COLOR_NORMAL);
+               } else {
+                       /* The string is too long for the colour bar */
+                       char ch;
+                       int off = mutt_wstr_trunc (buf2, sizeof (buf2), w, NULL);
+
+                       ch = buf2[off];
+                       buf2[off] = 0;
+                       SETCOLOR(MT_COLOR_PROGRESS);
+                       addstr (buf2);
+                       buf2[off] = ch;
+                       SETCOLOR(MT_COLOR_NORMAL);
+                       addstr (&buf2[off]);
+               }
+       }
+
+       clrtoeol();
+       mutt_refresh();
+}
+
 void mutt_progress_update (progress_t* progress, long pos, int percent)
 {
   char posstr[SHORT_STRING];
@@ -469,16 +524,16 @@ void mutt_progress_update (progress_t* progress, long pos, int percent)
 
     if (progress->size > 0)
     {
-      mutt_message ("%s %s/%s (%d%%)", progress->msg, posstr, progress->sizestr,
-                   percent > 0 ? percent :
-                       (int) (100.0 * (double) progress->pos / progress->size));
+      message_bar ((percent > 0) ? percent : (int) (100.0 * (double) progress->pos / progress->size),
+        "%s %s/%s (%d%%)", progress->msg, posstr, progress->sizestr,
+        (percent > 0) ? percent : (int) (100.0 * (double) progress->pos / progress->size));
     }
     else
     {
       if (percent > 0)
-       mutt_message ("%s %s (%d%%)", progress->msg, posstr, percent);
+        message_bar (percent, "%s %s (%d%%)", progress->msg, posstr, percent);
       else
-       mutt_message ("%s %s", progress->msg, posstr);
+        mutt_message ("%s %s", progress->msg, posstr);
     }
   }
 
index 62004491fcb44f0cec4574dd8c881f5492989303..9e91aea968b2c268636d86ceb574623167ed5d76 100644 (file)
@@ -123,6 +123,7 @@ enum
   MT_COLOR_UNDERLINE,
   MT_COLOR_INDEX,
   MT_COLOR_PROMPT,
+  MT_COLOR_PROGRESS,
 #ifdef USE_SIDEBAR
   MT_COLOR_DIVIDER,
   MT_COLOR_FLAGGED,