]> Pileus Git - lackey/blob - views/todo.c
Add todo view
[lackey] / views / todo.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 #include <ncurses.h>
19
20 #include "util.h"
21 #include "date.h"
22 #include "cal.h"
23 #include "view.h"
24
25 /* Static data */
26 static WINDOW *win;
27
28 static int show_new      = 1;
29 static int show_started  = 1;
30 static int show_finished = 1;
31
32 /* Helper functions */
33 static int print_todos(WINDOW *win, int y, todo_t *todos, status_t low, status_t high)
34 {
35         int n = 0;
36         for (todo_t *cur = todos; cur; cur = cur->next)
37                 if (low <= cur->status && cur->status <= high)
38                         todo_line(win, cur, y+n++, 2, COLS-2, 1);
39         return n;
40 }
41
42 static int print_group(WINDOW *win, int y, todo_t *todos,
43                 int show, const char *label, status_t low, status_t high)
44 {
45         int n = 1;
46
47         /* Label */
48         mvwprintw(win, y, 0, "%s", label);
49
50         /* Todos */
51         if (show)
52                 n = print_todos(win, y+1, todos, low, high);
53
54         /* Status */
55         if (!show)
56                 mvwprintw(win, y+1, 4, "[hidden]");
57         if (n == 0)
58                 mvwprintw(win, y+1, 4, "[no tasks]");
59
60         return y+1+MAX(n,1)+1;
61 }
62
63 /* Todo init */
64 void todo_init(WINDOW *_win)
65 {
66         win = _win;
67 }
68
69 /* Todo size */
70 void todo_size(int rows, int cols)
71 {
72 }
73
74 /* Todo draw */
75 void todo_draw(void)
76 {
77         int y = 0;
78
79         y = print_group(win, y, TODOS,
80                 show_new, "New Tasks", NEW, NEW);
81
82         y = print_group(win, y, TODOS,
83                 show_started, "Started Tasks", NEW+1, DONE-1);
84
85         y = print_group(win, y, TODOS,
86                 show_finished, "Finished Tasks", DONE, DONE);
87 }
88
89 /* Todo run */
90 int todo_run(int key, mmask_t btn, int row, int col)
91 {
92         int ref = 0;
93         switch (key)
94         {
95                 case 'n': ref = 1; show_new      ^= 1; break;
96                 case 's': ref = 1; show_started  ^= 1; break;
97                 case 'f': ref = 1; show_finished ^= 1; break;
98         }
99         if (ref) {
100                 werase(win);
101                 todo_draw();
102                 wrefresh(win);
103         }
104         return 0;
105 }