]> Pileus Git - lackey/blob - views/todo.c
Add option to show and hide weekends.
[lackey] / views / todo.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 #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     line;   // scroll offset
29 static int     rows;   // number of rows
30
31 static int     srow;   // selected row 
32 static int     cursor; // index of the selected row
33 static int     items;  // number of items displayed
34 static int     first;  // cursor on first item in group
35 static int     last;   // cursor on last item in group
36
37 static int show_new      = 1;
38 static int show_started  = 1;
39 static int show_finished = 0;
40
41 /* Helper functions */
42 static int print_todos(WINDOW *win, int row, todo_t *todos, status_t low, status_t high)
43 {
44         int n = 0, found = 0;
45         for (todo_t *cur = todos; cur; cur = cur->next) {
46                 if (low <= cur->status && cur->status <= high) {
47                         if (found)
48                                 last = 0;
49                         if (items++ == cursor) {
50                                 found = 1;
51                                 first = n == 0;
52                                 last  = 1;
53                                 TODO  = cur;
54                                 srow  = row+n;
55                         }
56                         todo_line(win, cur, row+n++, 4, COLS-4, SHOW_DETAILS | SHOW_ACTIVE);
57                 }
58         }
59         return n;
60 }
61
62 static int print_group(WINDOW *win, int row, todo_t *todos,
63                 int show, const char *label, status_t low, status_t high)
64 {
65         int n = 1;
66
67         /* Label */
68         wattron(win, A_BOLD | A_UNDERLINE);
69         mvwprintw(win, row, 0, "%c",  label[0]);
70         wattroff(win, A_UNDERLINE);
71         mvwprintw(win, row, 1, "%s", &label[1]);
72         wattroff(win, A_BOLD);
73
74         /* Todos */
75         if (show)
76                 n = print_todos(win, row+1, todos, low, high);
77
78         /* Status */
79         if (!show)
80                 mvwprintw(win, row+1, 4, "[hidden]");
81         if (n == 0)
82                 mvwprintw(win, row+1, 4, "[no tasks]");
83
84         return row+1+MAX(n,1)+1;
85 }
86
87 /* Todo init */
88 void todo_init(WINDOW *_win)
89 {
90         win = _win;
91 }
92
93 /* Todo size */
94 void todo_size(int rows, int cols)
95 {
96 }
97
98 /* Todo draw */
99 void todo_draw(void)
100 {
101         int row = -line;
102
103         TODO = 0;
104         items = 0;
105
106         row = print_group(win, row, TODOS,
107                 show_new, "New Tasks", NEW, NEW);
108
109         row = print_group(win, row, TODOS,
110                 show_started, "Started Tasks", NEW+1, DONE-1);
111
112         row = print_group(win, row, TODOS,
113                 show_finished, "Finished Tasks", DONE, DONE);
114
115         rows = row+line-1;
116 }
117
118 /* Todo run */
119 int todo_run(int key, mmask_t btn, int row, int col)
120 {
121         int scroll = 0, move = 0, ref = 0;
122         switch (key)
123         {
124                 case 'g':    ref = 1; scroll = -line;     break;
125                 case 'G':    ref = 1; scroll =  rows;     break;
126                 case '\005': ref = 1; scroll =  1;        break; // ctrl-e
127                 case '\031': ref = 1; scroll = -1;        break; // ctrl-y
128                 case 'j':    ref = 1; move   =  1;        break;
129                 case 'k':    ref = 1; move   = -1;        break;
130                 case 'n':    ref = 1; show_new      ^= 1; break;
131                 case 's':    ref = 1; show_started  ^= 1; break;
132                 case 'f':    ref = 1; show_finished ^= 1; break;
133                 case 'e':    view_edit(EDIT_TODO); return 1;
134                 case '\012': view_edit(EDIT_TODO); return 1; // enter
135         }
136
137         /* Move more if we're on the edge of a group */
138         int extra = 0;
139         if (move < 0 && first) extra = -2;
140         if (move > 0 && last)  extra =  2;
141
142         /* Scroll window when we move off screen */
143         int next = line + srow + move + extra;
144         int ymax = getmaxy(win)-1;
145         while (next-line < 0)    line--;
146         while (next-line > ymax) line++;
147
148         /* Update line and cursor positions */
149         line   = CLAMP(line+scroll, 0, rows-1);
150         cursor = CLAMP(cursor+move, 0, items-1);
151
152         /* Repaint */
153         if (ref) {
154                 werase(win);
155                 todo_draw();
156                 wrefresh(win);
157         }
158         return ref;
159 }