From 7edd7d3a9f0bdaaa8df9f68a73f7c84e2b514ce6 Mon Sep 17 00:00:00 2001 From: Andy Spencer Date: Sun, 11 Jun 2017 02:24:21 +0000 Subject: [PATCH] Refactor main and add print mode --- makefile | 4 +- src/args.c | 122 +++++++++++++--------------------------------------- src/args.h | 6 ++- src/conf.c | 13 +++--- src/conf.h | 2 +- src/main.c | 31 ++++++++----- src/print.c | 83 +++++++++++++++++++++++++++++++++++ src/print.h | 21 +++++++++ 8 files changed, 166 insertions(+), 116 deletions(-) create mode 100644 src/print.c create mode 100644 src/print.h diff --git a/makefile b/makefile index 14d6a71..b249a27 100644 --- a/makefile +++ b/makefile @@ -15,9 +15,9 @@ LDFLAGS ?= -lncursesw -lical # Sources PROG ?= lackey -PROG_SRC ?= main view date cal args conf util +PROG_SRC ?= main util args conf date cal view print TEST ?= test -TEST_SRC ?= test date cal conf util +TEST_SRC ?= test util conf date cal VIEWS ?= day week month year events todo settings help edit CALS ?= dummy ical diff --git a/src/args.c b/src/args.c index 9ad0640..49fe2f1 100644 --- a/src/args.c +++ b/src/args.c @@ -19,29 +19,25 @@ #include #include -#include "args.h" #include "util.h" -#include "conf.h" +#include "args.h" #include "date.h" #include "cal.h" -/* Setup info */ -static int argc; -static char **argv; +/* Global data */ +int PRINT = 0; + +/* Local data */ +char **calendars = NULL; /* Options */ -const char *short_options = "hdw"; +static char *short_options = "hp::d"; struct option long_options[] = { - {"help", 0, NULL, 'h'}, - {"day", 2, NULL, 'd'}, - {"week", 2, NULL, 'w'}, + {"help", 0, NULL, 'h'}, + {"print", 2, NULL, 'p'}, }; -static int print_day = 0; -static int print_week = 0; -static char *calendar = NULL; - /* Usage */ static void usage(char *name) { @@ -49,20 +45,12 @@ static void usage(char *name) printf(" %s [OPTION...] [CALENDAR]\n", name); printf("\n"); printf("Options:\n"); - printf(" -h, --help Print usage information\n"); - printf(" -d, --day Show today's events\n"); - printf(" -w, --week Show this week's events\n"); -} - -/* Initialize */ -void args_setup(int _argc, char **_argv) -{ - argc = _argc; - argv = _argv; + printf(" -h, --help Print usage information\n"); + printf(" -p, --print=[dw] Show upcomming events\n"); } /* Initialize */ -void args_init(void) +void args_setup(int argc, char **argv) { /* Parse arguments */ while (1) { @@ -75,95 +63,43 @@ void args_init(void) usage(argv[0]); exit(0); break; - case 'd': - print_day = 1; - break; - case 'w': - print_week = 1; + case 'p': + PRINT = match(optarg, NULL) ? 1 : + match(optarg, "d") ? 1 : + match(optarg, "day") ? 1 : + match(optarg, "w") ? 7 : + match(optarg, "week") ? 7 : -1; break; } } /* Save default calendar */ - calendar = argv[optind]; - - /* Load calendars */ - for (int i = optind; i < argc; i++) - cal_config("ical", argv[i], "location", argv[i]); + calendars = &argv[optind]; /* Validate arguments */ - if (print_day && print_week) - error("Cannot print both day and week"); + if (PRINT < 0) + error("Unknown print: %s\n", optarg); + + /* Load calendars */ + for (int i = 0; calendars[i]; i++) + cal_config("ical", calendars[i], "location", calendars[i]); } -void args_main(void) +void args_start(void) { /* Focus the default calendar */ - if (calendar) { + if (calendars[0]) { event_t *event = EVENTS; date_t start = {SEL.year, SEL.month, SEL.day, 0, 0}; while (event && compare(&start, &event->start) > 0) event = event->next; - while (event && !match(calendar, event->cal->name)) + while (event && !match(calendars[0], event->cal->name)) event = event->next; if (!event) event = EVENTS; - while (event && !match(calendar, event->cal->name)) + while (event && !match(calendars[0], event->cal->name)) event = event->next; if (event) SEL = event->start; } - - /* Print days or week */ - if (print_day || print_week) { - event_t *event = EVENTS; - for (int d = 0; d < (print_day ? 1 : 7); d++) { - /* Get start and end of the day */ - date_t start = {SEL.year, SEL.month, SEL.day, 0, 0}; - date_t end = {SEL.year, SEL.month, SEL.day, 24, 0}; - - add_days(&start.year, &start.month, &start.day, d); - add_days(&end.year, &end.month, &end.day, d); - - /* Print day header */ - wday_t wday = day_of_week(start.year, start.month, start.day); - if (d > 0) - printf("\n"); - printf("%s, %s %d, %d\n", - day_to_string(wday), - month_to_string(start.month), - start.day+1, - start.year); - - /* Skip forward to the day */ - while (event && compare(&start, &event->start) > 0) - event = event->next; - - /* Print event info */ - int printed = 0; - while (event && compare(&end, &event->start) > 0) { - if (printed > 0) - printf("\n"); - printf("* %02d:%02d - %02d:%02d", - event->start.hour, event->start.min, - event->end.hour, event->end.min); - if (!event->name) - printf("\n"); - if (event->name) - printf(" %s\n", event->name); - if (event->loc) - printf(" Location: %s\n", event->loc); - if (event->desc) - printf(" Description: %s\n", event->desc); - printed++; - event = event->next; - } - - /* Print no events */ - if (!printed) - printf(" No events\n"); - } - - exit(0); - } } diff --git a/src/args.h b/src/args.h index 8b327df..c62c9c2 100644 --- a/src/args.h +++ b/src/args.h @@ -15,7 +15,9 @@ * along with this program. If not, see . */ +/* Global data */ +extern int PRINT; + /* Functions */ void args_setup(int argc, char **argv); -void args_init(void); -void args_main(void); +void args_start(void); diff --git a/src/conf.c b/src/conf.c index 1a81b91..e82cd9a 100644 --- a/src/conf.c +++ b/src/conf.c @@ -43,7 +43,6 @@ static const char *booleans[] = { /* Setup info */ static char *filename; -static parser_t parser; /* Static data */ static line_t *settings; @@ -314,18 +313,16 @@ void conf_save(const char *path) } /* Initialize */ -void conf_setup(const char *_name, parser_t _parser) +void conf_setup(const char *name, parser_t parser) { const char *home = getenv("HOME"); - filename = alloc0(strlen(home) + 1 + strlen(_name) + 1); - sprintf(filename, "%s/%s", home, _name); - parser = _parser; + filename = alloc0(strlen(home) + 1 + strlen(name) + 1); + sprintf(filename, "%s/%s", home, name); + conf_load(filename, parser); } -/* Initialize */ -void conf_init(void) +void conf_start(void) { - conf_load(filename, parser); } /* Update */ diff --git a/src/conf.h b/src/conf.h index b716cf6..f765222 100644 --- a/src/conf.h +++ b/src/conf.h @@ -41,5 +41,5 @@ void set_name(const char *group, const char *name, /* Functions */ void conf_setup(const char *name, parser_t parser); -void conf_init(void); +void conf_start(void); void conf_sync(void); diff --git a/src/main.c b/src/main.c index ce35539..bf518ff 100644 --- a/src/main.c +++ b/src/main.c @@ -15,15 +15,17 @@ * along with this program. If not, see . */ +#include #include #include -#include "args.h" #include "util.h" +#include "args.h" #include "conf.h" #include "date.h" #include "cal.h" #include "view.h" +#include "print.h" /* Config parser */ static void on_config(const char *group, const char *name, const char *key, const char *value) @@ -36,7 +38,10 @@ static void on_config(const char *group, const char *name, const char *key, cons /* Control-C handler, so we don't hose the therminal */ static void on_sigint(int signum) { - view_exit(); + if (PRINT) + print_exit(); + else + view_exit(); exit(0); } @@ -51,19 +56,25 @@ int main(int argc, char **argv) conf_setup(".lackeyrc", on_config); /* Initialize */ - args_init(); util_init(); - conf_init(); date_init(); cal_init(); - /* Run args main */ - args_main(); + /* Common main */ + args_start(); + conf_start(); - /* Run view main */ - view_init(); - view_main(); - view_exit(); + /* Mode main */ + if (PRINT) { + print_init(); + print_main(); + print_exit(); + } + else { + view_init(); + view_main(); + view_exit(); + } return 0; } diff --git a/src/print.c b/src/print.c new file mode 100644 index 0000000..9daf92d --- /dev/null +++ b/src/print.c @@ -0,0 +1,83 @@ +/* + * Copyright (C) 2017 Andy Spencer + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include + +#include "args.h" +#include "date.h" +#include "cal.h" +#include "print.h" + +/* Initialize */ +void print_init(void) +{ +} + +void print_main(void) +{ + event_t *event = EVENTS; + for (int d = 0; d < PRINT; d++) { + /* Get start and end of the day */ + date_t start = {SEL.year, SEL.month, SEL.day, 0, 0}; + date_t end = {SEL.year, SEL.month, SEL.day, 24, 0}; + + add_days(&start.year, &start.month, &start.day, d); + add_days(&end.year, &end.month, &end.day, d); + + /* Print day header */ + wday_t wday = day_of_week(start.year, start.month, start.day); + if (d > 0) + printf("\n"); + printf("%s, %s %d, %d\n", + day_to_string(wday), + month_to_string(start.month), + start.day+1, + start.year); + + /* Skip forward to the day */ + while (event && compare(&start, &event->start) > 0) + event = event->next; + + /* Print event info */ + int printed = 0; + while (event && compare(&end, &event->start) > 0) { + if (printed > 0) + printf("\n"); + printf("* %02d:%02d - %02d:%02d", + event->start.hour, event->start.min, + event->end.hour, event->end.min); + if (!event->name) + printf("\n"); + if (event->name) + printf(" %s\n", event->name); + if (event->loc) + printf(" Location: %s\n", event->loc); + if (event->desc) + printf(" Description: %s\n", event->desc); + printed++; + event = event->next; + } + + /* Print no events */ + if (!printed) + printf(" No events\n"); + } +} + +void print_exit(void) +{ +} diff --git a/src/print.h b/src/print.h new file mode 100644 index 0000000..42b5d53 --- /dev/null +++ b/src/print.h @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2017 Andy Spencer + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/* Functions */ +void print_init(void); +void print_main(void); +void print_exit(void); -- 2.43.2