NeoMutt  2018-07-16 +952-a2da0a
Teaching an old dog new tricks
DOXYGEN
progress.c File Reference

Progress bar. More...

#include "config.h"
#include <errno.h>
#include <stdarg.h>
#include <stdbool.h>
#include <string.h>
#include <sys/time.h>
#include "mutt/mutt.h"
#include "progress.h"
#include "curs_lib.h"
#include "mutt_curses.h"
#include "mutt_logging.h"
#include "options.h"
+ Include dependency graph for progress.c:

Go to the source code of this file.

Functions

static void message_bar (int percent, const char *fmt,...)
 Draw a colourful progress bar. More...
 
void mutt_progress_init (struct Progress *progress, const char *msg, unsigned short flags, unsigned short inc, size_t size)
 Set up a progress bar. More...
 
void mutt_progress_update (struct Progress *progress, long pos, int percent)
 Update the state of the progress bar. More...
 

Variables

short TimeInc
 Config: Frequency of progress bar updates (milliseconds) More...
 

Detailed Description

Progress bar.

Authors
  • Richard Russon

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/.

Definition in file progress.c.

Function Documentation

static void message_bar ( int  percent,
const char *  fmt,
  ... 
)
static

Draw a colourful progress bar.

Parameters
percentage complete
fmtprintf(1)-like formatting string
...Arguments to formatting string

Definition at line 51 of file progress.c.

52 {
53  va_list ap;
54  char buf[STRING], buf2[STRING];
55  int w = percent * COLS / 100;
56  size_t l;
57 
58  va_start(ap, fmt);
59  vsnprintf(buf, sizeof(buf), fmt, ap);
60  l = mutt_strwidth(buf);
61  va_end(ap);
62 
63  mutt_simple_format(buf2, sizeof(buf2), 0, COLS - 2, FMT_LEFT, 0, buf, sizeof(buf), 0);
64 
65  move(LINES - 1, 0);
66 
67  if (ColorDefs[MT_COLOR_PROGRESS] == 0)
68  {
69  addstr(buf2);
70  }
71  else
72  {
73  if (l < w)
74  {
75  /* The string fits within the colour bar */
77  addstr(buf2);
78  w -= l;
79  while (w-- > 0)
80  {
81  addch(' ');
82  }
84  }
85  else
86  {
87  /* The string is too long for the colour bar */
88  char ch;
89  int off = mutt_wstr_trunc(buf2, sizeof(buf2), w, NULL);
90 
91  ch = buf2[off];
92  buf2[off] = '\0';
94  addstr(buf2);
95  buf2[off] = ch;
97  addstr(&buf2[off]);
98  }
99  }
100 
101  clrtoeol();
102  mutt_refresh();
103 }
void mutt_simple_format(char *buf, size_t buflen, int min_width, int max_width, int justify, char pad_char, const char *s, size_t n, int arboreal)
Format a string, like snprintf()
Definition: curs_lib.c:911
#define NORMAL_COLOR
Definition: mutt_curses.h:235
#define FMT_LEFT
Definition: curs_lib.h:41
Progress bar.
Definition: mutt_curses.h:143
void mutt_refresh(void)
Force a refresh of the screen.
Definition: curs_lib.c:99
int mutt_strwidth(const char *s)
Measure a string&#39;s width in screen cells.
Definition: curs_lib.c:1176
size_t mutt_wstr_trunc(const char *src, size_t maxlen, size_t maxwid, size_t *width)
Work out how to truncate a widechar string.
Definition: curs_lib.c:1126
int ColorDefs[MT_COLOR_MAX]
Array of all fixed colours, see enum ColorId.
Definition: color.c:51
#define SETCOLOR(X)
Definition: mutt_curses.h:220
#define STRING
Definition: string2.h:35

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void mutt_progress_init ( struct Progress progress,
const char *  msg,
unsigned short  flags,
unsigned short  inc,
size_t  size 
)

Set up a progress bar.

Parameters
progressProgress bar
msgMessage to display
flagsFlags, e.g. MUTT_PROGRESS_SIZE
incIncrements to display (0 disables updates)
sizeTotal size of expected file / traffic

Definition at line 113 of file progress.c.

