Some worked done toward adding a storage support.

This commit is contained in:
2026-02-07 01:40:29 -08:00
parent 0201a2ab95
commit 420df8e615
2 changed files with 146 additions and 51 deletions

107
main.c
View File

@@ -1,6 +1,11 @@
#include "base_context_cracking.h"
#include "base_core.h"
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <pwd.h>
#include <sys/stat.h>
#include <stdio.h>
#include <time.h>
#include "math.h"
@@ -27,6 +32,37 @@ global_state state;
// return ms;
// }
B32 path_exists(const char *path)
{
struct stat st;
return stat(path, &st) == 0;
}
B32 is_directory(const char *path)
{
struct stat st;
return (stat(path, &st) == 0) && S_ISDIR(st.st_mode);
}
const char *get_home_dir()
{
const char *home = getenv("HOME");
if(home && home[0] != '\0') return home;
struct passwd *pw = getpwuid(getuid());
if(pw && pw->pw_dir && pw->pw_dir[0] != '\0') return pw->pw_dir;
return NULL;
}
U64 file_size(FILE *f)
{
fseek(f, 0, SEEK_END);
U64 size = ftell(f);
fseek(f, 0, SEEK_SET); // TODO: NOTE: Save the current position, instead?
return size;
}
U64 now()
{
return (U64)time(NULL);
@@ -131,6 +167,9 @@ Hours break_time(U64 seconds)
int main(int argc, char *argv[])
{
(void)argc;
(void)argv;
U64 secs = now();
struct tm *t = localtime((time_t *)(&secs));
t->tm_hour = 0;
@@ -143,6 +182,62 @@ int main(int argc, char *argv[])
return 1;
}
// Check if 2025-02-10 available
// build array
// then, save the file periodically (every ~5 seconds)
// TODO: Make global constant or in config file
char date_buffer[16];
char dir_buffer[128];
char file_buffer[256];
strftime(date_buffer, sizeof(date_buffer), "%Y_%m_%d", t);
snprintf(dir_buffer, sizeof(dir_buffer), "%s/.timetracker", get_home_dir());
snprintf(file_buffer, sizeof(file_buffer), "%s/%s", dir_buffer, date_buffer);
if(!is_directory(dir_buffer)) {
mkdir(dir_buffer, 0700); // WARNING: Might fail, what then?
}
// TODO: Fix error handling.
FILE *f;
if(path_exists(file_buffer)) {
f = fopen(file_buffer, "r+");
if(!f) {
fprintf(stderr, "fopen(%s) failed: %s\n", file_buffer, strerror(errno));
return 1;
}
} else {
f = fopen(file_buffer, "w+");
}
char line[4096];
if(file_size(f) < 1) {
printf("EMPTY\n");
} else {
while(fgets(line, sizeof line, f)) {
size_t n = strcspn(line, "\r\n");
line[n] = '\0';
printf("%s\n", line);
/* if(current_activity >= 0) */
/* current_activity++; */
char test[64];
U64 var1;
U64 var2;
int l = sscanf(line, "%s %llu %llu", test, &var1, &var2);
}
}
if(ferror(f)) {
fprintf(stderr, "read error: %s\n", strerror(errno));
}
fclose(f);
return 0;
U64 lower_bound_s = (U64)ts;
U64 upper_bound_s = lower_bound_s + 86400;
// U64 upper_bound_s = lower_bound_s + 3600;
@@ -203,7 +298,7 @@ int main(int argc, char *argv[])
ClearBackground(BLACK);
F32 padding_x = 10.0f;
F32 padding_y = 10.0f;
// F32 padding_y = 10.0f;
DrawRectangle(0, 0, window_w, 40, GRAY);
@@ -224,7 +319,7 @@ int main(int argc, char *argv[])
}
Hours time = break_time(activities_stats[other].total_seconds);
snprintf(activities_stats[other].seconds_str, sizeof(activities_stats[other].seconds_str), "%02lu:%02lu:%02lu", time.hours, time.minutes, time.seconds);
snprintf(activities_stats[other].seconds_str, sizeof(activities_stats[other].seconds_str), "%02llu:%02llu:%02llu", time.hours, time.minutes, time.seconds);
if(activity_button(padding_x, 60, width, (window_h-70), "Other", activities_stats[other].seconds_str, font_size, RED)) {
if(activities[current_activity].activity != other) {
current_activity++;
@@ -237,7 +332,7 @@ int main(int argc, char *argv[])
}
time = break_time(activities_stats[work].total_seconds);
snprintf(activities_stats[work].seconds_str, sizeof(activities_stats[work].seconds_str), "%02lu:%02lu:%02lu", time.hours, time.minutes, time.seconds);
snprintf(activities_stats[work].seconds_str, sizeof(activities_stats[work].seconds_str), "%02llu:%02llu:%02llu", time.hours, time.minutes, time.seconds);
if(activity_button((padding_x*2)+(width*1), 60, width, (window_h-70), "Work", activities_stats[work].seconds_str, font_size, BLUE)) {
if(activities[current_activity].activity != work) {
current_activity++;
@@ -250,7 +345,7 @@ int main(int argc, char *argv[])
}
time = break_time(activities_stats[projects].total_seconds);
snprintf(activities_stats[projects].seconds_str, sizeof(activities_stats[projects].seconds_str), "%02lu:%02lu:%02lu", time.hours, time.minutes, time.seconds);
snprintf(activities_stats[projects].seconds_str, sizeof(activities_stats[projects].seconds_str), "%02llu:%02llu:%02llu", time.hours, time.minutes, time.seconds);
if(activity_button((padding_x*3)+(width*2), 60, width, (window_h-70), "Projects", activities_stats[projects].seconds_str, font_size, YELLOW)) {
if(activities[current_activity].activity != projects) {
printf("Activity PROJECTS\n");
@@ -265,7 +360,7 @@ int main(int argc, char *argv[])
}
time = break_time(activities_stats[gaming].total_seconds);
snprintf(activities_stats[gaming].seconds_str, sizeof(activities_stats[gaming].seconds_str), "%02lu:%02lu:%02lu", time.hours, time.minutes, time.seconds);
snprintf(activities_stats[gaming].seconds_str, sizeof(activities_stats[gaming].seconds_str), "%02llu:%02llu:%02llu", time.hours, time.minutes, time.seconds);
if(activity_button((padding_x*4)+(width*3), 60, width, (window_h-70), "Gaming", activities_stats[gaming].seconds_str, font_size, GREEN)) {
if(activities[current_activity].activity != gaming) {
printf("Activity GAMING\n");
@@ -280,7 +375,7 @@ int main(int argc, char *argv[])
}
time = break_time(activities_stats[exercise].total_seconds);
snprintf(activities_stats[exercise].seconds_str, sizeof(activities_stats[exercise].seconds_str), "%02lu:%02lu:%02lu", time.hours, time.minutes, time.seconds);
snprintf(activities_stats[exercise].seconds_str, sizeof(activities_stats[exercise].seconds_str), "%02llu:%02llu:%02llu", time.hours, time.minutes, time.seconds);
if(activity_button((padding_x*5)+(width*4), 60, width, (window_h-70), "Exercise", activities_stats[exercise].seconds_str, font_size, PURPLE)) {
if(activities[current_activity].activity != exercise) {
printf("Activity EXERCISE\n");