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