115 {
116  struct timeval tv = { 0, 0 };
117 
118  if (!progress)
119  return;
120  if (OptNoCurses)
121  return;
122 
123  memset(progress, 0, sizeof(struct Progress));
124  progress->inc = inc;
125  progress->flags = flags;
126  progress->msg = msg;
127  progress->size = size;
128  if (progress->size != 0)
129  {
130  if (progress->flags & MUTT_PROGRESS_SIZE)
131  {
132  mutt_str_pretty_size(progress->sizestr, sizeof(progress->sizestr),
133  progress->size);
134  }
135  else
136  snprintf(progress->sizestr, sizeof(progress->sizestr), "%zu", progress->size);
137  }
138  if (inc == 0)
139  {
140  if (size != 0)
141  mutt_message("%s (%s)", msg, progress->sizestr);
142  else
143  mutt_message(msg);
144  return;
145  }
146  if (gettimeofday(&tv, NULL) < 0)
147  mutt_debug(1, "gettimeofday failed: %d\n", errno);
148  /* if timestamp is 0 no time-based suppression is done */
149  if (TimeInc != 0)
150  {
151  progress->timestamp =
152  ((unsigned int) tv.tv_sec * 1000) + (unsigned int) (tv.tv_usec / 1000);
153  }
154  mutt_progress_update(progress, 0, 0);
155 }
#define MUTT_PROGRESS_SIZE
traffic-based progress
Definition: progress.h:32
unsigned int timestamp
Definition: progress.h:45
size_t size
Definition: progress.h:44
#define mutt_message(...)
Definition: logging.h:87
void mutt_progress_update(struct Progress *progress, long pos, int percent)
Update the state of the progress bar.
Definition: progress.c:170
unsigned short flags
Definition: progress.h:41
WHERE bool OptNoCurses
(pseudo) when sending in batch mode
Definition: options.h:45
A progress bar.
Definition: progress.h:38
unsigned short inc
Definition: progress.h:40
char sizestr[SHORT_STRING]
Definition: progress.h:46
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
void mutt_str_pretty_size(char *buf, size_t buflen, size_t num)
Display an abbreviated size, like 3.4K.
Definition: string.c:1015
const char * msg
Definition: progress.h:42
short TimeInc
Config: Frequency of progress bar updates (milliseconds)
Definition: progress.c:43

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void mutt_progress_update ( struct Progress progress,
long  pos,
int  percent 
)

Update the state of the progress bar.

Parameters
progressProgress bar
posPosition, or count
percentPercentage complete

If percent is -1, then the percentage will be calculated using pos and the size in progress.

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

Definition at line 170 of file progress.c.

171 {
172  char posstr[SHORT_STRING];
173  bool update = false;
174  struct timeval tv = { 0, 0 };
175  unsigned int now = 0;
176 
177  if (OptNoCurses)
178  return;
179 
180  if (progress->inc == 0)
181  goto out;
182 
183  /* refresh if size > inc */
184  if ((progress->flags & MUTT_PROGRESS_SIZE) &&
185  (pos >= (progress->pos + (progress->inc << 10))))
186  {
187  update = true;
188  }
189  else if (pos >= (progress->pos + progress->inc))
190  update = true;
191 
192  /* skip refresh if not enough time has passed */
193  if (update && progress->timestamp && (gettimeofday(&tv, NULL) == 0))
194  {
195  now = ((unsigned int) tv.tv_sec * 1000) + (unsigned int) (tv.tv_usec / 1000);
196  if (now && ((now - progress->timestamp) < TimeInc))
197  update = false;
198  }
199 
200  /* always show the first update */
201  if (pos == 0)
202  update = true;
203 
204  if (update)
205  {
206  if (progress->flags & MUTT_PROGRESS_SIZE)
207  {
208  pos = pos / (progress->inc << 10) * (progress->inc << 10);
209  mutt_str_pretty_size(posstr, sizeof(posstr), pos);
210  }
211  else
212  snprintf(posstr, sizeof(posstr), "%ld", pos);
213 
214  mutt_debug(5, "updating progress: %s\n", posstr);
215 
216  progress->pos = pos;
217  if (now)
218  progress->timestamp = now;
219 
220  if (progress->size > 0)
221  {
222  message_bar(
223  (percent > 0) ? percent :
224  (int) (100.0 * (double) progress->pos / progress->size),
225  "%s %s/%s (%d%%)", progress->msg, posstr, progress->sizestr,
226  (percent > 0) ? percent :
227  (int) (100.0 * (double) progress->pos / progress->size));
228  }
229  else
230  {
231  if (percent > 0)
232  message_bar(percent, "%s %s (%d%%)", progress->msg, posstr, percent);
233  else
234  mutt_message("%s %s", progress->msg, posstr);
235  }
236  }
237 
238 out:
239  if (pos >= progress->size)
241 }
#define MUTT_PROGRESS_SIZE
traffic-based progress
Definition: progress.h:32
#define SHORT_STRING
Definition: string2.h:34
unsigned int timestamp
Definition: progress.h:45
size_t size
Definition: progress.h:44
#define mutt_message(...)
Definition: logging.h:87
static void message_bar(int percent, const char *fmt,...)
Draw a colourful progress bar.
Definition: progress.c:51
unsigned short flags
Definition: progress.h:41
WHERE bool OptNoCurses
(pseudo) when sending in batch mode
Definition: options.h:45
void mutt_clear_error(void)
Clear the message line (bottom line of screen)
Definition: mutt_logging.c:141
unsigned short inc
Definition: progress.h:40
long pos
Definition: progress.h:43
char sizestr[SHORT_STRING]
Definition: progress.h:46
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
void mutt_str_pretty_size(char *buf, size_t buflen, size_t num)
Display an abbreviated size, like 3.4K.
Definition: string.c:1015
const char * msg
Definition: progress.h:42
short TimeInc
Config: Frequency of progress bar updates (milliseconds)
Definition: progress.c:43

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Variable Documentation

short TimeInc

Config: Frequency of progress bar updates (milliseconds)

Definition at line 43 of file progress.c.