General clean up.

This commit is contained in:
2026-02-23 00:14:38 -08:00
parent 239150ed26
commit 37cac42e4b

63
main.c
View File

@@ -1,3 +1,12 @@
//////////////////////////////////////////////////////////////////////////
// TODOS: //
// [ ] 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. //
//////////////////////////////////////////////////////////////////////////
#include "base_context_cracking.h"
#include "base_core.h"
@@ -22,6 +31,8 @@
#endif
typedef struct {
S32 window_w;
S32 window_h;
Vector2 mouse_pos;
Font font;
@@ -103,7 +114,6 @@ typedef enum activity_type {
activity_type_COUNT
} activity_type;
// TODO: Rename to something shorter, maybe ommit the "repreresentation."
char *activity_type_string_representation[activity_type_COUNT] = {
"Other",
"Studying",
@@ -114,7 +124,6 @@ char *activity_type_string_representation[activity_type_COUNT] = {
"Reading"
};
// TODO: Rename to something shorter, maybe ommit the "repreresentation."
Color activity_type_color_representation[activity_type_COUNT] = {
RED,
GREEN,
@@ -148,8 +157,7 @@ Hours break_time(U64 seconds)
Hours hours;
hours.hours = seconds / 60 / 60;
hours.minutes = seconds / 60 - (60 * hours.hours);
hours.seconds =
seconds - ((hours.hours * 60 * 60) + (hours.minutes * 60));
hours.seconds = seconds - ((hours.hours * 60 * 60) + (hours.minutes * 60));
return hours;
}
@@ -159,8 +167,7 @@ void switch_activity(activity *activities, U32 *current_activity, activity_type
(*current_activity)++;
activities[*current_activity].type = type;
activities[*current_activity].color =
activity_type_color_representation[type];
activities[*current_activity].color = activity_type_color_representation[type];
activities[*current_activity].began = time;
activities[*current_activity].ended = time;
}
@@ -208,7 +215,7 @@ char *get_file(char *dir, struct tm *t)
void save_activities(FILE *f, char *file_path, U32 current_activity, activity *activities)
{
// NOTE: We want to clear file contents before the next write.
// NOTE: We use "w+" because want to clear file contents before the next write.
f = freopen(file_path, "w+", f);
if(f) {
char write_buffer[128];
@@ -217,8 +224,8 @@ void save_activities(FILE *f, char *file_path, U32 current_activity, activity *a
size_t write_buffer_len = strlen(write_buffer);
if(fwrite(write_buffer, 1, write_buffer_len, f) != write_buffer_len) {
// NOTE: We do not care, probably for now, that it might have failed to write some data.
// The program will either overwrite everything again or the next launch will ignore the corrupted line.
// NOTE: We do not care, for now, that it might have failed to write some data.
// The program will overwrite everything on exit or next save interval.
}
}
@@ -265,8 +272,7 @@ int main(int argc, char *argv[])
char activity_name[64];
U64 lower;
U64 upper;
int l =
sscanf(line, "%s %llu %llu", activity_name, &lower, &upper);
int l = sscanf(line, "%s %llu %llu", activity_name, &lower, &upper);
if(l != 3)
continue;
@@ -288,8 +294,7 @@ int main(int argc, char *argv[])
continue;
activities[current_activity].type = type;
activities[current_activity].color =
activity_type_color_representation[type];
activities[current_activity].color = activity_type_color_representation[type];
activities[current_activity].began = lower;
activities[current_activity].ended = upper;
@@ -299,6 +304,7 @@ int main(int argc, char *argv[])
if(ferror(f)) {
fprintf(stderr, "read error: %s\n", strerror(errno));
return 1;
}
U64 last_save = 0;
@@ -307,16 +313,15 @@ int main(int argc, char *argv[])
U64 upper_bound_s = lower_bound_s + 86400;
// U64 upper_bound_s = lower_bound_s + 3600;
S32 window_w = 1400;
S32 window_h = 500;
state.window_w = 1400;
state.window_h = 300;
InitWindow(window_w, window_h, "Time Tracker");
InitWindow(state.window_w, state.window_h, "Time Tracker");
SetWindowState(FLAG_MSAA_4X_HINT|FLAG_WINDOW_RESIZABLE);
SetTargetFPS(30);
state.font = LoadFontFromMemory(".ttf", sourcecodepro_ttf,
sourcecodepro_ttf_len, 96, NULL, 0);
state.font = LoadFontFromMemory(".ttf", sourcecodepro_ttf, sourcecodepro_ttf_len, 96, NULL, 0);
if(!IsFontValid(state.font)) {
fprintf(stderr, "Unable to load font\n");
return 1;
@@ -354,18 +359,18 @@ int main(int argc, char *argv[])
return 1; // TODO: REMOVE.
}
activity_type type = activities[current_activity].type;
activity_type old_type = activities[current_activity].type;
memset(activities, 0, sizeof(activities));
memset(activities_stats, 0, sizeof(activities_stats));
current_activity = 0;
activities[current_activity].type = type;
activities[current_activity].color = activity_type_color_representation[type];
activities[current_activity].type = old_type;
activities[current_activity].color = activity_type_color_representation[old_type];
activities[current_activity].began = now();
}
window_w = GetScreenWidth();
window_h = GetScreenHeight();
state.window_w = GetScreenWidth();
state.window_h = GetScreenHeight();
state.mouse_pos = GetMousePosition();
@@ -386,15 +391,15 @@ int main(int argc, char *argv[])
F32 padding_x = 10.0f;
// F32 padding_y = 10.0f;
DrawRectangle(0, 0, window_w, 40, GRAY);
DrawRectangle(0, 0, state.window_w, 40, GRAY);
for(U32 i = 0; i <= current_activity; i++) {
F32 start_x = floor((F32)(activities[i].began - lower_bound_s) / (F32)(upper_bound_s - lower_bound_s) * (F32)window_w);
F32 end_x = floor(((F32)(activities[i].ended - lower_bound_s) / (F32)(upper_bound_s - lower_bound_s)) * window_w);
F32 start_x = floor((F32)(activities[i].began - lower_bound_s) / (F32)(upper_bound_s - lower_bound_s) * (F32)(state.window_w));
F32 end_x = floor(((F32)(activities[i].ended - lower_bound_s) / (F32)(upper_bound_s - lower_bound_s)) * (F32)(state.window_w));
DrawRectangle(start_x, 0, end_x - start_x, 40, activities[i].color);
}
F32 width = (window_w-padding_x*(float)(activity_type_COUNT+1)) / (float)activity_type_COUNT;
F32 width = (state.window_w-padding_x*(float)(activity_type_COUNT+1)) / (float)activity_type_COUNT;
for(U32 i = 0; i <= current_activity; i++) {
activities_stats[activities[i].type].total_seconds += activities[i].ended - activities[i].began;
@@ -404,7 +409,7 @@ int main(int argc, char *argv[])
Hours time = break_time(activities_stats[i].total_seconds);
snprintf(activities_stats[i].seconds_str, sizeof(activities_stats[i].seconds_str), "%02llu:%02llu:%02llu", time.hours, time.minutes, time.seconds);
Rectangle rect = { (padding_x*(i+1))+(width*i), 60, width, (window_h-70) };
Rectangle rect = { (padding_x*(i+1))+(width*i), 60, width, (state.window_h-70) };
if(activity_button(rect, activity_type_string_representation[i], activities_stats[i].seconds_str, state.font_size, (activities[current_activity].type == i ? WHITE : BLACK), activity_type_color_representation[i])) {
if(activities[current_activity].type != i) {
switch_activity(activities, &current_activity, i, now_s);
@@ -413,7 +418,7 @@ int main(int argc, char *argv[])
if(activities[current_activity].type == i)
DrawRectangleRoundedLinesEx(rect, 0.2f, 100, 5.0f, WHITE);
char index_buf[8];
local_persist char index_buf[8];
snprintf(index_buf, sizeof(index_buf), "%d", i + 1);
DrawTextEx(state.font, index_buf, (Vector2){ rect.x + 10.0f, rect.y + 10.0f }, state.font_size - 5.0f, 2, BLACK);
}