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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
|
#include <ctype.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <time.h>
#include <unistd.h>
#define todo_program "todo"
#define todo_version "0.1.0"
#define max_items 4096
#define max_text 256
enum State
{
STATE_TODO,
STATE_DOING,
STATE_DONE
};
struct Item
{
enum State state;
char text[max_text];
};
struct List
{
struct Item items[max_items];
int count;
};
enum Mode
{
MODE_NORMAL,
MODE_INSERT
};
static const char* mode_name(enum Mode mode)
{
switch (mode)
{
case MODE_NORMAL: return "NORMAL";
case MODE_INSERT: return "INSERT";
}
return "?";
}
static void todo_path(char* buffer, size_t size)
{
const char* env_path = getenv("TODO_FILE");
if (env_path != NULL && env_path[0] != '\0')
{
(void) snprintf(buffer, size, "%s", env_path);
return;
}
const char* home = getenv("HOME");
(void) snprintf(buffer, size, "%s/.todo.txt", home != NULL ? home : ".");
}
static void strip_newline(char* string)
{
size_t length = strlen(string);
while (length > 0 && (string[length - 1] == '\n' || string[length - 1] == '\r'))
string[--length] = '\0';
}
static void load(struct List* list, const char* path)
{
list->count = 0;
FILE* file = fopen(path, "r");
if (file == NULL)
return;
char line[max_text + 8];
while (list->count < max_items && fgets(line, sizeof line, file) != NULL)
{
strip_newline(line);
enum State state = STATE_TODO;
char* text = line;
if (line[0] == '[' && line[1] != '\0' && line[2] == ']')
{
if (line[1] == 'x' || line[1] == 'X')
state = STATE_DONE;
else if (line[1] == '-')
state = STATE_DOING;
text = line + 3;
while (*text == ' ')
text++;
}
if (*text == '\0')
continue;
struct Item* item = &list->items[list->count++];
item->state = state;
(void) snprintf(item->text, sizeof item->text, "%s", text);
}
(void) fclose(file);
}
static void save(const struct List* list, const char* path)
{
FILE* file = fopen(path, "w");
if (file == NULL)
return;
for (int i = 0; i < list->count; i++)
{
char marker = list->items[i].state == STATE_DONE ? 'x'
: list->items[i].state == STATE_DOING ? '-'
: ' ';
(void) fprintf(file, "[%c] %s\n", marker, list->items[i].text);
}
(void) fclose(file);
}
static void insert_item(struct List* list, int index)
{
if (list->count >= max_items)
return;
if (index < 0)
index = 0;
if (index > list->count)
index = list->count;
for (int i = list->count; i > index; i--)
list->items[i] = list->items[i - 1];
list->items[index].state = STATE_TODO;
list->items[index].text[0] = '\0';
list->count++;
}
static void delete_item(struct List* list, int index)
{
if (index < 0 || index >= list->count)
return;
for (int i = index; i < list->count - 1; i++)
list->items[i] = list->items[i + 1];
list->count--;
}
static struct termios saved_termios;
static void restore_terminal(void)
{
(void) tcsetattr(STDIN_FILENO, TCSAFLUSH, &saved_termios);
(void) write(STDOUT_FILENO, "\x1b[?25h\x1b[?1049l", 14);
}
static void on_signal(int signal_number)
{
(void) signal_number;
exit(EXIT_SUCCESS);
}
static int enter_raw_mode(void)
{
if (!isatty(STDIN_FILENO))
{
(void) fprintf(stderr, "%s: not a terminal\n", todo_program);
return -1;
}
if (tcgetattr(STDIN_FILENO, &saved_termios) != 0)
return -1;
struct termios raw = saved_termios;
raw.c_lflag &= ~((tcflag_t) (ICANON | ECHO | ISIG));
raw.c_cc[VMIN] = 1;
raw.c_cc[VTIME] = 0;
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw) != 0)
return -1;
(void) atexit(restore_terminal);
(void) signal(SIGINT, on_signal);
(void) signal(SIGTERM, on_signal);
(void) write(STDOUT_FILENO, "\x1b[?1049h\x1b[?25l", 14);
return 0;
}
static int screen_rows(void)
{
struct winsize window;
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &window) == 0 && window.ws_row > 0)
return window.ws_row;
return 24;
}
static int content_rows(void)
{
int rows = screen_rows() - 3;
return rows < 1 ? 1 : rows;
}
static void render(const struct List* list, int cursor, int scroll, enum Mode mode)
{
int rows = screen_rows();
int visible = content_rows();
int number_width = 1;
for (int total = list->count; total >= 10; total /= 10)
number_width++;
int done = 0;
for (int i = 0; i < list->count; i++)
done += list->items[i].state == STATE_DONE;
char date[64];
time_t now = time(NULL);
struct tm* local = localtime(&now);
if (local == NULL || strftime(date, sizeof date, "%a %d %b %Y", local) == 0)
date[0] = '\0';
char buffer[64 * 1024];
int length = 0;
length += snprintf(buffer + length, sizeof buffer - (size_t) length,
"\x1b[H\x1b[2m%s\x1b[0m\x1b[K\r\n\x1b[K\r\n", date);
for (int row = 0; row < visible; row++)
{
int index = scroll + row;
if (index >= list->count)
{
length += snprintf(buffer + length, sizeof buffer - (size_t) length, "\x1b[K\r\n");
continue;
}
const struct Item* item = &list->items[index];
const char* background = index == cursor ? "\x1b[48;5;235m" : "";
const char* background_reset = index == cursor ? "\x1b[49m" : "";
const char* box = item->state == STATE_DONE ? "[x]"
: item->state == STATE_DOING ? "[-]"
: "[ ]";
const char* dim = item->state == STATE_DONE ? "\x1b[2m" : "";
const char* dim_reset = item->state == STATE_DONE ? "\x1b[22m" : "";
length += snprintf(buffer + length, sizeof buffer - (size_t) length,
"%s\x1b[2m%*d.\x1b[22m %s %s%s%s\x1b[K%s\r\n",
background, number_width, index + 1,
box, dim, item->text, dim_reset, background_reset);
}
length += snprintf(buffer + length, sizeof buffer - (size_t) length,
"\x1b[%d;1H\x1b[2m-- %s -- %d/%d done\x1b[0m\x1b[K", rows, mode_name(mode), done, list->count);
if (mode == MODE_INSERT && cursor >= 0 && cursor < list->count)
{
int cursor_row = cursor - scroll + 3;
int cursor_column = number_width + 7 + (int) strlen(list->items[cursor].text);
length += snprintf(buffer + length, sizeof buffer - (size_t) length,
"\x1b[%d;%dH\x1b[?25h", cursor_row, cursor_column);
}
else
{
length += snprintf(buffer + length, sizeof buffer - (size_t) length, "\x1b[?25l");
}
(void) write(STDOUT_FILENO, buffer, (size_t) length);
}
static int normal_key(struct List* list, const char* path, int* cursor, enum Mode* mode, char character)
{
switch (character)
{
case 'q':
return 0;
case 'j':
(*cursor)++;
break;
case 'k':
(*cursor)--;
break;
case ' ':
if (*cursor >= 0 && *cursor < list->count)
{
struct Item* item = &list->items[*cursor];
item->state = (enum State) ((item->state + 1) % 3);
save(list, path);
}
break;
case 'x':
if (*cursor >= 0 && *cursor < list->count)
{
struct Item* item = &list->items[*cursor];
item->state = item->state == STATE_DONE ? STATE_TODO : STATE_DONE;
save(list, path);
}
break;
case 'a':
case 'o':
case 'i':
insert_item(list, *cursor + 1);
*cursor = *cursor + 1 < list->count ? *cursor + 1 : list->count - 1;
*mode = MODE_INSERT;
break;
case 'A':
if (list->count == 0)
{
insert_item(list, 0);
*cursor = 0;
}
if (*cursor >= 0 && *cursor < list->count)
*mode = MODE_INSERT;
break;
case 'R':
if (list->count == 0)
{
insert_item(list, 0);
*cursor = 0;
}
if (*cursor >= 0 && *cursor < list->count)
{
list->items[*cursor].text[0] = '\0';
*mode = MODE_INSERT;
}
break;
default:
break;
}
return 1;
}
static void insert_key(struct List* list, const char* path, int* cursor, enum Mode* mode, char character)
{
struct Item* item = &list->items[*cursor];
size_t length = strlen(item->text);
if (character == 27 || ((character == '\r' || character == '\n') && item->text[0] == '\0'))
{
if (item->text[0] == '\0')
{
delete_item(list, *cursor);
if (*cursor >= list->count)
*cursor = list->count - 1;
if (*cursor < 0)
*cursor = 0;
}
save(list, path);
*mode = MODE_NORMAL;
return;
}
if (character == '\r' || character == '\n')
{
save(list, path);
insert_item(list, *cursor + 1);
(*cursor)++;
return;
}
if (character == 127 || character == 8)
{
if (length > 0)
item->text[length - 1] = '\0';
return;
}
if (isprint((unsigned char) character) && length + 1 < sizeof item->text)
{
item->text[length] = character;
item->text[length + 1] = '\0';
}
}
static void run(struct List* list, const char* path)
{
if (enter_raw_mode() != 0)
exit(EXIT_FAILURE);
enum Mode mode = MODE_NORMAL;
int cursor = 0;
int scroll = 0;
for (;;)
{
int visible = content_rows();
if (cursor >= list->count)
cursor = list->count - 1;
if (cursor < 0)
cursor = 0;
if (cursor < scroll)
scroll = cursor;
if (cursor >= scroll + visible)
scroll = cursor - visible + 1;
render(list, cursor, scroll, mode);
char character;
if (read(STDIN_FILENO, &character, 1) != 1)
break;
switch (mode)
{
case MODE_NORMAL:
if (normal_key(list, path, &cursor, &mode, character) == 0)
return;
break;
case MODE_INSERT:
insert_key(list, path, &cursor, &mode, character);
break;
}
}
}
int main(void)
{
char path[4096];
todo_path(path, sizeof path);
struct List* list = malloc(sizeof *list);
if (list == NULL)
{
(void) fprintf(stderr, "%s: out of memory\n", todo_program);
return EXIT_FAILURE;
}
load(list, path);
run(list, path);
free(list);
return EXIT_SUCCESS;
}
|