aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.c90
-rw-r--r--readme.md3
2 files changed, 46 insertions, 47 deletions
diff --git a/main.c b/main.c
index a9e76a9..879b4a7 100644
--- a/main.c
+++ b/main.c
@@ -339,7 +339,7 @@ bool duplicate_task(database_st *db, task_st *task) {
return true;
}
-// Deletes the provided task.
+// Deletes task from database.
// If possible, shrinks the database capacity.
// Returns success.
bool delete_task(database_st *db, task_st *task) {
@@ -386,49 +386,46 @@ bool delete_task(database_st *db, task_st *task) {
return true;
}
-// Deletes the provided task. If possible, shrinks the database capacity.
-// Returns success.
-bool move_task(database_st *db, task_st *task, size_t target) {
+// 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) {
assert(db != NULL);
assert(task != NULL);
assert(task >= db->tasks && task - db->tasks < db->count);
- assert(target >= 0 && target < db->count);
- // Move tasks after the index position to their new positions.
- ptrdiff_t index = task - db->tasks; // TODO WIP (validate ptrdiff_t variables)
- task_st *target_task = &db->tasks[target];
- ptrdiff_t target_index = target_task - db->tasks;
+ ptrdiff_t target_index = index < 0 ? 0
+ : index >= db->count ? db->count - 1
+ : index;
+
+ task_st *target_task = db->tasks + target_index;
if (target_task == task) {
- return true;
+ return;
}
+ // Move task to new location.
task_st temp_task;
memcpy(&temp_task, task, SIZEOF_TASK_ST);
-
- // TODO Simplify code
- if (target_index > index) {
- memmove(task, task + 1, (target_index - index) * SIZEOF_TASK_ST);
+ if (target_task > task) {
+ memmove(task, task + 1, (target_task - task) * SIZEOF_TASK_ST);
}
else {
- memmove(target_task + 1, target_task, (index - target_index) * SIZEOF_TASK_ST);
+ memmove(target_task + 1, target_task, (task - target_task) * SIZEOF_TASK_ST);
}
-
memcpy(target_task, &temp_task, SIZEOF_TASK_ST);
- if (db->active_task == index) {
+ // Adjust active and selected tasks.
+ ptrdiff_t source_index = task - db->tasks;
+ if (db->active_task == source_index) {
db->active_task = target_index;
}
- else if (db->active_task > index && db->active_task <= target_index) {
+ else if (source_index < db->active_task && db->active_task <= target_index) {
db->active_task--;
}
- else if (db->active_task >= target_index && db->active_task < index) {
+ else if (target_index <= db->active_task && db->active_task < source_index) {
db->active_task++;
}
-
db->selected_task = target_index;
-
- return true;
}
// Updates the times on the active task (and adjusts database totals).
@@ -474,16 +471,17 @@ void update_times(database_st *db) {
void update_total_times(database_st *db) {
assert(db != NULL);
- memset(db->total_times, 0, NUM_WEEK_DAYS * SIZEOF_INT64);
+ int64_t *totals = db->total_times;
+ memset(totals, 0, NUM_WEEK_DAYS * SIZEOF_INT64);
for (size_t idx = 0; idx < db->count; idx++) {
int64_t *times = db->tasks[idx].times;
- db->total_times[0] = add_int64(db->total_times[0], times[0]);
- db->total_times[1] = add_int64(db->total_times[1], times[1]);
- db->total_times[2] = add_int64(db->total_times[2], times[2]);
- db->total_times[3] = add_int64(db->total_times[3], times[3]);
- db->total_times[4] = add_int64(db->total_times[4], times[4]);
- db->total_times[5] = add_int64(db->total_times[5], times[5]);
- db->total_times[6] = add_int64(db->total_times[6], times[6]);
+ totals[0] = add_int64(totals[0], times[0]);
+ totals[1] = add_int64(totals[1], times[1]);
+ totals[2] = add_int64(totals[2], times[2]);
+ totals[3] = add_int64(totals[3], times[3]);
+ totals[4] = add_int64(totals[4], times[4]);
+ totals[5] = add_int64(totals[5], times[5]);
+ totals[6] = add_int64(totals[6], times[6]);
}
}
@@ -715,16 +713,17 @@ bool append_to_csv(task_st *task, const char *path) {
return true;
}
-// TODO Add description.
-void select_task_by_index(database_st *db, ptrdiff_t idx) {
+// Selects task by index.
+// Index gets clamped to [0, db->count[.
+void select_task_by_index(database_st *db, ptrdiff_t index) {
assert(db != NULL);
db->selected_task = db->count == 0 ? -1
- : idx < 0 ? 0
- : idx >= db->count ? db->count - 1
- : idx;
+ : index < 0 ? 0
+ : index >= db->count ? db->count - 1
+ : index;
}
-// TODO Add description.
+// Selects task by delta relative to currently selected task.
void select_task_by_delta(database_st *db, ptrdiff_t delta) {
assert(db != NULL);
ptrdiff_t idx = (delta > 0 && db->selected_task > PTRDIFF_MAX - delta) ? PTRDIFF_MAX
@@ -733,7 +732,7 @@ void select_task_by_delta(database_st *db, ptrdiff_t delta) {
select_task_by_index(db, idx);
}
-// TODO Add description.
+// Selects task.
void select_task(database_st *db, task_st *task) {
assert(db != NULL);
assert(task != NULL);
@@ -741,7 +740,8 @@ void select_task(database_st *db, task_st *task) {
db->selected_task = task - db->tasks;
}
-// TODO Add description.
+// Set active task.
+// Passing task as NULL de-activates any previously active task.
void set_active_task(database_st *db, task_st *task) {
assert(db != NULL);
assert(task == NULL || (task >= db->tasks && task < &db->tasks[db->count]));
@@ -1520,7 +1520,7 @@ int main(int argc, char *argv[]) {
attrset(A_NORMAL);
char *parser; // TODO Rename var.
- intmax_t input = strtoimax(string_buffer, &parser, 10) - 1;
+ intmax_t input = strtoimax(string_buffer, &parser, 10);
// TODO Add comment about this comparison... is checking what? - ALSO ADD THIS BELOW
// If endptr is not NULL, strtol() stores the address of the first invalid character in *endptr. If there were no digits at all, strtol() stores the original value of nptr in *endptr (and returns 0). In particular, if *nptr is not '\0' but **endptr is '\0' on return, the entire string is valid.
@@ -1528,11 +1528,8 @@ int main(int argc, char *argv[]) {
break;
}
- // TODO Implement move-task-to logic.
- size_t target = input < 0 ? 0 :
- input >= db->count ? db->count - 1 :
- input;
- move_task(db, selected_task, target); // TODO Error checking.
+ ptrdiff_t target_index = (input < 1 ? 1 : input > MAX_DATABASE_TASKS ? MAX_DATABASE_TASKS : input) - 1;
+ move_task_to_index(db, selected_task, target_index);
trigger_autosave();
@@ -1557,7 +1554,7 @@ int main(int argc, char *argv[]) {
attrset(A_NORMAL);
char *parser;
- intmax_t input = strtoimax(string_buffer, &parser, 10) - 1;
+ intmax_t input = strtoimax(string_buffer, &parser, 10);
// TODO Add comment about this comparison... is checking what? - ALSO ADD THIS BELOW
// If endptr is not NULL, strtol() stores the address of the first invalid character in *endptr. If there were no digits at all, strtol() stores the original value of nptr in *endptr (and returns 0). In particular, if *nptr is not '\0' but **endptr is '\0' on return, the entire string is valid.
@@ -1565,7 +1562,8 @@ int main(int argc, char *argv[]) {
break;
}
- select_task_by_index(db, input < 0 ? 0 : input > MAX_DATABASE_TASKS ? MAX_DATABASE_TASKS : input);
+ ptrdiff_t target_index = (input < 1 ? 1 : input > MAX_DATABASE_TASKS ? MAX_DATABASE_TASKS : input) - 1;
+ select_task_by_index(db, target_index);
break;
}
diff --git a/readme.md b/readme.md
index 264c790..b3cabbe 100644
--- a/readme.md
+++ b/readme.md
@@ -70,6 +70,7 @@ Task Time Tracker
- [x] Fix bug: archiving/unarchiving task introduces " ," at end of name and increases the number of spaces before comma;
- [x] Check if draw_tui may be simplified by drawing entire lines of tasks at once and draw columns separators after;
- By having each column-print job decoulpled, we avoid havint to measure and compensate lengths of UTF8 strings;
-- [ ] Review all code for bugs related to auto-cast on ptrdiff_t/size_t (signed/unsigned);
+- [x] Review all code for bugs related to auto-cast on ptrdiff_t (signed/unsigned);
+- [ ] Review all code for bugs related to auto-cast on size_t (signed/unsigned);
- [ ] Try to fix flickering of ncurses;
- [ ] Go over all `TODO` items;