NeoMutt  2020-06-26-89-g172cd3
Teaching an old dog new tricks
DOXYGEN
progress.h File Reference

Progress bar. More...

#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
+ Include dependency graph for progress.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  Progress
 A progress bar. More...
 

Enumerations

enum  ProgressType { MUTT_PROGRESS_READ, MUTT_PROGRESS_WRITE, MUTT_PROGRESS_NET }
 What kind of operation is this progress tracking? More...
 

Functions

void mutt_progress_init (struct Progress *progress, const char *msg, enum ProgressType type, size_t size)
 Set up a progress bar. More...
 
void mutt_progress_update (struct Progress *progress, size_t pos, int percent)
 Update the state of the progress bar. More...
 

Variables

short C_TimeInc
 Config: Frequency of progress bar updates (milliseconds) More...
 
short C_ReadInc
 Config: Update the progress bar after this many records read (0 to disable) More...
 
short C_WriteInc
 Config: Update the progress bar after this many records written (0 to disable) More...
 
short C_NetInc
 Config: (socket) Update the progress bar after this many KB sent/received (0 to disable) More...
 

Detailed Description

Progress bar.

Authors
  • Richard Russon
  • Pietro Cerutti

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

Enumeration Type Documentation

◆ ProgressType

What kind of operation is this progress tracking?

Enumerator
MUTT_PROGRESS_READ 

Progress tracks elements, according to C_ReadInc.

MUTT_PROGRESS_WRITE 

Progress tracks elements, according to C_WriteInc.

MUTT_PROGRESS_NET 

Progress tracks bytes, according to C_NetInc.

Definition at line 40 of file progress.h.

41 {
45 };
Progress tracks bytes, according to C_NetInc.
Definition: progress.h:44
Progress tracks elements, according to C_WriteInc.
Definition: progress.h:43
Progress tracks elements, according to C_ReadInc.
Definition: progress.h:42

Function Documentation

◆ mutt_progress_init()

void mutt_progress_init ( struct Progress progress,
const char *  msg,
enum ProgressType  type,
size_t  size 
)

Set up a progress bar.

Parameters
progressProgress bar
msgMessage to display; this is copied into the Progress object
typeType, e.g. MUTT_PROGRESS_READ
sizeTotal size of expected file / traffic

Definition at line 153 of file progress.c.

155 {
156  if (!progress || OptNoCurses)
157  return;
158 
159  /* Initialize Progress structure */
160  memset(progress, 0, sizeof(struct Progress));
161  mutt_str_copy(progress->msg, msg, sizeof(progress->msg));
162  progress->size = size;
163  progress->inc = progress_choose_increment(type);
164  progress->is_bytes = (type == MUTT_PROGRESS_NET);
165 
166  /* Generate the size string, if a total size was specified */
167  if (progress->size != 0)
168  {
169  if (progress->is_bytes)
170  {
171  mutt_str_pretty_size(progress->sizestr, sizeof(progress->sizestr),
172  progress->size);
173  }
174  else
175  {
176  snprintf(progress->sizestr, sizeof(progress->sizestr), "%zu", progress->size);
177  }
178  }
179 
180  if (progress->inc == 0)
181  {
182  /* This progress bar does not increment - write the initial message */
183  if (progress->size == 0)
184  {
185  mutt_message(progress->msg);
186  }
187  else
188  {
189  mutt_message("%s (%s)", progress->msg, progress->sizestr);
190  }
191  }
192  else
193  {
194  /* This progress bar does increment - perform the initial update */
195  mutt_progress_update(progress, 0, 0);
196  }
197 }
Progress tracks bytes, according to C_NetInc.
Definition: progress.h:44
size_t size
Definition: progress.h:55
bool is_bytes
Definition: progress.h:58
#define mutt_message(...)
Definition: logging.h:83
WHERE bool OptNoCurses
(pseudo) when sending in batch mode
Definition: options.h:48
A progress bar.
Definition: progress.h:50
void mutt_progress_update(struct Progress *progress, size_t pos, int percent)
Update the state of the progress bar.
Definition: progress.c:212
static size_t progress_choose_increment(enum ProgressType type)
Choose the right increment given a ProgressType.
Definition: progress.c:116
void mutt_str_pretty_size(char *buf, size_t buflen, size_t num)
Display an abbreviated size, like 3.4K.
Definition: muttlib.c:1659
size_t inc
Definition: progress.h:56
char msg[1024]
Definition: progress.h:52
size_t mutt_str_copy(char *dest, const char *src, size_t dsize)
Copy a string into a buffer (guaranteeing NUL-termination)
Definition: string.c:721
char sizestr[24]
Definition: progress.h:53
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_progress_update()

