]> Pileus Git - lackey/blob - src/view.c
Add simple config file parser
[lackey] / src / view.c
1 /*
2  * Copyright (C) 2012 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         char   *name;
32         void  (*init)(WINDOW*);
33         void  (*size)(int,int);
34         void  (*draw)(void);
35         int   (*run)(int,mmask_t,int,int);
36         int     keys[8];
37         WINDOW *win;
38 } view_t;
39
40 /* Macros */
41 #define VIEW(name)                     \
42         void name##_init(WINDOW *win); \
43         void name##_size(int,int);     \
44         void name##_draw(void);        \
45         int  name##_run(int,mmask_t,int,int)
46
47 /* Prototypes */
48 VIEW(day);
49 VIEW(week);
50 VIEW(month);
51 VIEW(year);
52 VIEW(events);
53 VIEW(todo);
54 VIEW(settings);
55 VIEW(help);
56
57 /* View data */
58 static const char *names[] = {
59         "day", "week", "month", "year",
60         "|", "events", "todo",
61         "|", "settings", "help",
62 };
63
64 view_t views[] = {
65         { "Day",      day_init,      day_size,      day_draw,      day_run,      {KEY_F(1), '1',    } },
66         { "Week",     week_init,     week_size,     week_draw,     week_run,     {KEY_F(2), '2',    } },
67         { "Month",    month_init,    month_size,    month_draw,    month_run,    {KEY_F(3), '3',    } },
68         { "Year",     year_init,     year_size,     year_draw,     year_run,     {KEY_F(4), '4',    } },
69         { "|",        NULL,          NULL,          NULL,          NULL,         {                  } },
70         { "Events",   events_init,   events_size,   events_draw,   events_run,   {KEY_F(5), '5',    } },
71         { "Todo",     todo_init,     todo_size,     todo_draw,     todo_run,     {KEY_F(6), '6',    } },
72         { "|",        NULL,          NULL,          NULL,          NULL,         {                  } },
73         { "Settings", settings_init, settings_size, settings_draw, settings_run, {KEY_F(7), '7',    } },
74         { "Help",     help_init,     help_size,     help_draw,     help_run,     {KEY_F(8), '8', '?'} },
75 };
76
77 /* Config data */
78 int COMPACT = 0;
79 int ACTIVE  = 0;
80
81 /* Local functions */
82 static void draw_header(void)
83 {
84         move(0, 0);
85         attron(COLOR_PAIR(COLOR_TITLE));
86         for (int i = 0; i < N_ELEMENTS(views); i++) {
87                 if (i == ACTIVE)
88                         attron(A_BOLD);
89                 printw("%s ", views[i].name);
90                 if (i == ACTIVE)
91                         attroff(A_BOLD);
92         }
93         attroff(COLOR_PAIR(COLOR_TITLE));
94         if (!COMPACT)
95                 mvhline(1, 0, ACS_HLINE, COLS);
96         refresh();
97 }
98
99 static int get_color(const char *cat)
100 {
101         return cat == NULL           ? 0           :
102                match(cat, "class") ? COLOR_CLASS :
103                match(cat, "ec")    ? COLOR_EC    :
104                match(cat, "work")  ? COLOR_WORK  : COLOR_OTHER ;
105 }
106
107 /* Helper functions */
108 void event_box(WINDOW *win, event_t *event, int y, int x, int h, int w)
109 {
110         int l = 0;
111         int s = y < 0 ? -y-1 : 0;
112
113         int color = get_color(event->cat);
114
115         if (color) wattron(win, COLOR_PAIR(color));
116
117         if (h >= 2) mvwhline_set(win, y,     x+1,   WACS_T_HLINE, w-2);
118         if (h <= 1) mvwadd_wch(win,   y,     x,     WACS_BULLET);
119         if (h >= 2) mvwadd_wch(win,   y,     x,     WACS_T_ULCORNER);
120         if (h >= 2) mvwadd_wch(win,   y,     x+w-1, WACS_T_URCORNER);
121         if (h >= 3) mvwvline_set(win, y+1+s, x,     WACS_T_VLINE, h-2-s);
122         if (h >= 3) mvwvline_set(win, y+1+s, x+w-1, WACS_T_VLINE, h-2-s);
123         if (h >= 2) mvwadd_wch(win,   y+h-1, x,     WACS_T_LLCORNER);
124         if (h >= 2) mvwadd_wch(win,   y+h-1, x+w-1, WACS_T_LRCORNER);
125         if (h >= 2) mvwhline_set(win, y+h-1, x+1,   WACS_T_HLINE, w-2);
126
127         if (color) wattroff(win, COLOR_PAIR(color));
128
129         if (l<h && event->name) mvwprintw(win, y+l++, x+1, "%.*s",   w-2, event->name);
130         if (l<h && event->loc)  mvwprintw(win, y+l++, x+1, "@ %.*s", w-4, event->loc);
131         if (l<h && event->desc) mvwprintw(win, y+l++, x+1, "%.*s",   w-2, event->desc);
132 }
133
134 void event_line(WINDOW *win, event_t *event, int y, int x, int w, int full)
135 {
136         int color = get_color(event->cat);
137
138         if (color) wattron(win, COLOR_PAIR(color));
139         mvwaddch(win, y, x++, ACS_BLOCK);
140         if (color) wattroff(win, COLOR_PAIR(color));
141
142         if (full) {
143                 if (all_day(&event->start, &event->end))
144                         mvwprintw(win, y, x, " [all day]   -");
145                 else
146                         mvwprintw(win, y, x, " %2d:%02d-%2d:%02d -",
147                                         event->start.hour, event->start.min,
148                                         event->end.hour,   event->end.min);
149                 x += 15;
150         }
151         if (event->name) {
152                 const char *label = event->name ?: event->desc;
153                 mvwprintw(win, y, x, "%-*.*s", w-1, w-1, label);
154                 x += MIN(strlen(label), w-1);
155         }
156         if (full && event->loc) {
157                 mvwprintw(win, y, x, " @ %s", event->loc);
158         }
159 }
160
161 void todo_line(WINDOW *win, todo_t *todo, int y, int x, int w, int full)
162 {
163         char perc[16];
164         char desc[LINES];
165         sprintf(perc, "%2d%%", todo->status);
166
167         int cat    = get_color(todo->cat);
168         int status = todo->status == NEW  ? COLOR_NEW  :
169                      todo->status == DONE ? COLOR_DONE : COLOR_WIP;
170
171         sprintf(desc, "%s", todo->name ?: todo->desc ?: "");
172         strsub(desc, '\n', ';');
173
174         /* Print category */
175         if (cat) wattron(win, COLOR_PAIR(cat));
176         mvwaddch(win, y, x, ACS_BLOCK);
177         if (cat) wattroff(win, COLOR_PAIR(cat));
178         x += 2;
179
180         /* Print time */
181         if (no_date(&todo->due))
182                 mvwprintw(win, y, x, "[no due date]");
183         else
184                 mvwprintw(win, y, x, "%04d-%02d-%02d %2d:%02d",
185                                 todo->due.year, todo->due.month+1, todo->due.day+1,
186                                 todo->due.hour, todo->due.min);
187         x += 18;
188
189         /* Print status */
190         if (status) wattron(win, COLOR_PAIR(status));
191         mvwprintw(win, y, x, "%s",
192                 todo->status == NEW    ? "new"  :
193                 todo->status == DONE   ? "done" : perc);
194         if (status) wattroff(win, COLOR_PAIR(status));
195         x += 6;
196
197         /* Print description */
198         mvwprintw(win, y, x, "%s", desc);
199 }
200
201 /* Curses functions */
202 void wmvresize(WINDOW *win, int top, int left, int rows, int cols)
203 {
204         int y = getpary(win);
205         if (top < y)
206                 mvderwin(win, top, left);
207         wresize(win, rows, cols);
208         if (top > y)
209                 mvderwin(win, top, left);
210 }
211
212 void wshrink(WINDOW *win, int top)
213 {
214         int x    = getparx(win);
215         int y    = getpary(win);
216         int r    = getmaxy(win);
217         int c    = getmaxx(win);
218         int rows = r + (y - top);
219         if (top  <  y) mvderwin(win, top, x);
220         if (rows != r) wresize(win, rows, c);
221         if (top  >  y) mvderwin(win, top, x);
222 }
223
224 /* View init */
225 void view_init(void)
226 {
227         int hdr = COMPACT ? 1 : 2;
228         for (int i = 0; i < N_ELEMENTS(views); i++) {
229                 if (views[i].init) {
230                         views[i].win = newwin(LINES-hdr, COLS, hdr, 0);
231                         views[i].init(views[i].win);
232                 }
233                 if (views[i].size)
234                         views[i].size(LINES-hdr, COLS);
235         }
236 }
237
238 /* Config parser */
239 void view_config(const char *group, const char *name, const char *key, const char *value)
240 {
241         if (match(group, "view")) {
242                 if (match(key, "compact"))
243                         COMPACT = get_bool(value);
244                 else if (match(key, "active"))
245                         ACTIVE = get_enum(value, names, N_ELEMENTS(names));
246         }
247 }
248
249 /* View draw */
250 void view_resize(void)
251 {
252         int hdr = COMPACT ? 1 : 2;
253         for (int i = 0; i < N_ELEMENTS(views); i++) {
254                 if (views[i].win) {
255                         wresize(views[i].win, LINES-hdr, COLS);
256                         mvwin(views[i].win, hdr, 0);
257                 }
258                 if (views[i].size)
259                         views[i].size(LINES-hdr, COLS);
260         }
261 }
262
263 /* View draw */
264 void view_draw(void)
265 {
266         draw_header();
267         werase(views[ACTIVE].win);
268         views[ACTIVE].draw();
269         wrefresh(views[ACTIVE].win);
270 }
271
272 /* View set */
273 int view_set(int num)
274 {
275         if (ACTIVE != num) {
276                 ACTIVE = num;
277                 set_enum("view", 0, "active", ACTIVE,
278                                 names, N_ELEMENTS(names));
279                 view_draw();
280         }
281         return 1;
282 }
283
284 /* View run */
285 int view_run(int key, mmask_t btn, int row, int col)
286 {
287         /* Check for compact mode toggle */
288         if (key == 'c') {
289                 COMPACT ^= 1;
290                 set_bool("view", 0, "compact", COMPACT);
291                 view_resize();
292                 view_draw();
293                 return 1;
294         }
295
296         /* Check for mouse events */
297         if (key == KEY_MOUSE && row == 0) {
298                 int start = 1;
299                 for (int i = 0; i < N_ELEMENTS(views); i++) {
300                         int end = start + strlen(views[i].name) - 1;
301                         if (start <= col && col <= end && views[i].draw)
302                                 return view_set(i);
303                         start = end + 2;
304                 }
305         }
306
307         /* Check for view change */
308         for (int i = 0; i < N_ELEMENTS(views); i++) {
309                 if (i == ACTIVE)
310                         continue;
311                 for (int j = 0; j < N_ELEMENTS(views[i].keys); j++)
312                         if (views[i].keys[j] == key)
313                                 return view_set(i);
314         }
315
316         /* Shift windows */
317         int num   = ACTIVE;
318         int shift = key == KEY_RIGHT ? +1 :
319                     key == KEY_LEFT  ? -1 : 0;
320         while (shift) {
321                 num += shift;
322                 num += N_ELEMENTS(views);
323                 num %= N_ELEMENTS(views);
324                 if (views[num].run)
325                         return view_set(num);
326         }
327
328         /* Pass key to active view */
329         return views[ACTIVE].run(key, btn, row, col);
330 }