NeoMutt  2023-11-03-107-g582dc1
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
progress.c
Go to the documentation of this file.
1
31#include "config.h"
32#include <stdarg.h>
33#include <stdbool.h>
34#include <stdio.h>
35#include <unistd.h>
36#include "mutt/lib.h"
37#include "config/lib.h"
38#include "core/lib.h"
39#include "gui/lib.h"
40#include "lib.h"
41#include "globals.h"
42#include "mutt_logging.h"
43#include "window.h"
44
45struct Progress;
46
52static size_t choose_increment(enum ProgressType type)
53{
54 switch (type)
55 {
57 return cs_subset_number(NeoMutt->sub, "net_inc");
59 return cs_subset_number(NeoMutt->sub, "read_inc");
61 return cs_subset_number(NeoMutt->sub, "write_inc");
62 default:
63 return 0;
64 }
65}
66
81bool progress_update(struct Progress *progress, size_t pos, int percent)
82{
83 if (!progress)
84 return false;
85
86 // Decloak an opaque pointer
87 struct MuttWindow *win = (struct MuttWindow *) progress;
88 const bool updated = progress_window_update(win, pos, percent);
89
90 if (SigWinch)
91 {
92 SigWinch = false;
94 window_redraw(NULL);
95 }
96 else
97 {
98 if (updated)
99 {
100 window_redraw(win);
101 }
102 }
103
104 return updated;
105}
106
111void progress_free(struct Progress **ptr)
112{
113 if (!ptr)
114 return;
115
116 if (!*ptr)
117 {
119 return;
120 }
121
122 // Decloak an opaque pointer
123 struct MuttWindow **wptr = (struct MuttWindow **) ptr;
124 struct MuttWindow *win_pop = msgcont_pop_window();
125 if (win_pop != *wptr)
126 return;
127
128 mutt_window_free(wptr);
129}
130
140struct Progress *progress_new(enum ProgressType type, size_t size)
141{
142 if (OptNoCurses)
143 return NULL;
144
145 const size_t size_inc = choose_increment(type);
146 if (size_inc == 0) // The user has disabled the progress bar
147 return NULL;
148
149 const short c_time_inc = cs_subset_number(NeoMutt->sub, "time_inc");
150 const bool is_bytes = (type == MUTT_PROGRESS_NET);
151
152 struct MuttWindow *win = progress_window_new(size, size_inc, c_time_inc, is_bytes);
153
155
156 // Return an opaque pointer
157 return (struct Progress *) win;
158}
159
166void progress_set_message(struct Progress *progress, const char *fmt, ...)
167{
168 // Decloak an opaque pointer
169 struct MuttWindow *win = (struct MuttWindow *) progress;
170
171 va_list ap;
172 va_start(ap, fmt);
173
174 if (win)
175 {
176 progress_window_set_message(win, fmt, ap);
177 }
178 else
179 {
180 char msg[1024] = { 0 };
181 vsnprintf(msg, sizeof(msg), fmt, ap);
182 mutt_message("%s", msg);
183 }
184
185 va_end(ap);
186}
187
191void progress_set_size(struct Progress *progress, size_t size)
192{
193 // Decloak an opaque pointer
194 struct MuttWindow *win = (struct MuttWindow *) progress;
195
197}
short cs_subset_number(const struct ConfigSubset *sub, const char *name)
Get a number config item by name.
Definition: helpers.c:144
Convenience wrapper for the config headers.
Convenience wrapper for the core headers.
bool OptNoCurses
(pseudo) when sending in batch mode
Definition: globals.c:77
SIG_ATOMIC_VOLATILE_T SigWinch
true after SIGWINCH is received
Definition: globals.c:60
#define mutt_message(...)
Definition: logging2.h:91
Convenience wrapper for the gui headers.
void msgcont_push_window(struct MuttWindow *win)
Add a window to the Container Stack.
Definition: msgcont.c:93
struct MuttWindow * msgcont_pop_window(void)
Remove the last Window from the Container Stack.
Definition: msgcont.c:57
Convenience wrapper for the library headers.
bool notify_send(struct Notify *notify, enum NotifyType event_type, int event_subtype, void *event_data)
Send out a notification message.
Definition: notify.c:173
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:634
void mutt_window_free(struct MuttWindow **ptr)
Free a Window and its children.
Definition: mutt_window.c:202
@ NT_RESIZE
Window has been resized.
Definition: notify_type.h:52
ProgressType
What kind of operation is this progress tracking?
Definition: lib.h:80
@ MUTT_PROGRESS_NET
Progress tracks bytes, according to $net_inc
Definition: lib.h:81
@ MUTT_PROGRESS_READ
Progress tracks elements, according to $read_inc
Definition: lib.h:82
@ MUTT_PROGRESS_WRITE
Progress tracks elements, according to $write_inc
Definition: lib.h:83
void progress_window_set_size(struct MuttWindow *win, size_t size)
Set the progress size.
Definition: window.c:361
bool progress_window_update(struct MuttWindow *win, size_t pos, int percent)
Update the Progress Bar Window.
Definition: window.c:272
void progress_window_set_message(struct MuttWindow *win, const char *fmt, va_list ap)
Set the progress message.
Definition: window.c:344
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.
Definition: window.c:309
void progress_set_message(struct Progress *progress, const char *fmt,...)
Set the progress message.
Definition: progress.c:166
struct Progress * progress_new(enum ProgressType type, size_t size)
Create a new Progress Bar.
Definition: progress.c:140
void progress_free(struct Progress **ptr)
Free a Progress Bar.
Definition: progress.c:111
static size_t choose_increment(enum ProgressType type)
Choose the right increment given a ProgressType.
Definition: progress.c:52
bool progress_update(struct Progress *progress, size_t pos, int percent)
Update the state of the progress bar.
Definition: progress.c:81
void progress_set_size(struct Progress *progress, size_t size)
Set the progress size.
Definition: progress.c:191
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:41
struct Notify * notify_resize
Window resize notifications handler.
Definition: neomutt.h:43
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:45
Progress Bar Window.