void mutt_progress_update ( struct Progress progress,
size_t  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 212 of file progress.c.

213 {
214  if (OptNoCurses)
215  return;
216 
217  const uint64_t now = mutt_date_epoch_ms();
218 
219  const bool update = (pos == 0) /* always show the first update */ ||
220  (progress_pos_needs_update(progress, pos) &&
221  progress_time_needs_update(progress, now));
222 
223  if (progress->inc != 0 && update)
224  {
225  progress->pos = pos;
226  progress->timestamp = now;
227 
228  char posstr[128];
229  if (progress->is_bytes)
230  {
231  const size_t round_pos =
232  (progress->pos / (progress->inc << 10)) * (progress->inc << 10);
233  mutt_str_pretty_size(posstr, sizeof(posstr), round_pos);
234  }
235  else
236  {
237  snprintf(posstr, sizeof(posstr), "%zu", progress->pos);
238  }
239 
240  mutt_debug(LL_DEBUG4, "updating progress: %s\n", posstr);
241 
242  if (progress->size != 0)
243  {
244  if (percent < 0)
245  {
246  percent = 100.0 * progress->pos / progress->size;
247  }
248  message_bar(percent, "%s %s/%s (%d%%)", progress->msg, posstr,
249  progress->sizestr, percent);
250  }
251  else
252  {
253  if (percent > 0)
254  message_bar(percent, "%s %s (%d%%)", progress->msg, posstr, percent);
255  else
256  mutt_message("%s %s", progress->msg, posstr);
257  }
258  }
259 
260  if (progress->pos >= progress->size)
262 }
Log at debug level 4.
Definition: logging.h:43
uint64_t mutt_date_epoch_ms(void)
Return the number of milliseconds since the Unix epoch.
Definition: date.c:423
size_t size
Definition: progress.h:55
bool is_bytes
Definition: progress.h:58
#define mutt_message(...)
Definition: logging.h:83
static void message_bar(int percent, const char *fmt,...)
Draw a colourful progress bar.
Definition: progress.c:54
WHERE bool OptNoCurses
(pseudo) when sending in batch mode
Definition: options.h:48
static bool progress_pos_needs_update(const struct Progress *progress, long pos)
Do we need to update, given the current pos?
Definition: progress.c:128
void mutt_clear_error(void)
Clear the message line (bottom line of screen)
Definition: mutt_logging.c:113
size_t pos
Definition: progress.h:54
void mutt_str_pretty_size(char *buf, size_t buflen, size_t num)
Display an abbreviated size, like 3.4K.
Definition: muttlib.c:1659
size_t inc
Definition: progress.h:56
char msg[1024]
Definition: progress.h:52
uint64_t timestamp
Definition: progress.h:57
static bool progress_time_needs_update(const struct Progress *progress, size_t now)
Do we need to update, given the current time?
Definition: progress.c:140
char sizestr[24]
Definition: progress.h:53
#define mutt_debug(LEVEL,...)
Definition: logging.h:81
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ C_TimeInc

short C_TimeInc

Config: Frequency of progress bar updates (milliseconds)

Definition at line 43 of file progress.c.

◆ C_ReadInc

short C_ReadInc

Config: Update the progress bar after this many records read (0 to disable)

Definition at line 44 of file progress.c.

◆ C_WriteInc

short C_WriteInc

Config: Update the progress bar after this many records written (0 to disable)

Definition at line 45 of file progress.c.

◆ C_NetInc

short C_NetInc

Config: (socket) Update the progress bar after this many KB sent/received (0 to disable)

Definition at line 46 of file progress.c.