2 * Copyright (C) 2016 Andy Spencer <andy753421@gmail.com>
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.
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.
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/>.
33 const char *short_options = "hdw";
35 struct option long_options[] = {
36 {"help", 0, NULL, 'h'},
37 {"day", 2, NULL, 'd'},
38 {"week", 2, NULL, 'w'},
41 static int print_day = 0;
42 static int print_week = 0;
43 static char *calendar = NULL;
46 static void usage(char *name)
49 printf(" %s [OPTION...] [CALENDAR]\n", name);
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");
58 void args_setup(int _argc, char **_argv)
69 int c = getopt_long(argc, argv,
70 short_options, long_options, NULL);
87 /* Save default calendar */
88 calendar = argv[optind];
91 for (int i = optind; i < argc; i++)
92 cal_config("ical", argv[i], "location", argv[i]);
94 /* Validate arguments */
95 if (print_day && print_week)
96 error("Cannot print both day and week");
101 /* Focus the default 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)
107 while (event && !match(calendar, event->cal->name))
111 while (event && !match(calendar, event->cal->name))
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};
125 add_days(&start.year, &start.month, &start.day, d);
126 add_days(&end.year, &end.month, &end.day, d);
128 /* Print day header */
129 wday_t wday = day_of_week(start.year, start.month, start.day);
132 printf("%s, %s %d, %d\n",
134 month_to_string(start.month),
138 /* Skip forward to the day */
139 while (event && compare(&start, &event->start) > 0)
142 /* Print event info */
144 while (event && compare(&end, &event->start) > 0) {
147 printf("* %02d:%02d - %02d:%02d",
148 event->start.hour, event->start.min,
149 event->end.hour, event->end.min);
153 printf(" %s\n", event->name);
155 printf(" Location: %s\n", event->loc);
157 printf(" Description: %s\n", event->desc);
162 /* Print no events */
164 printf(" No events\n");