NeoMutt  2023-05-17-56-ga67199
Teaching an old dog new tricks
DOXYGEN
pbar.c
Go to the documentation of this file.
1
62#include "config.h"
63#include <inttypes.h> // IWYU pragma: keep
64#include <stdio.h>
65#include <sys/stat.h>
66#include "mutt/lib.h"
67#include "config/lib.h"
68#include "core/lib.h"
69#include "gui/lib.h"
70#include "pbar.h"
71#include "lib.h"
72#include "color/lib.h"
73#include "index/lib.h"
74#include "display.h"
75#include "format_flags.h"
76#include "hdrline.h"
77#include "mview.h"
78#include "private_data.h"
79
84{
88};
89
93static int pbar_recalc(struct MuttWindow *win)
94{
95 char buf[1024] = { 0 };
96
97 struct PBarPrivateData *pbar_data = win->wdata;
98 struct IndexSharedData *shared = pbar_data->shared;
99 struct PagerPrivateData *priv = pbar_data->priv;
100 if (!priv || !priv->pview)
101 return 0;
102
103 char pager_progress_str[65] = { 0 }; /* Lots of space for translations */
104
105 long offset;
106 if (priv->lines && (priv->cur_line <= priv->lines_used))
107 offset = priv->lines[priv->cur_line].offset;
108 else
109 offset = priv->bytes_read;
110
111 if (offset < (priv->st.st_size - 1))
112 {
113 const long percent = (100 * offset) / priv->st.st_size;
114 /* L10N: Pager position percentage.
115 `%ld` is the number, `%%` is the percent symbol.
116 They may be reordered, or space inserted, if you wish. */
117 snprintf(pager_progress_str, sizeof(pager_progress_str), _("%ld%%"), percent);
118 }
119 else
120 {
121 const char *msg = (priv->top_line == 0) ?
122 /* L10N: Status bar message: the entire email is visible in the pager */
123 _("all") :
124 /* L10N: Status bar message: the end of the email is visible in the pager */
125 _("end");
126 mutt_str_copy(pager_progress_str, msg, sizeof(pager_progress_str));
127 }
128
129 if ((priv->pview->mode == PAGER_MODE_EMAIL) || (priv->pview->mode == PAGER_MODE_ATTACH_E))
130 {
131 int msg_in_pager = shared->mailbox_view ? shared->mailbox_view->msg_in_pager : -1;
132
133 const char *c_pager_format = cs_subset_string(shared->sub, "pager_format");
134 mutt_make_string(buf, sizeof(buf), win->state.cols, NONULL(c_pager_format),
135 shared->mailbox, msg_in_pager, shared->email,
136 MUTT_FORMAT_NO_FLAGS, pager_progress_str);
137 }
138 else
139 {
140 snprintf(buf, sizeof(buf), "%s (%s)", priv->pview->banner, pager_progress_str);
141 }
142
143 if (!mutt_str_equal(buf, pbar_data->pager_format))
144 {
145 mutt_str_replace(&pbar_data->pager_format, buf);
146 win->actions |= WA_REPAINT;
147 }
148
149 return 0;
150}
151
155static int pbar_repaint(struct MuttWindow *win)
156{
157 struct PBarPrivateData *pbar_data = win->wdata;
158
159 mutt_window_move(win, 0, 0);
162
163 mutt_window_move(win, 0, 0);
164 mutt_draw_statusline(win, win->state.cols, pbar_data->pager_format,
165 mutt_str_len(pbar_data->pager_format));
167
168 mutt_debug(LL_DEBUG5, "repaint done\n");
169 return 0;
170}
171
176{
177 if (nc->event_type != NT_COLOR)
178 return 0;
179 if (!nc->global_data || !nc->event_data)
180 return -1;
181
182 struct EventColor *ev_c = nc->event_data;
183 enum ColorId cid = ev_c->cid;
184
185 if ((cid != MT_COLOR_STATUS) && (cid != MT_COLOR_NORMAL) && (cid != MT_COLOR_MAX))
186 return 0;
187
188 struct MuttWindow *win_pbar = nc->global_data;
189 win_pbar->actions |= WA_REPAINT;
190 mutt_debug(LL_DEBUG5, "color done, request WA_REPAINT\n");
191
192 return 0;
193}
194
199{
200 if (nc->event_type != NT_CONFIG)
201 return 0;
202 if (!nc->global_data || !nc->event_data)
203 return -1;
204
205 struct EventConfig *ev_c = nc->event_data;
206 if (!mutt_str_equal(ev_c->name, "pager_format"))
207 return 0;
208
209 struct MuttWindow *win_pbar = nc->global_data;
210 win_pbar->actions |= WA_RECALC;
211 mutt_debug(LL_DEBUG5, "config done, request WA_RECALC\n");
212
213 return 0;
214}
215
226{
227 if (!nc->global_data)
228 return -1;
229
230 struct MuttWindow *win_pbar = nc->global_data;
231 win_pbar->actions |= WA_RECALC;
232 mutt_debug(LL_DEBUG5, "index done, request WA_RECALC\n");
233
234 return 0;
235}
236
241{
242 if (nc->event_type != NT_PAGER)
243 return 0;
244 if (!nc->global_data)
245 return -1;
246
247 struct MuttWindow *win_pbar = nc->global_data;
248
250 {
251 win_pbar->actions |= WA_RECALC;
252 mutt_debug(LL_DEBUG5, "pager done, request WA_RECALC\n");
253 }
254
255 return 0;
256}
257
262{
263 if (nc->event_type != NT_WINDOW)
264 return 0;
265 if (!nc->global_data || !nc->event_data)
266 return -1;
267
268 struct MuttWindow *win_pbar = nc->global_data;
269 struct EventWindow *ev_w = nc->event_data;
270 if (ev_w->win != win_pbar)
271 return 0;
272
274 {
275 win_pbar->actions |= WA_RECALC | WA_REPAINT;
276 mutt_debug(LL_NOTIFY, "window state done, request WA_RECALC\n");
277 }
278 else if (nc->event_subtype == NT_WINDOW_DELETE)
279 {
280 struct PBarPrivateData *pbar_data = win_pbar->wdata;
281 struct IndexSharedData *shared = pbar_data->shared;
282
288
289 mutt_debug(LL_DEBUG5, "window delete done\n");
290 }
291
292 return 0;
293}
294
298static void pbar_data_free(struct MuttWindow *win, void **ptr)
299{
300 struct PBarPrivateData *pbar_data = *ptr;
301
302 FREE(&pbar_data->pager_format);
303
304 FREE(ptr);
305}
306
314 struct PagerPrivateData *priv)
315{
316 struct PBarPrivateData *pbar_data = mutt_mem_calloc(1, sizeof(struct PBarPrivateData));
317
318 pbar_data->shared = shared;
319 pbar_data->priv = priv;
320
321 return pbar_data;
322}
323
330struct MuttWindow *pbar_new(struct IndexSharedData *shared, struct PagerPrivateData *priv)
331{
335
336 win_pbar->wdata = pbar_data_new(shared, priv);
337 win_pbar->wdata_free = pbar_data_free;
338 win_pbar->recalc = pbar_recalc;
339 win_pbar->repaint = pbar_repaint;
340
346
347 return win_pbar;
348}
Color and attribute parsing.
ColorId
List of all colored objects.
Definition: color.h:38
@ MT_COLOR_MAX
Definition: color.h:92
@ MT_COLOR_STATUS
Status bar (takes a pattern)
Definition: color.h:75
@ MT_COLOR_NORMAL
Plain text.
Definition: color.h:57
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition: helpers.c:317
Convenience wrapper for the config headers.
Convenience wrapper for the core headers.
Pager Display.
void mutt_draw_statusline(struct MuttWindow *win, int cols, const char *buf, size_t buflen)
Draw a highlighted status bar.
Definition: dlg_index.c:926
Flags to control mutt_expando_format()
#define MUTT_FORMAT_NO_FLAGS
No flags are set.
Definition: format_flags.h:30
#define mutt_debug(LEVEL,...)
Definition: logging2.h:87
static int pbar_window_observer(struct NotifyCallback *nc)
Notification that a Window has changed - Implements observer_t -.
Definition: pbar.c:261
static int pbar_color_observer(struct NotifyCallback *nc)
Notification that a Color has changed - Implements observer_t -.
Definition: pbar.c:175
static int pbar_config_observer(struct NotifyCallback *nc)
Notification that a Config Variable has changed - Implements observer_t -.
Definition: pbar.c:198
static int pbar_pager_observer(struct NotifyCallback *nc)
Notification that the Pager has changed - Implements observer_t -.
Definition: pbar.c:240
static int pbar_index_observer(struct NotifyCallback *nc)
Notification that the Index has changed - Implements observer_t -.
Definition: pbar.c:225
static int pbar_recalc(struct MuttWindow *win)
Recalculate the Window data - Implements MuttWindow::recalc() -.
Definition: pbar.c:93
static int pbar_repaint(struct MuttWindow *win)
Repaint the Window - Implements MuttWindow::repaint() -.
Definition: pbar.c:155
static void pbar_data_free(struct MuttWindow *win, void **ptr)
Free the private data attached to the MuttWindow - Implements MuttWindow::wdata_free() -.
Definition: pbar.c:298
Convenience wrapper for the gui headers.
void mutt_make_string(char *buf, size_t buflen, int cols, const char *s, struct Mailbox *m, int inpgr, struct Email *e, MuttFormatFlags flags, const char *progress)
Create formatted strings using mailbox expandos.
Definition: hdrline.c:1489
String processing routines to generate the mail index.
GUI manage the main index (list of emails)
@ LL_DEBUG5
Log at debug level 5.
Definition: logging2.h:47
@ LL_NOTIFY
Log of notifications.
Definition: logging2.h:48
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
#define FREE(x)
Definition: memory.h:43
Convenience wrapper for the library headers.
#define _(a)
Definition: message.h:28
bool notify_observer_remove(struct Notify *notify, const observer_t callback, const void *global_data)
Remove an observer from an object.
Definition: notify.c:228
bool notify_observer_add(struct Notify *notify, enum NotifyType type, observer_t callback, void *global_data)
Add an observer to an object.
Definition: notify.c:189
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:798
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:568
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:653
char * mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition: string.c:327
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
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:180
int mutt_window_move(struct MuttWindow *win, int col, int row)
Move the cursor in a Window.
Definition: mutt_window.c:294
void mutt_window_clrtoeol(struct MuttWindow *win)
Clear to the end of the line.
Definition: mutt_window.c:241
#define WA_RECALC
Recalculate the contents of the Window.
Definition: mutt_window.h:110
@ 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
@ NT_WINDOW_STATE
Window state has changed, e.g. WN_VISIBLE.
Definition: mutt_window.h:206
@ NT_WINDOW_DELETE
Window is about to be deleted.
Definition: mutt_window.h:205
#define WA_REPAINT
Redraw the contents of the Window.
Definition: mutt_window.h:111
#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
View of a Mailbox.
@ NT_WINDOW
MuttWindow has changed, NotifyWindow, EventWindow.
Definition: notify_type.h:55
@ NT_CONFIG
Config has changed, NotifyConfig, EventConfig.
Definition: notify_type.h:43
@ NT_COLOR
Colour has changed, NotifyColor, EventColor.
Definition: notify_type.h:41
@ NT_PAGER
Pager data has changed, NotifyPager, PagerPrivateData.
Definition: notify_type.h:52
@ NT_ALL
Register for all notifications.
Definition: notify_type.h:35
#define NT_PAGER_VIEW
Pager View has changed.
Definition: lib.h:185
@ PAGER_MODE_EMAIL
Pager is invoked via 1st path. The mime part is selected automatically.
Definition: lib.h:136
@ PAGER_MODE_ATTACH_E
A special case of PAGER_MODE_ATTACH - attachment is a full-blown email message.
Definition: lib.h:138
Private state data for the Pager.
struct MuttWindow * pbar_new(struct IndexSharedData *shared, struct PagerPrivateData *priv)
Create the Pager Bar.
Definition: pbar.c:330
static struct PBarPrivateData * pbar_data_new(struct IndexSharedData *shared, struct PagerPrivateData *priv)
Free the private data attached to the MuttWindow.
Definition: pbar.c:313
Pager Bar.
Key value store.
#define NONULL(x)
Definition: string2.h:37
An Event that happened to a Colour.
Definition: notify2.h:53
enum ColorId cid
Colour ID that has changed.
Definition: notify2.h:54
A config-change event.
Definition: subset.h:71
const char * name
Name of config item that changed.
Definition: subset.h:73
An Event that happened to a Window.
Definition: mutt_window.h:215
struct MuttWindow * win
Window that changed.
Definition: mutt_window.h:216
Data shared between Index, Pager and Sidebar.
Definition: shared_data.h:37
struct Email * email
Currently selected Email.
Definition: shared_data.h:42
struct Mailbox * mailbox
Current Mailbox.
Definition: shared_data.h:41
struct ConfigSubset * sub
Config set to use.
Definition: shared_data.h:38
struct MailboxView * mailbox_view
Current Mailbox view.
Definition: shared_data.h:40
struct Notify * notify
Notifications: NotifyIndex, IndexSharedData.
Definition: shared_data.h:44
LOFF_T offset
Offset into Email file (PagerPrivateData->fp)
Definition: display.h:52
int msg_in_pager
Message currently shown in the pager.
Definition: mview.h:44
int(* repaint)(struct MuttWindow *win)
Definition: mutt_window.h:181
struct WindowState state
Current state of the Window.
Definition: mutt_window.h:127
void * wdata
Private data.
Definition: mutt_window.h:145
struct Notify * notify
Notifications: NotifyWindow, EventWindow.
Definition: mutt_window.h:138
int(* recalc)(struct MuttWindow *win)
Definition: mutt_window.h:170
void(* wdata_free)(struct MuttWindow *win, void **ptr)
Definition: mutt_window.h:159
WindowActionFlags actions
Actions to be performed, e.g. WA_RECALC.
Definition: mutt_window.h:132
Container for Accounts, Notifications.
Definition: neomutt.h:37
struct Notify * notify
Notifications handler.
Definition: neomutt.h:38
Data passed to a notification function.
Definition: observer.h:34
void * event_data
Data from notify_send()
Definition: observer.h:38
enum NotifyType event_type
Send: Event type, e.g. NT_ACCOUNT.
Definition: observer.h:36
int event_subtype
Send: Event subtype, e.g. NT_ACCOUNT_ADD.
Definition: observer.h:37
void * global_data
Data from notify_observer_add()
Definition: observer.h:39
Data to draw the Pager Bar.
Definition: pbar.c:84
struct PagerPrivateData * priv
Private Pager data.
Definition: pbar.c:86
char * pager_format
Cached status string.
Definition: pbar.c:87
struct IndexSharedData * shared
Shared Index data.
Definition: pbar.c:85
Private state data for the Pager.
Definition: private_data.h:41
int cur_line
Current line (last line visible on screen)
Definition: private_data.h:51
int lines_used
Size of lines array (used entries)
Definition: private_data.h:49
struct Line * lines
Array of text lines in pager.
Definition: private_data.h:48
struct Notify * notify
Notifications: NotifyPager, PagerPrivateData.
Definition: private_data.h:71
LOFF_T bytes_read
Number of bytes read from file.
Definition: private_data.h:46
int top_line
First visible line on screen.
Definition: private_data.h:55
struct stat st
Stats about Email file.
Definition: private_data.h:45
struct PagerView * pview
Object to view in the pager.
Definition: private_data.h:42
enum PagerMode mode
Pager mode.
Definition: lib.h:172
const char * banner
Title to display in status bar.
Definition: lib.h:174
short cols
Number of columns, can be MUTT_WIN_SIZE_UNLIMITED.
Definition: mutt_window.h:60