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