]> granicus.if.org Git - neomutt/commitdiff
split out progress functions
authorRichard Russon <rich@flatcap.org>
Sun, 1 Apr 2018 18:49:52 +0000 (19:49 +0100)
committerRichard Russon <rich@flatcap.org>
Sun, 1 Apr 2018 18:49:52 +0000 (19:49 +0100)
16 files changed:
Makefile.autosetup
curs_lib.c
curs_main.c
imap/imap.c
imap/message.c
mbox.c
mh.c
mutt_curses.h
mutt_notmuch.c
nntp.c
pattern.c
pop.c
pop_lib.c
progress.c [new file with mode: 0644]
progress.h [new file with mode: 0644]
smtp.c

index 847d2945505c0f1545e5f6be6fa9639704c90326..f33e1b72fcd512b3fc92a5a382a15339f76efdcc 100644 (file)
@@ -67,7 +67,7 @@ NEOMUTTOBJS=  addrbook.o alias.o attach.o bcache.o body.o browser.o buffy.o \
                main.o mbox.o menu.o mh.o muttlib.o mutt_account.o \
                mutt_logging.o mutt_signal.o mutt_socket.o mutt_window.o mx.o newsrc.o \
                nntp.o pager.o parse.o pattern.o pop.o pop_auth.o pop_lib.o \
-               postpone.o query.o recvattach.o recvcmd.o resize.o rfc1524.o \
+               postpone.o progress.o query.o recvattach.o recvcmd.o resize.o rfc1524.o \
                rfc2047.o rfc2231.o rfc3676.o safe_asprintf.o score.o send.o \
                sendlib.o sidebar.o smtp.o sort.o state.o status.o system.o \
                tags.o terminal.o thread.o url.o version.o
index 6d801dcdb178b6f7feed302d90d85b97d5c95e4c..c1afd7b1fd6eeafb252d00d3f48f46cbe0af6274 100644 (file)
@@ -372,176 +372,6 @@ void mutt_query_exit(void)
   SigInt = 0;
 }
 
