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