NeoMutt  2023-05-17-56-ga67199
Teaching an old dog new tricks
DOXYGEN
progress.c
Go to the documentation of this file.
1
31#include "config.h"
32#include <stdbool.h>
33#include <stdio.h>
34#include "mutt/lib.h"
35#include "config/lib.h"
36#include "core/lib.h"
37#include "gui/lib.h"
38#include "lib.h"
39#include "globals.h"
40#include "mutt_logging.h"
41#include "window.h"
42
43struct Progress;
44
50static size_t choose_increment(enum ProgressType type)
51{
52 const short c_read_inc = cs_subset_number(NeoMutt->sub, "read_inc");
53 const short c_write_inc = cs_subset_number(NeoMutt->sub, "write_inc");
54 const short c_net_inc = cs_subset_number(NeoMutt->sub, "net_inc");
55 const short *incs[] = { &c_read_inc, &c_write_inc, &c_net_inc };
56 return (type >= mutt_array_size(incs)) ? 0 : *incs[type];
57}
58
73bool progress_update(struct Progress *progress, size_t pos, int percent)
74{
75 // Decloak an opaque pointer
76 struct MuttWindow *win = (struct MuttWindow *) progress;
77 const bool updated = progress_window_update(win, pos, percent);
78 if (updated)
79 {
80 window_redraw(win);
81 }
82 return updated;
83}
84
89void progress_free(struct Progress **ptr)
90{
91 if (!ptr)
92 return;
93
94 if (!*ptr)
95 {
97 return;
98 }
99
100 // Decloak an opaque pointer
101 struct MuttWindow **wptr = (struct MuttWindow **) ptr;
102 struct MuttWindow *win_pop = msgcont_pop_window();
103 if (win_pop != *wptr)
104 return;
105
106 mutt_window_free(wptr);
107}
108
121struct Progress *progress_new(const char *msg, enum ProgressType type, size_t size)
122{
123 if (OptNoCurses)
124 return NULL;
125
126 const size_t size_inc = choose_increment(type);
127 if (size_inc == 0) // The user has disabled the progress bar
128 {
129 mutt_message(msg);
130 return NULL;
131 }
132
133 const short c_time_inc = cs_subset_number(NeoMutt->sub, "time_inc");
134 const bool is_bytes = (type == MUTT_PROGRESS_NET);
135
136 struct MuttWindow *win = progress_window_new(msg, size, size_inc, c_time_inc, is_bytes);
137
139
140 // Return an opaque pointer
141 return (struct Progress *) win;
142}
short cs_subset_number(const struct ConfigSubset *sub, const char *name)
Get a number config item by name.
Definition: helpers.c:169
Convenience wrapper for the config headers.
Convenience wrapper for the core headers.
bool OptNoCurses
(pseudo) when sending in batch mode
Definition: globals.c:82
#define mutt_message(...)
Definition: logging2.h:89
Convenience wrapper for the gui headers.
#define mutt_array_size(x)
Definition: memory.h:36
void msgcont_push_window(struct MuttWindow *win)
Add a window to the Container Stack.
Definition: msgcont.c:87
struct MuttWindow * msgcont_pop_window(void)
Remove the last Window from the Container Stack.
Definition: msgcont.c:56
Convenience wrapper for the library headers.
void mutt_clear_error(void)
Clear the message line (bottom line of screen)
Definition: mutt_logging.c:73
NeoMutt Logging.
void window_redraw(struct MuttWindow *win)
Reflow, recalc and repaint a tree of Windows.
Definition: mutt_window.c:605
void mutt_window_free(struct MuttWindow **ptr)
Free a Window and its children.
Definition: mutt_window.c:200
ProgressType
What kind of operation is this progress tracking?
Definition: lib.h:48
@ MUTT_PROGRESS_NET
Progress tracks bytes, according to $net_inc
Definition: lib.h:51
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.
Definition: window.c:280
bool progress_window_update(struct MuttWindow *win, size_t pos, int percent)
Update the Progress Bar Window.
Definition: window.c:242
void progress_free(struct Progress **ptr)
Free a Progress Bar.
Definition: progress.c:89
static size_t choose_increment(enum ProgressType type)
Choose the right increment given a ProgressType.
Definition: progress.c:50
bool progress_update(struct Progress *progress, size_t pos, int percent)
Update the state of the progress bar.
Definition: progress.c:73
struct Progress * progress_new(const char *msg, enum ProgressType type, size_t size)
Create a new Progress Bar.
Definition: progress.c:121
Key value store.
enum MuttWindowSize size
Type of Window, e.g. MUTT_WIN_SIZE_FIXED.
Definition: mutt_window.h:131
Container for Accounts, Notifications.
Definition: neomutt.h:37
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
Progress Bar Window.