-void mutt_progress_init(struct Progress *progress, const char *msg,
-                        unsigned short flags, unsigned short inc, size_t size)
-{
-  struct timeval tv = { 0, 0 };
-
-  if (!progress)
-    return;
-  if (OPT_NO_CURSES)
-    return;
-
-  memset(progress, 0, sizeof(struct Progress));
-  progress->inc = inc;
-  progress->flags = flags;
-  progress->msg = msg;
-  progress->size = size;
-  if (progress->size)
-  {
-    if (progress->flags & MUTT_PROGRESS_SIZE)
-      mutt_str_pretty_size(progress->sizestr, sizeof(progress->sizestr),
-                           progress->size);
-    else
-      snprintf(progress->sizestr, sizeof(progress->sizestr), "%zu", progress->size);
-  }
-  if (!inc)
-  {
-    if (size)
-      mutt_message("%s (%s)", msg, progress->sizestr);
-    else
-      mutt_message(msg);
-    return;
-  }
-  if (gettimeofday(&tv, NULL) < 0)
-    mutt_debug(1, "gettimeofday failed: %d\n", errno);
-  /* if timestamp is 0 no time-based suppression is done */
-  if (TimeInc)
-    progress->timestamp =
-        ((unsigned int) tv.tv_sec * 1000) + (unsigned int) (tv.tv_usec / 1000);
-  mutt_progress_update(progress, 0, 0);
-}
-
-/**
- * message_bar - Draw a colourful progress bar
- * @param percent %age complete
- * @param fmt     printf(1)-like formatting string
- * @param ...     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_simple_format(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(' ');
-      }
-      NORMAL_COLOR;
-    }
-    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;
-      NORMAL_COLOR;
-      addstr(&buf2[off]);
-    }
-  }
-
-  clrtoeol();
-  mutt_refresh();
-}
-
-void mutt_progress_update(struct Progress *progress, long pos, int percent)
-{
-  char posstr[SHORT_STRING];
-  bool update = false;
-  struct timeval tv = { 0, 0 };
-  unsigned int now = 0;
-
-  if (OPT_NO_CURSES)
-    return;
-
-  if (!progress->inc)
-    goto out;
-
-  /* refresh if size > inc */
-  if (progress->flags & MUTT_PROGRESS_SIZE && (pos >= progress->pos + (progress->inc << 10)))
-    update = true;
-  else if (pos >= progress->pos + progress->inc)
-    update = true;
-
-  /* skip refresh if not enough time has passed */
-  if (update && progress->timestamp && !gettimeofday(&tv, NULL))
-  {
-    now = ((unsigned int) tv.tv_sec * 1000) + (unsigned int) (tv.tv_usec / 1000);
-    if (now && now - progress->timestamp < TimeInc)
-      update = false;
-  }
-
-  /* always show the first update */
-  if (!pos)
-    update = true;
-
-  if (update)
-  {
-    if (progress->flags & MUTT_PROGRESS_SIZE)
-    {
-      pos = pos / (progress->inc << 10) * (progress->inc << 10);
-      mutt_str_pretty_size(posstr, sizeof(posstr), pos);
-    }
-    else
-      snprintf(posstr, sizeof(posstr), "%ld", pos);
-
-    mutt_debug(5, "updating progress: %s\n", posstr);
-
-    progress->pos = pos;
-    if (now)
-      progress->timestamp = now;
-
-    if (progress->size > 0)
-    {
-      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)
-        message_bar(percent, "%s %s (%d%%)", progress->msg, posstr, percent);
-      else
-        mutt_message("%s %s", progress->msg, posstr);
-    }
-  }
-
-out:
-  if (pos >= progress->size)
-    mutt_clear_error();
-}
-
 void mutt_show_error(void)
 {
   if (OPT_KEEP_QUIET || !ErrorBufMessage)
index f967f15ce6fd47571edcba49dc0a340fa80d6e87..3a5b74ad6dd4c037d4fcca0cb32bf24f55e116d0 100644 (file)
@@ -50,6 +50,7 @@
 #include "options.h"
 #include "pager.h"
 #include "pattern.h"
+#include "progress.h"
 #include "protos.h"
 #include "sort.h"
 #include "tags.h"
index ca7d19682217d9a230c9da021179ed92f216f391..a7415cde74c2cb5585ef0c0f6397005b44971657 100644 (file)
@@ -60,6 +60,7 @@
 #include "mx.h"
 #include "options.h"
 #include "pattern.h"
+#include "progress.h"
 #include "protos.h"
 #include "sort.h"
 #include "tags.h"
index 263ce1f820759ad8692cc95a6fc5d216f6edbe70..f8b0cc32a9afb05edbfa1de82f2f6b273af37588 100644 (file)
@@ -52,6 +52,7 @@
 #include "mutt_socket.h"
 #include "mx.h"
 #include "options.h"
+#include "progress.h"
 #include "protos.h"
 #include "tags.h"
 #ifdef USE_HCACHE
diff --git a/mbox.c b/mbox.c
index 6a846771e153f2844ef42ddaa4c563e1a927ffa1..05754d18b57f768ba7bfbd753a4cc3912c3bf0f4 100644 (file)
--- a/mbox.c
+++ b/mbox.c
@@ -45,6 +45,7 @@
 #include "mutt_curses.h"
 #include "mx.h"
 #include "options.h"
+#include "progress.h"
 #include "protos.h"
 #include "sort.h"
 #include "thread.h"
diff --git a/mh.c b/mh.c
index b97d06e45119131af505dc416dec78a00401b2e4..53bf776b97afc1fb0f119dcc30de42cfb8e74c53 100644 (file)
--- a/mh.c
+++ b/mh.c
@@ -51,6 +51,7 @@
 #include "mutt_curses.h"
 #include "mx.h"
 #include "options.h"
+#include "progress.h"
 #include "protos.h"
 #include "sort.h"
 #include "thread.h"
index 35ed76df76e41ef610b509251706d726246908e7..56e8057db0b337c1cd10cca2da8c42074a64731a 100644 (file)
@@ -202,30 +202,6 @@ struct ColorLine
 };
 STAILQ_HEAD(ColorLineHead, ColorLine);
 
-#define MUTT_PROGRESS_SIZE (1 << 0) /**< traffic-based progress */
-#define MUTT_PROGRESS_MSG  (1 << 1) /**< message-based progress */
-
-/**
- * struct Progress - A progress bar
- */
-struct Progress
-{
-  unsigned short inc;
-  unsigned short flags;
-  const char *msg;
-  long pos;
-  size_t size;
-  unsigned int timestamp;
-  char sizestr[SHORT_STRING];
-};
-
-void mutt_progress_init(struct Progress *progress, const char *msg,
-                        unsigned short flags, unsigned short inc, size_t size);
-/* If percent is positive, it is displayed as percentage, otherwise
- * percentage is calculated from progress->size and pos if progress
- * was initialized with positive size, otherwise no percentage is shown */
-void mutt_progress_update(struct Progress *progress, long pos, int percent);
-
 extern int *ColorQuote;
 extern int ColorQuoteUsed;
 extern int ColorDefs[];
