]> Pileus Git - lackey/blob - view/week.c
2b005d8731e573ba7ec0528010aef235a72e5c5c
[lackey] / view / week.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 "event.h"
26 #include "screen.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 /* Local functions */
36 static void print_event(event_t *event, wday_t day, hour_t hour, min_t min, float hstep)
37 {
38         int x = ROUND(day*hstep);
39         int y = hour*4 + min/15 - line;
40         int w = ROUND((day+1)*hstep) - 1 - x;
41         int h = (get_mins(&event->start, &event->end)-1)/15+1;
42         int l = 0;
43         int s = y < 0 ? -y-1 : 0;
44
45         int color = event->cat == NULL           ? 0           :
46                     !strcmp(event->cat, "class") ? COLOR_CLASS :
47                     !strcmp(event->cat, "ec")    ? COLOR_EC    :
48                     !strcmp(event->cat, "work")  ? COLOR_WORK  : COLOR_OTHER ;
49
50         if (color) wattron(body, COLOR_PAIR(color));
51
52         if (h <= 1) mvwadd_wch(body,   y,     x, WACS_BULLET);
53         if (h >= 2) mvwadd_wch(body,   y,     x, WACS_T_ULCORNER);
54         if (h >= 3) mvwvline_set(body, y+1+s, x, WACS_T_VLINE, h-2-s);
55         if (h >= 2) mvwadd_wch(body,   y+h-1, x, WACS_T_LLCORNER);
56
57         if (color) wattroff(body, COLOR_PAIR(color));
58
59         if (l<h && event->name) mvwprintw(body, y+l++, x+1, "%-*.*s",   w-1, w-1, event->name);
60         if (l<h && event->loc)  mvwprintw(body, y+l++, x+1, "@ %-*.*s", w-3, w-3, event->loc);
61         if (l<h && event->desc) mvwprintw(body, y+l++, x+1, "%-*.*s",   w-1, w-1, event->desc);
62
63         debug("week: event = %s\n", event->name);
64 }
65
66 static int before(date_t *start, int year, int month, int day, int hour, int min)
67 {
68         int rval = start->year  < year  ? 1 : start->year  > year ? 0 :
69                    start->month < month ? 1 : start->month > month? 0 :
70                    start->day   < day   ? 1 : start->day   > day  ? 0 :
71                    start->hour  < hour  ? 1 : start->hour  > hour ? 0 :
72                    start->min   < min   ? 1 : start->min   > min  ? 0 : 0;
73         debug("week: %04d-%02d-%02d %02d:%02d < %04d-%02d-%02d %02d:%02d == %d\n",
74                         start->year, start->month, start->day, start->hour, start->min,
75                         year, month, day, hour, min, rval);
76         return rval;
77 }
78
79 /* Week init */
80 void week_init(WINDOW *_win)
81 {
82         win   = _win; //  lines  cols  y  x
83         head  = derwin(win,         2, COLS,   0, 0);
84         times = derwin(win, LINES-2-3,      5, 3, 0);
85         body  = derwin(win, LINES-2-3, COLS-6, 3, 6);
86         line  = 10*4; // 10:00
87 }
88
89 /* Week size */
90 void week_size(int rows, int cols)
91 {
92         wresize(head,       2, cols  );
93         wresize(times, rows-3,      5);
94         wresize(body,  rows-3, cols-6);
95 }
96
97 /* Week draw */
98 void week_draw(void)
99 {
100         int x = 6;
101         int y = 3;
102         const float hstep = (float)(COLS-x)/7.0;
103
104         /* Clear */
105         werase(win);
106
107         /* Get start of week */
108         year_t  year  = YEAR;
109         month_t month = MONTH;
110         day_t   day   = DAY;
111         int shift = day_of_week(year, month, day);
112         add_days(&year, &month, &day, -shift);
113
114         /* Print Header */
115         mvwprintw(head, 0, 0, "%s",   month_to_str(MONTH));
116         mvwprintw(head, 1, 0, "%04d", YEAR);
117         for (int d = 0; d < 7; d++) {
118                 const char *str = hstep >= 10 ? day_to_string(d) : day_to_str(d);
119                 if (d == shift) wattron(head, A_BOLD);
120                 mvwprintw(head, 0, x+ROUND(d*hstep), "%s", str);
121                 mvwprintw(head, 1, x+ROUND(d*hstep), "%02d/%02d", month+1, day+1);
122                 if (d == shift) wattroff(head, A_BOLD);
123                 add_days(&year, &month, &day, 1);
124         }
125
126         /* Print times */
127         mvwprintw(times, 0, 0, "%02d:%02d", ((line/4)-1)%12+1, (line*15)%60);
128         for (int h = 0; h < 24; h++)
129                 mvwprintw(times, h*4-line, 0, "%02d:%02d", (h-1)%12+1, 0);
130
131         /* Print events */
132         event_t *event = EVENTS;
133         add_days(&year, &month, &day, -7);
134         for (int d = 0; d <  7; d++, add_days(&year,&month,&day,1))
135         for (int h = 0; h < 24; h++)
136         for (int m = 0; m < 60; m+=15)
137         while (event && before(&event->start,
138                         year, month, day, h+(m+15)/60, (m+15)%60)) {
139                 if (!before(&event->start, year, month, day, h, m))
140                         print_event(event, d, h, m, hstep);
141                 event = event->next;
142         }
143
144         /* Print lines */
145         mvwhline(win, y-1, 0, ACS_HLINE, COLS);
146         for (int d = 0; d < 7; d++)
147                 mvwvline(win, y, x+ROUND(d*hstep)-1, ACS_VLINE, LINES-y-2);
148
149         /* Draw today */
150         int l = x+ROUND((shift+0)*hstep)-1;
151         int r = x+ROUND((shift+1)*hstep)-1;
152         mvwhline    (win, y-1, l, ACS_BLOCK, r-l+1);
153         mvwvline_set(win, y,   l, WACS_T_VLINE, LINES-y-2);
154         mvwvline_set(win, y,   r, WACS_T_VLINE, LINES-y-2);
155 }
156
157 /* Week run */
158 int week_run(int key, mmask_t btn, int row, int col)
159 {
160         int days = 0, ref = 0;
161         switch (key)
162         {
163                 case 'h': ref = 1; days = -1; break;
164                 case 'l': ref = 1; days =  1; break;
165                 case 'i': ref = 1; days = -7; break;
166                 case 'o': ref = 1; days =  7; break;
167                 case 'k': ref = 1; line--;    break;
168                 case 'j': ref = 1; line++;    break;
169         }
170         line = CLAMP(line, 0, 24*4);
171         if (days)
172                 add_days(&YEAR, &MONTH, &DAY, days);
173         if (ref) {
174                 week_draw();
175                 wrefresh(win);
176         }
177         return 0;
178 }