1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
#include "print.h"
#include "entry.h"
#include "opt.h"
#include "err.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
static const char *weekday_names[] = {
"Sun",
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat"
};
static const char *weekday_names_l[] = {
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
};
static const char *month_names[] = {
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
};
struct buf_s {
time_t rt;
struct entry_s *ep;
};
static int buf_cmp(const void *a, const void *b)
{
const struct buf_s *a1 = a, *b1 = b;
if (b1->ep->urgent && !(a1->ep->urgent))
return 1;
if (a1->ep->urgent && !(b1->ep->urgent))
return -1;
if (a1->rt > b1->rt)
return 1;
if (b1->rt > a1->rt)
return -1;
return 0;
}
void print_console(struct calendar_s *cal)
{
size_t i, j;
struct tm t = { 0 }, ttmp;
time_t now = time(NULL), *rttmp;
time_t ototois_end, yesterdays_end, todays_end, tomorrows_end, weeks_end;
bool has_urgent = false, urgent_printed = false;
struct buf_s *buf = malloc(cal->count * sizeof(*buf));
if (buf == NULL) {
ERRM("failed to allocate memory");
return;
}
/* get interval limits */
t = *localtime(&now);
ttmp.tm_year = t.tm_year;
ttmp.tm_mon = t.tm_mon;
ttmp.tm_mday = t.tm_mday - 1;
ttmp.tm_hour = 0;
ttmp.tm_min = 0;
ttmp.tm_sec = 0;
ttmp.tm_isdst = -1;
ototois_end = mktime(&ttmp);
ttmp.tm_mday++;
yesterdays_end = mktime(&ttmp);
ttmp.tm_mday++;
todays_end = mktime(&ttmp);
ttmp.tm_mday++;
tomorrows_end = mktime(&ttmp);
ttmp.tm_mday = t.tm_mday + (6 - t.tm_wday) + 1;
weeks_end = mktime(&ttmp);
printf("%s %d %s %02d %02d:%02d\n\n",
weekday_names[t.tm_wday],
t.tm_year + 1900,
month_names[t.tm_mon],
t.tm_mday,
t.tm_hour,
t.tm_min
);
/* fill buffer with active events */
for (i = 0, j = 0; i < cal->count; i++) {
if ( !(rttmp = entry_is_active( &(cal->entries[i]), now)) )
continue;
buf[j].rt = *rttmp;
buf[j].ep = &(cal->entries[i]);
if (buf[j].ep->urgent)
has_urgent = true;
j++;
}
qsort(buf, j, sizeof(*buf), buf_cmp);
/* print from buffer */
if (has_urgent)
printf("\n[ URGENT ]\n\n");
for (i = 0; i < j; i++) {
t = *localtime( &(buf[i].rt) );
if (has_urgent && !urgent_printed && !(buf[i].ep->urgent) ) {
printf("\n\n[ normal ]\n\n");
urgent_printed = true;
}
/* date */
if (ototois_end > buf[i].rt) {
printf("%04d %s %02d",
t.tm_year + 1900,
month_names[t.tm_mon],
t.tm_mday
);
} else if (yesterdays_end > buf[i].rt) {
printf("Yesterday ");
} else if (todays_end > buf[i].rt) {
printf("Today ");
} else if (tomorrows_end > buf[i].rt) {
printf("Tomorrow ");
} else if (weeks_end > buf[i].rt) {
printf("%-11s", weekday_names_l[t.tm_wday]);
} else {
printf("%04d %s %02d",
t.tm_year + 1900,
month_names[t.tm_mon],
t.tm_mday
);
}
/* time */
if (t.tm_hour != 0 || t.tm_min != 0 || t.tm_sec != 0) {
printf(" %02d:%02d", t.tm_hour, t.tm_min);
if (t.tm_sec != 0)
printf(":%02d", t.tm_sec);
}
if (buf[i].ep->msg != NULL)
printf(" %s", buf[i].ep->msg);
puts("");
}
free(buf);
}
void print_script(struct calendar_s *cal)
{
size_t i, j;
struct tm t = { 0 };
time_t now = time(NULL), *rttmp;
char cd;
bool first = true;
struct buf_s *buf = malloc(cal->count * sizeof(*buf));
if (buf == NULL) {
ERRM("failed to allocate memory");
return;
}
cd = opt_col_delim();
/* fill buffer with active events */
for (i = 0, j = 0; i < cal->count; i++) {
if ( !(rttmp = entry_is_active( &(cal->entries[i]), now)) )
continue;
buf[j].rt = *rttmp;
buf[j].ep = &(cal->entries[i]);
j++;
}
qsort(buf, j, sizeof(*buf), buf_cmp);
/* print from buffer */
for (i = 0; i < j; i++) {
t = *localtime( &(buf[i].rt) );
if (!first)
printf("%c", opt_line_delim());
printf(
"%d%c%d%c%d%c%d%c%d%c%d%c" /* date */
"%d%c" /* urgent */
"%d%c" /* local */
"%s" /* msg */
,
t.tm_year + 1900, cd, t.tm_mon + 1, cd, t.tm_mday, cd,
t.tm_hour, cd, t.tm_min, cd, t.tm_sec, cd,
cal->entries[i].urgent, cd,
cal->entries[i].local, cd,
(cal->entries[i].msg == NULL ? "" : cal->entries[i].msg)
);
first = false;
}
free(buf);
}
|