index 31178891f4675fce696afd4112791848ef03680e..45c417082f4eb6c0e5090a8f08747e1671ab7c0f 100644 (file)
@@ -59,6 +59,7 @@
 #include "mailbox.h"
 #include "mutt_curses.h"
 #include "mx.h"
+#include "progress.h"
 #include "protos.h"
 #include "tags.h"
 #include "thread.h"
diff --git a/nntp.c b/nntp.c
index 5fbc286edbc35872bf9a21cdc0b5987e306f9cd7..6789affaa9afd0cc52b48dfaf8de9ade6a8db24f 100644 (file)
--- a/nntp.c
+++ b/nntp.c
@@ -46,6 +46,7 @@
 #include "mx.h"
 #include "ncrypt/ncrypt.h"
 #include "options.h"
+#include "progress.h"
 #include "protos.h"
 #include "thread.h"
 #include "url.h"
index b38700101bd6e1336822724bfa005ac98acb2430..680b42aa3f15eaa5690f2d189d0ad07633a133dd 100644 (file)
--- a/pattern.c
+++ b/pattern.c
@@ -51,6 +51,7 @@
 #include "ncrypt/ncrypt.h"
 #include "opcodes.h"
 #include "options.h"
+#include "progress.h"
 #include "protos.h"
 #include "state.h"
 #include "tags.h"
diff --git a/pop.c b/pop.c
index c53128cd6aea48a13057889336a94d1138b454af..8330b6681d352fc4631bf480c1bd99602eaa1c33 100644 (file)
--- a/pop.c
+++ b/pop.c
@@ -45,6 +45,7 @@
 #include "mx.h"
 #include "ncrypt/ncrypt.h"
 #include "options.h"
+#include "progress.h"
 #include "protos.h"
 #include "url.h"
 #ifdef USE_HCACHE
index fb881cb4811b0f048d9ca256ed7b054f773203e3..7c1c3318ccdf4f0704d1f4ae6d1e556212518706 100644 (file)
--- a/pop_lib.c
+++ b/pop_lib.c
@@ -39,6 +39,7 @@
 #include "mutt_socket.h"
 #include "options.h"
 #include "pop.h"
+#include "progress.h"
 #include "protos.h"
 #include "url.h"
 
