aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordam <dam@gudinoff>2022-09-21 23:32:55 +0000
committerdam <dam@gudinoff>2022-09-21 23:32:55 +0000
commita7d15d8f11a735e81713df39597ff4b66d4e2e8c (patch)
treeb1ddfcfc300f636490750068722a3070095e494e
parent62752ab73a5d751812c60d004672a23b54f322fb (diff)
downloadtask-time-tracker-a7d15d8f11a735e81713df39597ff4b66d4e2e8c.tar.zst
task-time-tracker-a7d15d8f11a735e81713df39597ff4b66d4e2e8c.zip
Trying to simplify draw_tui function.
-rw-r--r--main.c102
1 files changed, 59 insertions, 43 deletions
diff --git a/main.c b/main.c
index 2f08e43..5e1517f 100644
--- a/main.c
+++ b/main.c
@@ -606,7 +606,7 @@ void update_total_timers(database_t *db) {
char *line_buffer;
int size_x, size_y, pos_x, pos_y;
-uint8_t selected_layout = 0;
+uint8_t active_layout = 0;
#define NUM_OF_COLUMNS 9
@@ -616,6 +616,9 @@ typedef struct {
int column_widths[NUM_OF_COLUMNS];
char alignments[NUM_OF_COLUMNS];
int alignment_offsets[NUM_OF_COLUMNS];
+ int title_offset;
+ int days_offset;
+ int total_offset;
} layout_t;
layout_t *layouts = NULL;
@@ -626,7 +629,6 @@ void initialize_tui() {
layouts = calloc(2, sizeof(layout_t));
// Layout : 0 : normal.
- // TODO Headers must be dynamic according to FIRST_DAY_OF_WEEK
layouts[0] = (layout_t) {
.column_widths = { -1, 7, 7, 7, 7, 7, 7, 7, 9 },
.alignments = { 'L', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C' },
@@ -641,10 +643,12 @@ void initialize_tui() {
" Sat ",
" Total ",
},
+ .title_offset = 0,
+ .days_offset = 1,
+ .total_offset = 8,
};
// Layout : 1 : compact.
- // TODO Headers must be dynamic according to FIRST_DAY_OF_WEEK
layouts[1] = (layout_t){
.column_widths = { -1, 5, 5, 5, 5, 5, 5, 5, 5 },
.alignments = { 'L', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C' },
@@ -659,6 +663,9 @@ void initialize_tui() {
" S ",
" # ",
},
+ .title_offset = 0,
+ .days_offset = 1,
+ .total_offset = 8,
};
// Calculate alignment_offsets.
@@ -689,6 +696,7 @@ void initialize_tui() {
keypad(stdscr, TRUE); // I need that nifty F1
curs_set(0); // Set cursor invisible.
+ // Initialize pairs of colors.
start_color();
init_pair(1, COLOR_BLUE, COLOR_BLACK);
init_pair(2, COLOR_BLACK, COLOR_CYAN);
@@ -709,8 +717,28 @@ void free_memory() {
void draw_tui(database_t *db) {
- layout_t *layout = &layouts[selected_layout];
+ const static int days_map[] = {
+ (0 + FIRST_DAY_OF_WEEK) % 7,
+ (1 + FIRST_DAY_OF_WEEK) % 7,
+ (2 + FIRST_DAY_OF_WEEK) % 7,
+ (3 + FIRST_DAY_OF_WEEK) % 7,
+ (4 + FIRST_DAY_OF_WEEK) % 7,
+ (5 + FIRST_DAY_OF_WEEK) % 7,
+ (6 + FIRST_DAY_OF_WEEK) % 7,
+ };
+ const static int idx_map[] = {
+ 0,
+ 1 + days_map[0],
+ 1 + days_map[1],
+ 1 + days_map[2],
+ 1 + days_map[3],
+ 1 + days_map[4],
+ 1 + days_map[5],
+ 1 + days_map[6],
+ 8,
+ };
+ layout_t *layout = &layouts[active_layout];
// The first column expands to fill the remaining space dynamically.
layout->column_widths[0] = size_x - (NUM_OF_COLUMNS - 1) - 2;
@@ -718,19 +746,13 @@ void draw_tui(database_t *db) {
layout->column_widths[0] -= layout->column_widths[idx];
}
-
// TODO Unsure if this is needed.
erase();
// Draw outer border.
box(stdscr, 0, 0);
- // Draw edit symbol on title when in archive mode.
- if (db == &archive) {
- mvaddch(0, 0, ACS_DIAMOND);
- }
-
- // Draw column grids.
+ // Draw table grids.
int x = 0;
for (int idx = 0; idx < NUM_OF_COLUMNS - 1; idx++) {
x += 1 + layout->column_widths[idx];
@@ -741,22 +763,21 @@ void draw_tui(database_t *db) {
mvaddch(size_y - 1, x, ACS_BTEE);
}
+ // Draw diamond symbol on top left corner when in archive mode.
+ if (db == &archive) {
+ mvaddch(0, 0, ACS_DIAMOND);
+ }
+
// Draw headers.
- // TODO Missing some spacing on the initial column.
x = 0;
- int idx_fdow; // TODO Index - first day of week.
- for (int idx = 0; idx < NUM_OF_COLUMNS; idx++) {
- x += 1;
- idx_fdow = idx;
- if (idx_fdow > 0 && idx_fdow < 8) {
- idx_fdow = ((idx - 1) + FIRST_DAY_OF_WEEK) % 7 + 1;
- }
- int header_position = x + layout->alignment_offsets[idx_fdow];
- mvaddstr(0, header_position, layout->table_headers[idx_fdow]);
- x += layout->column_widths[idx_fdow];
+ for (int counter = 0; counter < NUM_OF_COLUMNS; counter++) {
+ x++;
+ int idx = idx_map[counter];
+ int header_position = x + layout->alignment_offsets[idx];
+ mvaddstr(0, header_position, layout->table_headers[idx]);
+ x += layout->column_widths[idx];
}
-
// Draw tasks.
uint64_t total_time = 0;
int times_offset = 1;
@@ -802,11 +823,8 @@ void draw_tui(database_t *db) {
column_width = layout->column_widths[0];
sprintf(line_buffer, "%*s", column_width, "");
- // TODO Which one should I use?
-// memset(line_buffer, ' ', column_width);
-// line_buffer[column_width] = '\0';
mvaddnstr(y, x, line_buffer, column_width);
- mvaddnstr(y, x, task->name, column_width); // TODO Trim at utf8-char using not-yet-implemented-function.
+ mvaddnstr(y, x, task->name, column_width);
x += layout->column_widths[0];
@@ -815,7 +833,7 @@ void draw_tui(database_t *db) {
for (int idx = 0; idx < 7; idx++) {
x++;
- idx_fdow = (idx + FIRST_DAY_OF_WEEK) % 7;
+ int idx_fdow = (idx + FIRST_DAY_OF_WEEK) % 7;
int column_width = layout->column_widths[idx_fdow + times_offset];
int64_t task_time = task->times[idx_fdow];
@@ -841,34 +859,32 @@ void draw_tui(database_t *db) {
attroff(COLOR_PAIR(color_pair));
attroff(A_BOLD);
// TEST -- STOP
-
-
- // Show current selected task and total number of tasks.
- sprintf(line_buffer, " %td/%zd ", db->selected_task+1, db->count);
- if (strlen(line_buffer) > layout->column_widths[0]) {
- sprintf(line_buffer, "%td", db->selected_task+1);
- }
- mvaddstr(size_y-1, 1, line_buffer);
}
+ // Show current selected task and total number of tasks.
+ sprintf(line_buffer, " %td/%zd ", db->selected_task+1, db->count);
+ if (strlen(line_buffer) > layout->column_widths[0]) {
+ sprintf(line_buffer, "%td", db->selected_task+1);
+ }
+ mvaddstr(size_y-1, 1, line_buffer);
// Daily totals.
y = size_y-1;
x = 0 + 1 + layout->column_widths[0];
total_time = 0;
- for (int idx = 0; idx < 7; idx++) {
+ for (int counter = 0; counter < 7; counter++) {
x++;
- idx_fdow = (idx + FIRST_DAY_OF_WEEK) % 7;
- int64_t daily_total = db->total_times[idx_fdow];
- column_width = layout->column_widths[idx_fdow + times_offset];
-// total_time += daily_total; TODO
+ int mapped_day = days_map[counter];
+ int days_offset = layout->days_offset;
+ int64_t daily_total = db->total_times[mapped_day];
+ column_width = layout->column_widths[mapped_day + days_offset];
total_time = add_time(total_time, daily_total);
format_time(daily_total, line_buffer, column_width);
mvaddstr(y, x, line_buffer);
x += column_width;
}
x++;
- column_width = layout->column_widths[7 + times_offset];
+ column_width = layout->column_widths[8];
format_time(total_time, line_buffer, column_width);
mvaddstr(y, x, line_buffer);
x += column_width;
@@ -1125,7 +1141,7 @@ int main(int argc, char *argv[]) {
}
if (size_x >= 60 && size_y > 2) {
- selected_layout = size_x > 100 ? 0 : 1;
+ active_layout = size_x > 100 ? 0 : 1;
draw_tui(db);
}
else {