diff options
| author | dam <dam@gudinoff> | 2022-09-27 00:52:10 +0000 |
|---|---|---|
| committer | dam <dam@gudinoff> | 2022-09-27 00:52:10 +0000 |
| commit | 6265ee44562abfe5ca38f18d10663462c700937d (patch) | |
| tree | 07d2d14a7755610793f128af2e3230db02ccaacb | |
| parent | a35524b221949e33ea12c60d3fd77912f9a60217 (diff) | |
| download | task-time-tracker-6265ee44562abfe5ca38f18d10663462c700937d.tar.zst task-time-tracker-6265ee44562abfe5ca38f18d10663462c700937d.zip | |
Implemented is_empty_string. Made sure that string_buffer has enought space for MAX_TASK_NAME and size_x. Fixed input row for creating and renaming tasks.
| -rw-r--r-- | main.c | 106 | ||||
| -rw-r--r-- | readme.md | 4 |
2 files changed, 64 insertions, 46 deletions
@@ -72,6 +72,24 @@ size_t truncate_string_utf8(char *string, size_t length) { return length - idx; } +// Returns true when the string is empty or consists of white space characters. +bool is_empty_string(char *string) { + for(int idx = 0; string[idx] != '\0'; idx++) { + switch(string[idx]) { + case ' ': + case '\t': + case '\v': + case '\f': + case '\r': + case '\n': + break; + default: + return false; + } + } + return true; +} + // Uses strchr to replace all instances of find by replace. // Returns string. char *replace_char(char *string, char find, char replace) { @@ -605,7 +623,7 @@ void prototype(test_et level) { } -char *line_buffer; +char *string_buffer; int size_x, size_y, pos_x, pos_y; #define NUM_OF_COLUMNS 9 @@ -836,8 +854,8 @@ void draw_tui(database_t *db, layout_t *layout) { // Task title. x++; column_width = layout->column_widths[L_TITLE_IDX]; - sprintf(line_buffer, "%*s", column_width, ""); - mvaddnstr(y, x, line_buffer, column_width); + sprintf(string_buffer, "%*s", column_width, ""); + mvaddnstr(y, x, string_buffer, column_width); mvaddnstr(y, x, task->name, column_width); x += column_width; @@ -851,16 +869,16 @@ void draw_tui(database_t *db, layout_t *layout) { int column_width = layout->column_widths[L_DAYS_IDX + day_idx]; int64_t task_time = task->times[day_idx]; total_time = add_time(total_time, task_time); - format_time(line_buffer, task_time, column_width); - mvaddstr(y, x, line_buffer); + format_time(string_buffer, task_time, column_width); + mvaddstr(y, x, string_buffer); x += column_width; } // Task total. x++; column_width = layout->column_widths[L_TOTAL_IDX]; // TODO - format_time(line_buffer, total_time, column_width); - mvaddstr(y, x, line_buffer); + format_time(string_buffer, total_time, column_width); + mvaddstr(y, x, string_buffer); // Reset theme. attrset(A_NORMAL); @@ -869,11 +887,11 @@ void draw_tui(database_t *db, layout_t *layout) { /////////////////////////////////////////////////////////////////////////// // Draw selected/total tasks. - sprintf(line_buffer, " %td/%zd ", db->selected_task+1, db->count); - if (strlen(line_buffer) > layout->column_widths[L_TITLE_IDX]) { - sprintf(line_buffer, "%td", db->selected_task+1); + sprintf(string_buffer, " %td/%zd ", db->selected_task+1, db->count); + if (strlen(string_buffer) > layout->column_widths[L_TITLE_IDX]) { + sprintf(string_buffer, "%td", db->selected_task+1); } - mvaddstr(size_y-1, 1, line_buffer); + mvaddstr(size_y-1, 1, string_buffer); /////////////////////////////////////////////////////////////////////////// // Draw daily totals. @@ -886,14 +904,14 @@ void draw_tui(database_t *db, layout_t *layout) { int64_t daily_total = db->total_times[day_idx]; column_width = layout->column_widths[day_idx + L_DAYS_IDX]; total_time = add_time(total_time, daily_total); - format_time(line_buffer, daily_total, column_width); - mvaddstr(y, x, line_buffer); + format_time(string_buffer, daily_total, column_width); + mvaddstr(y, x, string_buffer); x += column_width; } x++; column_width = layout->column_widths[L_TOTAL_IDX]; - format_time(line_buffer, total_time, column_width); - mvaddstr(y, x, line_buffer); + format_time(string_buffer, total_time, column_width); + mvaddstr(y, x, string_buffer); x += column_width; } @@ -902,8 +920,8 @@ void free_memory() { reset_database(&database); reset_database(&archive); - free(line_buffer); - line_buffer = NULL; + free(string_buffer); + string_buffer = NULL; free(layouts); layouts = NULL; @@ -1013,7 +1031,7 @@ int main(int argc, char *argv[]) { case KEY_RESIZE: clear(); getmaxyx(stdscr, size_y, size_x); - line_buffer = realloc(line_buffer, size_x); + string_buffer = realloc(string_buffer, 511 | MAX_TASK_NAME | size_x); break; case KEY_F(1): @@ -1024,30 +1042,28 @@ int main(int argc, char *argv[]) { // ERROR break; } - int row = db->selected_task; + + // TODO THIS SUCKS + db->selected_task = db->count-1; + draw_tui(db, &layouts[size_x > 100 ? L_NORMAL : L_COMPACT]); + int row = (db->selected_task % (size_y - 2)) + 1; + + attron(A_BOLD); mvaddch(row, 0, ACS_DIAMOND); clrtoeol(); mvaddch(row, size_x-1, ACS_VLINE); curs_set(1); - // TODO Make cursor blink. + mvgetnstr(row, 1, new_task->name, MAX_TASK_NAME-1); - // 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) { + new_task->name[MAX_TASK_NAME-1] = '\0'; + truncate_string_utf8(new_task->name, MAX_TASK_NAME-1); + + if (is_empty_string(new_task->name) == true) { strcpy(new_task->name, "-- new task --"); } - new_task->name[MAX_TASK_NAME-1] = '\0'; - char *name = new_task->name; - truncate_string_utf8(name, MAX_TASK_NAME-1); + curs_set(0); + attrset(A_NORMAL); break; } @@ -1058,23 +1074,21 @@ int main(int argc, char *argv[]) { break; } // rename stuff - int row = db->selected_task + 1; - attron(COLOR_PAIR(selected_task == active_task ? THEME_E : THEME_D)); - attron(A_BOLD); + int row = (db->selected_task % (size_y - 2)) + 1; + attron(COLOR_PAIR(selected_task == active_task ? THEME_E : THEME_D) | A_BOLD); mvaddch(row, 0, ACS_DIAMOND); clrtoeol(); mvaddch(row, size_x-1, ACS_VLINE); curs_set(1); - // TODO Make cursor blink. -// mvgetnstr(row, 1, selected_task->name, MAX_TASK_NAME-1); - static char input[MAX_TASK_NAME]; - mvgetnstr(row, 1, input, MAX_TASK_NAME-1); // TODO We are not sure the max task name is lower than the line_buffer size. Maybe the line_buffer should have a lower limit? - WIP Maybe guarantee that line_buffer is bigger than MAX_TASK_NAME and size_x? - input[MAX_TASK_NAME-1] = '\0'; - truncate_string_utf8(input, MAX_TASK_NAME-1); - if (strlen(input) > 0) { - strcpy(selected_task->name, input); + + mvgetnstr(row, 1, string_buffer, MAX_TASK_NAME-1); + string_buffer[MAX_TASK_NAME-1] = '\0'; + truncate_string_utf8(string_buffer, MAX_TASK_NAME-1); + + if (is_empty_string(string_buffer) == false) { + strcpy(selected_task->name, string_buffer); } + curs_set(0); attrset(A_NORMAL); break; @@ -29,6 +29,10 @@ Task Time Tracker - [x] Review code: char !uint8_t; - [x] Make sure that only one task is running at each time; - [x] Mouse selection is broken due to entire TUI update: No, it was fixed by using `erase()` on the `draw_tui` instead of `clear()`; +- [ ] I bet the headers are no longer being used all on a single cycle. Let's separate them and include "header_title_archive"; +- [ ] rename layout members: title_header, archive_header, total_header, days_headers, column_widths, column_alignments, headers_paddings. +- [ ] using the archive header, we can remove the top-left-corner diamond on the archive. +- [ ] MAYBE... IS THIS REALLY NEEDED? add helper func: get_selected_screen_row(db*, out int selected_row); It seems to be used in KEY_F(1) and KEY_F(2) cases of input management. - [ ] Allow to cancel a rename_task operation. - [ ] Improve total_times: - Create function to recalculate them. Shouldn't take so long, right? |