diff --git a/progress.c b/progress.c
new file mode 100644 (file)
index 0000000..ed4e1af
--- /dev/null
@@ -0,0 +1,223 @@
+/**
+ * @file
+ * Progress bar
+ *
+ * @authors
+ * Copyright (C) 2018 Richard Russon <rich@flatcap.org>
+ *
+ * @copyright
+ * This program is free software: you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License as published by the Free Software
+ * Foundation, either version 2 of the License, or (at your option) any later
+ * version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+#include <errno.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <string.h>
+#include <sys/time.h>
+#include "mutt/mutt.h"
+#include "mutt.h"
+#include "progress.h"
+#include "globals.h"
+#include "mutt_curses.h"
+#include "options.h"
+#include "protos.h"
+
+/**
+ * message_bar - Draw a colourful progress bar
+ * @param percent %age complete
+ * @param fmt     printf(1)-like formatting string
+ * @param ...     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_simple_format(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-- > 0)
+      {
+        addch(' ');
+      }
+      NORMAL_COLOR;
+    }
+    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;
+      NORMAL_COLOR;
+      addstr(&buf2[off]);
+    }
+  }
+
+  clrtoeol();
+  mutt_refresh();
+}
+
+/**
+ * mutt_progress_init - Set up a progress bar
+ * @param progress Progress bar
+ * @param msg      Message to display
+ * @param flags    Flags, e.g. #MUTT_PROGRESS_SIZE
+ * @param inc      Increments to display (0 disables updates)
+ * @param size     Total size of expected file / traffic
+ */
+void mutt_progress_init(struct Progress *progress, const char *msg,
+                        unsigned short flags, unsigned short inc, size_t size)
+{
+  struct timeval tv = { 0, 0 };
+
+  if (!progress)
+    return;
+  if (OPT_NO_CURSES)
+    return;
+
+  memset(progress, 0, sizeof(struct Progress));
+  progress->inc = inc;
+  progress->flags = flags;
+  progress->msg = msg;
+  progress->size = size;
+  if (progress->size != 0)
+  {
+    if (progress->flags & MUTT_PROGRESS_SIZE)
+      mutt_str_pretty_size(progress->sizestr, sizeof(progress->sizestr),
+                           progress->size);
+    else
+      snprintf(progress->sizestr, sizeof(progress->sizestr), "%zu", progress->size);
+  }
+  if (inc == 0)
+  {
+    if (size != 0)
+      mutt_message("%s (%s)", msg, progress->sizestr);
+    else
+      mutt_message(msg);
+    return;
+  }
+  if (gettimeofday(&tv, NULL) < 0)
+    mutt_debug(1, "gettimeofday failed: %d\n", errno);
+  /* if timestamp is 0 no time-based suppression is done */
+  if (TimeInc != 0)
+    progress->timestamp =
+        ((unsigned int) tv.tv_sec * 1000) + (unsigned int) (tv.tv_usec / 1000);
+  mutt_progress_update(progress, 0, 0);
+}
+
+/**
+ * mutt_progress_update - Update the state of the progress bar
+ * @param progress Progress bar
+ * @param pos      Position, or count
+ * @param percent  Percentage complete
+ *
+ * If percent is -1, then the percentage will be calculated using pos and the
+ * size in progress.
+ */
+void mutt_progress_update(struct Progress *progress, long pos, int percent)
+{
+  char posstr[SHORT_STRING];
+  bool update = false;
+  struct timeval tv = { 0, 0 };
+  unsigned int now = 0;
+
+  if (OPT_NO_CURSES)
+    return;
+
+  if (progress->inc == 0)
+    goto out;
+
+  /* refresh if size > inc */
+  if ((progress->flags & MUTT_PROGRESS_SIZE) && (pos >= (progress->pos + (progress->inc << 10))))
+    update = true;
+  else if (pos >= (progress->pos + progress->inc))
+    update = true;
+
+  /* skip refresh if not enough time has passed */
+  if (update && progress->timestamp && (gettimeofday(&tv, NULL) == 0))
+  {
+    now = ((unsigned int) tv.tv_sec * 1000) + (unsigned int) (tv.tv_usec / 1000);
+    if (now && ((now - progress->timestamp) < TimeInc))
+      update = false;
+  }
+
+  /* always show the first update */
+  if (pos == 0)
+    update = true;
+
+  if (update)
+  {
+    if (progress->flags & MUTT_PROGRESS_SIZE)
+    {
+      pos = pos / (progress->inc << 10) * (progress->inc << 10);
+      mutt_str_pretty_size(posstr, sizeof(posstr), pos);
+    }
+    else
+      snprintf(posstr, sizeof(posstr), "%ld", pos);
+
+    mutt_debug(5, "updating progress: %s\n", posstr);
+
+    progress->pos = pos;
+    if (now)
+      progress->timestamp = now;
+
+    if (progress->size > 0)
+    {
+      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)
+        message_bar(percent, "%s %s (%d%%)", progress->msg, posstr, percent);
+      else
+        mutt_message("%s %s", progress->msg, posstr);
+    }
+  }
+
+out:
+  if (pos >= progress->size)
+    mutt_clear_error();
+}
+
diff --git a/progress.h b/progress.h
new file mode 100644 (file)
index 0000000..9865365
--- /dev/null
@@ -0,0 +1,53 @@
+/**
+ * @file
+ * Progress bar
+ *
+ * @authors
+ * Copyright (C) 2018 Richard Russon <rich@flatcap.org>
+ *
+ * @copyright
+ * This program is free software: you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License as published by the Free Software
+ * Foundation, either version 2 of the License, or (at your option) any later
+ * version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef _MUTT_PROGRESS_H
+#define _MUTT_PROGRESS_H
+
+#include <stdio.h>
+#include "mutt/mutt.h"
+
+#define MUTT_PROGRESS_SIZE (1 << 0) /**< traffic-based progress */
+#define MUTT_PROGRESS_MSG  (1 << 1) /**< message-based progress */
+
+/**
+ * struct Progress - A progress bar
+ */
+struct Progress
+{
+  unsigned short inc;
+  unsigned short flags;
+  const char *msg;
+  long pos;
+  size_t size;
+  unsigned int timestamp;
+  char sizestr[SHORT_STRING];
+};
+
+void mutt_progress_init(struct Progress *progress, const char *msg,
+                        unsigned short flags, unsigned short inc, size_t size);
+/* If percent is positive, it is displayed as percentage, otherwise
+ * percentage is calculated from progress->size and pos if progress
+ * was initialized with positive size, otherwise no percentage is shown */
+void mutt_progress_update(struct Progress *progress, long pos, int percent);
+
+#endif /* _MUTT_PROGRESS_H */
diff --git a/smtp.c b/smtp.c
index 09279170de70bf90be43bb960cca69514cb7efbc..0707a4bad034ee896ced8aa7fd6350b2759729af 100644 (file)
--- a/smtp.c
+++ b/smtp.c
@@ -39,6 +39,7 @@
 #include "mutt_curses.h"
 #include "mutt_socket.h"
 #include "options.h"
+#include "progress.h"
 #include "protos.h"
 #include "url.h"
 #ifdef USE_SASL