diff options
| author | dam <dam@gudinoff> | 2023-04-07 02:10:01 +0100 |
|---|---|---|
| committer | dam <dam@gudinoff> | 2023-04-07 02:10:01 +0100 |
| commit | 3bda0a6e82af3e09336b7f715de4d5aded28b78b (patch) | |
| tree | b0b4869d8d9443f8c8be6956b78b64931d339e3f /ttt.jai | |
| parent | 5a6e67d6be44e168d690d262935d5affade37fc6 (diff) | |
| download | task-time-tracker-3bda0a6e82af3e09336b7f715de4d5aded28b78b.tar.zst task-time-tracker-3bda0a6e82af3e09336b7f715de4d5aded28b78b.zip | |
Porting delete_task - WIP.
Diffstat (limited to 'ttt.jai')
| -rw-r--r-- | ttt.jai | 75 |
1 files changed, 38 insertions, 37 deletions
@@ -61,6 +61,7 @@ Database :: struct { capacity : s64; } +SIZE_OF_TASK :: #run size_of(Task); // const char DB_FILE_SIGN[] = DB_FILE_SIGN_STR; // const size_t DB_FILE_SIGN_LENGTH = sizeof(DB_FILE_SIGN_STR)-1; // const size_t SIZEOF_TASK_ST = sizeof(task_st); @@ -322,6 +323,10 @@ get_selected_task :: inline (db: Database) -> *Task { return task; } +contains_task :: inline (db: *Database, task: *Task) -> bool { + return task >= db.tasks.data && task - db.tasks.data < db.tasks.count; +} + // Adds a task to the database. // If necessary, expands database capacity. // Returns success. @@ -347,7 +352,7 @@ add_task :: (db: *Database, task: Task) -> success: bool { ifx current_capacity > (S64_MAX >> 1) then S64_MAX else current_capacity << 1; - new_tasks := realloc(db.tasks.data, new_capacity * size_of(Task), current_capacity * size_of(Task)); + new_tasks := realloc(db.tasks.data, new_capacity * SIZE_OF_TASK, current_capacity * SIZE_OF_TASK); if (new_tasks == null) { print_error("Failed to expand database."); return false; @@ -439,54 +444,54 @@ bool duplicate_task(database_st *db, task_st *task) { return true; } - +*/ // Deletes task from database. // If possible, shrinks the database capacity. // Returns success. -bool delete_task(database_st *db, task_st *task) { - assert(db != NULL); - assert(task != NULL); - assert(task >= db->tasks && task - db->tasks < db->count); +delete_task :: (db: *Database, task: *Task) -> bool { + assert(db != null); + assert(task != null); + assert(contains_task(db, task)); // Remove task timer values from total timers. - for (int idx = 0; idx < NUM_WEEK_DAYS; idx++) { - db->total_times[idx] = sub_int64(db->total_times[idx], task->times[idx]); + for task.times { + db.total_times[it_index] = sub_int64(db.total_times[it_index], it); } // Move tasks after the index position to their new positions. - ptrdiff_t index = task - db->tasks; - memmove(task, task + 1, (db->count - index - 1) * SIZEOF_TASK_ST); - db->count--; + index := task - db.tasks.data; + memmove(task, task + 1, (db.tasks.count - index - 1) * SIZE_OF_TASK); // FIXME memmove is not implemented + db.tasks.count -= 1; // Adjust selected task. - if (db->selected_task >= db->count) { - db->selected_task--; + if (db.selected_idx >= db.tasks.count) { + db.selected_idx -= 1; } // Adjust active task. - if (db->active_task > index) { - db->active_task--; + if (db.active_idx > index) { + db.active_idx -= 1; } - else if (db->active_task == index) { - db->active_task = -1; + else if (db.active_idx == index) { + db.active_idx = -1; } // If possible, shrink database capacity. - size_t current_capacity = db->capacity; - if (db->count <= (current_capacity >> 2)) { - size_t new_capacity = current_capacity >> 1; - task_st *new_tasks = realloc(db->tasks, new_capacity * SIZEOF_TASK_ST); - if (new_tasks == NULL && new_capacity > 0) { + current_capacity := db.capacity; + if (db.tasks.count <= (current_capacity >> 2)) { + new_capacity := current_capacity >> 1; + new_tasks := realloc(db.tasks.data, new_capacity * SIZEOF_TASK_ST); + if (new_tasks == null && new_capacity > 0) { print_error("Failed to shrink database."); return false; } - db->capacity = new_capacity; - db->tasks = new_tasks; + db.capacity = new_capacity; + db.tasks.data = new_tasks; } return true; } - +/* // Moves task to index. // Index gets clamped to [0, db->count[. void move_task_to_index(database_st *db, task_st *task, ptrdiff_t index) { @@ -592,7 +597,7 @@ update_total_times :: (db: *Database) { reset_task_times :: (db: *Database, task: *Task) { assert(db != null); assert(task != null); - assert(task >= db.tasks.data && task - db.tasks.data < db.tasks.count); + assert(contains_task(db, task)); // Make sure we sync before applying the changes. update_times(db); @@ -655,7 +660,7 @@ store_database :: (db: Database, path: string) -> success: bool { file_write(*file, DB_FILE_SIGN_STR); file_write(*file, *db, size_of(Database)); - file_write(*file, db.tasks.data, size_of(Task) * db.tasks.count); + file_write(*file, db.tasks.data, SIZE_OF_TASK * db.tasks.count); return true; } @@ -693,11 +698,11 @@ load_database :: (db: *Database, path: string) -> success: bool { assert(read_success == true, "Failed to read database info from '%'.", path); // Reserve database capacity for tasks. - db.tasks.data = alloc(db.tasks.count * size_of(Task)); + db.tasks.data = alloc(db.tasks.count * SIZE_OF_TASK); db.capacity = db.tasks.count; // Read database tasks. - file_read(file, db.tasks.data, size_of(Task) * db.tasks.count); + file_read(file, db.tasks.data, SIZE_OF_TASK * db.tasks.count); // Make sure we are reading all the file. buffer: u8; @@ -1579,19 +1584,15 @@ main :: () { reset_task_times(db, selected_task); trigger_autosave(); } - /* - case KEY_DC: { // Delete - if (selected_task == NULL || selected_task == active_task) { - break; - } + + case KEY_DC; // Delete + if (selected_task == null || selected_task == active_task) break; // BUG if (read_enter_confirmation(selected_task_row, action_style, " Press enter to delete task. ") == true) { delete_task(db, selected_task); trigger_autosave(); } - break; - } - +/* case '1': case '2': case '3': |
