From 127e4e3fe6377ef5857b790e3f843f8449f1e9d9 Mon Sep 17 00:00:00 2001 From: dam Date: Wed, 28 Sep 2022 07:03:31 +0000 Subject: Fixed bug in delete_task. Improved user input control for creating new task. Implemented prototype version of task-archive/unarchive feature. --- main.c | 65 ++++++++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 40 insertions(+), 25 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index f704ccc..6275e90 100644 --- a/main.c +++ b/main.c @@ -298,7 +298,7 @@ bool delete_task(database_t *db, task_t *task) { if (db->count <= (current_capacity >> 2)) { size_t new_capacity = current_capacity >> 1; task_t *new_tasks = realloc(db->tasks, new_capacity * SIZEOF_TASK_T); - if (new_tasks == NULL) { + if (new_tasks == NULL && new_capacity > 0) { fprintf(stderr, "Failed to shrink database.\n"); return false; } @@ -996,16 +996,15 @@ int main(int argc, char *argv[]) { store_database(&database, DB_BIN_PATH_NAME); } - int ch; ungetch(KEY_RESIZE); - while((ch = getch()) != 'q') { - + for (int key; (key = getch()) != 'q'; ) { + timeout(INPUT_AWAIT_INF); task_t *active_task = get_active_task(db); task_t *selected_task = get_selected_task(db); update_timers(&database); - switch(ch) { + switch(key) { // When getch() times out. case ERR: @@ -1020,34 +1019,25 @@ int main(int argc, char *argv[]) { case KEY_F(1): { - + // Create new task. task_t *new_task; if (create_task(db, &new_task) == false) { - // ERROR + // TODO ERROR break; } - // TODO THIS SUCKS - db->selected_task = db->count-1; - draw_tui(db, &layouts[size_x > 100 ? L_NORMAL : L_COMPACT]); - int row = (db->selected_task % (size_y - 2)) + 1; - - attron(A_BOLD); - mvaddch(row, 0, ACS_DIAMOND); - clrtoeol(); - mvaddch(row, size_x-1, ACS_VLINE); - curs_set(1); + // Set new task name. + time_t now_utc = time(NULL); + struct tm *now_local = localtime(&now_utc); + strftime(new_task->name, MAX_TASK_NAME, "%Y-%m-%d %H:%M:%S", now_local); - mvgetnstr(row, 1, new_task->name, MAX_TASK_NAME-1); - new_task->name[MAX_TASK_NAME-1] = '\0'; - truncate_string_utf8(new_task->name, MAX_TASK_NAME-1); + // Select new task. + selected_task = new_task; + db->selected_task = selected_task - db->tasks; - if (is_empty_string(new_task->name) == true) { - strcpy(new_task->name, "-- new task --"); - } + // Force rename action. + ungetch(KEY_F(2)); - curs_set(0); - attrset(A_NORMAL); break; } @@ -1087,6 +1077,7 @@ int main(int argc, char *argv[]) { } case 'c': + case 'C': if (active_task != NULL) { db->selected_task = db->active_task; } @@ -1126,6 +1117,30 @@ int main(int argc, char *argv[]) { } break; + case 'a': + case 'A': + if (selected_task == NULL || selected_task == active_task) { + break; + } + + // TODO + // Would be nice to mark several tasks to be archived then really do it, + // instead of loading adding and storing every single time. + // If the archive was in CSV, we could simply append to it. Let's do this! + if (db == &database) { + if (load_database(&archive, AR_BIN_PATH_NAME) == false) { + store_database(&archive, AR_BIN_PATH_NAME); + } + } + database_t *target = (db == &database ? &archive : &database); + add_task(target, selected_task); + delete_task(db, selected_task); + if (db == &database) { + store_database(&archive, AR_BIN_PATH_NAME); + reset_database(&archive); + } + break; + case KEY_LEFT: break; -- cgit v1.2.3