]> Pileus Git - lackey/blob - src/view.c
ceb9fec7328019bc154f067645e234fea3d68d80
[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
19 #define _XOPEN_SOURCE_EXTENDED
20
21 #include <stdlib.h>
22 #include <string.h>
23 #include <locale.h>
24 #include <ncurses.h>
25
26 #include "util.h"
27 #include "conf.h"
28 #include "date.h"
29 #include "cal.h"
30 #include "view.h"
31
32 /* Types */
33 typedef struct {
34         const char *name;
35         const char *title;
36         void      (*init)(WINDOW*);
37         void      (*size)(int,int);
38         void      (*draw)(void);
39         int       (*run)(int,mmask_t,int,int);
40         int         keys[8];
41         WINDOW     *win;
42 } view_t;
43
44 /* Macros */
45 #define VIEW(name, title, ...)                \
46         void name##_init(WINDOW *win);        \
47         void name##_size(int,int);            \
48         void name##_draw(void);               \
49         int  name##_run(int,mmask_t,int,int); \
50         view_t name##_view = {                \
51                 #name,                        \
52                 title,                        \
53                 name##_init,                  \
54                 name##_size,                  \
55                 name##_draw,                  \
56                 name##_run,                   \
57                 { __VA_ARGS__ }               \
58         }
59
60 /* Views */
61 VIEW(day,      "Day",      KEY_F(1), '1');
62 VIEW(week,     "Week",     KEY_F(2), '2');
63 VIEW(month,    "Month",    KEY_F(3), '3');
64 VIEW(year,     "Year",     KEY_F(4), '4');
65 VIEW(events,   "Events",   KEY_F(5), '5');
66 VIEW(todo,     "Todo",     KEY_F(6), '6');
67 VIEW(settings, "Settings", KEY_F(7), '7');
68 VIEW(help,     "Help",     KEY_F(8), '8');
69 VIEW(edit,     "Edit");
70
71 /* View data */
72 view_t  spacer = { "|", "|" };
73
74 view_t *views[] = {
75         &day_view, &week_view, &month_view, &year_view,
76         &events_view, &todo_view,
77         &settings_view, &help_view,
78         &edit_view
79 };
80
81 view_t *menu[] = {
82         &day_view, &week_view, &month_view, &year_view,
83         &spacer, &events_view, &todo_view,
84         &spacer, &settings_view, &help_view
85 };
86
87 /* Config data */
88 int COMPACT = 0;
89 int MORNING = 8;
90
91 /* Global data */
92 edit_t EDIT = EDIT_NONE;
93
94 /* Local data */
95 view_t *view    = &day_view;
96 view_t *active  = &day_view;
97 view_t *popup   = NULL;
98 int     running = 0;
99
100 /* Local functions */
101 static void draw_header(void)
102 {
103         move(0, 0);
104         attron(COLOR_PAIR(COLOR_TITLE));
105         clrtoeol();
106
107         /* Draw menu */
108         for (int i = 0; i < N_ELEMENTS(menu); i++) {
109                 if (menu[i] == active)
110                         attron(A_BOLD);
111                 printw("%s ", menu[i]->title);
112                 if (menu[i] == active)
113                         attroff(A_BOLD);
114         }
115
116         /* Draw popup window */
117         if (popup) {
118                 printw("| ");
119                 attron(A_BOLD);
120                 printw("[%s]", popup->title);
121                 attroff(A_BOLD);
122         }
123
124         /* Draw date */
125         move(0, COLS-19);
126         printw("%04d-%02d-%02d %02d:%02d:%02d",
127                         NOW.year, NOW.month+1, NOW.day+1,
128                         NOW.hour, NOW.min,     NOW.sec);
129
130         attroff(COLOR_PAIR(COLOR_TITLE));
131         if (!COMPACT)
132                 mvhline(1, 0, ACS_HLINE, COLS);
133         refresh();
134 }
135
136 static int get_color(const char *cat)
137 {
138         return cat == NULL           ? 0           :
139                match(cat, "class") ? COLOR_CLASS :
140                match(cat, "ec")    ? COLOR_EC    :
141                match(cat, "work")  ? COLOR_WORK  : COLOR_OTHER ;
142 }
143
144 static void update_sizes(void)
145 {
146         int hdr = COMPACT ? 1 : 2;
147         for (int i = 0; i < N_ELEMENTS(views); i++) {
148                 wresize(views[i]->win, LINES-hdr, COLS);
149                 mvwin(views[i]->win, hdr, 0);
150                 views[i]->size(LINES-hdr, COLS);
151         }
152 }
153
154 static void draw_view(void)
155 {
156         draw_header();
157         werase(view->win);
158         view->draw();
159         wrefresh(view->win);
160 }
161
162 static int set_view(view_t *_active, view_t *_popup)
163 {
164         view = _popup ?: _active;
165         if (active != _active) {
166                 active = _active;
167                 set_string("view", 0, "active", active->name);
168                 draw_view();
169         }
170         if (popup != _popup) {
171                 popup = _popup;
172                 draw_view();
173         }
174         return 1;
175 }
176
177 static int process(int key, mmask_t btn, int row, int col)
178 {
179         /* Refresh timestamp */
180         draw_header();
181
182         /* Check for mouse events on the menu */
183         if (key == KEY_MOUSE && row == 0) {
184                 int start = 1;
185                 for (int i = 0; i < N_ELEMENTS(menu); i++) {
186                         int end = start + strlen(menu[i]->name) - 1;
187                         if (start <= col && col <= end && menu[i]->draw)
188                                 return set_view(menu[i], NULL);
189                         start = end + 2;
190                 }
191         }
192
193         /* Look though menu for hotkeys */
194         for (int i = 0; i < N_ELEMENTS(menu); i++) {
195                 for (int j = 0; j < N_ELEMENTS(menu[i]->keys); j++)
196                         if (menu[i]->keys[j] == key)
197                                 return set_view(menu[i], NULL);
198         }
199
200         /* Shift windows with left/right keys */
201         int shift = key == KEY_RIGHT ? +1 :
202                     key == KEY_LEFT  ? -1 : 0;
203         if (shift) {
204                 int num = 0;
205                 for (int i = 0; i < N_ELEMENTS(menu); i++)
206                         if (menu[i] == active)
207                                 num = i;
208                 do  {
209                         num += shift;
210                         num += N_ELEMENTS(menu);
211                         num %= N_ELEMENTS(menu);
212                 } while (menu[num] == &spacer);
213                 return set_view(menu[num], NULL);
214         }
215
216         /* Handle other keys */
217         switch (key) {
218                 case KEY_RESIZE:
219                         endwin();
220                         refresh();
221                         update_sizes();
222                         draw_view();
223                         return 1;
224                 case '\14': // Ctrl-L
225                         clear();
226                 case '\7':  // Ctrl-G
227                         update_sizes();
228                         draw_view();
229                         return 1;
230                 case '\033': // escape
231                         return set_view(active, NULL);
232                 case '?':    // help
233                         return set_view(active, &help_view);
234                 case 'c':
235                         COMPACT ^= 1;
236                         set_bool("view", 0, "compact", COMPACT);
237                         update_sizes();
238                         draw_view();
239                         return 1;
240                 case 'e':    // edit
241                         return set_view(active, &edit_view);
242         }
243
244         /* Pass key to active view */
245         return view->run(key, btn, row, col);
246 }
247
248 /* Curses functions */
249 void wmvresize(WINDOW *win, int top, int left, int rows, int cols)
250 {
251         int y = getpary(win);
252         if (top < y)
253                 mvderwin(win, top, left);
254         wresize(win, rows, cols);
255         if (top > y)
256                 mvderwin(win, top, left);
257 }
258
259 void wshrink(WINDOW *win, int top)
260 {
261         int x    = getparx(win);
262         int y    = getpary(win);
263         int r    = getmaxy(win);
264         int c    = getmaxx(win);
265         int rows = r + (y - top);
266         if (top  <  y) mvderwin(win, top, x);
267         if (rows != r) wresize(win, rows, c);
268         if (top  >  y) mvderwin(win, top, x);
269 }
270
271 /* Helper functions */
272 void event_box(WINDOW *win, event_t *event, int y, int x, int h, int w)
273 {
274         int i, l = 0;
275         int s = y < 0 ? -y-1 : 0;
276
277         int color = get_color(event->cat);
278
279         if (color) wattron(win, COLOR_PAIR(color));
280
281         if (h >= 2) mvwhline_set(win, y,     x+1,   WACS_T_HLINE, w-2);
282         if (h <= 1) mvwadd_wch(win,   y,     x,     WACS_BULLET);
283         if (h >= 2) mvwadd_wch(win,   y,     x,     WACS_T_ULCORNER);
284         if (h >= 2) mvwadd_wch(win,   y,     x+w-1, WACS_T_URCORNER);
285         if (h >= 3) mvwvline_set(win, y+1+s, x,     WACS_T_VLINE, h-2-s);
286         if (h >= 3) mvwvline_set(win, y+1+s, x+w-1, WACS_T_VLINE, h-2-s);
287         if (h >= 2) mvwadd_wch(win,   y+h-1, x,     WACS_T_LLCORNER);
288         if (h >= 2) mvwadd_wch(win,   y+h-1, x+w-1, WACS_T_LRCORNER);
289         if (h >= 2) mvwhline_set(win, y+h-1, x+1,   WACS_T_HLINE, w-2);
290
291         for (i = 1; i < h-1; i++)
292                 mvwhline(win, y+i, x+1, ' ', w-2);
293
294         if (color) wattroff(win, COLOR_PAIR(color));
295
296         if (event == EVENT)     wattron(win, WA_BOLD | WA_REVERSE);
297         if (event == EVENT)     mvwhline(win, y+s, x, ' ', w);
298         if (l<h && event->name) mvwprintw(win, y+l++, x+1, "%.*s",   w-2, event->name);
299         if (event == EVENT)     wattroff(win, WA_REVERSE);
300         if (l<h && event->loc)  mvwprintw(win, y+l++, x+1, "@ %.*s", w-4, event->loc);
301         if (l<h && event->desc) mvwprintw(win, y+l++, x+1, "%.*s",   w-2, event->desc);
302         if (event == EVENT)     wattroff(win, WA_BOLD);
303 }
304
305 void event_line(WINDOW *win, event_t *event, int y, int x, int w, int flags)
306 {
307         int color = get_color(event->cat);
308
309         if (color) wattron(win, COLOR_PAIR(color));
310         mvwaddch(win, y, x++, ACS_BLOCK);
311         if (color) wattroff(win, COLOR_PAIR(color));
312
313         if (flags & SHOW_ACTIVE && event == EVENT)
314                 wattron(win, A_REVERSE | A_BOLD);
315         if (flags & SHOW_DETAILS) {
316                 if (all_day(&event->start, &event->end))
317                         mvwprintw(win, y, x+1, "[all day]   - ");
318                 else
319                         mvwprintw(win, y, x+1, "%2d:%02d-%2d:%02d - ",
320                                         event->start.hour, event->start.min,
321                                         event->end.hour,   event->end.min);
322                 x += 15;
323                 w -= 15;
324         }
325         if (event->name) {
326                 const char *label = event->name ?: event->desc;
327                 mvwprintw(win, y, x, "%-*.*s", w-1, w-1, label);
328                 x += MIN(strlen(label), w-1);
329         }
330         if (flags & SHOW_DETAILS && event->loc) {
331                 mvwprintw(win, y, x, " @ %s", event->loc);
332         }
333         if (flags & SHOW_ACTIVE && event == EVENT)
334                 wattroff(win, A_REVERSE | A_BOLD);
335 }
336
337 void todo_line(WINDOW *win, todo_t *todo, int y, int x, int w, int flags)
338 {
339         char perc[16];
340         char desc[LINES];
341         sprintf(perc, "%2d%%", todo->status);
342
343         int cat    = get_color(todo->cat);
344         int status = todo->status == NEW  ? COLOR_NEW  :
345                      todo->status == DONE ? COLOR_DONE : COLOR_WIP;
346
347         sprintf(desc, "%s", todo->name ?: todo->desc ?: "");
348         strsub(desc, '\n', ';');
349
350         /* Print category */
351         if (cat) wattron(win, COLOR_PAIR(cat));
352         mvwaddch(win, y, x, ACS_BLOCK);
353         if (cat) wattroff(win, COLOR_PAIR(cat));
354         x += 2;
355
356         /* Set background */
357         if (flags & SHOW_ACTIVE && todo == TODO)
358                 wattron(win, A_REVERSE | A_BOLD);
359         mvwhline(win, y, x, ' ', COLS-x);
360
361         /* Print time */
362         if (no_date(&todo->due))
363                 mvwprintw(win, y, x, "[no due date]");
364         else
365                 mvwprintw(win, y, x, "%04d-%02d-%02d %2d:%02d",
366                                 todo->due.year, todo->due.month+1, todo->due.day+1,
367                                 todo->due.hour, todo->due.min);
368         x += 18;
369
370         /* Print status */
371         if (status) wattron(win, COLOR_PAIR(status));
372         mvwprintw(win, y, x, "%s",
373                 todo->status == NEW    ? "new"  :
374                 todo->status == DONE   ? "done" : perc);
375         if (status) wattroff(win, COLOR_PAIR(status));
376         x += 6;
377
378         /* Print description */
379         mvwprintw(win, y, x, "%s", desc);
380
381         /* Reset flags */
382         if (flags & SHOW_ACTIVE && todo == TODO)
383                 wattroff(win, A_REVERSE | A_BOLD);
384 }
385
386 /* View init */
387 void view_init(void)
388 {
389         /* Set default escape timeout */
390         if (!getenv("ESCDELAY"))
391                 putenv("ESCDELAY=25");
392
393         /* Setup Curses */
394         setlocale(LC_ALL, "");
395         initscr();
396         cbreak();
397         noecho();
398         keypad(stdscr, TRUE);
399         start_color();
400         curs_set(false);
401         timeout(100);
402         use_default_colors();
403         mousemask(ALL_MOUSE_EVENTS, NULL);
404
405         init_pair(COLOR_TITLE, COLOR_GREEN,   -1);
406         init_pair(COLOR_ERROR, COLOR_RED,     -1);
407
408         init_pair(COLOR_NEW,   COLOR_RED,     -1);
409         init_pair(COLOR_WIP,   COLOR_YELLOW,  -1);
410         init_pair(COLOR_DONE,  COLOR_GREEN,   -1);
411
412         init_pair(COLOR_CLASS, COLOR_BLUE,    -1);
413         init_pair(COLOR_EC,    COLOR_GREEN,   -1);
414         init_pair(COLOR_WORK,  COLOR_MAGENTA, -1);
415         init_pair(COLOR_OTHER, COLOR_RED,     -1);
416
417         running = 1;
418
419         /* Setup windows */
420         for (int i = 0; i < N_ELEMENTS(views); i++) {
421                 int hdr = COMPACT ? 1 : 2;
422                 views[i]->win = newwin(LINES-hdr, COLS, hdr, 0);
423                 views[i]->init(views[i]->win);
424                 views[i]->size(LINES-hdr, COLS);
425         }
426 }
427
428 /* Config parser */
429 void view_config(const char *group, const char *name, const char *key, const char *value)
430 {
431         if (match(group, "view")) {
432                 if (match(key, "compact")) {
433                         COMPACT = get_bool(value);
434                 } else if (match(key, "morning")) {
435                         MORNING = get_number(value);
436                 } else if (match(key, "active")) {
437                         for (int i = 0; i < N_ELEMENTS(views); i++) {
438                                 if (match(value, views[i]->name)) {
439                                         get_string(value);
440                                         view = active = views[i];
441                                         break;
442                                 }
443                         }
444                 }
445         }
446 }
447
448 /* View event */
449 void view_edit(edit_t mode)
450 {
451         EDIT = mode;
452         set_view(active, &edit_view);
453 }
454
455 void view_main(void)
456 {
457         /* Draw initial view */
458         draw_view();
459
460         /* Run */
461         while (1) {
462                 MEVENT btn;
463                 conf_sync();
464                 int chr = getch();
465                 date_sync();
466                 if (chr == KEY_MOUSE)
467                         if (getmouse(&btn) != OK)
468                                 continue;
469                 if (process(chr, btn.bstate, btn.y, btn.x))
470                         continue;
471                 if (chr == ERR) // timeout
472                         continue;
473                 if (chr == 'q')
474                         break;
475                 debug("main: Unhandled key - Dec %3d,  Hex %02x,  Oct %03o,  Chr <%c>",
476                                 chr, chr, chr, chr);
477         }
478
479         /* Cleanup window */
480         view_exit();
481 }
482
483 void view_exit(void)
484 {
485         if (running)
486                 endwin();
487 }
488
489 void view_debug(const char *fmt, va_list ap)
490 {
491         if (running) {
492                 int rev = COMPACT ? A_BOLD : 0;
493                 if (!COMPACT)
494                         mvhline(LINES-2, 0, ACS_HLINE, COLS);
495                 move(LINES-1, 0);
496                 attron(COLOR_PAIR(COLOR_ERROR) | rev);
497                 vwprintw(stdscr, fmt, ap);
498                 attroff(COLOR_PAIR(COLOR_ERROR) | rev);
499                 if (!COMPACT)
500                         clrtoeol();
501         }
502 }