]> Pileus Git - lackey/blob - src/args.c
d577fdc32b64549e923d6471bd5793285c8b3864
[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 static char *calendar   = NULL;
44
45 /* Usage */
46 static void usage(char *name)
47 {
48         printf("Usage:\n");
49         printf("  %s [OPTION...] [CALENDAR]\n", name);
50         printf("\n");
51         printf("Options:\n");
52         printf("  -h, --help  Print usage information\n");
53         printf("  -d, --day   Show today's events\n");
54         printf("  -w, --week  Show this week's events\n");
55 }
56
57 /* Initialize */
58 void args_setup(int _argc, char **_argv)
59 {
60         argc = _argc;
61         argv = _argv;
62 }
63
64 /* Initialize */
65 void args_init(void)
66 {
67         /* Parse arguments */
68         while (1) {
69                 int c = getopt_long(argc, argv,
70                                 short_options, long_options, NULL);
71                 if (c == -1)
72                         break;
73                 switch (c) {
74                         case 'h':
75                                 usage(argv[0]);
76                                 exit(0);
77                                 break;
78                         case 'd':
79                                 print_day = 1;
80                                 break;
81                         case 'w':
82                                 print_week = 1;
83                                 break;
84                 }
85         }
86
87         /* Save default calendar */
88         calendar = argv[optind];
89
90         /* Load calendars */
91         for (int i = optind; i < argc; i++)
92                 cal_config("ical", argv[i], "location", argv[i]);
93
94         /* Validate arguments */
95         if (print_day && print_week)
96                 error("Cannot print both day and week");
97 }
98
99 void args_main(void)
100 {
101         /* Focus the default calendar */
102         if (calendar) {
103                 event_t *event = EVENTS;
104                 date_t   start = {SEL.year, SEL.month, SEL.day, 0, 0};
105                 while (event && compare(&start, &event->start) > 0)
106                         event = event->next;
107                 while (event && !match(calendar, event->cal->name))
108                         event = event->next;
109                 if (!event)
110                         event = EVENTS;
111                 while (event && !match(calendar, event->cal->name))
112                         event = event->next;
113                 if (event)
114                         SEL = event->start;
115         }
116
117         /* Print days or week */
118         if (print_day || print_week) {
119                 event_t *event = EVENTS;
120                 for (int d = 0; d < (print_day ? 1 : 7); d++) {
121                         /* Get start and end of the day */
122                         date_t start = {SEL.year, SEL.month, SEL.day,  0, 0};
123                         date_t end   = {SEL.year, SEL.month, SEL.day, 24, 0};
124
125                         add_days(&start.year, &start.month, &start.day, d);
126                         add_days(&end.year,   &end.month,   &end.day,   d);
127
128
129                         /* Print day header */
130                         wday_t wday = day_of_week(start.year, start.month, start.day);
131                         printf("%s%s, %s %d, %d\n",
132                                 d ? "\n" : "",
133                                 day_to_string(wday),
134                                 month_to_string(start.month),
135                                 start.day+1,
136                                 start.year);
137
138                         /* Skip forward to the day */
139                         while (event && compare(&start, &event->start) > 0)
140                                 event = event->next;
141
142                         /* Print event info */
143                         int printed = 0;
144                         while (event && compare(&end, &event->start) > 0) {
145                                 printf("%s  %02d:%02d",
146                                         printed ? "\n" : "",
147                                         event->start.hour,
148                                         event->start.min);
149                                 if (event->name)
150                                         printf("  %s\n", event->name);
151                                 if (event->desc)
152                                         printf("\n  %s\n", event->desc);
153                                 printed++;
154                                 event = event->next;
155                         }
156
157                         /* Print no events */
158                         if (!printed)
159                                 printf("  No events\n");
160                 }
161
162                 exit(0);
163         }
164 }