NeoMutt  2022-04-29-249-gaae397
Teaching an old dog new tricks
DOXYGEN
window.c File Reference

Progress Bar Window. More...

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

Go to the source code of this file.

Functions

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

Detailed Description

Progress Bar Window.

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 window.c.

Function Documentation

◆ message_bar()

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

Draw a colourful progress bar.

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

Definition at line 73 of file window.c.

74{
75 if (!fmt || !win)
76 return;
77
78 va_list ap;
79 char buf[256], buf2[256];
80 int w = (percent * win->state.cols) / 100;
81 size_t l;
82
83 va_start(ap, fmt);
84 vsnprintf(buf, sizeof(buf), fmt, ap);
85 l = mutt_strwidth(buf);
86 va_end(ap);
87
88 mutt_simple_format(buf2, sizeof(buf2), 0, win->state.cols - 2, JUSTIFY_LEFT,
89 0, buf, sizeof(buf), false);
90
91 mutt_window_move(win, 0, 0);
92
94 {
95 if (l < w)
96 {
97 /* The string fits within the colour bar */
99 mutt_window_addstr(win, buf2);
100 w -= l;
101 while (w-- > 0)
102 {
103 mutt_window_addch(win, ' ');
104 }
106 }
107 else
108 {
109 /* The string is too long for the colour bar */
110 int off = mutt_wstr_trunc(buf2, sizeof(buf2), w, NULL);
111
112 char ch = buf2[off];
113 buf2[off] = '\0';
115 mutt_window_addstr(win, buf2);
116 buf2[off] = ch;
118 mutt_window_addstr(win, &buf2[off]);
119 }
120 }
121 else
122 {
123 mutt_window_addstr(win, buf2);
124 }
125
127}
bool simple_color_is_set(enum ColorId cid)
Is the object coloured?
Definition: simple.c:95
@ MT_COLOR_PROGRESS
Progress bar.
Definition: color.h:56
@ MT_COLOR_NORMAL
Plain text.
Definition: color.h:54
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:858
void mutt_simple_format(char *buf, size_t buflen, int min_width, int max_width, enum FormatJustify justify, char pad_char, const char *s, size_t n, bool arboreal)
Format a string, like snprintf()
Definition: curs_lib.c:639
size_t mutt_strwidth(const char *s)
Measure a string's width in screen cells.
Definition: curs_lib.c:907
@ JUSTIFY_LEFT
Left justify the text.
Definition: curs_lib.h:42
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:65
struct AttrColor * mutt_curses_set_color_by_id(enum ColorId cid)
Set the colour and attributes by the colour id.
Definition: mutt_curses.c:81
int mutt_window_move(struct MuttWindow *win, int col, int row)
Move the cursor in a Window.
Definition: mutt_window.c:293
int mutt_window_addstr(struct MuttWindow *win, const char *str)
Write a string to a Window.
Definition: mutt_window.c:409
void mutt_window_clrtoeol(struct MuttWindow *win)
Clear to the end of the line.
Definition: mutt_window.c:242
int mutt_window_addch(struct MuttWindow *win, int ch)
Write one character to a Window.
Definition: mutt_window.c:381
struct WindowState state
Current state of the Window.
Definition: mutt_window.h:127
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 
)
static

Do we need to update, given the current percentage?

Parameters
wdataPrivate data
percentUpdated percentage
Return values
trueProgress needs an update

Definition at line 191 of file window.c.

192{
193 return (percent > wdata->display_percent);
194}
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 
)
static

Do we need to update, given the current pos?

Parameters
wdataPrivate data
posCurrent pos
Return values
trueProgress needs an update

Definition at line 202 of file window.c.

203{
204 const unsigned shift = wdata->is_bytes ? 10 : 0;
205 return pos >= (wdata->display_pos + (wdata->size_inc << shift));
206}
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 
)
static

Do we need to update, given the current time?

