Add day and week output flags
authorAndy Spencer <andy753421@gmail.com>
Sun, 27 Nov 2016 04:39:06 +0000 (04:39 +0000)
committerAndy Spencer <andy753421@gmail.com>
Mon, 28 Nov 2016 09:54:02 +0000 (09:54 +0000)
src/args.c
src/args.h
src/main.c

index afc0b48..1fceff6 100644 (file)
 
 #include "args.h"
 #include "util.h"
+#include "conf.h"
+#include "date.h"
+#include "cal.h"
 
 /* Setup info */
 static int    argc;
 static char **argv;
 
 /* Options */
-const char *short_options = "h";
+const char *short_options = "hdw";
 
 struct option long_options[] = {
        {"help", 0, NULL, 'h'},
+       {"day",  2, NULL, 'd'},
+       {"week", 2, NULL, 'w'},
 };
 
+static int print_day  = 0;
+static int print_week = 0;
+
 /* Usage */
 static void usage(char *name)
 {
@@ -41,6 +49,8 @@ static void usage(char *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 */
@@ -53,6 +63,7 @@ void args_setup(int _argc, char **_argv)
 /* Initialize */
 void args_init(void)
 {
+       /* Parse arguments */
        while (1) {
                int c = getopt_long(argc, argv,
                                short_options, long_options, NULL);
@@ -63,6 +74,66 @@ void args_init(void)
                                usage(argv[0]);
                                exit(0);
                                break;
+                       case 'd':
+                               print_day = 1;
+                               break;
+                       case 'w':
+                               print_week = 1;
+                               break;
+               }
+       }
+
+       /* Validate arguments */
+       if (print_day && print_week)
+               error("Cannot print both day and week");
+}
+
+void args_main(void)
+{
+       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);
+                       printf("%s%s, %s %d, %d\n",
+                               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) {
+                               printf("%s  %02d:%02d",
+                                       printed ? "\n" : "",
+                                       event->start.hour,
+                                       event->start.min);
+                               if (event->name)
+                                       printf("  %s\n", event->name);
+                               if (event->desc)
+                                       printf("\n  %s\n", event->desc);
+                               printed++;
+                               event = event->next;
+                       }
+
+                       /* Print no events */
+                       if (!printed)
+                               printf("  No events\n");
                }
+
+               exit(0);
        }
 }
index cca1b5e..8b327df 100644 (file)
@@ -18,3 +18,4 @@
 /* Functions */
 void args_setup(int argc, char **argv);
 void args_init(void);
+void args_main(void);
index f957575..99816b1 100644 (file)
@@ -56,6 +56,9 @@ int main(int argc, char **argv)
        date_init();
        cal_init();
 
+       /* Run args main */
+       args_main();
+
        /* Run view main */
        view_init();
        view_main();