From: Andy Spencer Date: Fri, 5 Oct 2012 04:58:02 +0000 (+0000) Subject: Add day and week view X-Git-Url: http://pileus.org/git/?p=lackey;a=commitdiff_plain;h=a5916a4d929af1ba7835c08966121cf2afa81668 Add day and week view --- diff --git a/.gitignore b/.gitignore index a85852f..b2aac9a 100644 --- a/.gitignore +++ b/.gitignore @@ -4,5 +4,6 @@ *~ .vimrc acal +config.mk test tags diff --git a/src/config.mk.example b/src/config.mk.example new file mode 100644 index 0000000..b323a2f --- /dev/null +++ b/src/config.mk.example @@ -0,0 +1 @@ +default: run-acal diff --git a/src/main.c b/src/main.c index 4161b03..146e55e 100644 --- a/src/main.c +++ b/src/main.c @@ -32,6 +32,10 @@ int debug(char *fmt, ...) int rval; va_list ap; + /* Open log file */ + if (!debug_fd) + debug_fd = fopen("acal.log", "w+"); + /* Log to debug file */ va_start(ap, fmt); vfprintf(debug_fd, "debug: ", ap); @@ -54,7 +58,6 @@ int debug(char *fmt, ...) int main(int argc, char **argv) { /* Misc setup */ - debug_fd = fopen("acal.log", "w+"); struct sigaction act; sigemptyset(&act.sa_mask); act.sa_flags = 0; diff --git a/src/main.h b/src/main.h index bed6fb1..9f2d6ac 100644 --- a/src/main.h +++ b/src/main.h @@ -1,6 +1,10 @@ #define COLOR_TITLE 1 #define COLOR_ERROR 2 -#define N_ELEMENTS(x) (sizeof(x)/sizeof((x)[0])) +/* Debugging */ +const static int YEAR = 2012; +const static int MONTH = 8; +const static int DAY = 29; +/* Debug functions */ int debug(char *fmt, ...); diff --git a/src/makefile b/src/makefile index afa1115..0b6c98f 100644 --- a/src/makefile +++ b/src/makefile @@ -1,17 +1,17 @@ -# Settings -CC = gcc -CFLAGS = -Wall --std=c99 -CPPFLAGS = -I. -LDFLAGS = -lncursesw -PROG = acal -TEST = test - -# Views -SOURCES = main screen util -TESTS = test util -VIEWS = day week month year todo notes settings help +-include config.mk -default: run-$(PROG) +# Settings +CC ?= gcc +CFLAGS ?= -Wall --std=c99 +CPPFLAGS ?= -Isrc +LDFLAGS ?= -lncursesw + +# Sources +PROG = acal +TEST = test +SOURCES = main screen util +TESTS = test util +VIEWS = day week month year todo notes settings help # Targets all: $(PROG) $(TEST) @@ -24,13 +24,13 @@ run-$(TEST): $(TEST) ./$< clean: - rm -f *.o view/*.o $(PROG) $(TEST) + rm -f src/*.o view/*.o $(PROG) $(TEST) # Rules -$(PROG): $(SOURCES:%=%.o) $(VIEWS:%=view/%.o) +$(PROG): $(SOURCES:%=src/%.o) $(VIEWS:%=view/%.o) $(CC) $(CLFAGS) -o $@ $+ $(LDFLAGS) -$(TEST): $(TESTS:%=%.o) $(VIEWS:%=view/%.o) +$(TEST): $(TESTS:%=src/%.o) $(VIEWS:%=view/%.o) $(CC) $(CLFAGS) -o $@ $+ $(LDFLAGS) %.o: %.c $(SOURCES:%=%.h) makefile diff --git a/src/screen.c b/src/screen.c index 1fdedc8..f9bfd4f 100644 --- a/src/screen.c +++ b/src/screen.c @@ -2,6 +2,7 @@ #include #include "main.h" #include "screen.h" +#include "util.h" /* Types */ typedef struct { @@ -27,7 +28,7 @@ view_t views[] = { { "Help", help_init, help_draw, help_run, {KEY_F(8), '8', 'h', '?'} }, }; -int active = 3; +int active = 0; /* Local functions */ void draw_header(void) diff --git a/src/util.c b/src/util.c index 55cd265..9f7bdfa 100644 --- a/src/util.c +++ b/src/util.c @@ -1,31 +1,18 @@ /* Time Keeping Bugs Abound! */ #include +#include #include "util.h" -/* Helper functions */ -static int is_leap_year(year_t year) +/* Time functions */ +int is_leap_year(year_t year) { return (year % 400 == 0) ? 1 : (year % 100 == 0) ? 0 : (year % 4 == 0) ? 1 : 0; } -static wday_t day_of_week(year_t year, month_t month, day_t day) -{ - static int tmp[] = {0, 3, 2, 5, 0, 3, - 5, 1, 4, 6, 2, 4}; - if (month < 3) - year--; - int start = year + year / 4 - - year / 100 - + year / 400 - + tmp[month]; - return (start + day) % 7; -} - -/* Time functions */ int days_in_year(year_t year) { return 365 + is_leap_year(year); @@ -47,18 +34,45 @@ int weeks_in_month(year_t year, month_t month) return ((start + days)-1) / 7 + 1; } +wday_t day_of_week(year_t year, month_t month, day_t day) +{ + static int tmp[] = {0, 3, 2, 5, 0, 3, + 5, 1, 4, 6, 2, 4}; + if (month < 3) + year--; + int start = year + year / 4 + - year / 100 + + year / 400 + + tmp[month]; + return (start + day) % 7; +} + wday_t start_of_month(year_t year, month_t month) { return day_of_week(year, month, 1); } +void add_days(year_t *year, month_t *month, day_t *day, int days) +{ + time_t time = mktime(&(struct tm){ + .tm_year = *year-1900, + .tm_mon = *month, + .tm_mday = *day+1, + .tm_hour = 12}); + time += days*24*60*60; + struct tm *tm = localtime(&time); + *year = tm->tm_year+1900; + *month = tm->tm_mon; + *day = tm->tm_mday-1; +} + /* Debug functions */ const char *month_to_str(month_t month) { static const char *map[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", }; - return map[month]; + return map[month%12]; } const char *month_to_string(month_t month) { @@ -66,27 +80,27 @@ const char *month_to_string(month_t month) { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; - return map[month]; + return map[month%12]; } const char *day_to_st(wday_t day) { static const char *map[] = { "Su","Mo", "Tu", "We", "Th", "Fr", "Sa" }; - return map[day]; + return map[day%7]; } const char *day_to_str(wday_t day) { static const char *map[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; - return map[day]; + return map[day%7]; } const char *day_to_string(wday_t day) { static const char *map[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; - return map[day]; + return map[day%7]; } /* Test functions */ diff --git a/src/util.h b/src/util.h index 57e7afc..497666c 100644 --- a/src/util.h +++ b/src/util.h @@ -1,4 +1,10 @@ -/* Types */ +/* Macros */ +#define MIN(a,b) ((a) < (b) ? (a) : (b)) +#define MAX(a,b) ((a) > (b) ? (a) : (b)) +#define ROUND(x) ((int)((x)+0.5)) +#define N_ELEMENTS(x) (sizeof(x)/sizeof((x)[0])) + +/* Time types */ typedef int year_t; typedef int day_t; @@ -28,12 +34,16 @@ typedef enum { } wday_t; /* Time functions */ +int is_leap_year(year_t year); int days_in_year(year_t year); int days_in_month(year_t year, month_t month); int weeks_in_month(year_t year, month_t month); +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); -/* String functions */ +/* Time to string functions */ const char *month_to_str(month_t month); const char *month_to_string(month_t month); const char *day_to_st(wday_t day); diff --git a/src/view/day.c b/src/view/day.c index a1763f8..ed5ee6f 100644 --- a/src/view/day.c +++ b/src/view/day.c @@ -1,5 +1,9 @@ +#include #include +#include "main.h" +#include "util.h" + /* Static data */ static WINDOW *win; @@ -12,8 +16,19 @@ void day_init(WINDOW *_win) /* Day draw */ void day_draw(void) { - mvwprintw(win, 0, 1, "%s\n", "day"); - wrefresh(win); + const char *mstr = month_to_string(MONTH); + const char *dstr = day_to_string(day_of_week(YEAR, MONTH, DAY)); + + /* Print Header */ + mvwprintw(win, 0, 0, "%s, %s %d", dstr, mstr, DAY); + mvwprintw(win, 0, COLS-10, "%d-%02d-%02d", YEAR, MONTH, DAY); + mvwhline(win, 1, 0, ACS_HLINE, COLS); + + /* Print times */ + int start = 8; + for (int h = 0; h < (LINES-5)/4+1; h++) + mvwprintw(win, 2+h*4, 0,"%02d:%02d", (start+h)%12, 0); + mvwvline(win, 2, 5, ACS_VLINE, LINES-4); } /* Day run */ diff --git a/src/view/month.c b/src/view/month.c index d5bb538..a7747d4 100644 --- a/src/view/month.c +++ b/src/view/month.c @@ -1,19 +1,12 @@ #include #include +#include "main.h" #include "util.h" -/* Macros */ -#define ROUND(x) ((int)((x)+0.5)) - /* Static data */ static WINDOW *win; -/* Test data */ -const static int YEAR = 2012; -const static int MONTH = SEP; -const static int DAY = 29; - /* Month init */ void month_init(WINDOW *_win) { @@ -33,8 +26,10 @@ void month_draw(void) /* Print Header */ mvwprintw(win, 0, midpt, "%s %d", name, YEAR); - for (int d = 0; d < 7; d++) - mvwprintw(win, 1, ROUND(d*hstep), "%s", day_to_str(d+SUN)); + for (int d = 0; d < 7; d++) { + const char *str = hstep >= 10 ? day_to_string(d+SUN) : day_to_str(d+SUN); + mvwprintw(win, 1, ROUND(d*hstep), "%s", str); + } mvwhline(win, 2, 0, ACS_HLINE, COLS); /* Print days */ diff --git a/src/view/week.c b/src/view/week.c index d885c8f..c2d3089 100644 --- a/src/view/week.c +++ b/src/view/week.c @@ -1,5 +1,9 @@ +#include #include +#include "main.h" +#include "util.h" + /* Static data */ static WINDOW *win; @@ -12,8 +16,36 @@ void week_init(WINDOW *_win) /* Week draw */ void week_draw(void) { - mvwprintw(win, 0, 1, "%s\n", "week"); - wrefresh(win); + int x = 6; + int y = 3; + const float hstep = (float)(COLS-x)/5.0; + + /* Get start of week */ + year_t year = YEAR; + month_t month = MONTH; + day_t day = DAY; + int shift = day_of_week(year, month, day); + add_days(&year, &month, &day, -shift+MON); + + /* Print Header */ + mvwprintw(win, 1, 0, "%s", month_to_str(MONTH)); + for (int d = 0; d < 5; d++) { + // FIXME.. + const char *str = hstep >= 10 ? day_to_string(d+MON) : day_to_str(d+MON); + mvwprintw(win, 0, x+ROUND(d*hstep), "%02d/%02d", month, day); + mvwprintw(win, 1, x+ROUND(d*hstep), "%s", str); + add_days(&year, &month, &day, 1); + } + + /* Print times */ + int start = 8; + for (int h = 0; h < (LINES-6)/4+1; h++) + mvwprintw(win, 3+h*4, 0,"%02d:%02d", (start+h)%12, 0); + + /* Print lines */ + mvwhline(win, y-1, 0, ACS_HLINE, COLS); + for (int d = 0; d < 5; d++) + mvwvline(win, y, x+ROUND(d*hstep)-1, ACS_VLINE, LINES-y-2); } /* Week run */ diff --git a/src/view/year.c b/src/view/year.c index dd9494d..19554e9 100644 --- a/src/view/year.c +++ b/src/view/year.c @@ -1,20 +1,15 @@ #include #include +#include "main.h" #include "util.h" -/* Macros */ -#define MAX(a,b) ((a) > (b) ? (a) : (b)) +/* Constants */ #define MW (2*7+6) /* Static data */ static WINDOW *win; -/* Test data */ -const static int YEAR = 2012; -const static int MONTH = SEP; -const static int DAY = 29; - /* Helper functions */ static void print_month(month_t month, int y, int x) {