]> Pileus Git - lackey/blob - src/view/month.c
Add day and week view
[lackey] / src / view / month.c
1 #include <string.h>
2 #include <ncurses.h>
3
4 #include "main.h"
5 #include "util.h"
6
7 /* Static data */
8 static WINDOW *win;
9
10 /* Month init */
11 void month_init(WINDOW *_win)
12 {
13         win = _win;
14 }
15
16 /* Month draw */
17 void month_draw(void)
18 {
19         const char *name  = month_to_string(MONTH);
20         const int   start = start_of_month(YEAR, MONTH);
21         const int   days  = days_in_month(YEAR, MONTH);
22         const int   weeks = weeks_in_month(YEAR, MONTH);
23         const float midpt = (float)COLS/2.0 - (strlen(name) + 1 + 4)/2.0;
24         const float hstep = (float)COLS/7.0;
25         const float vstep = (float)(LINES-4)/weeks;
26
27         /* Print Header */
28         mvwprintw(win, 0, midpt, "%s %d", name, YEAR);
29         for (int d = 0; d < 7; d++) {
30                 const char *str = hstep >= 10 ? day_to_string(d+SUN) : day_to_str(d+SUN);
31                 mvwprintw(win, 1, ROUND(d*hstep), "%s", str);
32         }
33         mvwhline(win, 2, 0, ACS_HLINE, COLS);
34
35         /* Print days */
36         for (int d = 0; d < days; d++) {
37                 int row = (start + d) / 7;
38                 int col = (start + d) % 7;
39                 mvwprintw(win, ROUND(3+row*vstep), ROUND(col*hstep), "%d", d+1);
40         }
41
42         /* Print lines */
43         for (int w = 1; w < weeks; w++)
44                 mvwhline(win, ROUND(2+w*vstep), 0, ACS_HLINE, COLS);
45         for (int d = 1; d < 7; d++) {
46                 int top = d >=  start         ? 0     : 1;
47                 int bot = d <= (start+days)%7 ? weeks : weeks-1;
48                 mvwvline(win, ROUND(3+top*vstep), ROUND(d*hstep-1),
49                                 ACS_VLINE, (bot-top)*vstep);
50                 for (int w = 1; w < weeks; w++) {
51                         int chr = w == top ? ACS_TTEE :
52                                   w == bot ? ACS_BTEE : ACS_PLUS;
53                         mvwaddch(win, ROUND(2+w*vstep), ROUND(d*hstep-1), chr);
54                 }
55         }
56 }
57
58 /* Month run */
59 int month_run(int key, mmask_t btn, int row, int col)
60 {
61         return 0;
62 }