forked from jsks/bitbar-org-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.c
360 lines (292 loc) · 11.2 KB
/
plugin.c
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
* Org-mode bitbar plugin
*
* Displays TODO items sorted according to DEADLINE and SCHEDULED
* timestamps. Configuration options can be found in config.h.
*
*/
#define _GNU_SOURCE
#define __USE_XOPEN
#include <errno.h>
#include <glob.h>
#include <libgen.h>
#include <regex.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "config.h"
#define VEC_CAPACITY 50
typedef enum {
DEADLINED,
SCHEDULED,
OPEN,
} task_type;
typedef struct {
char *file;
char *line;
time_t dln_time, sched_time;
struct tm dln, sched;
} todo_t;
static regex_t dln_re;
static regex_t sched_re;
#define menu_item(fmt, ...) \
printf(fmt " length=%d font=%s size=%d\n", \
##__VA_ARGS__, MAX_WIDTH, font, font_size)
#ifdef DEBUG
#define dbg(fmt, ...) \
fprintf(stderr, "%s:%d: " fmt, __FILE__, __LINE__, ##__VA_ARGS__)
#else
#define dbg(...) {}
#endif
#define array_len(x) (sizeof(x) / sizeof(x[0]))
#define die(msg) \
do { \
dbg("%s\n", msg); \
exit(EXIT_FAILURE); \
} while (0);
#define GEN_VEC(type) \
typedef struct { \
size_t n, capacity; \
type *data; \
void (*destroy)(); \
} vec_##type; \
\
vec_##type *init_##type(int capacity, void (*destroy)()) { \
vec_##type *v; \
size_t size = sizeof(vec_##type); \
\
if (!(v = malloc(size)) || !(v->data = calloc(capacity, sizeof(type)))) \
die("Memory error"); \
\
v->capacity = capacity; \
v->n = 0; \
v->destroy = destroy; \
\
return v; \
} \
\
void resize_##type(vec_##type *v) { \
size_t old_size = v->capacity * sizeof(type); \
v->capacity *= 2; \
size_t new_size = v->capacity * sizeof(type); \
\
if (!(v->data = realloc(v->data, new_size))) \
die("Memory error"); \
\
memset(((char *) v->data) + old_size, 0, new_size - old_size); \
} \
\
void push_##type(vec_##type *v, type x) { \
if (v->n >= v->capacity) \
resize_##type(v); \
\
v->data[v->n++] = x; \
} \
\
type *get_##type(vec_##type *v, size_t i) { \
if (i >= v->n) \
die("Out of bounds access to vector"); \
\
return &v->data[i]; \
} \
\
void destroy_##type(vec_##type **v) { \
vec_##type *p = *v; \
\
if (p->destroy) { \
for (size_t i = 0; i < p->n; i++) \
p->destroy(get_##type(p, i)); \
} \
\
free(p->data); \
free(p); \
*v = NULL; \
} \
GEN_VEC(todo_t)
GEN_VEC(size_t)
void destroy_tasks(todo_t *task) {
free(task->file);
free(task->line);
}
char *trimlc(char *str, char ch) {
if (!str)
return NULL;
while (str && *str == ch) str++;
return str;
}
char *is_header(char *str) {
if (!str || str[0] != '*')
return NULL;
if (*(str = trimlc(str, '*')) != ' ')
return NULL;
return str;
}
char *is_todo(char *str) {
if (!str || (str[0] != ' ' && str[0] != 'T'))
return NULL;
str = trimlc(str, ' ');
return strncmp(str, "TODO", 4) == 0 ? str + 4 : NULL;
}
bool set_date(char *s, regex_t *re, struct tm *t) {
regmatch_t pmatch[2];
if (regexec(re, s, 2, pmatch, 0) != 0)
return false;
size_t len = pmatch[1].rm_eo - pmatch[1].rm_so + 1;
char ss[len + 1];
memcpy(&ss, s + pmatch[1].rm_so, len);
ss[len] = '\0';
strptime(ss, "%Y-%m-%d", t);
return true;
}
bool parse_file(vec_todo_t *v, char *f) {
char *filename;
if (!(filename = basename(f)))
return false;
FILE *fp;
if (!(fp = fopen(f, "r")))
return false;
// Strip .org extension
char *c = rindex(filename, '.');
*c = '\0';
char *line = NULL;
size_t linecap = 0;
ssize_t len;
char *ss;
bool sched = false, dln = false;
todo_t *task = NULL;
while ((len = getline(&line, &linecap, fp)) > 0) {
// Strip newline
line[len - 1] = '\0';
if ((ss = is_header(line))) {
if (task) {
v->n++;
sched = dln = false;
task = NULL;
}
if (!(ss = is_todo(ss)))
continue;
if (v->n >= v->capacity)
resize_todo_t(v);
task = &v->data[v->n];
if (!(task->file = strdup(filename)) || !(task->line = strdup(ss)))
die("Memory error");
} else if (task) {
if (!sched && set_date(line, &sched_re, &task->sched)) {
task->sched_time = mktime(&task->sched);
sched = true;
}
if (!dln && set_date(line, &dln_re, &task->dln)) {
task->dln_time = mktime(&task->dln);
dln = true;
}
}
}
if (task)
v->n++;
free(line);
fclose(fp);
return true;
}
void partition(vec_todo_t *tasks, vec_size_t **visitors) {
todo_t * task;
for (size_t i = 0; i < tasks->n; i++) {
task = get_todo_t(tasks, i);
if (task->dln_time > 0)
push_size_t(visitors[DEADLINED], i);
else if (task->sched_time > 0)
push_size_t(visitors[SCHEDULED], i);
else
push_size_t(visitors[OPEN], i);
}
}
int dlnCmp(const void *a, const void *b, void *thunk) {
return difftime(get_todo_t(thunk, *(size_t *) a)->dln_time,
get_todo_t(thunk, *(size_t *) b)->dln_time);
}
int schedCmp(const void *a, const void *b, void *thunk) {
return difftime((get_todo_t(thunk, *(size_t *) a))->sched_time,
(get_todo_t(thunk, *(size_t *) b))->sched_time);
}
const char *get_line_color(char *str, const char *fallback) {
str = trimlc(str, ' ');
if (str[0] != '[')
return fallback;
for (size_t i = 0; i < array_len(priorities); i++) {
if (strncasecmp(str, priorities[i], 4) != 0)
continue;
return priority_colors[i];
}
return fallback;
}
void print_group(vec_todo_t *v, vec_size_t *idx, char *header, task_type type) {
static int limit = MAX_PRINT;
char date[20];
if (limit <= 0 || idx->n == 0)
return;
todo_t *task;
menu_item("%s | color=%s", header, header_color);
for (size_t i = 0; i < idx->n && limit >= 0; i++, limit--) {
task = get_todo_t(v, *get_size_t(idx, i));
switch (type) {
case DEADLINED:
strftime(date, sizeof(char) * 20, "%h-%d", &task->dln);
menu_item("%s: %s %s | color=%s", task->file, date, task->line,
get_line_color(task->line, deadline_task_color));
break;
case SCHEDULED:
strftime(date, sizeof(char) * 20, "%h-%d", &task->sched);
menu_item("%s: %s %s | color=%s", task->file, date, task->line,
get_line_color(task->line, scheduled_task_color));
break;
case OPEN:
menu_item("%s: %s | color=%s", task->file, task->line,
get_line_color(task->line, unscheduled_task_color));
break;
}
}
}
void print(vec_todo_t *v, vec_size_t **visitors) {
printf("Org-todo: %ld\n", v->n);
puts("---");
print_group(v, visitors[DEADLINED], "Deadlines", DEADLINED);
print_group(v, visitors[SCHEDULED], "Scheduled", SCHEDULED);
print_group(v, visitors[OPEN], "Other tasks", OPEN);
}
int main(void) {
vec_todo_t *tasks = init_todo_t(VEC_CAPACITY, destroy_tasks);
int ret;
ret = regcomp(&dln_re, "DEADLINE: <([0-9]{4}-[0-9]{2}-[0-9]{2})", REG_EXTENDED);
if (ret)
die("Unable to compile deadline regex");
ret = regcomp(&sched_re, "SCHEDULED: <([0-9]{4}-[0-9]{2}-[0-9]{2})", REG_EXTENDED);
if (ret)
die("Unable to compile scheduled regex");
glob_t g;
if (glob(org_file_pattern, GLOB_TILDE, NULL, &g) != 0)
die(strerror(errno));
for (size_t i = 0; i < g.gl_pathc; i++) {
if (!parse_file(tasks, g.gl_pathv[i]))
dbg("Unable to parse file: %s", g.gl_pathv[i]);
}
vec_size_t *visitors[3];
for (size_t i = 0; i < 3; i++)
visitors[i] = init_size_t(tasks->n, NULL);
partition(tasks, visitors);
// GNU/Linux only
qsort_r(visitors[DEADLINED]->data, visitors[DEADLINED]->n,
sizeof(size_t), dlnCmp, tasks);
qsort_r(visitors[SCHEDULED]->data, visitors[SCHEDULED]->n,
sizeof(size_t), schedCmp, tasks);
print(tasks, visitors);
#ifdef DEBUG
regfree(&dln_re);
regfree(&sched_re);
globfree(&g);
destroy_todo_t(&tasks);
for (size_t i = 0; i < 3; i++)
destroy_size_t(&visitors[i]);
#endif
return 0;
}