]> Pileus Git - lackey/blob - views/todo.c
3f2b634a0ed904bda0f8bf33bd5d97448c4a56b0
[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);
69         mvwprintw(win, row, 0, "%s", label);
70         wattroff(win, A_BOLD);
71
72         /* Todos */
73         if (show)
74                 n = print_todos(win, row+1, todos, low, high);
75
76         /* Status */
77         if (!show)
78                 mvwprintw(win, row+1, 4, "[hidden]");
79         if (n == 0)
80                 mvwprintw(win, row+1, 4, "[no tasks]");
81
82         return row+1+MAX(n,1)+1;
83 }
84
85 /* Todo init */
86 void todo_init(WINDOW *_win)
87 {
88         win = _win;
89 }
90
91 /* Todo size */
92 void todo_size(int rows, int cols)
93 {
94 }
95
96 /* Todo draw */
97 void todo_draw(void)
98 {
99         int row = -line;
100
101         TODO = 0;
102         items = 0;
103
104         row = print_group(win, row, TODOS,
105                 show_new, "New Tasks", NEW, NEW);
106
107         row = print_group(win, row, TODOS,
108                 show_started, "Started Tasks", NEW+1, DONE-1);
109
110         row = print_group(win, row, TODOS,
111                 show_finished, "Finished Tasks", DONE, DONE);
112
113         rows = row+line-1;
114 }
115
116 /* Todo run */
117 int todo_run(int key, mmask_t btn, int row, int col)
118 {
119         int scroll = 0, move = 0, ref = 0;
120         switch (key)
121         {
122                 case 'g':    ref = 1; scroll = -line;     break;
123                 case 'G':    ref = 1; scroll =  rows;     break;
124                 case '\005': ref = 1; scroll =  1;        break; // ctrl-e
125                 case '\031': ref = 1; scroll = -1;        break; // ctrl-y
126                 case 'j':    ref = 1; move   =  1;        break;
127                 case 'k':    ref = 1; move   = -1;        break;
128                 case 'n':    ref = 1; show_new      ^= 1; break;
129                 case 's':    ref = 1; show_started  ^= 1; break;
130                 case 'f':    ref = 1; show_finished ^= 1; break;
131                 case '\012': // enter
132                         view_edit(EDIT_TODO);
133                         return 1;
134         }
135
136         /* Move more if we're on the edge of a group */
137         int extra = 0;
138         if (move < 0 && first) extra = -2;
139         if (move > 0 && last)  extra =  2;
140
141         /* Scroll window when we move off screen */
142         int next = line + srow + move + extra;
143         int ymax = getmaxy(win)-1;
144         while (next-line < 0)    line--;
145         while (next-line > ymax) line++;
146
147         /* Update line and cursor positions */
148         line   = CLAMP(line+scroll, 0, rows-1);
149         cursor = CLAMP(cursor+move, 0, items-1);
150
151         /* Repaint */
152         if (ref) {
153                 werase(win);
154                 todo_draw();
155                 wrefresh(win);
156         }
157         return ref;
158 }