]> Pileus Git - lackey/blobdiff - src/print.c
Refactor main and add print mode
[lackey] / src / print.c
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)
+{
+}