From 7875033562db9f337d00a8f125560efb139be611 Mon Sep 17 00:00:00 2001 From: dam Date: Tue, 30 Aug 2022 18:19:31 +0000 Subject: Improved task deletion and creation. --- main.c | 88 ++++++++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 53 insertions(+), 35 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index 946eca8..51f7bc8 100644 --- a/main.c +++ b/main.c @@ -90,6 +90,9 @@ void print_task(const task_t* task) { printf("t[6]: '%" PRIu32 "'\n", task->time[6]); } +// Macro used to calculate index of task on a database. +#define task_idx(db,task) ((ptrdiff_t)(task - db->tasks)) + // Adds task to database. If necessary, expands database capacity. // Returns success. bool add_task(database_t* db, const task_t* task) { @@ -124,9 +127,9 @@ bool add_task(database_t* db, const task_t* task) { return true; } -// Creates new database entry and points task to it. If necessary, expands database capacity. +// Creates new task returned in the pointer. If necessary, expands database capacity. // Returns success. -bool create_task(database_t* db, const task_t** task) { +bool create_task(database_t* db, task_t** task) { assert(db != NULL); if (db->count == UINT32_MAX) { @@ -146,12 +149,15 @@ bool create_task(database_t* db, const task_t** task) { fprintf(stderr, "Failed to expand database.\n"); return false; } + db->capacity = new_capacity; db->tasks = new_tasks; } - // Set new task pointer. - *task = db->tasks[db->count]; + // Prepare new task. + *task = &db->tasks[db->count]; + memset(*task, 0, SIZEOF_TASK_T); + db->count++; return true; @@ -188,6 +194,11 @@ bool remove_task(database_t* db, uint32_t index) { return true; } +bool delete_task(database_t* db, task_t* task) { + assert(task > db->tasks && task < &db->tasks[db->count]); + return remove_task(db, task - db->tasks); +} + void clear_database(database_t* db) { free(db->tasks); memset(db, 0, SIZEOF_TASK_T); @@ -316,7 +327,7 @@ bool import_from_csv(database_t* db, const char* path_name) { } uint32_t number_of_entries = 0; - task_t task; + task_t* task; char *line_buffer = NULL; size_t line_buffer_size = 0; ssize_t read_characters = 0; @@ -335,8 +346,8 @@ bool import_from_csv(database_t* db, const char* path_name) { break; } - // Clear task struct. - memset(&task, 0, SIZEOF_TASK_T); + // Prepare new task. + create_task(db, &task); // Find task name string limits. name_delimiter = strchr(line_buffer, ','); @@ -346,28 +357,28 @@ bool import_from_csv(database_t* db, const char* path_name) { } // Import task name. - memcpy(task.name, line_buffer, name_length); - truncate_string_utf8(task.name, name_length); + memcpy(task->name, line_buffer, name_length); + truncate_string_utf8(task->name, name_length); // Parse task times. if(sscanf(name_delimiter+1, "%" SCNu32 ",%" SCNu32 ",%" SCNu32 ",%" SCNu32 ",%" SCNu32 ",%" SCNu32 ",%" SCNu32, - &task.time[0], - &task.time[1], - &task.time[2], - &task.time[3], - &task.time[4], - &task.time[5], - &task.time[6] + &task->time[0], + &task->time[1], + &task->time[2], + &task->time[3], + &task->time[4], + &task->time[5], + &task->time[6] ) != 7) { replace_char(line_buffer, '\n', ' '); fprintf(stderr, "Discarding invalid line '%s' and continuing.\n", line_buffer); + delete_task(db, task); continue; } // Add new database entry. number_of_entries++; - add_task(db, &task); } fclose(file); @@ -717,8 +728,7 @@ void draw_table() { } void draw_footer() { - const char *app_name = "Task Tracker"; - const char *app_version = "v1.0"; + const char *app_name_version = "TTT v1"; int row, col; /* to store the number of rows and the number of colums of the screen */ initscr(); /* start the curses mode */ @@ -726,14 +736,11 @@ void draw_footer() { mvaddch(row-1, 0, ACS_LLCORNER); addch(' '); - printw(app_name); + printw(app_name_version); addch(' '); - for (int idx = strlen(app_name) + 3; idx < col - strlen(app_version) - 3; idx ++) { + for (int idx = strlen(app_name_version) + 3; idx < col - 1; idx ++) { addch(ACS_HLINE); } - addch(' '); - printw(app_version); - addch(' '); addch(ACS_LRCORNER); @@ -741,7 +748,7 @@ void draw_footer() { // TODO This code is now repeated accross the header, table and footer. Clean this up. layout_t* layout = &layouts[selected_layout]; int table_size = layout->table_size; - char** table_headers = layout->table_headers; +// char** table_headers = layout->table_headers; int start_columns = size_x - 1 - 8*table_size - 2; int columns[] = { @@ -756,7 +763,7 @@ void draw_footer() { start_columns+(table_size*7), }; - mvaddstr(row-1, columns[0]+2, table_headers[0]); +// mvaddstr(row-1, columns[0]+2, table_headers[0]); for (int idx = 1; idx < 9; idx++) { mvaddch(row-1, columns[idx], ACS_BTEE); } @@ -858,7 +865,6 @@ int main(int argc, char *argv[]) { my_win = create_newwin(height, width, starty, startx); ch = KEY_RESIZE; - ESCDELAY = 0; do { switch(ch) { @@ -867,21 +873,33 @@ int main(int argc, char *argv[]) { task_t* new_task; create_task(&database, &new_task); move(database.count, 2); + clrtoeol(); + mvaddch(database.count, size_x-1, ACS_VLINE); curs_set(1); - getnstr(new_task->name, MAX_TASK_NAME-1); - curs_set(0); + mvgetnstr(database.count, 2, new_task->name, MAX_TASK_NAME-1); -// task_t new_task; -// add_task(&database, &new_task); -// move(database.count, 2); -// curs_set(1); -// getnstr(database.tasks[database.count-1].name, MAX_TASK_NAME-1); -// curs_set(0); + // TODO Move this empty-name-cleaning code elsewhere. + bool is_empty = true; + int size = strlen(new_task->name); + for (int idx=0; idx < size; idx++) { + char name_char = new_task->name[idx]; + if (name_char != ' ' && name_char != '\t') { + is_empty = false; + break; + } + } + if (strlen(new_task->name) == 0 || is_empty) { + strcpy(new_task->name, "-- new task --"); + } + curs_set(0); break; } case KEY_F(2): remove_task(&database, selected_task); + if (selected_task >= database.count) { + selected_task = database.count-1; + } break; case '\n': -- cgit v1.2.3