]> Pileus Git - lackey/blob - src/args.c
1fceff6b85ab3e16f47c8e07592e4571438cc82b
[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...]\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         /* Validate arguments */
87         if (print_day && print_week)
88                 error("Cannot print both day and week");
89 }
90
91 void args_main(void)
92 {
93         if (print_day || print_week) {
94                 event_t *event = EVENTS;
95                 for (int d = 0; d < (print_day ? 1 : 7); d++) {
96                         /* Get start and end of the day */
97                         date_t start = {SEL.year, SEL.month, SEL.day,  0, 0};
98                         date_t end   = {SEL.year, SEL.month, SEL.day, 24, 0};
99
100                         add_days(&start.year, &start.month, &start.day, d);
101                         add_days(&end.year,   &end.month,   &end.day,   d);
102
103
104                         /* Print day header */
105                         wday_t wday = day_of_week(start.year, start.month, start.day);
106                         printf("%s%s, %s %d, %d\n",
107                                 d ? "\n" : "",
108                                 day_to_string(wday),
109                                 month_to_string(start.month),
110                                 start.day+1,
111                                 start.year);
112
113                         /* Skip forward to the day */
114                         while (event && compare(&start, &event->start) > 0)
115                                 event = event->next;
116
117                         /* Print event info */
118                         int printed = 0;
119                         while (event && compare(&end, &event->start) > 0) {
120                                 printf("%s  %02d:%02d",
121                                         printed ? "\n" : "",
122                                         event->start.hour,
123                                         event->start.min);
124                                 if (event->name)
125                                         printf("  %s\n", event->name);
126                                 if (event->desc)
127                                         printf("\n  %s\n", event->desc);
128                                 printed++;
129                                 event = event->next;
130                         }
131
132                         /* Print no events */
133                         if (!printed)
134                                 printf("  No events\n");
135                 }
136
137                 exit(0);
138         }
139 }