diff options
| author | dam <dam@gudinoff> | 2022-09-29 18:42:32 +0000 |
|---|---|---|
| committer | dam <dam@gudinoff> | 2022-09-29 18:42:32 +0000 |
| commit | 4e8e9182d8620a35bc9cae0052bbdeb173922a39 (patch) | |
| tree | b783d0e9857c25596e0904598ec46836ab595ee0 /main.c | |
| parent | 0474a1e9181dd169c8f7a324430c0088f76d2e5b (diff) | |
| download | task-time-tracker-4e8e9182d8620a35bc9cae0052bbdeb173922a39.tar.zst task-time-tracker-4e8e9182d8620a35bc9cae0052bbdeb173922a39.zip | |
Cleaned up some defines and re-ordered the to-do list.
Diffstat (limited to 'main.c')
| -rw-r--r-- | main.c | 97 |
1 files changed, 51 insertions, 46 deletions
@@ -18,14 +18,14 @@ #define VERSION "1.0" // Use only 3 chars (to fit layouts). #define MAX_TASK_NAME 58 // Maximum task name length (includes NUL). #define FIRST_DAY_OF_WEEK 1 // (0-6, Sunday = 0). -#define WEEK_DAYS 7 // Just to avoid magic numbers. +#define NUM_WEEK_DAYS 7 // Just to avoid magic numbers. #define LOG_FILE_NAME "log.txt" #define DB_BIN_PATH_NAME "./database.bin" #define AR_CSV_PATH_NAME "./archive.csv" typedef struct { - int64_t times[WEEK_DAYS]; + int64_t times[NUM_WEEK_DAYS]; char name[MAX_TASK_NAME]; } task_st; @@ -36,7 +36,7 @@ typedef struct { ptrdiff_t active_task; ptrdiff_t selected_task; int64_t modified_on; - int64_t total_times[WEEK_DAYS]; + int64_t total_times[NUM_WEEK_DAYS]; } database_st; #define DB_FILE_SIGN_STR "TTT:B:01" @@ -269,7 +269,7 @@ bool add_task(database_st *db, task_st *task) { memcpy(new_task, task, SIZEOF_TASK_T); // Add task timer values to total timers. - for (int idx = 0; idx < WEEK_DAYS; idx++) { + for (int idx = 0; idx < NUM_WEEK_DAYS; idx++) { // db->total_times[idx] += task->times[idx]; TODO db->total_times[idx] = add_time(db->total_times[idx], task->times[idx]); } @@ -285,7 +285,7 @@ bool delete_task(database_st *db, task_st *task) { assert(task >= db->tasks && task < &db->tasks[db->count]); // Remove task timer values from total timers. - for (int idx = 0; idx < WEEK_DAYS; idx++) { + for (int idx = 0; idx < NUM_WEEK_DAYS; idx++) { // db->total_times[idx] -= task->times[idx]; TODO db->total_times[idx] = sub_time(db->total_times[idx], task->times[idx]); } @@ -485,7 +485,7 @@ bool import_from_csv(database_st *db, const char *path_name) { &task->times[4], &task->times[5], &task->times[6] - ) != WEEK_DAYS) { + ) != NUM_WEEK_DAYS) { replace_char(csv_buffer, '\n', ' '); fprintf(stderr, "Discarding invalid line '%s' and continuing.\n", csv_buffer); delete_task(db, task); @@ -493,7 +493,7 @@ bool import_from_csv(database_st *db, const char *path_name) { } // Add task timer values to total timers. - for (int idx = 0; idx < WEEK_DAYS; idx++) { + for (int idx = 0; idx < NUM_WEEK_DAYS; idx++) { // db->total_times[idx] += task->times[idx]; TODO db->total_times[idx] = add_time(db->total_times[idx], task->times[idx]); } @@ -585,7 +585,7 @@ void update_total_timers(database_st *db) { int64_t *d4 = &db->total_times[4]; int64_t *d5 = &db->total_times[5]; int64_t *d6 = &db->total_times[6]; - memset(db->total_times, WEEK_DAYS, sizeof(int64_t)); + memset(db->total_times, NUM_WEEK_DAYS, sizeof(int64_t)); for (size_t idx = 0; idx < db->count; idx++) { int64_t *times = db->tasks[idx].times; @@ -600,7 +600,28 @@ void update_total_timers(database_st *db) { } -#define NUM_OF_COLUMNS 9 +#define INPUT_TIMEOUT_MS 1000 +#define INPUT_AWAIT_INF -1 + +#define NUM_HEADER_ROWS 1 +#define NUM_FOOTER_ROWS 1 +#define NUM_TABLE_ROWS (size_y - NUM_HEADER_ROWS - NUM_FOOTER_ROWS) +#define NUM_COLUMNS 9 + +#define L_NORMAL 0 +#define L_COMPACT 1 + +#define L_TITLE_IDX 0 +#define L_DAYS_IDX 1 +#define L_TOTAL_IDX 8 + +#define THEME_A 1 +#define THEME_B 2 +#define THEME_C 3 +#define THEME_D 4 +#define THEME_E 5 + + typedef struct { char *header; @@ -610,27 +631,12 @@ typedef struct { } column_st; typedef struct { - column_st columns[NUM_OF_COLUMNS]; + column_st columns[NUM_COLUMNS]; char *archive_title; } layout_st; layout_st layouts[2]; -#define L_NORMAL 0 -#define L_COMPACT 1 - -#define L_TITLE_IDX 0 -#define L_DAYS_IDX 1 -#define L_TOTAL_IDX 8 - - -#define THEME_A 1 -#define THEME_B 2 -#define THEME_C 3 -#define THEME_D 4 -#define THEME_E 5 - - void initialize_tui() { // Normal layout. @@ -667,7 +673,7 @@ void initialize_tui() { // Calculate alignment_offsets. for(layout_st *layout = layouts; layout <= layouts + 1; layout++) { - for (column_st *col = layout->columns; col <= layout->columns + NUM_OF_COLUMNS; col++) { + for (column_st *col = layout->columns; col <= layout->columns + NUM_COLUMNS; col++) { int offset; switch(col->alignment) { default: @@ -705,13 +711,13 @@ void initialize_tui() { void draw_tui(database_st *db, layout_st *layout) { const static int adjust_first_day_of_week[] = { - (0 + FIRST_DAY_OF_WEEK) % WEEK_DAYS, - (1 + FIRST_DAY_OF_WEEK) % WEEK_DAYS, - (2 + FIRST_DAY_OF_WEEK) % WEEK_DAYS, - (3 + FIRST_DAY_OF_WEEK) % WEEK_DAYS, - (4 + FIRST_DAY_OF_WEEK) % WEEK_DAYS, - (5 + FIRST_DAY_OF_WEEK) % WEEK_DAYS, - (6 + FIRST_DAY_OF_WEEK) % WEEK_DAYS, + (0 + FIRST_DAY_OF_WEEK) % NUM_WEEK_DAYS, + (1 + FIRST_DAY_OF_WEEK) % NUM_WEEK_DAYS, + (2 + FIRST_DAY_OF_WEEK) % NUM_WEEK_DAYS, + (3 + FIRST_DAY_OF_WEEK) % NUM_WEEK_DAYS, + (4 + FIRST_DAY_OF_WEEK) % NUM_WEEK_DAYS, + (5 + FIRST_DAY_OF_WEEK) % NUM_WEEK_DAYS, + (6 + FIRST_DAY_OF_WEEK) % NUM_WEEK_DAYS, }; int x, y; @@ -724,8 +730,8 @@ void draw_tui(database_st *db, layout_st *layout) { int now_week_day = localtime(&now_utc)->tm_wday; // The first column expands to fill the remaining space dynamically. - layout->columns[0].width = size_x - (NUM_OF_COLUMNS - 1) - 2; - for (int idx = 1; idx < NUM_OF_COLUMNS; idx++) { + layout->columns[0].width = size_x - (NUM_COLUMNS - 1) - 2; + for (int idx = 1; idx < NUM_COLUMNS; idx++) { layout->columns[0].width -= layout->columns[idx].width; } @@ -739,7 +745,7 @@ void draw_tui(database_st *db, layout_st *layout) { // Draw table grids. y = 0; x = 0; - for (int idx = 0; idx < NUM_OF_COLUMNS - 1; idx++) { + for (int idx = 0; idx < NUM_COLUMNS - 1; idx++) { x += 1 + layout->columns[idx].width; mvaddch(y, x, ACS_TTEE); for (y = 1; y < size_y - 1; y++) { @@ -761,7 +767,7 @@ void draw_tui(database_st *db, layout_st *layout) { x += col->width; // Headers : days - for (int raw_idx = 0; raw_idx < WEEK_DAYS; raw_idx++) { + for (int raw_idx = 0; raw_idx < NUM_WEEK_DAYS; raw_idx++) { int idx = adjust_first_day_of_week[raw_idx]; x++; @@ -796,7 +802,7 @@ void draw_tui(database_st *db, layout_st *layout) { // TODO This is some sort of pagination to allow scrolling through the tasks. // TODO How does this behaves when no task is selected? y = 0; - size_t available_rows = size_y - 2; + size_t available_rows = NUM_TABLE_ROWS; size_t idx_start = (db->selected_task / available_rows) * available_rows; size_t idx_stop = idx_start + (available_rows > db->count - idx_start ? db->count - idx_start : available_rows); for (size_t idx = idx_start; idx < idx_stop; idx++) { @@ -826,10 +832,10 @@ void draw_tui(database_st *db, layout_st *layout) { // Task times. total_time = 0; - for (int idx = 0; idx < WEEK_DAYS; idx++) { + for (int idx = 0; idx < NUM_WEEK_DAYS; idx++) { x++; - int day_idx = (idx + FIRST_DAY_OF_WEEK) % WEEK_DAYS; + int day_idx = (idx + FIRST_DAY_OF_WEEK) % NUM_WEEK_DAYS; column_width = layout->columns[L_DAYS_IDX + day_idx].width; int64_t task_stime = task->times[day_idx]; @@ -862,7 +868,7 @@ void draw_tui(database_st *db, layout_st *layout) { y = size_y-1; x = 0 + 1 + layout->columns[L_TITLE_IDX].width; total_time = 0; - for (int raw_idx = 0; raw_idx < WEEK_DAYS; raw_idx++) { + for (int raw_idx = 0; raw_idx < NUM_WEEK_DAYS; raw_idx++) { int idx = adjust_first_day_of_week[raw_idx]; int64_t daily_total = db->total_times[idx]; x++; @@ -899,8 +905,7 @@ void free_memory() { string_buffer = NULL; } -#define INPUT_TIMEOUT_MS 1000 -#define INPUT_AWAIT_INF -1 + int main(int argc, char *argv[]) { @@ -1022,7 +1027,7 @@ int main(int argc, char *argv[]) { break; } // rename stuff - int row = (db->selected_task % (size_y - 2)) + 1; + int row = (db->selected_task % NUM_TABLE_ROWS) + NUM_HEADER_ROWS; attron(COLOR_PAIR(selected_task == active_task ? THEME_E : THEME_D) | A_BOLD); mvaddch(row, 0, ACS_DIAMOND); clrtoeol(); @@ -1126,7 +1131,7 @@ int main(int argc, char *argv[]) { break; case KEY_PPAGE: - db->selected_task -= (size_y - 2); + db->selected_task -= NUM_TABLE_ROWS; if (db->selected_task < 0) { db->selected_task = 0; } @@ -1145,7 +1150,7 @@ int main(int argc, char *argv[]) { break; case KEY_NPAGE: - db->selected_task += (size_y - 2); + db->selected_task += NUM_TABLE_ROWS; if (db->selected_task >= db->count) { db->selected_task = db->count - 1; } |
