]> Pileus Git - lackey/blob - views/day.c
Add compact mode and view config data
[lackey] / views / day.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 "date.h"
25 #include "cal.h"
26 #include "view.h"
27
28 /* Static data */
29 static int     line;
30 static WINDOW *win;
31 static WINDOW *head;
32 static WINDOW *times;
33 static WINDOW *body;
34
35 /* Box packing helpers */
36 static void clear_old(event_t **list, int n, event_t *cur)
37 {
38         for (int i = 0; i < n; i++)
39                 if (list[i] && compare(&list[i]->end, &cur->start) <= 0)
40                         list[i] = NULL;
41 }
42
43 static int next_free(event_t **list, int n)
44 {
45         for (int i = 0; i < n; i++)
46                 if (!list[i])
47                         return i;
48         return n-1;
49 }
50
51 static int count_busy(event_t **list, int n)
52 {
53         int sum = 0;
54         for (int i = 0; i < n; i++)
55                 if (list[i])
56                         sum++;
57         return sum;
58 }
59
60 static int get_ncols(event_t *event, int n)
61 {
62         int ncols = 0;
63         event_t *preview[n];
64         memset(preview, 0, sizeof(preview));
65         for (event_t *cur = event; cur; cur = cur->next) {
66                 int col = next_free(preview, n);
67                 preview[col] = cur;
68                 ncols = MAX(ncols, col+1);
69                 if (cur->next)
70                         clear_old(preview, n, cur->next);
71                 if (count_busy(preview, n) == 0)
72                         break;
73         }
74         return ncols;
75 }
76
77 static int get_col(event_t **list, int n, event_t *event, int *ncols)
78 {
79         clear_old(list, n, event);
80
81         /* If there are current events, then recalculate
82          * ncols for the next series of events */
83         if (count_busy(list, n) == 0)
84                 *ncols = get_ncols(event, n);
85
86         /* Find next open slot */
87         int col = next_free(list, n);
88         list[col] = event;
89         return col;
90 }
91
92 /* Day init */
93 void day_init(WINDOW *_win)
94 {
95         win   = _win; //    lines      cols    y  x
96         head  = derwin(win,         1, COLS,   0, 0);
97         times = derwin(win, LINES-2-2,      5, 2, 0);
98         body  = derwin(win, LINES-2-2, COLS-6, 2, 6);
99         line  = 10*4; // 10:00
100 }
101
102 /* Day size */
103 void day_size(int rows, int cols)
104 {
105         mvderwin(times, 2-COMPACT, 0);
106         mvderwin(body,  2-COMPACT, 6);
107         wresize(head,   1,         cols);
108         wresize(times,  rows-2-COMPACT,      5);
109         wresize(body,   rows-2-COMPACT, cols-6);
110 }
111
112 /* Day draw */
113 void day_draw(void)
114 {
115         const char *mstr = month_to_string(MONTH);
116         const char *dstr = day_to_string(day_of_week(YEAR, MONTH, DAY));
117
118         /* Print Header */
119         if (COMPACT) wattron(head, A_REVERSE | A_BOLD);
120         mvwprintw(head, 0, 0, "%s, %s %-*d", dstr, mstr, COLS, DAY+1);
121         mvwprintw(head, 0, COLS-10, "%d-%02d-%02d", YEAR, MONTH, DAY+1);
122         if (COMPACT) wattroff(head, A_REVERSE | A_BOLD);
123
124         /* Print times */
125         mvwprintw(times, 0, 0, "%02d:%02d", ((line/4)-1)%12+1, (line*15)%60);
126         for (int h = 0; h < 24; h++)
127                 mvwprintw(times, h*4-line, 0, "%02d:%02d", (h-1)%12+1, 0);
128
129         /* Print events */
130         event_t *event = EVENTS;
131         event_t *active[10] = {};
132         int ncols = 0;
133         for (int h = 0; h < 24; h++)
134         for (int m = 0; m < 60; m+=15)
135         while (event && before(&event->start,
136                         YEAR, MONTH, DAY, h+(m+15)/60, (m+15)%60)) {
137                 if (!before(&event->start, YEAR, MONTH, DAY, h, m)) {
138                         int col    = get_col(active, N_ELEMENTS(active), event, &ncols);
139                         int left   = ROUND((col+0.0)*(COLS-6)/ncols);
140                         int right  = ROUND((col+1.0)*(COLS-6)/ncols);
141                         int row    = h*4 + m/15 - line;
142                         int height = (get_mins(&event->start, &event->end)-1)/15+1;
143                         event_box(body, event, row, left, height, right-left);
144                 }
145                 event = event->next;
146         }
147
148         /* Print lines */
149         if (!COMPACT)
150                 mvwhline(win, 1, 0, ACS_HLINE, COLS);
151         mvwvline(win, 2-COMPACT, 5, ACS_VLINE, LINES-4+COMPACT+COMPACT);
152 }
153
154 /* Day run */
155 int day_run(int key, mmask_t btn, int row, int col)
156 {
157         int days = 0, ref = 0;
158         switch (key)
159         {
160                 case 'h': ref = 1; days = -1; break;
161                 case 'l': ref = 1; days =  1; break;
162                 case 'i': ref = 1; days = -7; break;
163                 case 'o': ref = 1; days =  7; break;
164                 case 'k': ref = 1; line--;    break;
165                 case 'j': ref = 1; line++;    break;
166         }
167         line = CLAMP(line, 0, 24*4);
168         if (days)
169                 add_days(&YEAR, &MONTH, &DAY, days);
170         if (ref) {
171                 werase(win);
172                 day_draw();
173                 wrefresh(win);
174         }
175         return ref;
176 }