*~
.vimrc
acal
+config.mk
test
tags
--- /dev/null
+default: run-acal
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);
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;
#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, ...);
-# 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)
./$<
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
#include <ncurses.h>
#include "main.h"
#include "screen.h"
+#include "util.h"
/* Types */
typedef struct {
{ "Help", help_init, help_draw, help_run, {KEY_F(8), '8', 'h', '?'} },
};
-int active = 3;
+int active = 0;
/* Local functions */
void draw_header(void)
/* Time Keeping Bugs Abound! */
#include <stdio.h>
+#include <time.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;
}
-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);
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)
{
{ "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 */
-/* 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;
} 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);
+#include <string.h>
#include <ncurses.h>
+#include "main.h"
+#include "util.h"
+
/* Static data */
static 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 */
#include <string.h>
#include <ncurses.h>
+#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)
{
/* 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 */
+#include <string.h>
#include <ncurses.h>
+#include "main.h"
+#include "util.h"
+
/* Static data */
static 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 */
#include <string.h>
#include <ncurses.h>
+#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)
{