State is now being saved between sessions. Needs cleanup.

This commit is contained in:
2026-02-12 00:13:47 -08:00
parent aa1b1cae0f
commit 5b7ee56afc

32
main.c
View File

@@ -149,7 +149,7 @@ Color activity_type_color_representation[activity_type_COUNT] = {
// TODO: String representation. // TODO: String representation.
typedef struct Activity { typedef struct Activity {
enum activity_type activity; // TODO: Rename to kind or type. activity_type activity; // TODO: Rename to kind or type.
Color color; Color color;
U64 began; U64 began;
U64 ended; U64 ended;
@@ -220,7 +220,7 @@ int main(int argc, char *argv[])
// WARNING: Not bullet proof, yet. // WARNING: Not bullet proof, yet.
FILE *f; FILE *f;
if(path_exists(file_buffer)) { if(path_exists(file_buffer)) {
f = fopen(file_buffer, "r+"); f = fopen(file_buffer, "r");
if(!f) { if(!f) {
fprintf(stderr, "fopen(%s) failed: %s\n", file_buffer, strerror(errno)); fprintf(stderr, "fopen(%s) failed: %s\n", file_buffer, strerror(errno));
return 1; return 1;
@@ -278,6 +278,8 @@ int main(int argc, char *argv[])
/* return 0; */ /* return 0; */
U64 last_save = 0;
U64 lower_bound_s = (U64)ts; U64 lower_bound_s = (U64)ts;
U64 upper_bound_s = lower_bound_s + 86400; U64 upper_bound_s = lower_bound_s + 86400;
// U64 upper_bound_s = lower_bound_s + 3600; // 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++) { for(U32 i = 0; i <= current_activity; i++) {
activities_stats[activities[i].activity].total_seconds = 0; activities_stats[activities[i].activity].total_seconds = 0;
} }
// 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");
}
} }
CloseWindow(); if (fflush(f) != 0)
perror("fflush");
last_save = now_s;
}
}
fclose(f); fclose(f);
CloseWindow();
return 0; return 0;
} }