]> Pileus Git - lackey/blob - src/args.c
d7a6c685ec6d29dc49c3c4b5cacba787b2135a7b
[lackey] / src / args.c
1 /*
2  * Copyright (C) 2016 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 <stdlib.h>
19 #include <stdio.h>
20 #include <getopt.h>
21
22 #include "args.h"
23 #include "util.h"
24 #include "conf.h"
25 #include "date.h"
26 #include "cal.h"
27
28 /* Setup info */
29 static int    argc;
30 static char **argv;
31
32 /* Options */
33 const char *short_options = "hdw";
34
35 struct option long_options[] = {
36         {"help", 0, NULL, 'h'},
37         {"day",  2, NULL, 'd'},
38         {"week", 2, NULL, 'w'},
39 };
40
41 static int print_day  = 0;
42 static int print_week = 0;
43
44 /* Usage */
45 static void usage(char *name)
46 {
47         printf("Usage:\n");
48         printf("  %s [OPTION...] [CALENDAR]\n", name);
49         printf("\n");
50         printf("Options:\n");
51         printf("  -h, --help  Print usage information\n");
52         printf("  -d, --day   Show today's events\n");
53         printf("  -w, --week  Show this week's events\n");
54 }
55
56 /* Initialize */
57 void args_setup(int _argc, char **_argv)
58 {
59         argc = _argc;
60         argv = _argv;
61 }
62
63 /* Initialize */
64 void args_init(void)
65 {
66         /* Parse arguments */
67         while (1) {
68                 int c = getopt_long(argc, argv,
69                                 short_options, long_options, NULL);
70                 if (c == -1)
71                         break;
72                 switch (c) {
73                         case 'h':
74                                 usage(argv[0]);
75                                 exit(0);
76                                 break;
77                         case 'd':
78                                 print_day = 1;
79                                 break;
80                         case 'w':
81                                 print_week = 1;
82                                 break;
83                 }
84         }
85
86         /* Load calendars */
87         for (int i = optind; i < argc; i++)
88                 cal_config("ical", argv[i], "location", argv[i]);
89
90         /* Validate arguments */
91         if (print_day && print_week)
92                 error("Cannot print both day and week");
93 }
94
95 void args_main(void)
96 {
97         if (print_day || print_week) {
98                 event_t *event = EVENTS;
99                 for (int d = 0; d < (print_day ? 1 : 7); d++) {
100                         /* Get start and end of the day */
101                         date_t start = {SEL.year, SEL.month, SEL.day,  0, 0};
102                         date_t end   = {SEL.year, SEL.month, SEL.day, 24, 0};
103
104                         add_days(&start.year, &start.month, &start.day, d);
105                         add_days(&end.year,   &end.month,   &end.day,   d);
106
107
108                         /* Print day header */
109                         wday_t wday = day_of_week(start.year, start.month, start.day);
110                         printf("%s%s, %s %d, %d\n",
111                                 d ? "\n" : "",
112                                 day_to_string(wday),
113                                 month_to_string(start.month),
114                                 start.day+1,
115                                 start.year);
116
117                         /* Skip forward to the day */
118                         while (event && compare(&start, &event->start) > 0)
119                                 event = event->next;
120
121                         /* Print event info */
122                         int printed = 0;
123                         while (event && compare(&end, &event->start) > 0) {
124                                 printf("%s  %02d:%02d",
125                                         printed ? "\n" : "",
126                                         event->start.hour,
127                                         event->start.min);
128                                 if (event->name)
129                                         printf("  %s\n", event->name);
130                                 if (event->desc)
131                                         printf("\n  %s\n", event->desc);
132                                 printed++;
133                                 event = event->next;
134                         }
135
136                         /* Print no events */
137                         if (!printed)
138                                 printf("  No events\n");
139                 }
140
141                 exit(0);
142         }
143 }