forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatus.c
413 lines (378 loc) · 12.3 KB
/
status.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/**
* @file
* GUI display a user-configurable status line
*
* @authors
* Copyright (C) 1996-2000,2007 Michael R. Elkins <[email protected]>
*
* @copyright
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* 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/>.
*/
/**
* @page status GUI display a user-configurable status line
*
* GUI display a user-configurable status line
*/
#include "config.h"
#include <stdbool.h>
#include <stdio.h>
#include "mutt/mutt.h"
#include "config/lib.h"
#include "core/lib.h"
#include "gui/lib.h"
#include "status.h"
#include "context.h"
#include "format_flags.h"
#include "globals.h"
#include "mutt_mailbox.h"
#include "mutt_menu.h"
#include "muttlib.h"
#include "options.h"
#include "protos.h"
#include "sort.h"
/* These Config Variables are only used in status.c */
struct MbTable *C_StatusChars; ///< Config: Indicator characters for the status bar
/**
* get_sort_str - Get the sort method as a string
* @param buf Buffer for the sort string
* @param buflen Length of the buffer
* @param method Sort method, see #SortType
* @retval ptr Buffer pointer
*/
static char *get_sort_str(char *buf, size_t buflen, enum SortType method)
{
snprintf(buf, buflen, "%s%s%s", (method & SORT_REVERSE) ? "reverse-" : "",
(method & SORT_LAST) ? "last-" : "",
mutt_map_get_name(method & SORT_MASK, SortMethods));
return buf;
}
/**
* status_format_str - Create the status bar string - Implements ::format_t
*
* | Expando | Description
* |:--------|:--------------------------------------------------------
* | \%b | Number of incoming folders with unread messages
* | \%D | Description of the mailbox
* | \%d | Number of deleted messages
* | \%f | Full mailbox path
* | \%F | Number of flagged messages
* | \%h | Hostname
* | \%l | Length of mailbox (in bytes)
* | \%L | Size (in bytes) of the messages shown (or limited)
* | \%M | Number of messages shown (virtual message count when limiting)
* | \%m | Total number of messages
* | \%n | Number of new messages
* | \%o | Number of old unread messages
* | \%p | Number of postponed messages
* | \%P | Percent of way through index
* | \%R | Number of read messages
* | \%r | Readonly/wontwrite/changed flag
* | \%S | Current aux sorting method (`$sort_aux`)
* | \%s | Current sorting method (`$sort`)
* | \%t | # of tagged messages
* | \%u | Number of unread messages
* | \%V | Currently active limit pattern
* | \%v | NeoMutt version
*/
static const char *status_format_str(char *buf, size_t buflen, size_t col, int cols,
char op, const char *src, const char *prec,
const char *if_str, const char *else_str,
unsigned long data, MuttFormatFlags flags)
{
char fmt[128], tmp[128];
bool optional = (flags & MUTT_FORMAT_OPTIONAL);
struct Menu *menu = (struct Menu *) data;
*buf = '\0';
switch (op)
{
case 'b':
if (!optional)
{
snprintf(fmt, sizeof(fmt), "%%%sd", prec);
snprintf(buf, buflen, fmt,
mutt_mailbox_check(Context ? Context->mailbox : NULL, 0));
}
else if (mutt_mailbox_check(Context ? Context->mailbox : NULL, 0) == 0)
optional = false;
break;
case 'd':
if (!optional)
{
snprintf(fmt, sizeof(fmt), "%%%sd", prec);
snprintf(buf, buflen, fmt,
(Context && Context->mailbox) ? Context->mailbox->msg_deleted : 0);
}
else if (!Context || !Context->mailbox || (Context->mailbox->msg_deleted == 0))
optional = false;
break;
case 'D':
{
struct Mailbox *m = Context ? Context->mailbox : NULL;
// If there's a descriptive name, use it. Otherwise, fall-through
if (m && m->name)
{
mutt_str_strfcpy(tmp, m->name, sizeof(tmp));
snprintf(fmt, sizeof(fmt), "%%%ss", prec);
snprintf(buf, buflen, fmt, tmp);
break;
}
}
/* fallthrough */
case 'f':
{
struct Mailbox *m = Context ? Context->mailbox : NULL;
#ifdef USE_COMPRESSED
if (m && m->compress_info && (m->realpath[0] != '\0'))
{
mutt_str_strfcpy(tmp, m->realpath, sizeof(tmp));
mutt_pretty_mailbox(tmp, sizeof(tmp));
}
else
#endif
if (m && (m->magic == MUTT_NOTMUCH) && m->name)
{
mutt_str_strfcpy(tmp, m->name, sizeof(tmp));
}
else if (m && !mutt_buffer_is_empty(&m->pathbuf))
{
mutt_str_strfcpy(tmp, mailbox_path(m), sizeof(tmp));
mutt_pretty_mailbox(tmp, sizeof(tmp));
}
else
mutt_str_strfcpy(tmp, _("(no mailbox)"), sizeof(tmp));
snprintf(fmt, sizeof(fmt), "%%%ss", prec);
snprintf(buf, buflen, fmt, tmp);
break;
}
case 'F':
if (!optional)
{
snprintf(fmt, sizeof(fmt), "%%%sd", prec);
snprintf(buf, buflen, fmt,
(Context && Context->mailbox) ? Context->mailbox->msg_flagged : 0);
}
else if (!Context || !Context->mailbox || (Context->mailbox->msg_flagged == 0))
optional = false;
break;
case 'h':
snprintf(fmt, sizeof(fmt), "%%%ss", prec);
snprintf(buf, buflen, fmt, NONULL(ShortHostname));
break;
case 'l':
if (!optional)
{
snprintf(fmt, sizeof(fmt), "%%%ss", prec);
mutt_str_pretty_size(tmp, sizeof(tmp),
(Context && Context->mailbox) ? Context->mailbox->size : 0);
snprintf(buf, buflen, fmt, tmp);
}
else if (!Context || !Context->mailbox || !Context->mailbox->size)
optional = false;
break;
case 'L':
if (!optional)
{
snprintf(fmt, sizeof(fmt), "%%%ss", prec);
mutt_str_pretty_size(tmp, sizeof(tmp), Context ? Context->vsize : 0);
snprintf(buf, buflen, fmt, tmp);
}
else if (!Context || !Context->pattern)
optional = false;
break;
case 'm':
if (!optional)
{
snprintf(fmt, sizeof(fmt), "%%%sd", prec);
snprintf(buf, buflen, fmt,
(Context && Context->mailbox) ? Context->mailbox->msg_count : 0);
}
else if (!Context || !Context->mailbox || (Context->mailbox->msg_count == 0))
optional = false;
break;
case 'M':
if (!optional)
{
snprintf(fmt, sizeof(fmt), "%%%sd", prec);
snprintf(buf, buflen, fmt,
(Context && Context->mailbox) ? Context->mailbox->vcount : 0);
}
else if (!Context || !Context->pattern)
optional = false;
break;
case 'n':
if (!optional)
{
snprintf(fmt, sizeof(fmt), "%%%sd", prec);
snprintf(buf, buflen, fmt,
(Context && Context->mailbox) ? Context->mailbox->msg_new : 0);
}
else if (!Context || !Context->mailbox || (Context->mailbox->msg_new == 0))
optional = false;
break;
case 'o':
if (!optional)
{
snprintf(fmt, sizeof(fmt), "%%%sd", prec);
snprintf(buf, buflen, fmt,
(Context && Context->mailbox) ?
(Context->mailbox->msg_unread - Context->mailbox->msg_new) :
0);
}
else if (!Context || !Context->mailbox ||
((Context->mailbox->msg_unread - Context->mailbox->msg_new) == 0))
optional = false;
break;
case 'p':
{
int count = mutt_num_postponed(Context ? Context->mailbox : NULL, false);
if (!optional)
{
snprintf(fmt, sizeof(fmt), "%%%sd", prec);
snprintf(buf, buflen, fmt, count);
}
else if (count == 0)
optional = false;
break;
}
case 'P':
{
if (!menu)
break;
char *cp = NULL;
if (menu->top + menu->pagelen >= menu->max)
{
cp = menu->top ?
/* L10N: Status bar message: the end of the list emails is visible in the index */
_("end") :
/* L10N: Status bar message: all the emails are visible in the index */
_("all");
}
else
{
int count = (100 * (menu->top + menu->pagelen)) / menu->max;
snprintf(tmp, sizeof(tmp), "%d%%", count);
cp = tmp;
}
snprintf(fmt, sizeof(fmt), "%%%ss", prec);
snprintf(buf, buflen, fmt, cp);
break;
}
case 'r':
{
size_t i = 0;
if (Context && Context->mailbox)
{
i = OptAttachMsg ?
3 :
((Context->mailbox->readonly || Context->mailbox->dontwrite) ?
2 :
(Context->mailbox->changed ||
/* deleted doesn't necessarily mean changed in IMAP */
(Context->mailbox->magic != MUTT_IMAP && Context->mailbox->msg_deleted)) ?
1 :
0);
}
if (!C_StatusChars || !C_StatusChars->len)
buf[0] = '\0';
else if (i >= C_StatusChars->len)
snprintf(buf, buflen, "%s", C_StatusChars->chars[0]);
else
snprintf(buf, buflen, "%s", C_StatusChars->chars[i]);
break;
}
case 'R':
{
int read = (Context && Context->mailbox) ?
(Context->mailbox->msg_count - Context->mailbox->msg_unread) :
0;
if (!optional)
{
snprintf(fmt, sizeof(fmt), "%%%sd", prec);
snprintf(buf, buflen, fmt, read);
}
else if (read == 0)
optional = false;
break;
}
case 's':
snprintf(fmt, sizeof(fmt), "%%%ss", prec);
snprintf(buf, buflen, fmt, get_sort_str(tmp, sizeof(tmp), C_Sort));
break;
case 'S':
snprintf(fmt, sizeof(fmt), "%%%ss", prec);
snprintf(buf, buflen, fmt, get_sort_str(tmp, sizeof(tmp), C_SortAux));
break;
case 't':
if (!optional)
{
snprintf(fmt, sizeof(fmt), "%%%sd", prec);
snprintf(buf, buflen, fmt,
(Context && Context->mailbox) ? Context->mailbox->msg_tagged : 0);
}
else if (!Context || !Context->mailbox || (Context->mailbox->msg_tagged == 0))
optional = false;
break;
case 'u':
if (!optional)
{
snprintf(fmt, sizeof(fmt), "%%%sd", prec);
snprintf(buf, buflen, fmt,
(Context && Context->mailbox) ? Context->mailbox->msg_unread : 0);
}
else if (!Context || !Context->mailbox || (Context->mailbox->msg_unread == 0))
optional = false;
break;
case 'v':
snprintf(buf, buflen, "%s", mutt_make_version());
break;
case 'V':
if (!optional)
{
snprintf(fmt, sizeof(fmt), "%%%ss", prec);
snprintf(buf, buflen, fmt, (Context && Context->pattern) ? Context->pattern : "");
}
else if (!Context || !Context->pattern)
optional = false;
break;
case 0:
*buf = '\0';
return src;
default:
snprintf(buf, buflen, "%%%s%c", prec, op);
break;
}
if (optional)
{
mutt_expando_format(buf, buflen, col, cols, if_str, status_format_str,
(unsigned long) menu, MUTT_FORMAT_NO_FLAGS);
}
else if (flags & MUTT_FORMAT_OPTIONAL)
{
mutt_expando_format(buf, buflen, col, cols, else_str, status_format_str,
(unsigned long) menu, MUTT_FORMAT_NO_FLAGS);
}
return src;
}
/**
* menu_status_line - Create the status line
* @param[out] buf Buffer in which to save string
* @param[in] buflen Buffer length
* @param[in] menu Current menu
* @param[in] p Format string
*/
void menu_status_line(char *buf, size_t buflen, struct Menu *menu, const char *p)
{
mutt_expando_format(buf, buflen, 0, menu ? menu->win_ibar->state.cols : buflen, p,
status_format_str, (unsigned long) menu, MUTT_FORMAT_NO_FLAGS);
}