]> Pileus Git - lackey/blob - src/print.c
Refactor main and add print mode
[lackey] / src / print.c
1 /*
2  * Copyright (C) 2017 Andy Spencer <andy753421@gmail.com>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <stdio.h>
19
20 #include "args.h"
21 #include "date.h"
22 #include "cal.h"
23 #include "print.h"
24
25 /* Initialize */
26 void print_init(void)
27 {
28 }
29
30 void print_main(void)
31 {
32         event_t *event = EVENTS;
33         for (int d = 0; d < PRINT; d++) {
34                 /* Get start and end of the day */
35                 date_t start = {SEL.year, SEL.month, SEL.day,  0, 0};
36                 date_t end   = {SEL.year, SEL.month, SEL.day, 24, 0};
37
38                 add_days(&start.year, &start.month, &start.day, d);
39                 add_days(&end.year,   &end.month,   &end.day,   d);
40
41                 /* Print day header */
42                 wday_t wday = day_of_week(start.year, start.month, start.day);
43                 if (d > 0)
44                         printf("\n");
45                 printf("%s, %s %d, %d\n",
46                                 day_to_string(wday),
47                                 month_to_string(start.month),
48                                 start.day+1,
49                                 start.year);
50
51                 /* Skip forward to the day */
52                 while (event && compare(&start, &event->start) > 0)
53                         event = event->next;
54
55                 /* Print event info */
56                 int printed = 0;
57                 while (event && compare(&end, &event->start) > 0) {
58                         if (printed > 0)
59                                 printf("\n");
60                         printf("* %02d:%02d - %02d:%02d",
61                                         event->start.hour, event->start.min,
62                                         event->end.hour,   event->end.min);
63                         if (!event->name)
64                                 printf("\n");
65                         if (event->name)
66                                 printf("  %s\n", event->name);
67                         if (event->loc)
68                                 printf("  Location:      %s\n", event->loc);
69                         if (event->desc)
70                                 printf("  Description:   %s\n", event->desc);
71                         printed++;
72                         event = event->next;
73                 }
74
75                 /* Print no events */
76                 if (!printed)
77                         printf("  No events\n");
78         }
79 }
80
81 void print_exit(void)
82 {
83 }