NeoMutt  2024-04-25-89-g194907
Teaching an old dog new tricks
No Matches
window.c File Reference

Progress Bar Window. More...

#include "config.h"
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include "mutt/lib.h"
#include "gui/lib.h"
#include "color/lib.h"
#include "expando/lib.h"
#include "muttlib.h"
#include "wdata.h"
+ Include dependency graph for window.c:

Go to the source code of this file.


static void message_bar (struct MuttWindow *win, int percent, const char *fmt,...)
 Draw a colourful progress bar.
static int progress_window_recalc (struct MuttWindow *win)
 Recalculate the Progress Bar - Implements MuttWindow::recalc() -.
static int progress_window_repaint (struct MuttWindow *win)
 Repaint the Progress Bar - Implements MuttWindow::repaint() -.
static bool percent_needs_update (const struct ProgressWindowData *wdata, int percent)
 Do we need to update, given the current percentage?
static bool pos_needs_update (const struct ProgressWindowData *wdata, long pos)
 Do we need to update, given the current pos?
static bool time_needs_update (const struct ProgressWindowData *wdata, size_t now)
 Do we need to update, given the current time?
bool progress_window_update (struct MuttWindow *win, size_t pos, int percent)
 Update the Progress Bar Window.
struct MuttWindowprogress_window_new (size_t size, size_t size_inc, size_t time_inc, bool is_bytes)
 Create a new Progress Bar Window.
void progress_window_set_message (struct MuttWindow *win, const char *fmt, va_list ap)
 Set the progress message.
void progress_window_set_size (struct MuttWindow *win, size_t size)
 Set the progress size.

Detailed Description

Progress Bar Window.

  • 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

Definition in file window.c.

Function Documentation

◆ message_bar()

