]> Pileus Git - lackey/blob - src/view.c
Output cal field for ical events and todos
[lackey] / src / view.c
1 /*
2  * Copyright (C) 2012-2013 Andy Spencer <andy753421@gmail.com>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #define _XOPEN_SOURCE_EXTENDED
19
20 #include <string.h>
21 #include <ncurses.h>
22
23 #include "util.h"
24 #include "conf.h"
25 #include "date.h"
26 #include "cal.h"
27 #include "view.h"
28
29 /* Types */
30 typedef struct {
31         const char *name;
32         const char *title;
33         void      (*init)(WINDOW*);
34         void      (*size)(int,int);
35         void      (*draw)(void);
36         int       (*run)(int,mmask_t,int,int);
37         int         keys[8];
38         WINDOW     *win;
39 } view_t;
40
41 /* Macros */
42 #define VIEW(name, title, ...)                \
43         void name##_init(WINDOW *win);        \
44         void name##_size(int,int);            \
45         void name##_draw(void);               \
46         int  name##_run(int,mmask_t,int,int); \
47         view_t name##_view = {                \
48                 #name,                        \
49                 title,                        \
50                 name##_init,                  \
51                 name##_size,                  \
52                 name##_draw,                  \
53                 name##_run,                   \
54                 { __VA_ARGS__ }               \
55         }
56
57 /* Views */
58 VIEW(day,      "Day",      KEY_F(1), '1');
59 VIEW(week,     "Week",     KEY_F(2), '2');
60 VIEW(month,    "Month",    KEY_F(3), '3');
61 VIEW(year,     "Year",     KEY_F(4), '4');
62 VIEW(events,   "Events",   KEY_F(5), '5');
63 VIEW(todo,     "Todo",     KEY_F(6), '6');
64 VIEW(settings, "Settings", KEY_F(7), '7');
65 VIEW(help,     "Help",     KEY_F(8), '8');
66 VIEW(edit,     "Edit");
67
68 /* View data */
69 view_t  spacer = { "|", "|" };
70
71 view_t *views[] = {
72         &day_view, &week_view, &month_view, &year_view,
73         &events_view, &todo_view,
74         &settings_view, &help_view,
75         &edit_view
76 };
77
78 view_t *menu[] = {
79         &day_view, &week_view, &month_view, &year_view,
80         &spacer, &events_view, &todo_view,
81         &spacer, &settings_view, &help_view
82 };
83
84 /* Global data */
85 int COMPACT = 0;
86
87 /* Local data */
88 view_t *view   = &day_view;
89 view_t *active = &day_view;
90 view_t *popup  = NULL;
91
92 /* Local functions */
93 static void draw_header(void)
94 {
95         move(0, 0);
96         attron(COLOR_PAIR(COLOR_TITLE));
97         for (int i = 0; i < N_ELEMENTS(menu); i++) {
98                 if (menu[i] == active)
99                         attron(A_BOLD);
100                 printw("%s ", menu[i]->title);
101                 if (menu[i] == active)
102                         attroff(A_BOLD);
103         }
104         clrtoeol();
105         if (popup) {
106                 attron(A_BOLD);
107                 move(0, COLS-strlen(popup->title)-2);
108                 printw("[%s]", popup->title);
109                 attroff(A_BOLD);
110         }
111         attroff(COLOR_PAIR(COLOR_TITLE));
112         if (!COMPACT)
113                 mvhline(1, 0, ACS_HLINE, COLS);
114         refresh();
115 }
116
117 static int get_color(const char *cat)
118 {
119         return cat == NULL           ? 0           :
120                match(cat, "class") ? COLOR_CLASS :
121                match(cat, "ec")    ? COLOR_EC    :
122                match(cat, "work")  ? COLOR_WORK  : COLOR_OTHER ;
123 }
124
125 static int set_view(view_t *_active, view_t *_popup)
126 {
127         view = _popup ?: _active;
128         if (active != _active) {
129                 active = _active;
130                 set_string("view", 0, "active", active->name);
131                 view_draw();
132         }
133         if (popup != _popup) {
134                 popup = _popup;
135                 view_draw();
136         }
137         return 1;
138 }
139
140 /* Curses functions */
141 void wmvresize(WINDOW *win, int top, int left, int rows, int cols)
142 {
143         int y = getpary(win);
144         if (top < y)
145                 mvderwin(win, top, left);
146         wresize(win, rows, cols);
147         if (top > y)
148                 mvderwin(win, top, left);
149 }
150
151 void wshrink(WINDOW *win, int top)
152 {
153         int x    = getparx(win);
154         int y    = getpary(win);
155         int r    = getmaxy(win);
156         int c    = getmaxx(win);
157         int rows = r + (y - top);
158         if (top  <  y) mvderwin(win, top, x);
159         if (rows != r) wresize(win, rows, c);
160         if (top  >  y) mvderwin(win, top, x);
161 }
162
163 /* Helper functions */
164 void event_box(WINDOW *win, event_t *event, int y, int x, int h, int w)
165 {
166         int l = 0;
167         int s = y < 0 ? -y-1 : 0;
168
169         int color = get_color(event->cat);
170
171         if (color) wattron(win, COLOR_PAIR(color));
172
173         if (h >= 2) mvwhline_set(win, y,     x+1,   WACS_T_HLINE, w-2);
174         if (h <= 1) mvwadd_wch(win,   y,     x,     WACS_BULLET);
175         if (h >= 2) mvwadd_wch(win,   y,     x,     WACS_T_ULCORNER);
176         if (h >= 2) mvwadd_wch(win,   y,     x+w-1, WACS_T_URCORNER);
177         if (h >= 3) mvwvline_set(win, y+1+s, x,     WACS_T_VLINE, h-2-s);
178         if (h >= 3) mvwvline_set(win, y+1+s, x+w-1, WACS_T_VLINE, h-2-s);
179         if (h >= 2) mvwadd_wch(win,   y+h-1, x,     WACS_T_LLCORNER);
180         if (h >= 2) mvwadd_wch(win,   y+h-1, x+w-1, WACS_T_LRCORNER);
181         if (h >= 2) mvwhline_set(win, y+h-1, x+1,   WACS_T_HLINE, w-2);
182
183         if (color) wattroff(win, COLOR_PAIR(color));
184
185         if (l<h && event->name) mvwprintw(win, y+l++, x+1, "%.*s",   w-2, event->name);
186         if (l<h && event->loc)  mvwprintw(win, y+l++, x+1, "@ %.*s", w-4, event->loc);
187         if (l<h && event->desc) mvwprintw(win, y+l++, x+1, "%.*s",   w-2, event->desc);
188 }
189
190 void event_line(WINDOW *win, event_t *event, int y, int x, int w, int full)
191 {
192         int color = get_color(event->cat);
193
194         if (color) wattron(win, COLOR_PAIR(color));
195         mvwaddch(win, y, x++, ACS_BLOCK);
196         if (color) wattroff(win, COLOR_PAIR(color));
197
198         if (full) {
199                 if (all_day(&event->start, &event->end))
200                         mvwprintw(win, y, x, " [all day]   -");
201                 else
202                         mvwprintw(win, y, x, " %2d:%02d-%2d:%02d -",
203                                         event->start.hour, event->start.min,
204                                         event->end.hour,   event->end.min);
205                 x += 15;
206         }
207         if (event->name) {
208                 const char *label = event->name ?: event->desc;
209                 mvwprintw(win, y, x, "%-*.*s", w-1, w-1, label);
210                 x += MIN(strlen(label), w-1);
211         }
212         if (full && event->loc) {
213                 mvwprintw(win, y, x, " @ %s", event->loc);
214         }
215 }
216
217 void todo_line(WINDOW *win, todo_t *todo, int y, int x, int w, int full)
218 {
219         char perc[16];
220         char desc[LINES];
221         sprintf(perc, "%2d%%", todo->status);
222
223         int cat    = get_color(todo->cat);
224         int status = todo->status == NEW  ? COLOR_NEW  :
225                      todo->status == DONE ? COLOR_DONE : COLOR_WIP;
226
227         sprintf(desc, "%s", todo->name ?: todo->desc ?: "");
228         strsub(desc, '\n', ';');
229
230         /* Print category */
231         if (cat) wattron(win, COLOR_PAIR(cat));
232         mvwaddch(win, y, x, ACS_BLOCK);
233         if (cat) wattroff(win, COLOR_PAIR(cat));
234         x += 2;
235
236         /* Print time */
237         if (no_date(&todo->due))
238                 mvwprintw(win, y, x, "[no due date]");
239         else
240                 mvwprintw(win, y, x, "%04d-%02d-%02d %2d:%02d",
241                                 todo->due.year, todo->due.month+1, todo->due.day+1,
242                                 todo->due.hour, todo->due.min);
243         x += 18;
244
245         /* Print status */
246         if (status) wattron(win, COLOR_PAIR(status));
247         mvwprintw(win, y, x, "%s",
248                 todo->status == NEW    ? "new"  :
249                 todo->status == DONE   ? "done" : perc);
250         if (status) wattroff(win, COLOR_PAIR(status));
251         x += 6;
252
253         /* Print description */
254         mvwprintw(win, y, x, "%s", desc);
255 }
256
257 /* View init */
258 void view_init(void)
259 {
260         int hdr = COMPACT ? 1 : 2;
261         for (int i = 0; i < N_ELEMENTS(views); i++) {
262                 views[i]->win = newwin(LINES-hdr, COLS, hdr, 0);
263                 views[i]->init(views[i]->win);
264                 views[i]->size(LINES-hdr, COLS);
265         }
266 }
267
268 /* Config parser */
269 void view_config(const char *group, const char *name, const char *key, const char *value)
270 {
271         if (match(group, "view")) {
272                 if (match(key, "compact")) {
273                         COMPACT = get_bool(value);
274                 } else if (match(key, "active")) {
275                         for (int i = 0; i < N_ELEMENTS(views); i++) {
276                                 if (match(value, views[i]->name)) {
277                                         get_string(value);
278                                         view = active = views[i];
279                                         break;
280                                 }
281                         }
282                 }
283         }
284 }
285
286 /* View draw */
287 void view_resize(void)
288 {
289         int hdr = COMPACT ? 1 : 2;
290         for (int i = 0; i < N_ELEMENTS(views); i++) {
291                 wresize(views[i]->win, LINES-hdr, COLS);
292                 mvwin(views[i]->win, hdr, 0);
293                 views[i]->size(LINES-hdr, COLS);
294         }
295 }
296
297 /* View draw */
298 void view_draw(void)
299 {
300         draw_header();
301         werase(view->win);
302         view->draw();
303         wrefresh(view->win);
304 }
305
306 /* View run */
307 int view_run(int key, mmask_t btn, int row, int col)
308 {
309         /* Check for mouse events on the menu */
310         if (key == KEY_MOUSE && row == 0) {
311                 int start = 1;
312                 for (int i = 0; i < N_ELEMENTS(menu); i++) {
313                         int end = start + strlen(menu[i]->name) - 1;
314                         if (start <= col && col <= end && menu[i]->draw)
315                                 return set_view(menu[i], NULL);
316                         start = end + 2;
317                 }
318         }
319
320         /* Look though menu for hotkeys */
321         for (int i = 0; i < N_ELEMENTS(menu); i++) {
322                 for (int j = 0; j < N_ELEMENTS(menu[i]->keys); j++)
323                         if (menu[i]->keys[j] == key)
324                                 return set_view(menu[i], NULL);
325         }
326
327         /* Shift windows with left/right keys */
328         int shift = key == KEY_RIGHT ? +1 :
329                     key == KEY_LEFT  ? -1 : 0;
330         if (shift) {
331                 int num = 0;
332                 for (int i = 0; i < N_ELEMENTS(menu); i++)
333                         if (menu[i] == active)
334                                 num = i;
335                 do  {
336                         num += shift;
337                         num += N_ELEMENTS(menu);
338                         num %= N_ELEMENTS(menu);
339                 } while (menu[num] == &spacer);
340                 return set_view(menu[num], NULL);
341         }
342
343         /* Handle other keys */
344         switch (key) {
345                 case 'c':
346                         COMPACT ^= 1;
347                         set_bool("view", 0, "compact", COMPACT);
348                         view_resize();
349                         view_draw();
350                         return 1;
351                 case '\033': // escape
352                         return set_view(active, NULL);
353                 case '?':    // help
354                         return set_view(active, &help_view);
355                 case 'e':    // edit
356                         return set_view(active, &edit_view);
357         }
358
359         /* Pass key to active view */
360         return view->run(key, btn, row, col);
361 }