Parameters
wdataPrivate data
nowTime
Return values
trueProgress needs an update

Definition at line 214 of file window.c.

215{
216 if (wdata->time_inc == 0)
217 return true;
218
219 if (now < wdata->display_time)
220 return true;
221
222 const size_t elapsed = (now - wdata->display_time);
223 return (wdata->time_inc < elapsed);
224}
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.

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

Definition at line 233 of file window.c.

234{
235 if (!win || !win->wdata)
236 return false;
237
238 struct ProgressWindowData *wdata = win->wdata;
239
240 if (wdata->size == 0)
241 {
242 if (!percent_needs_update(wdata, percent))
243 return false;
244 }
245 else
246 {
247 if (!pos_needs_update(wdata, pos))
248 return false;
249 }
250
251 const uint64_t now = mutt_date_epoch_ms();
252 if (!time_needs_update(wdata, now))
253 return false;
254
255 wdata->update_pos = pos;
256 wdata->update_percent = percent;
257 wdata->update_time = now;
259 return true;
260}
uint64_t mutt_date_epoch_ms(void)
Return the number of milliseconds since the Unix epoch.
Definition: date.c:437
#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:191
static bool time_needs_update(const struct ProgressWindowData *wdata, size_t now)
Do we need to update, given the current time?
Definition: window.c:214
static bool pos_needs_update(const struct ProgressWindowData *wdata, long pos)
Do we need to update, given the current pos?
Definition: window.c:202
void * wdata
Private data.
Definition: mutt_window.h:145
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
size_t size
Total expected size.
Definition: wdata.h:42
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 ( const char *  msg,
size_t  size,
size_t  size_inc,
size_t  time_inc,
bool  is_bytes 
)

Create a new Progress Bar Window.

Parameters
msgProgress message to display
sizeExpected number of records or size of traffic
size_incSize increment (step size)
time_incTime increment
is_bytestrue if measuing bytes
Return values
ptrNew Progress Window

Definition at line 271 of file window.c.

273{
274 if (size_inc == 0) // The user has disabled the progress bar
275 return NULL;
276
282 win->actions |= WA_RECALC;
283
284 struct ProgressWindowData *wdata = progress_wdata_new();
285 wdata->win = win;
286 wdata->size = size;
287 wdata->size_inc = size_inc;
288 wdata->time_inc = time_inc;
289 wdata->is_bytes = is_bytes;
290 mutt_str_copy(wdata->msg, msg, sizeof(wdata->msg));
291
292 if (is_bytes)
293 mutt_str_pretty_size(wdata->pretty_size, sizeof(wdata->pretty_size), size);
294
295 win->wdata = wdata;
297
298 return win;
299}
static int progress_window_recalc(struct MuttWindow *win)
Recalculate the Progress Bar - Implements MuttWindow::recalc() -.
Definition: window.c:132
static int progress_window_repaint(struct MuttWindow *win)
Repaint the Progress Bar - Implements MuttWindow::repaint() -.
Definition: window.c:156
void progress_wdata_free(struct MuttWindow *win, void **ptr)
Free Progress Bar Window data - Implements MuttWindow::wdata_free() -.
Definition: wdata.c:45
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:652
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:181
@ WT_STATUS_BAR
Status Bar containing extra info about the Index/Pager/etc.
Definition: mutt_window.h:102
@ MUTT_WIN_ORIENT_VERTICAL
Window uses all available vertical space.
Definition: mutt_window.h:38
#define MUTT_WIN_SIZE_UNLIMITED
Use as much space as possible.
Definition: mutt_window.h:52
@ MUTT_WIN_SIZE_FIXED
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:1673
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:182
int(* recalc)(struct MuttWindow *win)
Definition: mutt_window.h:171
void(* wdata_free)(struct MuttWindow *win, void **ptr)
Definition: mutt_window.h:160
char msg[1024]
Message to display.
Definition: wdata.h:40
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: