]> Pileus Git - lackey/commitdiff
Add size function, update ical/week
authorAndy Spencer <andy753421@gmail.com>
Sat, 13 Oct 2012 01:02:31 +0000 (01:02 +0000)
committerAndy Spencer <andy753421@gmail.com>
Sat, 13 Oct 2012 01:05:58 +0000 (01:05 +0000)
19 files changed:
cal/ical.c
makefile
src/date.c
src/date.h
src/event.c
src/event.h
src/main.c
src/screen.c
src/screen.h
src/util.c
src/util.h
view/day.c
view/help.c
view/month.c
view/notes.c
view/settings.c
view/todo.c
view/week.c
view/year.c

index 23844a335bcac64e1483c04abb966747431665d9..817da8d52650ed7fe98de34b30763490433bb179 100644 (file)
@@ -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);
index 0162b3a9d80ffb4fc2a5e8faeeb07dd867d55baa..0f58f12ce39f0cc8d66a9fa34a9a3eea10cfd2c3 100644 (file)
--- 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
 
index 965f15a4a6c4e8a4e8e3481e2828bc1e25f7eab4..2e1f7f8af1bd0a2666896454acba64e0a0732e9d 100644 (file)
@@ -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)
 {
index 9b4d278fd23d0e64d5497963c4b2ab690574fe42..673b3b33ef43481a18835dbd15f2a86a2065b474 100644 (file)
@@ -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);
index e4dfe798fcb089acb24645bedab878b4f73ec0be..df375492c6a777f7f8c60c738466e5c3b7d598b4 100644 (file)
@@ -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);
 }
index 43e50b27869ae6d0d2a9ed478d9cd3445678a65d..5af341f79a36a6f7d89873792a539c218202bfaa 100644 (file)
@@ -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;
index 4be87502b6224040c53358dda14cfb29cd2fd69b..75fb53c6de7ec1c6ff590709cc570c330920e9e9 100644 (file)
@@ -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();
index 1618f1022451c1ea3f8a1c07524b3fdb86625333..6fbda206291fc72d2c15d7d57130a76d1bb55cbe 100644 (file)
@@ -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 */
index bce2383b4c09347f8742d9126eeea3c5ddfcc75b..46cdf94156448a73697e728a5f36b55e2404422b 100644 (file)
 #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);
index 53f5cef71f691aa6b7ad3be9bd43520f9a609328..f13ad443c5f560418e5a30f4e27d4ad09abcebea 100644 (file)
@@ -1,4 +1,5 @@
 /*
+#include <string.h>
  * Copyright (C) 2012 Andy Spencer <andy753421@gmail.com>
  * 
  * This program is free software: you can redistribute it and/or modify
@@ -16,6 +17,8 @@
  */
 
 #include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 #include <ncurses.h>
 
 #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, ...)
 {
index 98fb33285a8b40c242703b93843c5e3d52329c93..25e6d8d753d8169621e36a4a806cf7c4b47ea8d1 100644 (file)
 /* 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, ...);
index 7761c9532aa497b6b950af85489e74b1e3fd562b..37f9e2f4b6d7d289f6144dec9ad2578e5bb922ea 100644 (file)
@@ -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)
 {
index 31de286bd9326d84a5575e119da26784c3a7061b..12a0e8fb406ca651b94ed24c07c7445d8e8fa12e 100644 (file)
@@ -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)
 {
index 5642c6df00a7d244ccdbd5ab4815d672b60be598..3849492e9205388159d3eeeeeb686eb1a281983a 100644 (file)
@@ -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)
 {
index 72528dddc46b0e05f0bb8459ff2ad946e14620d9..3414ff3737961990db51a8dc8a34fa46e9c57e1a 100644 (file)
@@ -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)
 {
index 7237296c6bd2227db887b6533e2764aac2f01362..4015876dd3a9d3b7cc0c2f6595cfb7088e42c56f 100644 (file)
@@ -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)
 {
index 5c2f91ea9b05e28324e5fc8f407b13d7842046e7..e2658c6995dad724a5eaece6c08624cdd08bcba4 100644 (file)
@@ -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)
 {
index 9f8419db1dfefe43d86596f18cc48a79857ee2e2..2b005d8731e573ba7ec0528010aef235a72e5c5c 100644 (file)
 
 #define _XOPEN_SOURCE_EXTENDED
 
+#include <string.h>
 #include <ncurses.h>
 
 #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 (l<h && event->name) mvwprintw(body, y+l++, x+1, "%-*.*s",   w-1, w-1, event->name);
+       if (l<h && event->loc)  mvwprintw(body, y+l++, x+1, "@ %-*.*s", w-3, w-3, event->loc);
+       if (l<h && event->desc) 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);
        }
index 04beba66ca0dc9e871e18abf2b7dea32bdf8472b..ba2eaad764f5fab1bc7ceac25ecdbd88ef7a7868 100644 (file)
@@ -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)
 {