From: Andy Spencer Date: Sat, 6 Oct 2012 08:01:38 +0000 (+0000) Subject: Make month view interactive X-Git-Url: http://pileus.org/git/?p=lackey;a=commitdiff_plain;h=6d8ce73551b4e8f7a0f2556eba525018f4cf684e Make month view interactive --- diff --git a/src/main.c b/src/main.c index d1a1fce..20bea55 100644 --- a/src/main.c +++ b/src/main.c @@ -19,15 +19,16 @@ #include #include #include +#include #include #include "main.h" #include "screen.h" /* Debugging */ -int YEAR = 2012; -int MONTH = 8; -int DAY = 29; +year_t YEAR = 2012; +month_t MONTH = 8; +day_t DAY = 29; /* Static data */ static FILE *debug_fd = NULL; @@ -78,6 +79,7 @@ int main(int argc, char **argv) DAY = tm->tm_mday-1; /* Curses setup */ + setlocale(LC_ALL, ""); initscr(); cbreak(); noecho(); @@ -108,9 +110,8 @@ int main(int argc, char **argv) screen_resize(); screen_draw(); continue; - case 'L': + case '\14': clear(); - case 'l': case '\7': screen_draw(); continue; diff --git a/src/main.h b/src/main.h index dd7c3fb..d1d4511 100644 --- a/src/main.h +++ b/src/main.h @@ -15,13 +15,20 @@ * along with this program. If not, see . */ +#ifndef MAIN_H +#define MAIN_H + #define COLOR_TITLE 1 #define COLOR_ERROR 2 +#include "util.h" + /* Debugging */ -extern int YEAR; -extern int MONTH; -extern int DAY; +extern year_t YEAR; +extern month_t MONTH; +extern day_t DAY; /* Debug functions */ int debug(char *fmt, ...); + +#endif diff --git a/src/screen.c b/src/screen.c index e10a54d..feb23c6 100644 --- a/src/screen.c +++ b/src/screen.c @@ -33,16 +33,16 @@ typedef struct { /* Data */ view_t views[] = { - { "Day", day_init, day_draw, day_run, {KEY_F(1), '1', 'd', } }, - { "Week", week_init, week_draw, week_run, {KEY_F(2), '2', 'w', } }, - { "Month", month_init, month_draw, month_run, {KEY_F(3), '3', 'm', } }, - { "Year", year_init, year_draw, year_run, {KEY_F(4), '4', 'y', } }, - { "|", NULL, NULL, NULL, { } }, - { "Todo", todo_init, todo_draw, todo_run, {KEY_F(5), '5', 't', } }, - { "Notes", notes_init, notes_draw, notes_run, {KEY_F(6), '6', 'n', } }, - { "|", NULL, NULL, NULL, { } }, - { "Settings", settings_init, settings_draw, settings_run, {KEY_F(7), '7', 's', } }, - { "Help", help_init, help_draw, help_run, {KEY_F(8), '8', 'h', '?'} }, + { "Day", day_init, day_draw, day_run, {KEY_F(1), '1', } }, + { "Week", week_init, week_draw, week_run, {KEY_F(2), '2', } }, + { "Month", month_init, month_draw, month_run, {KEY_F(3), '3', } }, + { "Year", year_init, year_draw, year_run, {KEY_F(4), '4', } }, + { "|", NULL, NULL, NULL, { } }, + { "Todo", todo_init, todo_draw, todo_run, {KEY_F(5), '5', } }, + { "Notes", notes_init, notes_draw, notes_run, {KEY_F(6), '6', } }, + { "|", NULL, NULL, NULL, { } }, + { "Settings", settings_init, settings_draw, settings_run, {KEY_F(7), '7', } }, + { "Help", help_init, help_draw, help_run, {KEY_F(8), '8', '?'} }, }; int active = 0; diff --git a/src/screen.h b/src/screen.h index 0235c82..f5b8671 100644 --- a/src/screen.h +++ b/src/screen.h @@ -15,6 +15,9 @@ * along with this program. If not, see . */ +#ifndef SCREEN_H +#define SCREEN_H + /* Screen functions */ void screen_init(void); void screen_resize(void); @@ -50,3 +53,5 @@ int todo_run(int,mmask_t,int,int); int notes_run(int,mmask_t,int,int); int settings_run(int,mmask_t,int,int); int help_run(int,mmask_t,int,int); + +#endif diff --git a/src/util.c b/src/util.c index 688cc94..e40fa8e 100644 --- a/src/util.c +++ b/src/util.c @@ -83,6 +83,13 @@ void add_days(year_t *year, month_t *month, day_t *day, int days) *day = tm->tm_mday-1; } +void add_months(year_t *year, month_t *month, int months) +{ + int total = *year*12 + *month + months; + *year = total / 12; + *month = total % 12; +} + /* Debug functions */ const char *month_to_str(month_t month) { @@ -123,14 +130,15 @@ const char *day_to_string(wday_t day) /* Test functions */ void test_time(void) { - printf("Year Month Start Weeks Days\n"); + printf("Info\n"); + printf(" Year Month Start Weeks Days\n"); for (int y = 2012; y <= 2012; y++) for (int m = JAN; m <= DEC; m++) { - printf("%-5d", y); - printf("%-10s", month_to_string(m)); - printf("%-6s", day_to_str(start_of_month(y,m))); - printf("%-6d", weeks_in_month(y,m)); - printf("%-2d", days_in_month(y,m)); + printf(" %-5d", y); + printf(" %-10s", month_to_string(m)); + printf(" %-6s", day_to_str(start_of_month(y,m))); + printf(" %-6d", weeks_in_month(y,m)); + printf(" %-2d", days_in_month(y,m)); printf("\n"); } } diff --git a/src/util.h b/src/util.h index e814dfd..f073205 100644 --- a/src/util.h +++ b/src/util.h @@ -15,6 +15,9 @@ * along with this program. If not, see . */ +#ifndef UTIL_H +#define UTIL_H + /* Macros */ #define MIN(a,b) ((a) < (b) ? (a) : (b)) #define MAX(a,b) ((a) > (b) ? (a) : (b)) @@ -59,6 +62,7 @@ wday_t day_of_week(year_t year, month_t month, day_t day); wday_t start_of_month(year_t year, month_t month); day_t start_of_week(year_t year, month_t month, day_t day); void add_days(year_t *year, month_t *month, day_t *day, int days); +void add_months(year_t *year, month_t *month, int months); /* Time to string functions */ const char *month_to_str(month_t month); @@ -69,3 +73,5 @@ const char *day_to_string(wday_t day); /* Tests */ void test_time(void); + +#endif diff --git a/view/month.c b/view/month.c index fb2be06..f93bc74 100644 --- a/view/month.c +++ b/view/month.c @@ -15,6 +15,8 @@ * along with this program. If not, see . */ +#define _XOPEN_SOURCE_EXTENDED + #include #include @@ -41,6 +43,9 @@ void month_draw(void) const float hstep = (float)COLS/7.0; const float vstep = (float)(LINES-4)/weeks; + /* Clear */ + werase(win); + /* Print Header */ mvwprintw(win, 0, midpt, "%s %d", name, YEAR); for (int d = 0; d < 7; d++) { @@ -70,10 +75,42 @@ void month_draw(void) mvwaddch(win, ROUND(2+w*vstep), ROUND(d*hstep-1), chr); } } + + /* Draw today */ + int col = day_of_week(YEAR, MONTH, DAY); + int row = (start+DAY) / 7; + int l = ROUND((col+0)*hstep-1); + int r = ROUND((col+1)*hstep-1); + int t = ROUND((row+0)*vstep+2); + int b = ROUND((row+1)*vstep+2); + mvwvline_set(win, t, l, WACS_T_VLINE, b-t); + mvwvline_set(win, t, r, WACS_T_VLINE, b-t); + mvwhline_set(win, t, l, WACS_T_HLINE, r-l); + mvwhline_set(win, b, l, WACS_T_HLINE, r-l); + mvwadd_wch(win, t, l, WACS_T_ULCORNER); + mvwadd_wch(win, t, r, WACS_T_URCORNER); + mvwadd_wch(win, b, l, WACS_T_LLCORNER); + mvwadd_wch(win, b, r, WACS_T_LRCORNER); } /* Month run */ int month_run(int key, mmask_t btn, int row, int col) { + int days = 0, months = 0; + switch (key) + { + case 'h': days = -1; break; + case 'j': days = 7; break; + case 'k': days = -7; break; + case 'l': days = 1; break; + case 'i': months = -1; break; + case 'o': months = 1; break; + } + if (days || months) { + add_days(&YEAR, &MONTH, &DAY, days); + add_months(&YEAR, &MONTH, months); + month_draw(); + wrefresh(win); + } return 0; }