]> Pileus Git - lackey/commitdiff
Add day and week view
authorAndy Spencer <andy753421@gmail.com>
Fri, 5 Oct 2012 04:58:02 +0000 (04:58 +0000)
committerAndy Spencer <andy753421@gmail.com>
Fri, 5 Oct 2012 04:58:02 +0000 (04:58 +0000)
12 files changed:
.gitignore
src/config.mk.example [new file with mode: 0644]
src/main.c
src/main.h
src/makefile
src/screen.c
src/util.c
src/util.h
src/view/day.c
src/view/month.c
src/view/week.c
src/view/year.c

index a85852fab334e26e21da73d6dac1e4966757b4af..b2aac9a4252fafd6accf94b8678c8a8296585352 100644 (file)
@@ -4,5 +4,6 @@
 *~
 .vimrc
 acal
 *~
 .vimrc
 acal
+config.mk
 test
 tags
 test
 tags
diff --git a/src/config.mk.example b/src/config.mk.example
new file mode 100644 (file)
index 0000000..b323a2f
--- /dev/null
@@ -0,0 +1 @@
+default: run-acal
index 4161b031deb31d77ed554a8d64f9bdb1e959a654..146e55e3fcd3bb7c356bab5c0ddd34817d74c39e 100644 (file)
@@ -32,6 +32,10 @@ int debug(char *fmt, ...)
        int rval;
        va_list ap;
 
        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);
        /* 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 */
 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;
        struct sigaction act;
        sigemptyset(&act.sa_mask);
        act.sa_flags   = 0;
index bed6fb1aa5065c74a0c3f740cd90ba93f66c31f2..9f2d6acb58ca57b76c055e7c4dd47d790f5ccfde 100644 (file)
@@ -1,6 +1,10 @@
 #define COLOR_TITLE 1
 #define COLOR_ERROR 2
 
 #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, ...);
 int debug(char *fmt, ...);
index afa11157ac7f83ea327ec01277ffa7576a0cbbf9..0b6c98f615dbda9cbca4a87113e13bbf3a473f09 100644 (file)
@@ -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)
 
 # Targets
 all: $(PROG) $(TEST)
@@ -24,13 +24,13 @@ run-$(TEST): $(TEST)
        ./$<
 
 clean:
        ./$<
 
 clean:
-       rm -f *.o view/*.o $(PROG) $(TEST)
+       rm -f src/*.o view/*.o $(PROG) $(TEST) 
 
 # Rules
 
 # Rules
-$(PROG): $(SOURCES:%=%.o) $(VIEWS:%=view/%.o)
+$(PROG): $(SOURCES:%=src/%.o) $(VIEWS:%=view/%.o)
        $(CC) $(CLFAGS) -o $@ $+ $(LDFLAGS)
 
        $(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
        $(CC) $(CLFAGS) -o $@ $+ $(LDFLAGS)
 
 %.o: %.c $(SOURCES:%=%.h) makefile
index 1fdedc8680402e89910f88bab08f1a32e2e6185a..f9bfd4f01a1ab48917fde756c442b55cf9e0b566 100644 (file)
@@ -2,6 +2,7 @@
 #include <ncurses.h>
 #include "main.h"
 #include "screen.h"
 #include <ncurses.h>
 #include "main.h"
 #include "screen.h"
+#include "util.h"
 
 /* Types */
 typedef struct {
 
 /* Types */
 typedef struct {
@@ -27,7 +28,7 @@ view_t views[] = {
        { "Help",     help_init,     help_draw,     help_run,     {KEY_F(8), '8', 'h', '?'} },
 };
 
        { "Help",     help_init,     help_draw,     help_run,     {KEY_F(8), '8', 'h', '?'} },
 };
 
-int active = 3;
+int active = 0;
 
 /* Local functions */
 void draw_header(void)
 
 /* Local functions */
 void draw_header(void)
index 55cd265a14b45812ea66a743f39ff139f83303f5..9f7bdfaf7cf5bb034d541ca42f1d0774985e6f96 100644 (file)
@@ -1,31 +1,18 @@
 /* Time Keeping Bugs Abound! */
 
 #include <stdio.h>
 /* Time Keeping Bugs Abound! */
 
 #include <stdio.h>
+#include <time.h>
 
 #include "util.h"
 
 
 #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;
 }
 
 {
        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);
 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;
 }
 
        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);
 }
 
 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", };
 /* 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)
 {
 }
 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" };
                { "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" };
 }
 
 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" };
 }
 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" };
 }
 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 */
 }
 
 /* Test functions */
index 57e7afca2ddb631f7df984fc8b8cd36a6f1b81ce..497666c681c17711cc22a828a4d6117db6c856c6 100644 (file)
@@ -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;
 
 typedef int year_t;
 typedef int day_t;
 
@@ -28,12 +34,16 @@ typedef enum {
 } wday_t;
 
 /* Time functions */
 } 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);
 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);
 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);
 const char *month_to_str(month_t month);
 const char *month_to_string(month_t month);
 const char *day_to_st(wday_t day);
index a1763f852ba483fa254c4fba08b814fc5b74a2d0..ed5ee6f1848778b0a7d17f7d22fdedb98db28cca 100644 (file)
@@ -1,5 +1,9 @@
+#include <string.h>
 #include <ncurses.h>
 
 #include <ncurses.h>
 
+#include "main.h"
+#include "util.h"
+
 /* Static data */
 static WINDOW *win;
 
 /* Static data */
 static WINDOW *win;
 
@@ -12,8 +16,19 @@ void day_init(WINDOW *_win)
 /* Day draw */
 void day_draw(void)
 {
 /* 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 */
 }
 
 /* Day run */
index d5bb538bf95e0607e8927cf7521e27895fbb90dc..a7747d4472a14dd52924bfa4dfd47bfe79642dff 100644 (file)
@@ -1,19 +1,12 @@
 #include <string.h>
 #include <ncurses.h>
 
 #include <string.h>
 #include <ncurses.h>
 
+#include "main.h"
 #include "util.h"
 
 #include "util.h"
 
-/* Macros */
-#define ROUND(x) ((int)((x)+0.5))
-
 /* Static data */
 static WINDOW *win;
 
 /* 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)
 {
 /* 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);
 
        /* 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 */
        mvwhline(win, 2, 0, ACS_HLINE, COLS);
 
        /* Print days */
index d885c8f97bc36bdec2a9637941c999a9c246b743..c2d3089fc11f5ecd94936633f8f589b943f0e0c9 100644 (file)
@@ -1,5 +1,9 @@
+#include <string.h>
 #include <ncurses.h>
 
 #include <ncurses.h>
 
+#include "main.h"
+#include "util.h"
+
 /* Static data */
 static WINDOW *win;
 
 /* Static data */
 static WINDOW *win;
 
@@ -12,8 +16,36 @@ void week_init(WINDOW *_win)
 /* Week draw */
 void week_draw(void)
 {
 /* 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 */
 }
 
 /* Week run */
index dd9494d076fbc12fa2081e87b285e98976400bdc..19554e969ceb429d2a868f9b1775cfa387518919 100644 (file)
@@ -1,20 +1,15 @@
 #include <string.h>
 #include <ncurses.h>
 
 #include <string.h>
 #include <ncurses.h>
 
+#include "main.h"
 #include "util.h"
 
 #include "util.h"
 
-/* Macros */
-#define MAX(a,b) ((a) > (b) ? (a) : (b))
+/* Constants */
 #define MW (2*7+6)
 
 /* Static data */
 static WINDOW *win;
 
 #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)
 {
 /* Helper functions */
 static void print_month(month_t month, int y, int x)
 {