From 0b42611b0dc53e25aa615dae9cb530f839dd0de3 Mon Sep 17 00:00:00 2001 From: igor Date: Tue, 24 Feb 2026 00:09:25 -0800 Subject: [PATCH] Some updates. --- main.c | 114 ++++++++++++++++++++++++++++++--------------------------- 1 file changed, 61 insertions(+), 53 deletions(-) diff --git a/main.c b/main.c index a429a0f..186d46c 100644 --- a/main.c +++ b/main.c @@ -1,7 +1,7 @@ ////////////////////////////////////////////////////////////////////////// // TODOS: // -// [ ] Lower the the time text lower below the activity title. // -// [ ] On timeline hower, display additional activity chunk info. // +// [X] Lower the the time text lower below the activity title. // +// [ ] On timeline, hower, display additional activity chunk info. // // [ ] Display distribution timeline below the activity line. // // [ ] Better file save error handling. // ////////////////////////////////////////////////////////////////////////// @@ -98,7 +98,7 @@ B32 activity_button(Rectangle rect, char *title, char *subtitle, DrawRectangleRounded(rect, 0.2f, 100, background_color); DrawTextEx(state.font, title, (Vector2){ (rect.x + rect.width / 2.0f - title_font_d.x / 2.0f), (rect.y + rect.height / 2.0f - title_font_d.y / 2.0f) }, font_size, 2, font_color); - DrawTextEx(state.font, subtitle, (Vector2){ (rect.x + rect.width / 2.0f - subtitle_font_d.x / 2.0f), (rect.y + rect.height / 2.0f - subtitle_font_d.y / 2.0f) + 20.0f }, subtitle_font_size, 2, font_color); + DrawTextEx(state.font, subtitle, (Vector2){ (rect.x + rect.width / 2.0f - subtitle_font_d.x / 2.0f), (rect.y + rect.height / 2.0f - subtitle_font_d.y / 2.0f) + 22.0f }, subtitle_font_size, 2, font_color); return button_pressed; } @@ -213,6 +213,62 @@ char *get_file(char *dir, struct tm *t) return buffer; } +FILE *load_activities(char *file_path, activity *activities, U32 *current_activity) +{ + FILE *f = fopen(file_path, "a+"); + if(!f) { + fprintf(stderr, "fopen(%s) failed: %s\n", file_path, strerror(errno)); + exit(1); + } + + local_persist char line[4096]; + if(file_size(f) > 0) { + while(fgets(line, sizeof line, f)) { + size_t n = strcspn(line, "\r\n"); + line[n] = '\0'; + + char activity_name[64]; + U64 lower; + U64 upper; + int l = sscanf(line, "%s %llu %llu", activity_name, &lower, &upper); + if(l != 3) + continue; + + if(lower > upper) + continue; + + activity_type type = other; + if(strcmp(activity_name, activity_type_string_representation[other]) == 0) + type = other; + else if(strcmp(activity_name, activity_type_string_representation[studying]) == 0) + type = studying; + else if(strcmp(activity_name, activity_type_string_representation[projects]) == 0) + type = projects; + else if(strcmp(activity_name, activity_type_string_representation[gaming]) == 0) + type = gaming; + else if(strcmp(activity_name, activity_type_string_representation[exercise]) == 0) + type = exercise; + else + continue; + + activities[*current_activity].type = type; + activities[*current_activity].color = activity_type_color_representation[type]; + activities[*current_activity].began = lower; + activities[*current_activity].ended = upper; + + (*current_activity)++; + } + } + + // TODO: Do we really need this? + if(ferror(f)) { + fprintf(stderr, "read error: %s\n", strerror(errno)); + exit(1); + } + + return f; +} + void save_activities(FILE *f, char *file_path, U32 current_activity, activity *activities) { // NOTE: We use "w+" because want to clear file contents before the next write. @@ -256,56 +312,8 @@ int main(int argc, char *argv[]) } } - FILE *f; - f = fopen(file_path, "a+"); - if(!f) { - fprintf(stderr, "fopen(%s) failed: %s\n", file_path, strerror(errno)); - return 1; - } - - char line[4096]; - if(file_size(f) > 0) { - while(fgets(line, sizeof line, f)) { - size_t n = strcspn(line, "\r\n"); - line[n] = '\0'; - - char activity_name[64]; - U64 lower; - U64 upper; - int l = sscanf(line, "%s %llu %llu", activity_name, &lower, &upper); - if(l != 3) - continue; - - if(lower > upper) - continue; - - activity_type type = other; - if(strcmp(activity_name, activity_type_string_representation[other]) == 0) - type = other; - else if(strcmp(activity_name, activity_type_string_representation[studying]) == 0) - type = studying; - else if(strcmp(activity_name, activity_type_string_representation[projects]) == 0) - type = projects; - else if(strcmp(activity_name, activity_type_string_representation[gaming]) == 0) - type = gaming; - else if(strcmp(activity_name, activity_type_string_representation[exercise]) == 0) - type = exercise; - else - continue; - - activities[current_activity].type = type; - activities[current_activity].color = activity_type_color_representation[type]; - activities[current_activity].began = lower; - activities[current_activity].ended = upper; - - current_activity++; - } - } - - if(ferror(f)) { - fprintf(stderr, "read error: %s\n", strerror(errno)); - return 1; - } + // NOTE: Not sure this is great, but that is what we'll go with for now. + FILE *f = load_activities(file_path, activities, ¤t_activity); U64 last_save = 0;