#include #include #include #include #include #include #include #include #include #define TODO_PROGRAM "todo" #define TODO_VERSION "0.1.0" #define MAX_ITEMS 4096 #define MAX_TEXT 256 #define LEFT_MARGIN 2 #define SECTION_INDENT 2 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 int is_section(const struct Item* item) { return item->text[0] == '='; } 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++) { if (is_section(&list->items[i])) { (void) fprintf(file, "%s\n", list->items[i].text); continue; } 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) { static const char reset[] = "\x1b[?25h\x1b[0 q\x1b[?1049l"; (void) tcsetattr(STDIN_FILENO, TCSAFLUSH, &saved_termios); (void) write(STDOUT_FILENO, reset, sizeof reset - 1); } 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 to_roman(int number, char* buffer, size_t size) { static const int values[] = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 }; static const char* const symbols[] = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" }; size_t length = 0; buffer[0] = '\0'; for (int i = 0; i < 13; i++) while (number >= values[i] && length + 2 < size) { length += (size_t) snprintf(buffer + length, size - length, "%s", symbols[i]); number -= values[i]; } } static void render(const struct List* list, int cursor, int scroll, enum Mode mode) { int rows = screen_rows(); int visible = content_rows(); int total_items = 0; int done = 0; for (int i = 0; i < list->count; i++) if (!is_section(&list->items[i])) { total_items++; done += list->items[i].state == STATE_DONE; } int max_run = 0; int run = 0; for (int i = 0; i < list->count; i++) { if (is_section(&list->items[i])) run = 0; else if (++run > max_run) max_run = run; } int number_width = 1; for (int count = max_run; count >= 10; count /= 10) number_width++; 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%*s\x1b[2m%s\x1b[0m\x1b[K\r\n\x1b[K\r\n", LEFT_MARGIN, "", date); int emitted = 0; int index = scroll; while (emitted < visible && index < list->count) { const struct Item* item = &list->items[index]; const char* background = index == cursor ? "\x1b[48;5;235m" : ""; const char* background_reset = index == cursor ? "\x1b[49m" : ""; if (is_section(item)) { if (emitted > 0) { length += snprintf(buffer + length, sizeof buffer - (size_t) length, "\x1b[K\r\n"); emitted++; if (emitted >= visible) break; } if (mode == MODE_INSERT && index == cursor) { length += snprintf(buffer + length, sizeof buffer - (size_t) length, "%s%*s\x1b[1m%s\x1b[22m\x1b[K%s\r\n", background, LEFT_MARGIN, "", item->text, background_reset); } else { int section_number = 0; for (int k = 0; k <= index; k++) if (is_section(&list->items[k])) section_number++; char roman[16]; to_roman(section_number, roman, sizeof roman); const char* name = item->text + 1; while (*name == ' ') name++; length += snprintf(buffer + length, sizeof buffer - (size_t) length, "%s%*s\x1b[1m%s. %s\x1b[22m\x1b[K%s\r\n", background, LEFT_MARGIN, "", roman, name, background_reset); } emitted++; index++; continue; } int number = 0; int scan = index; while (scan >= 0 && !is_section(&list->items[scan])) { number++; scan--; } int item_indent = LEFT_MARGIN + (scan >= 0 ? SECTION_INDENT : 0); 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%*s\x1b[2m%*d.\x1b[22m %s %s%s%s\x1b[K%s\r\n", background, item_indent, "", number_width, number, box, dim, item->text, dim_reset, background_reset); emitted++; index++; } while (emitted < visible) { length += snprintf(buffer + length, sizeof buffer - (size_t) length, "\x1b[K\r\n"); emitted++; } length += snprintf(buffer + length, sizeof buffer - (size_t) length, "\x1b[%d;1H%*s\x1b[2m-- %s -- %d/%d done\x1b[0m\x1b[K", rows, LEFT_MARGIN, "", mode_name(mode), done, total_items); if (cursor >= 0 && cursor < list->count) { int rows_before = 0; for (int i = scroll; i < cursor; i++) rows_before += is_section(&list->items[i]) && i > scroll ? 2 : 1; if (is_section(&list->items[cursor]) && cursor > scroll) rows_before++; int cursor_row = 3 + rows_before; int text_length = (int) strlen(list->items[cursor].text); int cursor_column; if (is_section(&list->items[cursor])) { cursor_column = mode == MODE_INSERT ? LEFT_MARGIN + text_length + 1 : LEFT_MARGIN + 1; } else { int scan = cursor - 1; while (scan >= 0 && !is_section(&list->items[scan])) scan--; int item_indent = LEFT_MARGIN + (scan >= 0 ? SECTION_INDENT : 0); int text_start = item_indent + number_width + 7; cursor_column = mode == MODE_INSERT ? text_start + text_length : text_start; } const char* shape = mode == MODE_INSERT ? "\x1b[6 q" : "\x1b[2 q"; length += snprintf(buffer + length, sizeof buffer - (size_t) length, "\x1b[%d;%dH%s\x1b[?25h", cursor_row, cursor_column, shape); } 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 && !is_section(&list->items[*cursor])) { 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 && !is_section(&list->items[*cursor])) { 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; case 'g': { char next; if (read(STDIN_FILENO, &next, 1) == 1 && next == 'g') *cursor = 0; break; } case 'G': *cursor = list->count - 1; break; case '}': { int next = *cursor + 1; while (next < list->count && !is_section(&list->items[next])) next++; *cursor = next < list->count ? next : list->count - 1; break; } case '{': { int previous = *cursor - 1; while (previous >= 0 && !is_section(&list->items[previous])) previous--; *cursor = previous >= 0 ? previous : 0; break; } case 'd': { char next; if (read(STDIN_FILENO, &next, 1) == 1 && next == 'd') { delete_item(list, *cursor); save(list, path); } break; } case 'J': if (*cursor >= 0 && *cursor + 1 < list->count) { struct Item moved = list->items[*cursor]; list->items[*cursor] = list->items[*cursor + 1]; list->items[*cursor + 1] = moved; (*cursor)++; save(list, path); } break; case 'K': if (*cursor > 0 && *cursor < list->count) { struct Item moved = list->items[*cursor]; list->items[*cursor] = list->items[*cursor - 1]; list->items[*cursor - 1] = moved; (*cursor)--; save(list, path); } break; case 'D': { int kept = 0; for (int i = 0; i < list->count; i++) if (list->items[i].state != STATE_DONE) list->items[kept++] = list->items[i]; list->count = kept; save(list, path); 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; }