static void message_bar ( struct MuttWindow win,
int  percent,
const char *  fmt,

Draw a colourful progress bar.

winWindow to draw on
percentage complete
fmtprintf(1)-like formatting string
...Arguments to formatting string

Definition at line 75 of file window.c.

77 if (!fmt || !win || !win->wdata)
78 return;
80 va_list ap;
81 char buf[1024] = { 0 };
82 struct Buffer *buf2 = buf_pool_get();
83 int w = (percent * win->state.cols) / 100;
84 size_t l;
86 va_start(ap, fmt);
87 vsnprintf(buf, sizeof(buf), fmt, ap);
88 l = mutt_strwidth(buf);
89 va_end(ap);
91 format_string(buf2, 0, win->state.cols - 2, JUSTIFY_LEFT, 0, buf, sizeof(buf), false);
93 mutt_window_move(win, 0, 0);
95 if ((percent != -1) && simple_color_is_set(MT_COLOR_PROGRESS))
96 {
97 if (l < w)
98 {
99 /* The string fits within the colour bar */
101 mutt_window_addstr(win, buf_string(buf2));
102 w -= l;
103 while (w-- > 0)
104 {
105 mutt_window_addch(win, ' ');
106 }
108 }
109 else
110 {
111 /* The string is too long for the colour bar */
112 int off = mutt_wstr_trunc(buf_string(buf2), buf2->dsize, w, NULL);
114 char ch = buf_at(buf2, off);
115 buf2->data[off] = '\0';
117 mutt_window_addstr(win, buf_string(buf2));
118 buf2->data[off] = ch;
120 mutt_window_addstr(win, buf2->data + off);
121 }
122 }
123 else
124 {
125 mutt_window_addstr(win, buf_string(buf2));
126 }
129 buf_pool_release(&buf2);
char buf_at(const struct Buffer *buf, size_t offset)
Return the character at the given offset.
Definition: buffer.c:670
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:96
bool simple_color_is_set(enum ColorId cid)
Is the object coloured?
Definition: simple.c:109
Progress bar.
Definition: color.h:61
Plain text.
Definition: color.h:59
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:383
size_t mutt_strwidth(const char *s)
Measure a string's width in screen cells.
Definition: curs_lib.c:443
int format_string(struct Buffer *buf, int min_cols, int max_cols, enum FormatJustify justify, char pad_char, const char *str, size_t n, bool arboreal)
Format a string, like snprintf()
Definition: format.c:108
Left justify the text.
Definition: format.h:34
const struct AttrColor * mutt_curses_set_normal_backed_color_by_id(enum ColorId cid)
Set the colour and attributes by the colour id.
Definition: mutt_curses.c:63
const struct AttrColor * mutt_curses_set_color_by_id(enum ColorId cid)
Set the colour and attributes by the colour id.
Definition: mutt_curses.c:79
int mutt_window_move(struct MuttWindow *win, int col, int row)
Move the cursor in a Window.
Definition: mutt_window.c:297
int mutt_window_addstr(struct MuttWindow *win, const char *str)
Write a string to a Window.
Definition: mutt_window.c:416
void mutt_window_clrtoeol(struct MuttWindow *win)
Clear to the end of the line.
Definition: mutt_window.c:244
int mutt_window_addch(struct MuttWindow *win, int ch)
Write one character to a Window.
Definition: mutt_window.c:388
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:81
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:94
String manipulation buffer.
Definition: buffer.h:36
size_t dsize
Length of data.
Definition: buffer.h:39
char * data
Pointer to data.
Definition: buffer.h:37
struct WindowState state
Current state of the Window.
Definition: mutt_window.h:127
void * wdata
Private data.
Definition: mutt_window.h:145
short cols
Number of columns, can be MUTT_WIN_SIZE_UNLIMITED.
Definition: mutt_window.h:60
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ percent_needs_update()

static bool percent_needs_update ( const struct ProgressWindowData wdata,
int  percent 

Do we need to update, given the current percentage?

wdataPrivate data
percentUpdated percentage
Return values
trueProgress needs an update

Definition at line 232 of file window.c.

234 return (percent > wdata->display_percent);
int display_percent
Displayed percentage complete.
Definition: wdata.h:49
+ Here is the caller graph for this function:

◆ pos_needs_update()

static bool pos_needs_update ( const struct ProgressWindowData wdata,
long  pos 

Do we need to update, given the current pos?

wdataPrivate data
posCurrent pos
Return values
trueProgress needs an update

Definition at line 243 of file window.c.

245 const unsigned shift = wdata->is_bytes ? 10 : 0;
246 return pos >= (wdata->display_pos + (wdata->size_inc << shift));
size_t display_pos
Displayed position.
Definition: wdata.h:48
size_t size_inc
Size increment.
Definition: wdata.h:43
bool is_bytes
true if measuring bytes
Definition: wdata.h:45
+ Here is the caller graph for this function:

◆ time_needs_update()

static bool time_needs_update ( const struct ProgressWindowData wdata,
size_t  now 

Do we need to update, given the current time?

wdataPrivate data
Return values
trueProgress needs an update

Definition at line 255 of file window.c.

257 if (wdata->time_inc == 0)
258 return true;
260 if (now < wdata->display_time)
261 return true;
263 const size_t elapsed = (now - wdata->display_time);
264 return (wdata->time_inc < elapsed);
size_t time_inc
Time increment.
Definition: wdata.h:44
uint64_t display_time
Time of last display.
Definition: wdata.h:50
+ Here is the caller graph for this function:

◆ progress_window_update()

bool progress_window_update ( struct MuttWindow win,
size_t  pos,
int  percent 

Update the Progress Bar Window.

winWindow to draw on
posPosition, or count
percentPercentage complete
Return values
trueScreen update is needed

Definition at line 274 of file window.c.

276 if (!win || !win->wdata)
277 return false;
279 struct ProgressWindowData *wdata = win->wdata;
281 if (percent >= 0)
282 {
283 if (!percent_needs_update(wdata, percent))
284 return false;
285 }
286 else
287 {
288 if (!pos_needs_update(wdata, pos))
289 return false;
290 }
292 const uint64_t now = mutt_date_now_ms();
293 if (!time_needs_update(wdata, now))
294 return false;
296 wdata->update_pos = pos;
297 wdata->update_percent = percent;
298 wdata->update_time = now;
300 return true;
uint64_t mutt_date_now_ms(void)
Return the number of milliseconds since the Unix epoch.
Definition: date.c:465
#define WA_RECALC
Recalculate the contents of the Window.
Definition: mutt_window.h:110
static bool percent_needs_update(const struct ProgressWindowData *wdata, int percent)
Do we need to update, given the current percentage?
Definition: window.c:232
static bool time_needs_update(const struct ProgressWindowData *wdata, size_t now)
Do we need to update, given the current time?
Definition: window.c:255
static bool pos_needs_update(const struct ProgressWindowData *wdata, long pos)
Do we need to update, given the current pos?
Definition: window.c:243
WindowActionFlags actions
Actions to be performed, e.g. WA_RECALC.
Definition: mutt_window.h:132
Progress Bar Window Data.
Definition: wdata.h:36
int update_percent
Updated percentage complete.
Definition: wdata.h:55
uint64_t update_time
Time of last update.
Definition: wdata.h:56
size_t update_pos
Updated position.
Definition: wdata.h:54
struct MuttWindow * win
Window to draw on.
Definition: wdata.h:37
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ progress_window_new()

struct MuttWindow * progress_window_new ( size_t  size,
size_t  size_inc,
size_t  time_inc,
bool  is_bytes 

Create a new Progress Bar Window.

sizeExpected number of records or size of traffic
size_incSize increment (step size)
time_incTime increment
is_bytestrue if measuring bytes
Return values
ptrNew Progress Window

Definition at line 311 of file window.c.

314 if (size_inc == 0) // The user has disabled the progress bar
315 return NULL;
322 win->actions |= WA_RECALC;
324 struct ProgressWindowData *wdata = progress_wdata_new();
325 wdata->win = win;
326 wdata->size = size;
327 wdata->size_inc = size_inc;
328 wdata->time_inc = time_inc;
329 wdata->is_bytes = is_bytes;
331 if (is_bytes)
332 mutt_str_pretty_size(wdata->pretty_size, sizeof(wdata->pretty_size), size);
334 win->wdata = wdata;
337 return win;
static int progress_window_recalc(struct MuttWindow *win)
Recalculate the Progress Bar - Implements MuttWindow::recalc() -.
Definition: window.c:135
static int progress_window_repaint(struct MuttWindow *win)
Repaint the Progress Bar - Implements MuttWindow::repaint() -.
Definition: window.c:159
void progress_wdata_free(struct MuttWindow *win, void **ptr)
Free Progress Bar Window data - Implements MuttWindow::wdata_free() -.
Definition: wdata.c:45
struct MuttWindow * mutt_window_new(enum WindowType type, enum MuttWindowOrientation orient, enum MuttWindowSize size, int cols, int rows)
Create a new Window.
Definition: mutt_window.c:182
Status Bar containing extra info about the Index/Pager/etc.
Definition: mutt_window.h:102
Window uses all available vertical space.
Definition: mutt_window.h:38
Use as much space as possible.
Definition: mutt_window.h:52
Window has a fixed size.
Definition: mutt_window.h:47
void mutt_str_pretty_size(char *buf, size_t buflen, size_t num)
Display an abbreviated size, like 3.4K.
Definition: muttlib.c:1066
struct ProgressWindowData * progress_wdata_new(void)
Create new Progress Bar Window Data.
Definition: wdata.c:37
int(* repaint)(struct MuttWindow *win)
Definition: mutt_window.h:187
int(* recalc)(struct MuttWindow *win)
Definition: mutt_window.h:173
void(* wdata_free)(struct MuttWindow *win, void **ptr)
Definition: mutt_window.h:159
size_t size
Total expected size.
Definition: wdata.h:42
char pretty_size[24]
Pretty string for size.
Definition: wdata.h:41
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ progress_window_set_message()

void progress_window_set_message ( struct MuttWindow win,
const char *  fmt,
va_list  ap 

Set the progress message.

winWindow to draw on
fmtprintf format string
apprintf arguments

Definition at line 346 of file window.c.

348 if (!win || !win->wdata || !fmt)
349 return;
351 struct ProgressWindowData *wdata = win->wdata;
353 vsnprintf(wdata->msg, sizeof(wdata->msg), fmt, ap);
char msg[1024]
Message to display.
Definition: wdata.h:40
+ Here is the caller graph for this function:

◆ progress_window_set_size()

void progress_window_set_size ( struct MuttWindow win,
size_t  size 

Set the progress size.

winWindow to draw on
sizeNew size

Definition at line 363 of file window.c.

365 if (!win || !win->wdata)
366 return;
368 struct ProgressWindowData *wdata = win->wdata;
370 wdata->size = size;
371 wdata->display_pos = 0;
372 wdata->display_percent = 0;
373 wdata->display_time = 0;
+ Here is the caller graph for this function: