From 41f73b0a45ba3e63ac33ebdc99f12481f5b7cdab Mon Sep 17 00:00:00 2001 From: katherine Date: Fri, 20 Dec 2019 09:28:54 -0700 Subject: implement printing --- src/print.c | 246 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 246 insertions(+) create mode 100644 src/print.c (limited to 'src/print.c') diff --git a/src/print.c b/src/print.c new file mode 100644 index 0000000..93ce295 --- /dev/null +++ b/src/print.c @@ -0,0 +1,246 @@ +#include "print.h" + +#include "entry.h" +#include "opt.h" +#include "err.h" + +#include +#include +#include + +static const char *weekday_names[] = { + "Sun", + "Mon", + "Tue", + "Wed", + "Thu", + "Fri", + "Sat" +}; + +static const char *weekday_names_l[] = { + "Sunday", + "Monday", + "Tuesday", + "Wednesday", + "Thursday", + "Friday", + "Saturday" +}; + +static const char *month_names[] = { + "Jan", + "Feb", + "Mar", + "Apr", + "May", + "Jun", + "Jul", + "Aug", + "Sep", + "Oct", + "Nov", + "Dec" +}; + +struct buf_s { + time_t rt; + struct entry_s e; +}; + +static int buf_cmp(const void *a, const void *b) +{ + const struct buf_s *a1 = a, *b1 = b; + + if (b1->e.urgent && !(a1->e.urgent)) + return 1; + + if (a1->e.urgent && !(b1->e.urgent)) + return -1; + + if (a1->rt > b1->rt) + return 1; + + if (b1->rt > a1->rt) + return -1; + + return 0; +} + +void print_console(struct calendar_s *cal) +{ + size_t i, j; + struct tm t = { 0 }, ttmp; + time_t now = time(NULL), *rttmp; + time_t ototois_end, yesterdays_end, todays_end, tomorrows_end, weeks_end; + bool has_urgent = false, urgent_printed = false; + + struct buf_s *buf = malloc(cal->count * sizeof(*buf)); + + if (buf == NULL) { + ERRM("failed to allocate memory"); + return; + } + + /* get interval limits */ + + t = *localtime(&now); + + ttmp.tm_year = t.tm_year; + ttmp.tm_mon = t.tm_mon; + ttmp.tm_mday = t.tm_mday - 1; + ttmp.tm_hour = 0; + ttmp.tm_min = 0; + ttmp.tm_sec = 0; + ttmp.tm_isdst = -1; + + ototois_end = mktime(&ttmp); + + ttmp.tm_mday++; + + yesterdays_end = mktime(&ttmp); + + ttmp.tm_mday++; + + todays_end = mktime(&ttmp); + + ttmp.tm_mday++; + + tomorrows_end = mktime(&ttmp); + + ttmp.tm_mday = t.tm_mday + (6 - t.tm_wday) + 1; + + weeks_end = mktime(&ttmp); + + printf("%s %d %s %02d %02d:%02d\n\n", + weekday_names[t.tm_wday], + t.tm_year + 1900, + month_names[t.tm_mon], + t.tm_mday, + t.tm_hour, + t.tm_min + ); + + /* fill buffer with active events */ + for (i = 0, j = 0; i < cal->count; i++) { + if ( !(rttmp = entry_is_active( &(cal->entries[i]), now)) ) + continue; + + buf[j].rt = *rttmp; + buf[j].e = cal->entries[i]; + + if (buf[j].e.urgent) + has_urgent = true; + + j++; + } + + qsort(buf, j, sizeof(*buf), buf_cmp); + + /* print from buffer */ + if (has_urgent) + printf("\n[ URGENT ]\n\n"); + + for (i = 0; i < j; i++) { + t = *localtime( &(buf[i].rt) ); + + if (has_urgent && !urgent_printed && !(buf[i].e.urgent) ) { + printf("\n\n[ normal ]\n\n"); + urgent_printed = true; + } + + /* date */ + if (ototois_end > buf[i].rt) { + printf("%04d %s %02d", + t.tm_year + 1900, + month_names[t.tm_mon], + t.tm_mday + ); + } else if (yesterdays_end > buf[i].rt) { + printf("Yesterday "); + } else if (todays_end > buf[i].rt) { + printf("Today "); + } else if (tomorrows_end > buf[i].rt) { + printf("Tomorrow "); + } else if (weeks_end > buf[i].rt) { + printf("%-11s", weekday_names_l[t.tm_wday]); + } else { + printf("%04d %s %02d", + t.tm_year + 1900, + month_names[t.tm_mon], + t.tm_mday + ); + } + + /* time */ + if (t.tm_hour != 0 || t.tm_min != 0 || t.tm_sec != 0) { + printf(" %02d:%02d", t.tm_hour, t.tm_min); + + if (t.tm_sec != 0) + printf(":%02d", t.tm_sec); + } + + + if (buf[i].e.msg != NULL) + printf(" %s", buf[i].e.msg); + + puts(""); + } + + free(buf); +} + +void print_script(struct calendar_s *cal) +{ + size_t i, j; + struct tm t = { 0 }; + time_t now = time(NULL), *rttmp; + char cd; + bool first = true; + struct buf_s *buf = malloc(cal->count * sizeof(*buf)); + + if (buf == NULL) { + ERRM("failed to allocate memory"); + return; + } + + cd = opt_col_delim(); + + /* fill buffer with active events */ + for (i = 0, j = 0; i < cal->count; i++) { + if ( !(rttmp = entry_is_active( &(cal->entries[i]), now)) ) + continue; + + buf[j].rt = *rttmp; + buf[j].e = cal->entries[i]; + + j++; + } + + qsort(buf, j, sizeof(*buf), buf_cmp); + + /* print from buffer */ + for (i = 0; i < j; i++) { + t = *localtime( &(buf[i].rt) ); + + if (!first) + printf("%c", opt_line_delim()); + + printf( + "%d%c%d%c%d%c%d%c%d%c%d%c" /* date */ + "%d%c" /* urgent */ + "%d%c" /* local */ + "%s" /* msg */ + , + t.tm_year + 1900, cd, t.tm_mon + 1, cd, t.tm_mday, cd, + t.tm_hour, cd, t.tm_min, cd, t.tm_sec, cd, + cal->entries[i].urgent, cd, + cal->entries[i].local, cd, + (cal->entries[i].msg == NULL ? "" : cal->entries[i].msg) + ); + + first = false; + } + + free(buf); +} -- cgit v1.2.3