From 5b7ee56afc7b1e1d4cd05235caa7e81dad540e6f Mon Sep 17 00:00:00 2001 From: igor Date: Thu, 12 Feb 2026 00:13:47 -0800 Subject: [PATCH] State is now being saved between sessions. Needs cleanup. --- main.c | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/main.c b/main.c index c07405f..be0ae41 100644 --- a/main.c +++ b/main.c @@ -149,7 +149,7 @@ Color activity_type_color_representation[activity_type_COUNT] = { // TODO: String representation. typedef struct Activity { - enum activity_type activity; // TODO: Rename to kind or type. + activity_type activity; // TODO: Rename to kind or type. Color color; U64 began; U64 ended; @@ -220,7 +220,7 @@ int main(int argc, char *argv[]) // WARNING: Not bullet proof, yet. FILE *f; if(path_exists(file_buffer)) { - f = fopen(file_buffer, "r+"); + f = fopen(file_buffer, "r"); if(!f) { fprintf(stderr, "fopen(%s) failed: %s\n", file_buffer, strerror(errno)); return 1; @@ -278,6 +278,8 @@ int main(int argc, char *argv[]) /* return 0; */ + U64 last_save = 0; + U64 lower_bound_s = (U64)ts; U64 upper_bound_s = lower_bound_s + 86400; // U64 upper_bound_s = lower_bound_s + 3600; @@ -425,11 +427,35 @@ int main(int argc, char *argv[]) for(U32 i = 0; i <= current_activity; i++) { activities_stats[activities[i].activity].total_seconds = 0; } - } - CloseWindow(); + // Save the state every 5 seconds. + if((now_s - last_save) >= 5) { + f = freopen(file_buffer, "w", f); // WARNING: TODO: Might fail. + + char write_buffer[128]; + for(U32 i = 0; i <= current_activity; i++) { + snprintf(write_buffer, sizeof(write_buffer), + "%s %llu %llu\n", + activity_type_string_representation[activities[i].activity], + activities[i].began, + activities[i].ended); + + size_t write_buffer_len = strlen(write_buffer); + if(fwrite(write_buffer, 1, write_buffer_len, f) != write_buffer_len) { + perror("fwrite"); + } + } + + if (fflush(f) != 0) + perror("fflush"); + + last_save = now_s; + } + } fclose(f); + CloseWindow(); + return 0; }