Refactor main and add print mode
authorAndy Spencer <andy753421@gmail.com>
Sun, 11 Jun 2017 02:24:21 +0000 (02:24 +0000)
committerAndy Spencer <andy753421@gmail.com>
Sun, 11 Jun 2017 02:32:10 +0000 (02:32 +0000)
makefile
src/args.c
src/args.h
src/conf.c
src/conf.h
src/main.c
src/print.c [new file with mode: 0644]
src/print.h [new file with mode: 0644]

index 14d6a71..b249a27 100644 (file)
--- 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
 
index 9ad0640..49fe2f1 100644 (file)
 #include <stdio.h>
 #include <getopt.h>
 
-#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);
-       }
 }
index 8b327df..c62c9c2 100644 (file)
@@ -15,7 +15,9 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+/* 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);
index 1a81b91..e82cd9a 100644 (file)
@@ -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 */
index b716cf6..f765222 100644 (file)
@@ -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);
index ce35539..bf518ff 100644 (file)
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <stdio.h>
 #include <stdlib.h>
 #include <signal.h>
 
-#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 (file)
index 0000000..9daf92d
--- /dev/null
@@ -0,0 +1,83 @@
+/*
+ * Copyright (C) 2017 Andy Spencer <andy753421@gmail.com>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <stdio.h>
+
+#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 (file)
index 0000000..42b5d53
--- /dev/null
@@ -0,0 +1,21 @@
+/*
+ * Copyright (C) 2017 Andy Spencer <andy753421@gmail.com>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+/* Functions */
+void print_init(void);
+void print_main(void);
+void print_exit(void);