From: Andy Spencer Date: Sat, 13 Oct 2012 01:02:31 +0000 (+0000) Subject: Add size function, update ical/week X-Git-Url: http://pileus.org/git/?p=lackey;a=commitdiff_plain;h=01371b6e19aafedb854146b00ce8fb1a90e3b216 Add size function, update ical/week --- diff --git a/cal/ical.c b/cal/ical.c index 23844a3..817da8d 100644 --- a/cal/ical.c +++ b/cal/ical.c @@ -54,9 +54,13 @@ static date_t to_date(struct icaltimetype time) static event_t *to_event(ical_inst *inst) { + icalproperty *prop = icalcomponent_get_first_property(inst->comp, ICAL_CATEGORIES_PROPERTY); + event_t *event = calloc(1, sizeof(event_t)); event->name = icalcomponent_get_summary(inst->comp); event->desc = icalcomponent_get_description(inst->comp); + event->loc = icalcomponent_get_location(inst->comp); + event->cat = icalproperty_get_value_as_string(prop); event->start = to_date(inst->start); event->end = to_date(inst->end); return event; @@ -139,6 +143,8 @@ event_t *ical_get(cal_t *cal, year_t year, month_t month, day_t day, int days) { /* Load ical */ FILE *file = fopen("data/all.ics", "r"); + if (!file) + return NULL; icalparser *parser = icalparser_new(); icalparser_set_gen_data(parser, file); icalcomponent *ical = icalparser_parse(parser, (void*)fgets); diff --git a/makefile b/makefile index 0162b3a..0f58f12 100644 --- a/makefile +++ b/makefile @@ -10,7 +10,7 @@ LDFLAGS ?= -lncursesw -lical PROG = lackey PROG_SRC = main screen date event util TEST = test -TEST_SRC = test date +TEST_SRC = test date util VIEWS = day week month year todo notes settings help CALS = dummy ical diff --git a/src/date.c b/src/date.c index 965f15a..2e1f7f8 100644 --- a/src/date.c +++ b/src/date.c @@ -38,9 +38,9 @@ void date_init(void) DAY = tm->tm_mday-1; // Testing */ - YEAR = 2009; - MONTH = MAY; - DAY = 1; + //YEAR = 2009; + //MONTH = MAY; + //DAY = 1; } /* Time functions */ @@ -111,6 +111,21 @@ void add_months(year_t *year, month_t *month, int months) *month = total % 12; } +stamp_t get_time(date_t *date) +{ + return mktime(&(struct tm){ + .tm_year = date->year-1900, + .tm_mon = date->month, + .tm_mday = date->day+1, + .tm_hour = date->hour, + .tm_min = date->min}); +} + +int get_mins(date_t *start, date_t *end) +{ + return (get_time(end)-get_time(start))/60; +} + /* Debug functions */ const char *month_to_str(month_t month) { diff --git a/src/date.h b/src/date.h index 9b4d278..673b3b3 100644 --- a/src/date.h +++ b/src/date.h @@ -16,6 +16,8 @@ */ /* Time types */ +typedef long long stamp_t; + typedef int year_t; typedef int day_t; typedef int hour_t; @@ -46,6 +48,14 @@ typedef enum { SAT = 6, } wday_t; +typedef struct { + year_t year; + month_t month; + day_t day; + hour_t hour; + min_t min; +} date_t; + /* Global data */ extern year_t YEAR; extern month_t MONTH; @@ -65,6 +75,9 @@ 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); +stamp_t get_stamp(date_t *date); +int get_mins(date_t *start, date_t *end); + /* Time to string functions */ const char *month_to_str(month_t month); const char *month_to_string(month_t month); diff --git a/src/event.c b/src/event.c index e4dfe79..df37549 100644 --- a/src/event.c +++ b/src/event.c @@ -37,6 +37,6 @@ void event_init(void) /* Event get */ event_t *event_get(year_t year, month_t month, day_t day, int days) { - //return dummy_get(0, year, month, day, days); - return ical_get(0, year, month, day, days); + return ical_get(0, year, month, day, days) + ?: dummy_get(0, year, month, day, days); } diff --git a/src/event.h b/src/event.h index 43e50b2..5af341f 100644 --- a/src/event.h +++ b/src/event.h @@ -23,18 +23,12 @@ typedef struct { } cal_t; /* Event types */ -typedef struct { - year_t year; - month_t month; - day_t day; - hour_t hour; - min_t min; -} date_t; - typedef struct event_t { const cal_t *cal; const char *name; const char *desc; + const char *loc; + const char *cat; date_t start; date_t end; struct event_t *next; diff --git a/src/main.c b/src/main.c index 4be8750..75fb53c 100644 --- a/src/main.c +++ b/src/main.c @@ -46,9 +46,16 @@ int main(int argc, char **argv) keypad(stdscr, TRUE); start_color(); curs_set(false); + use_default_colors(); mousemask(ALL_MOUSE_EVENTS, NULL); - init_pair(COLOR_TITLE, COLOR_GREEN, COLOR_BLACK); - init_pair(COLOR_ERROR, COLOR_RED, COLOR_BLACK); + + init_pair(COLOR_TITLE, COLOR_GREEN, -1); + init_pair(COLOR_ERROR, COLOR_RED, -1); + + init_pair(COLOR_CLASS, COLOR_BLUE, -1); + init_pair(COLOR_EC, COLOR_GREEN, -1); + init_pair(COLOR_WORK, COLOR_MAGENTA, -1); + init_pair(COLOR_OTHER, COLOR_RED, -1); /* Initialize */ util_init(); diff --git a/src/screen.c b/src/screen.c index 1618f10..6fbda20 100644 --- a/src/screen.c +++ b/src/screen.c @@ -26,6 +26,7 @@ typedef struct { char *name; void (*init)(WINDOW*); + void (*size)(int,int); void (*draw)(void); int (*run)(int,mmask_t,int,int); int keys[8]; @@ -34,16 +35,16 @@ typedef struct { /* Data */ view_t views[] = { - { "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', '?'} }, + { "Day", day_init, day_size, day_draw, day_run, {KEY_F(1), '1', } }, + { "Week", week_init, week_size, week_draw, week_run, {KEY_F(2), '2', } }, + { "Month", month_init, month_size, month_draw, month_run, {KEY_F(3), '3', } }, + { "Year", year_init, year_size, year_draw, year_run, {KEY_F(4), '4', } }, + { "|", NULL, NULL, NULL, NULL, { } }, + { "Todo", todo_init, todo_size, todo_draw, todo_run, {KEY_F(5), '5', } }, + { "Notes", notes_init, notes_size, notes_draw, notes_run, {KEY_F(6), '6', } }, + { "|", NULL, NULL, NULL, NULL, { } }, + { "Settings", settings_init, settings_size, settings_draw, settings_run, {KEY_F(7), '7', } }, + { "Help", help_init, help_size, help_draw, help_run, {KEY_F(8), '8', '?'} }, }; int active = 1; @@ -79,9 +80,12 @@ void screen_init(void) /* Screen draw */ void screen_resize(void) { - for (int i = 0; i < N_ELEMENTS(views); i++) - if (views[i].init) + for (int i = 0; i < N_ELEMENTS(views); i++) { + if (views[i].win) wresize(views[i].win, LINES-2, COLS); + if (views[i].size) + views[i].size(LINES-2, COLS); + } } /* Screen draw */ diff --git a/src/screen.h b/src/screen.h index bce2383..46cdf94 100644 --- a/src/screen.h +++ b/src/screen.h @@ -19,6 +19,11 @@ #define COLOR_TITLE 1 #define COLOR_ERROR 2 +#define COLOR_CLASS 3 +#define COLOR_EC 4 +#define COLOR_WORK 5 +#define COLOR_OTHER 6 + /* Screen functions */ void screen_init(void); void screen_resize(void); @@ -35,6 +40,16 @@ void notes_init(WINDOW *win); void settings_init(WINDOW *win); void help_init(WINDOW *win); +/* View size functions */ +void day_size(int,int); +void week_size(int,int); +void month_size(int,int); +void year_size(int,int); +void todo_size(int,int); +void notes_size(int,int); +void settings_size(int,int); +void help_size(int,int); + /* View draw functions */ void day_draw(void); void week_draw(void); diff --git a/src/util.c b/src/util.c index 53f5cef..f13ad44 100644 --- a/src/util.c +++ b/src/util.c @@ -1,4 +1,5 @@ /* +#include * Copyright (C) 2012 Andy Spencer * * This program is free software: you can redistribute it and/or modify @@ -16,6 +17,8 @@ */ #include +#include +#include #include #include "screen.h" @@ -29,6 +32,17 @@ void util_init(void) debug_fd = fopen("/tmp/lackey.log", "w+"); } +/* Misc functions */ +char *sdup(const char *str) +{ + if (str == NULL) + return NULL; + int len = strlen(str); + char *dup = malloc(len+1); + memcpy(dup, str, len+1); + return dup; +} + /* Debugging functions */ int debug(char *fmt, ...) { diff --git a/src/util.h b/src/util.h index 98fb332..25e6d8d 100644 --- a/src/util.h +++ b/src/util.h @@ -18,12 +18,16 @@ /* Macros */ #define MIN(a,b) ((a) < (b) ? (a) : (b)) #define MAX(a,b) ((a) > (b) ? (a) : (b)) +#define CLAMP(x,l,h) MIN(MAX(x,l),h) #define ROUND(x) ((int)((x)+0.5)) #define N_ELEMENTS(x) (sizeof(x)/sizeof((x)[0])) /* Debug functions */ void util_init(void); +/* Misc functions */ +char *sdup(const char *str); + /* Debug functions */ #ifdef DEBUG int debug(char *fmt, ...); diff --git a/view/day.c b/view/day.c index 7761c95..37f9e2f 100644 --- a/view/day.c +++ b/view/day.c @@ -28,6 +28,11 @@ void day_init(WINDOW *_win) win = _win; } +/* Day size */ +void day_size(int rows, int cols) +{ +} + /* Day draw */ void day_draw(void) { diff --git a/view/help.c b/view/help.c index 31de286..12a0e8f 100644 --- a/view/help.c +++ b/view/help.c @@ -26,6 +26,11 @@ void help_init(WINDOW *_win) win = _win; } +/* Help size */ +void help_size(int rows, int cols) +{ +} + /* Help draw */ void help_draw(void) { diff --git a/view/month.c b/view/month.c index 5642c6d..3849492 100644 --- a/view/month.c +++ b/view/month.c @@ -32,6 +32,11 @@ void month_init(WINDOW *_win) win = _win; } +/* Month size */ +void month_size(int rows, int cols) +{ +} + /* Month draw */ void month_draw(void) { diff --git a/view/notes.c b/view/notes.c index 72528dd..3414ff3 100644 --- a/view/notes.c +++ b/view/notes.c @@ -26,6 +26,11 @@ void notes_init(WINDOW *_win) win = _win; } +/* Notes size */ +void notes_size(int rows, int cols) +{ +} + /* Notes draw */ void notes_draw(void) { diff --git a/view/settings.c b/view/settings.c index 7237296..4015876 100644 --- a/view/settings.c +++ b/view/settings.c @@ -26,6 +26,11 @@ void settings_init(WINDOW *_win) win = _win; } +/* Settings init */ +void settings_size(int rows, int cols) +{ +} + /* Settings draw */ void settings_draw(void) { diff --git a/view/todo.c b/view/todo.c index 5c2f91e..e2658c6 100644 --- a/view/todo.c +++ b/view/todo.c @@ -26,6 +26,11 @@ void todo_init(WINDOW *_win) win = _win; } +/* Todo size */ +void todo_size(int rows, int cols) +{ +} + /* Todo draw */ void todo_draw(void) { diff --git a/view/week.c b/view/week.c index 9f8419d..2b005d8 100644 --- a/view/week.c +++ b/view/week.c @@ -17,24 +17,50 @@ #define _XOPEN_SOURCE_EXTENDED +#include #include #include "util.h" #include "date.h" #include "event.h" +#include "screen.h" /* Static data */ +static int line; static WINDOW *win; +static WINDOW *head; +static WINDOW *times; +static WINDOW *body; /* Local functions */ static void print_event(event_t *event, wday_t day, hour_t hour, min_t min, float hstep) { - int x = 6+ROUND(day*hstep); - int y = 3+hour*4; - int l = (event->end.min - event->start.min)/15; - mvwprintw(win, y, x, "%s", event->name); + int x = ROUND(day*hstep); + int y = hour*4 + min/15 - line; + int w = ROUND((day+1)*hstep) - 1 - x; + int h = (get_mins(&event->start, &event->end)-1)/15+1; + int l = 0; + int s = y < 0 ? -y-1 : 0; + + int color = event->cat == NULL ? 0 : + !strcmp(event->cat, "class") ? COLOR_CLASS : + !strcmp(event->cat, "ec") ? COLOR_EC : + !strcmp(event->cat, "work") ? COLOR_WORK : COLOR_OTHER ; + + if (color) wattron(body, COLOR_PAIR(color)); + + if (h <= 1) mvwadd_wch(body, y, x, WACS_BULLET); + if (h >= 2) mvwadd_wch(body, y, x, WACS_T_ULCORNER); + if (h >= 3) mvwvline_set(body, y+1+s, x, WACS_T_VLINE, h-2-s); + if (h >= 2) mvwadd_wch(body, y+h-1, x, WACS_T_LLCORNER); + + if (color) wattroff(body, COLOR_PAIR(color)); + + if (lname) mvwprintw(body, y+l++, x+1, "%-*.*s", w-1, w-1, event->name); + if (lloc) mvwprintw(body, y+l++, x+1, "@ %-*.*s", w-3, w-3, event->loc); + if (ldesc) mvwprintw(body, y+l++, x+1, "%-*.*s", w-1, w-1, event->desc); + debug("week: event = %s\n", event->name); - (void)l; } static int before(date_t *start, int year, int month, int day, int hour, int min) @@ -53,7 +79,19 @@ static int before(date_t *start, int year, int month, int day, int hour, int min /* Week init */ void week_init(WINDOW *_win) { - win = _win; + win = _win; // lines cols y x + head = derwin(win, 2, COLS, 0, 0); + times = derwin(win, LINES-2-3, 5, 3, 0); + body = derwin(win, LINES-2-3, COLS-6, 3, 6); + line = 10*4; // 10:00 +} + +/* Week size */ +void week_size(int rows, int cols) +{ + wresize(head, 2, cols ); + wresize(times, rows-3, 5); + wresize(body, rows-3, cols-6); } /* Week draw */ @@ -74,42 +112,40 @@ void week_draw(void) add_days(&year, &month, &day, -shift); /* Print Header */ - mvwprintw(win, 1, 0, "%s", month_to_str(MONTH)); + mvwprintw(head, 0, 0, "%s", month_to_str(MONTH)); + mvwprintw(head, 1, 0, "%04d", YEAR); for (int d = 0; d < 7; d++) { const char *str = hstep >= 10 ? day_to_string(d) : day_to_str(d); - if (d == shift) wattron(win, A_BOLD); - mvwprintw(win, 0, x+ROUND(d*hstep), "%02d/%02d", month+1, day+1); - mvwprintw(win, 1, x+ROUND(d*hstep), "%s", str); - if (d == shift) wattroff(win, A_BOLD); + if (d == shift) wattron(head, A_BOLD); + mvwprintw(head, 0, x+ROUND(d*hstep), "%s", str); + mvwprintw(head, 1, x+ROUND(d*hstep), "%02d/%02d", month+1, day+1); + if (d == shift) wattroff(head, A_BOLD); add_days(&year, &month, &day, 1); } /* Print times */ - hour_t 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 < 7; d++) - mvwvline(win, y, x+ROUND(d*hstep)-1, ACS_VLINE, LINES-y-2); + mvwprintw(times, 0, 0, "%02d:%02d", ((line/4)-1)%12+1, (line*15)%60); + for (int h = 0; h < 24; h++) + mvwprintw(times, h*4-line, 0, "%02d:%02d", (h-1)%12+1, 0); /* Print events */ event_t *event = EVENTS; add_days(&year, &month, &day, -7); - for (int d = 0; d < 7; d++) { - for (int h = 0; h < (LINES-6)/4+1; h++) { - for (int m = 0; m < 60; m+=15) { - while (event && before(&event->start, year, month, day, start+h+(m+15)/60, (m+15)%60)) { - if (!before(&event->start, year, month, day, start+h, m)) - print_event(event, d, h, m, hstep); - event = event->next; - } - } - } - add_days(&year, &month, &day, 1); + for (int d = 0; d < 7; d++, add_days(&year,&month,&day,1)) + for (int h = 0; h < 24; h++) + for (int m = 0; m < 60; m+=15) + while (event && before(&event->start, + year, month, day, h+(m+15)/60, (m+15)%60)) { + if (!before(&event->start, year, month, day, h, m)) + print_event(event, d, h, m, hstep); + event = event->next; } + /* Print lines */ + mvwhline(win, y-1, 0, ACS_HLINE, COLS); + for (int d = 0; d < 7; d++) + mvwvline(win, y, x+ROUND(d*hstep)-1, ACS_VLINE, LINES-y-2); + /* Draw today */ int l = x+ROUND((shift+0)*hstep)-1; int r = x+ROUND((shift+1)*hstep)-1; @@ -121,16 +157,20 @@ void week_draw(void) /* Week run */ int week_run(int key, mmask_t btn, int row, int col) { - int days = 0; + int days = 0, ref = 0; switch (key) { - case 'h': days = -1; break; - case 'l': days = 1; break; - case 'i': days = -7; break; - case 'o': days = 7; break; + case 'h': ref = 1; days = -1; break; + case 'l': ref = 1; days = 1; break; + case 'i': ref = 1; days = -7; break; + case 'o': ref = 1; days = 7; break; + case 'k': ref = 1; line--; break; + case 'j': ref = 1; line++; break; } - if (days) { + line = CLAMP(line, 0, 24*4); + if (days) add_days(&YEAR, &MONTH, &DAY, days); + if (ref) { week_draw(); wrefresh(win); } diff --git a/view/year.c b/view/year.c index 04beba6..ba2eaad 100644 --- a/view/year.c +++ b/view/year.c @@ -52,6 +52,11 @@ void year_init(WINDOW *_win) win = _win; } +/* Year size */ +void year_size(int rows, int cols) +{ +} + /* Year draw */ void year_draw(void) {