-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimetracker.c
446 lines (410 loc) · 9.99 KB
/
timetracker.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#include <curses.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
/*************************************************************************
* macros
************************************************************************/
#define TT_NAME_SZ 80
#define MAX_TIMETRACKERS 9
#define TO_STR_HELPER(x) #x
#define TO_STR(x) TO_STR_HELPER(x)
/*************************************************************************
* types
************************************************************************/
struct timetracker {
/** name of the timer. */
char name[TT_NAME_SZ];
/** nonzero if the timer is running. */
int running;
/** When running = 0, this is the amount of time remaining. */
time_t remaining_seconds;
/** When running = 1, this is the time when the timer will be
* finished. */
time_t finish_time;
};
/** Represents a timetracker color */
struct tt_color {
int fg, bg;
};
/** Names a timetracker color */
enum tt_color_name {
TT_COLOR_NORMAL = 1,
TT_COLOR_RUNNING = 2,
TT_COLOR_DONE = 3,
TT_COLOR_MAX,
};
/*************************************************************************
* globals
************************************************************************/
/** Nonzero if we are drawing colors */
int g_use_color;
const static struct tt_color tt_colors[TT_COLOR_MAX] = {
[ TT_COLOR_NORMAL ] = { COLOR_BLACK, COLOR_WHITE },
[ TT_COLOR_RUNNING ] = { COLOR_GREEN, COLOR_WHITE },
};
/*************************************************************************
* functions
************************************************************************/
/** Turns off a timetracker
*
* @param timetracker the timetracker
*
* @return 0 on success; error code otherwise
*/
static int timetracker_off(struct timetracker *timetracker)
{
time_t cur_time;
if (! timetracker->running)
return 0;
cur_time = time(NULL);
if (timetracker->finish_time <= cur_time) {
timetracker->remaining_seconds = 0;
timetracker->running = 0;
timetracker->finish_time = 0;
return 0;
}
timetracker->remaining_seconds = timetracker->finish_time - cur_time;
timetracker->running = 0;
timetracker->finish_time = 0;
return 0;
}
/** Turns on a timetracker
*
* @param timetracker the timetracker
*
* @return 0 on success; error code otherwise
*/
static int timetracker_on(struct timetracker *timetracker)
{
time_t cur_time;
if (timetracker->running)
return 0;
cur_time = time(NULL);
timetracker->finish_time = cur_time + timetracker->remaining_seconds;
timetracker->running = 1;
timetracker->remaining_seconds = 0;
return 0;
}
/** Parse a line in a timetracker file
*
* @param line The line to parse
* @param timetracker (out param) the timetracker
*
* @return negative values on error,
* 0 if no timetrackers were created,
* the number of timetrackers created otherwise.
*/
static int parse_timetracker(char *line,
struct timetracker *timetracker)
{
int len, res;
int minutes;
// reject comments
if (line[0] == '#')
return 0;
// reject empty lines
len = strlen(line);
if (len < 1)
return 0;
// trim newline if present
if (line[len - 1] == '\n') {
line[len - 1] = '\0';
len--;
}
res = sscanf(line, "%" TO_STR(TT_NAME_SZ) "[^=]=%dM",
timetracker->name, &minutes);
if (res != 2) {
return -1000;
}
timetracker->running = 0;
timetracker->remaining_seconds = minutes * 60;
timetracker->finish_time = 0;
return 1;
}
/** Parse the config options, which are provided as environment variables.
*
* @param filename The name of the configuration file to use
* @param timetrackers (out param) A handle to an array of timetrackers
* @param num_trackers (out param) The number of timetrackers in the array
*
* @return 0 on success; error code otherwise
*/
static int get_timetrackers(const char *filename,
struct timetracker **timetrackers, int *num_trackers)
{
FILE *f;
struct timetracker *ret;
int num_tt, line_no;
f = fopen(filename, "r");
if (!f) {
int err = errno;
fprintf(stderr, "%s: failed to open %s: %s (%d)\n",
__func__, filename, strerror(err), err);
goto cleanup0;
}
num_tt = 0;
line_no = 0;
ret = calloc(1, sizeof(struct timetracker) * (MAX_TIMETRACKERS + 1));
if (! ret) {
fprintf(stderr, "%s: out of memory\n", __func__);
goto cleanup1;
}
while (1) {
char line[160];
int out;
const char *res = fgets(line, sizeof(line), f);
line_no++;
if (! res) {
int err;
if (feof(f))
break;
err = errno;
fprintf(stderr, "%s: fgets error on line %d: "
"%s (%d)\n",
__func__, line_no, strerror(err), err);
goto cleanup2;
}
if (num_tt >= MAX_TIMETRACKERS) {
fprintf(stderr, "%s: off-by-one error.\n",
__func__);
goto cleanup2;
}
out = parse_timetracker(line, ret + num_tt);
if (out < 0) {
fprintf(stderr, "%s: failed to parse line %d (%s)\n",
__func__, line_no, line);
goto cleanup2;
}
else if (out > 0) {
num_tt += out;
}
}
if (fclose(f)) {
int err = errno;
fprintf(stderr, "%s: fclose returned error %s (%d)\n",
__func__, strerror(err), err);
goto cleanup2;
}
if (num_tt == 0) {
fprintf(stderr, "%s: no timetrackers found.\n",
__func__);
goto cleanup1;
}
// explicitly zero the name of the last entry.
ret[num_tt].name[0] = '\0';
*num_trackers = num_tt;
*timetrackers = ret;
return 0;
cleanup2:
fclose(f);
cleanup1:
free(ret);
cleanup0:
return 1;
}
/** Wrapper around mvaddstr that provides printf formatting semantics.
*
* @param win the curses window to draw to
* @param y y-coordinate to print the string at
* @param x x-coordinate to print the string at
* @param fmt printf-style formatting string
* @param ... Format arguments
*/
void mvaddstr_printf(WINDOW *win, int y, int x, const char *fmt, ...)
{
char str[160];
va_list ap;
va_start(ap, fmt);
vsnprintf(str, sizeof(str), fmt, ap);
va_end(ap);
mvwaddstr(win, y, x, str);
}
/** Draws all timetrackers on the screen.
*
* @param win the curses window to draw to
* @param timetracker the array of timetrackers
*/
static void draw_timetrackers(WINDOW *win, struct timetracker *timetracker)
{
int item_no = 1, line_no = 2;
time_t cur_time = time(NULL);
clear();
for (; timetracker->name[0]; timetracker++) {
time_t rem;
int min, sec;
int tt_color = COLOR_PAIR(TT_COLOR_NORMAL);
if (!timetracker->running) {
rem = timetracker->remaining_seconds;
}
else if (timetracker->finish_time < cur_time) {
timetracker_off(timetracker);
rem = 0;
}
else {
rem = timetracker->finish_time - cur_time;
}
min = rem / 60;
sec = rem - (min * 60);
if (g_use_color) {
if (timetracker->running)
tt_color = COLOR_PAIR(TT_COLOR_RUNNING);
attron(tt_color);
}
mvaddstr_printf(win, line_no, 3,
" [%d] %3d:%02d %s",
item_no, min, sec, timetracker->name);
if (g_use_color) {
attroff(tt_color);
}
line_no += 2;
item_no++;
}
mvwaddstr(win, 0, 0, " ");
}
static void shutdown_curses(void)
{
clear();
endwin();
}
/** Initialize colors in the curses library
*
* @return 0 on success; error code otherwise
*/
static int timetracker_init_color(void)
{
size_t i;
if (assume_default_colors(COLOR_BLACK, COLOR_WHITE)) {
fprintf(stderr, "%s: your terminal does not support "
"assume_default_colors. Avoiding color because it "
"would probably look ugly.\n", __func__);
return 1000;
}
if (start_color() == ERR) {
fprintf(stderr, "%s: start_color failed.\n", __func__);
return 1001;
}
for (i = 1; i < TT_COLOR_MAX; i++) {
const struct tt_color *c = tt_colors + i;
if (init_pair(i, c->fg, c->bg) == ERR) {
fprintf(stderr, "%s: init_pair(%Zd) failed.\n",
__func__, i);
}
}
return 0;
}
/** Initialize curses
*
* @return A pointer to stdscr on success; NULL
* otherwise
*/
static WINDOW *init_curses(void)
{
WINDOW *ret;
if (atexit(shutdown_curses)) {
fprintf(stderr, "%s: registering shutdown_curses failed.\n",
__func__);
return NULL;
}
ret = initscr();
if (! ret)
return NULL;
if (g_use_color) {
int res = timetracker_init_color();
if (res) {
g_use_color = 0;
fprintf(stderr, "%s: start_color failed with "
"error code %d. Does this terminal "
"support color?\n", __func__, res);
}
}
noecho();
intrflush(stdscr, FALSE);
halfdelay(1);
//timeout(1);
keypad(stdscr, TRUE);
clear();
return ret;
}
static void usage(void)
{
printf("timetracker: a program to track time.\n\
This program maintains multiple stopwatches to track time.\n\
The timers are defined in a configuration file.\n\
\n\
usage: timetracker [options]\n\
options include:\n\
-f [conf file] The configuration file to use\n\
-h Print this help message and quit\n\
-N Do *not* try to use color\n\
");
}
int main(int argc, char **argv)
{
int c;
const char *conf_file = NULL;
WINDOW *win;
struct timetracker *timetrackers;
int num_timetrackers;
chtype k;
g_use_color = 1;
while ((c = getopt(argc, argv, "f:hN")) != -1) {
switch(c) {
case 'f':
conf_file = optarg;
break;
case 'h':
usage();
exit(1);
break;
case 'N':
g_use_color = 0;
break;
case ':':
fprintf(stderr,
"Option -%c requires an operand\n", optopt);
usage();
exit(1);
break;
case '?':
fprintf(stderr, "Unrecognized option: -%c\n", optopt);
usage();
exit(1);
break;
}
}
if (!conf_file) {
fprintf(stderr, "You must specify a configuration file.\n");
usage();
exit(1);
}
// parse config options
if (get_timetrackers(conf_file, &timetrackers, &num_timetrackers)) {
fprintf(stderr, "error initializing timetrackers.\n");
exit(1);
}
win = init_curses();
if (! win) {
fprintf(stderr, "error initializing the curses library.\n");
exit(1);
}
while (1) {
draw_timetrackers(win, timetrackers);
k = getch();
if (k == 'q')
exit(0);
if (k >= '1' && k <= '9') {
int n = k - '1';
if (n < num_timetrackers) {
if (timetrackers[n].running)
timetracker_off(timetrackers + n);
else
timetracker_on(timetrackers + n);
}
}
}
return 0;
}