Update to latest madtty.
[apps/madmutt.git] / lib-ui / status.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
4  *
5  * This file is part of mutt-ng, see http://www.muttng.org/.
6  * It's licensed under the GNU General Public License,
7  * please see the file GPL in the top level source directory.
8  */
9
10 #include <lib-ui/lib-ui.h>
11
12 #include <lib-ui/menu.h>
13 #include <lib-mx/mx.h>
14
15 #include "mutt.h"
16 #include "sort.h"
17 #include "buffy.h"
18
19 static char *get_sort_str (char *buf, ssize_t buflen, int method)
20 {
21   snprintf (buf, buflen, "%s%s%s",
22             (method & SORT_REVERSE) ? "reverse-" : "",
23             (method & SORT_LAST) ? "last-" : "",
24             mutt_getnamebyvalue (method & SORT_MASK, SortMethods));
25   return buf;
26 }
27
28 /* %b = number of incoming folders with unread messages [option]
29  * %B = short mailbox path
30  * %d = number of deleted messages [option]
31  * %f = full mailbox path
32  * %F = number of flagged messages [option]
33  * %h = hostname
34  * %l = length of mailbox (in bytes) [option]
35  * %m = total number of messages [option]
36  * %M = number of messages shown (virutal message count when limiting) [option]
37  * %n = number of new messages [option]
38  * %p = number of postponed messages [option]
39  * %P = percent of way through index
40  * %r = readonly/wontwrite/changed flag
41  * %s = current sorting method ($sort)
42  * %S = current aux sorting method ($sort_aux)
43  * %t = # of tagged messages [option]
44  * %v = Madmutt version 
45  * %V = currently active limit pattern [option] */
46 static const char *
47 status_format_str(char *buf, ssize_t buflen, char op, const char *src,
48                   const char *prefix, const char *ifstr, const char *elstr,
49                   anytype data, format_flag flags)
50 {
51   char fmt[STRING], tmp[STRING];
52   const char *cp, *p;
53   int count, optional = (flags & M_FORMAT_OPTIONAL);
54   MUTTMENU *menu = data.ptr;
55
56   *buf = 0;
57   switch (op) {
58   case 'b':
59     if (!optional) {
60       snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
61       snprintf (buf, buflen, fmt, buffy_check (0));
62     }
63     else if (!buffy_check (0))
64       optional = 0;
65     break;
66
67   case 'B':
68     snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
69     if (Context && Context->cinfo && Context->realpath) {
70       if ((p = strrchr (Context->realpath, '/')))
71         m_strcpy(tmp, sizeof(tmp), p + 1);
72       else
73         m_strcpy(tmp, sizeof(tmp), Context->realpath);
74     }
75     else
76     if (Context && Context->path) {
77       if ((p = strrchr (Context->path, '/')))
78         m_strcpy(tmp, sizeof(tmp), p + 1);
79       else
80         m_strcpy(tmp, sizeof(tmp), Context->path);
81     }
82     else
83       m_strcpy(tmp, sizeof(tmp), _("no mailbox"));
84     snprintf (buf, buflen, fmt, tmp);
85     break;
86
87   case 'd':
88     if (!optional) {
89       snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
90       snprintf (buf, buflen, fmt, Context ? Context->deleted : 0);
91     }
92     else if (!Context || !Context->deleted)
93       optional = 0;
94     break;
95
96   case 'h':
97     snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
98     snprintf (buf, buflen, fmt, NONULL (mod_core.shorthost));
99     break;
100
101   case 'f':
102     snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
103     if (Context && Context->cinfo && Context->realpath) {
104       m_strcpy(tmp, sizeof(tmp), Context->realpath);
105       mutt_pretty_mailbox (tmp);
106     } else
107     if (Context && Context->path) {
108       m_strcpy(tmp, sizeof(tmp), Context->path);
109       mutt_pretty_mailbox (tmp);
110     } else {
111       m_strcpy(tmp, sizeof(tmp), _("(no mailbox)"));
112     }
113     snprintf (buf, buflen, fmt, tmp);
114     break;
115
116   case 'F':
117     if (!optional) {
118       snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
119       snprintf (buf, buflen, fmt, Context ? Context->flagged : 0);
120     }
121     else if (!Context || !Context->flagged)
122       optional = 0;
123     break;
124
125   case 'l':
126     if (!optional) {
127       snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
128       mutt_pretty_size (tmp, sizeof (tmp), Context ? Context->size : 0);
129       snprintf (buf, buflen, fmt, tmp);
130     }
131     else if (!Context || !Context->size)
132       optional = 0;
133     break;
134
135   case 'L':
136     if (!optional) {
137       snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
138       mutt_pretty_size (tmp, sizeof (tmp), Context ? Context->vsize : 0);
139       snprintf (buf, buflen, fmt, tmp);
140     }
141     else if (!Context || !Context->pattern)
142       optional = 0;
143     break;
144
145   case 'm':
146     if (!optional) {
147       snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
148       snprintf (buf, buflen, fmt, Context ? Context->msgcount : 0);
149     }
150     else if (!Context || !Context->msgcount)
151       optional = 0;
152     break;
153
154   case 'M':
155     if (!optional) {
156       snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
157       snprintf (buf, buflen, fmt, Context ? Context->vcount : 0);
158     }
159     else if (!Context || !Context->pattern)
160       optional = 0;
161     break;
162
163   case 'n':
164     if (!optional) {
165       snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
166       snprintf (buf, buflen, fmt, Context ? Context->new : 0);
167     }
168     else if (!Context || Context->new <= 0)
169       optional = 0;
170     break;
171
172   case 'o':
173     if (!optional) {
174       snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
175       snprintf (buf, buflen, fmt,
176                 Context ? Context->unread - Context->new : 0);
177     }
178     else if (!Context || !(Context->unread - Context->new))
179       optional = 0;
180     break;
181
182   case 'p':
183     count = mutt_num_postponed (0);
184     if (!optional) {
185       snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
186       snprintf (buf, buflen, fmt, count);
187     }
188     else if (!count)
189       optional = 0;
190     break;
191
192   case 'P':
193     if (menu->top + menu->pagelen >= menu->max)
194       cp = menu->top ? "end" : "all";
195     else {
196       count = (100 * (menu->top + menu->pagelen)) / menu->max;
197       snprintf (tmp, sizeof (tmp), "%d%%", count);
198       cp = tmp;
199     }
200     snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
201     snprintf (buf, buflen, fmt, cp);
202     break;
203
204   case 'r':
205     {
206       int i = 0;
207
208       if (Context) {
209         /* XXX: deleted doesn't necessarily mean changed in IMAP */
210         i = option (OPTATTACHMSG) ? 3
211             : ((Context->readonly || Context->dontwrite) ? 2
212                : (Context->changed || (Context-> magic != M_IMAP && Context->
213                                         deleted)) ? 1 : 0);
214       }
215
216       if (!StChars)
217         buf[0] = 0;
218       else if (i >= m_strlen(StChars))
219         buf[0] = StChars[0];
220       else
221         buf[0] = StChars[i];
222
223       buf[1] = 0;
224       break;
225     }
226
227   case 's':
228     snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
229     snprintf (buf, buflen, fmt, get_sort_str (tmp, sizeof (tmp), Sort));
230     break;
231
232   case 'S':
233     snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
234     snprintf (buf, buflen, fmt, get_sort_str (tmp, sizeof (tmp), SortAux));
235     break;
236
237   case 't':
238     if (!optional) {
239       snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
240       snprintf (buf, buflen, fmt, Context ? Context->tagged : 0);
241     }
242     else if (!Context || !Context->tagged)
243       optional = 0;
244     break;
245
246   case 'u':
247     if (!optional) {
248       snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
249       snprintf (buf, buflen, fmt, Context ? Context->unread : 0);
250     }
251     else if (!Context || !Context->unread)
252       optional = 0;
253     break;
254
255   case 'v':
256     m_strcpy(buf, buflen, madmutt_version);
257     break;
258
259   case 'V':
260     if (!optional) {
261       snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
262       snprintf (buf, buflen, fmt,
263                 (Context && Context->pattern) ? Context->pattern : "");
264     }
265     else if (!Context || !Context->pattern)
266       optional = 0;
267     break;
268
269   case 0:
270     *buf = 0;
271     return src;
272
273   default:
274     *buf = 0;
275     break;
276   }
277
278   if (flags & M_FORMAT_OPTIONAL)
279     menu_status_line(buf, buflen, menu, optional ? ifstr : elstr);
280
281   return src;
282 }
283
284 void menu_status_line(char* buf, ssize_t len, MUTTMENU* menu, const char* p)
285 {
286     m_strformat(buf, len, getmaxx(main_w), p, status_format_str, menu, 0);
287 }