aboutsummaryrefslogtreecommitdiff
path: root/ttt.jai
diff options
context:
space:
mode:
Diffstat (limited to 'ttt.jai')
-rw-r--r--ttt.jai305
1 files changed, 138 insertions, 167 deletions
diff --git a/ttt.jai b/ttt.jai
index 655fac8..3be8718 100644
--- a/ttt.jai
+++ b/ttt.jai
@@ -17,8 +17,10 @@
#import "Basic";
#import "System";
#import "Math";
+#import "File";
+#import "String";
#import "curses";
-// #import "File_Utilities";
+
VERSION :: "2.0"; // Use only 3 chars (to fit layouts).
YEAR :: "2023";
@@ -54,71 +56,64 @@ Database :: struct {
-/*
-const char DB_FILE_SIGN[] = DB_FILE_SIGN_STR;
-const size_t DB_FILE_SIGN_LENGTH = sizeof(DB_FILE_SIGN_STR)-1;
-const size_t SIZEOF_TASK_ST = sizeof(task_st);
-const size_t SIZEOF_DATABASE_ST = sizeof(database_st);
-const size_t SIZEOF_CHAR = sizeof(char);
-const size_t SIZEOF_INT64 = sizeof(int64_t);
-
-
-
-database_st database = { .tasks = NULL };
-database_st archive = { .tasks = NULL };
-database_st *db = NULL;
-bool is_autosave_enabled = true;
-int countdown_to_autosave = -1;
-char *app_folder = NULL;
-char *db_file_path = NULL;
-char *ar_file_path = NULL;
-char *string_buffer = NULL; // A temporary buffer for localized actions. Please avoid data leaks and out-of-bounds errors.
-size_t string_buffer_size = 0;
-int size_x, size_y, pos_x, pos_y;
-
-
-
-typedef enum {
- STYLE_SELECTED = 1,
- STYLE_SELECTED_INVERTED,
- STYLE_ACTIVE,
- STYLE_ACTIVE_SELECTED,
- STYLE_ERROR,
-} styles_et;
-
-
-
-WINDOW *error_window = NULL;
-time_t error_time_limit = 0;
-
-void print_error(const char *format, ...) {
- va_list args;
- va_start(args, format);
- if (stdscr == NULL || isendwin() == true) { // Not in ncurses mode?
- vfprintf(stderr, format, args);
- fprintf(stderr, "\n");
- }
- else {
- int w_size_x = size_x > 120 ? 120 : size_x - 2;
- int w_size_y = 4;
- if (error_window == NULL) {
- error_window = newwin(w_size_y, w_size_x, (size_y - w_size_y) / 2, (size_x - w_size_x) / 2);
- wattron(error_window, COLOR_PAIR(STYLE_ERROR));
- wborder(error_window, ' ', ' ', 0, 0, ACS_HLINE, ACS_HLINE, ACS_HLINE, ACS_HLINE);
- mvwprintw(error_window, 0, 1, " Error ");
- wmove(error_window, 1, 0);
- }
- else {
- waddch(error_window, ' ');
- }
- vw_printw(error_window, format, args);
- error_time_limit = time(NULL) + 5;
- }
-
- va_end(args);
+// const char DB_FILE_SIGN[] = DB_FILE_SIGN_STR;
+// const size_t DB_FILE_SIGN_LENGTH = sizeof(DB_FILE_SIGN_STR)-1;
+// const size_t SIZEOF_TASK_ST = sizeof(task_st);
+// const size_t SIZEOF_DATABASE_ST = sizeof(database_st);
+// const size_t SIZEOF_CHAR = sizeof(char);
+// const size_t SIZEOF_INT64 = sizeof(int64_t);
+//
+//
+//
+// database_st database = { .tasks = NULL };
+// database_st archive = { .tasks = NULL };
+// database_st *db = NULL;
+is_autosave_enabled := true;
+// int countdown_to_autosave = -1;
+app_directory : string;
+db_file_path : string;
+ar_file_path : string;
+// char *string_buffer = NULL; // A temporary buffer for localized actions. Please avoid data leaks and out-of-bounds errors.
+// size_t string_buffer_size = 0;
+// int size_x, size_y, pos_x, pos_y;
+
+// typedef enum {
+// STYLE_SELECTED = 1,
+// STYLE_SELECTED_INVERTED,
+// STYLE_ACTIVE,
+// STYLE_ACTIVE_SELECTED,
+// STYLE_ERROR,
+// } styles_et;
+
+error_window : *WINDOW = null;
+error_time_limit := Apollo_Time.{0, 0};
+
+print_error :: (format :string, args : .. Any) {
+ // NOTE Implement me please.
+// if stdscr == null || isendwin() == true { // Not in ncurses mode?
+ print(format, ..args);
+ print("\n");
+// }
+// else {
+// int w_size_x = size_x > 120 ? 120 : size_x - 2;
+// int w_size_y = 4;
+// if (error_window == NULL) {
+// error_window = newwin(w_size_y, w_size_x, (size_y - w_size_y) / 2, (size_x - w_size_x) / 2);
+// wattron(error_window, COLOR_PAIR(STYLE_ERROR));
+// wborder(error_window, ' ', ' ', 0, 0, ACS_HLINE, ACS_HLINE, ACS_HLINE, ACS_HLINE);
+// mvwprintw(error_window, 0, 1, " Error ");
+// wmove(error_window, 1, 0);
+// }
+// else {
+// waddch(error_window, ' ');
+// }
+// vw_printw(error_window, format, args);
+// error_time_limit = time(NULL) + 5; // NOTE Instead of time use the current_time_monotonic()
+// }
}
-void draw_error_window() {
+draw_error_window :: () {
+ /* NOTE Implement me please.
if (error_window == NULL) {
return;
}
@@ -144,8 +139,10 @@ void draw_error_window() {
refresh();
touchwin(error_window);
wrefresh(error_window);
+ */
}
+/*
void trigger_autosave() {
countdown_to_autosave = 13375; // ms
}
@@ -1127,49 +1124,19 @@ void *mem_alloc(size_t mem_size, const char *error_tag) {
*/
free_memory :: () {
+ print(">> FREE <<\n");
/* TODO
reset_database(&database);
reset_database(&archive);
free(string_buffer); string_buffer = NULL;
- free(app_folder); app_folder = NULL;
+ free(app_directory); app_directory = NULL;
free(db_file_path); db_file_path = NULL;
free(ar_file_path); ar_file_path = NULL;
*/
}
/*
-bool initialize_app_folder() {
- size_t temp_size;
-
- char *home_path = getenv(HOME_PATH_ENV);
- if (home_path == NULL) {
- home_path = ".";
- }
- temp_size = strlen(home_path) + 1 + strlen(APP_FOLDER_NAME) + 1; // Add space for folder separator and '\0'.
- app_folder = mem_alloc(temp_size, "app folder");
- snprintf(app_folder, temp_size, "%s/%s", home_path, APP_FOLDER_NAME);
-
- // Create app folder.
- mkdir(app_folder, 0740);
- if (errno != 0 && errno != EEXIST) {
- print_error("Failed to create app folder '%s': %s.", app_folder, strerror(errno));
- return false;
- }
-
- // Set database file path.
- temp_size = strlen(app_folder) + 1 + strlen(DB_FILE_NAME) + 1; // Add space for folder separator and '\0'.
- db_file_path = mem_alloc(temp_size, "database file path");
- snprintf(db_file_path, temp_size, "%s/%s", app_folder, DB_FILE_NAME);
-
- // Set archive file path.
- temp_size = strlen(app_folder) + 1 + strlen(AR_FILE_NAME) + 1; // Add space for folder separator and '\0'.
- ar_file_path = mem_alloc(temp_size, "archive file path");
- snprintf(ar_file_path, temp_size, "%s/%s", app_folder, AR_FILE_NAME);
-
- return true;
-}
-
void exit_gracefully(int signal) {
flushinp();
ungetch('q');
@@ -1239,13 +1206,39 @@ bool read_enter_confirmation(int row, int style, const char *message) {
main :: () {
- /*
- if (initialize_app_folder() == false) {
- print_error("Failed to initialize app folder.");
- free_memory();
- return EXIT_FAILURE;
- }
+ defer free_memory();
+
+ { // Initialize app directory.
+
+ home_dir, success := get_home_directory();
+ if success == false {
+ home_dir = ".";
+ }
+
+ app_directory, success = get_absolute_path(home_dir); // TODO Returned in the temporary allocator.
+ if success == false {
+ print_error("Failed to find home directory '%'.", home_dir);
+ return;
+ }
+
+ app_directory = join(app_directory, "/", APP_FOLDER_NAME, allocator = temp);
+ // TODO DEBUG
+ // Check what's going on with the temp allocator:
+ // Is it really the responsible for these paths?
+ // It seems that the new compiler allows us to check this pretty easily.
+// free(app_directory);
+ print("bazinga: '%'\n", app_directory);
+// if make_directory_if_it_does_not_exist(app_directory, false) == false {
+// print_error("Failed to create app directory '%'.", app_directory);
+// return;
+// }
+ db_file_path = join(app_directory, "/", DB_FILE_NAME, allocator = temp);
+ ar_file_path = join(app_directory, "/", AR_FILE_NAME, allocator = temp);
+ }
+ return; // TODO DEBUG
+
+ /*
db = &database;
reset_database(&database);
reset_database(&archive);
@@ -1253,16 +1246,14 @@ main :: () {
if (is_file_accessible(db_file_path) == false) {
if (store_database(&database, db_file_path) == false) {
print_error("Failed to initialize database.");
- free_memory();
- return EXIT_FAILURE;
+ return;
}
}
if (is_file_accessible(ar_file_path) == false) {
if (export_to_csv(&archive, ar_file_path) == false) {
print_error("Failed to initialize archive.");
- free_memory();
- return EXIT_FAILURE;
+ return;
}
}
*/
@@ -1270,14 +1261,6 @@ main :: () {
args := get_command_line_arguments();
defer array_reset(*args);
-
-
- for args {
- if it_index == 0 continue;
-
-
- }
-
if args.count > 1 {
is_exit_requested := false;
@@ -1321,93 +1304,82 @@ main :: () {
print(" END Select last/bottom task.\n");
print("\n");
print("Notes\n");
- print("- All data files are stored '%/.task_time_tracker'.\n", get_home_directory());
+ print("- All data files are stored '%/.task_time_tracker'.\n", app_directory);
print(" If the home directory is undefined, './.task_time_tracker' will be used.\n");
print(" The database entries are stored in binary format on the 'database.bin' file.\n");
print(" The archived entries are stored in CSV format on the 'archive.csv' file.\n");
print("- During intensive tasks such as saving to file or recalculating totals times,\n");
print(" a diamond symbol is shown on the top left corner.\n");
- free_memory();
return;
}
-
if is_equal_to_any(args[it], "--version", "-v") {
print("Task Time Tracker version % \nCopyright % Daniel Martins\nLicense GPL-3.0-or-later\n", VERSION, YEAR);
- free_memory();
return;
}
- }}// TODO To be removed once we continue our journey...
- /*
- if (is_equal_to_any(args[it], "--import-csv", "-i")) {
- idx++;
- if (idx >= argc) {
+
+ if is_equal_to_any(args[it], "--import-csv", "-i") {
+ it += 1;
+ if it >= args.count {
print_error("Missing CSV file path to import.");
- free_memory();
- return EXIT_FAILURE;
+ return;
}
- if (load_database(&database, db_file_path) == false) {
- print_error("Failed to load database.");
- free_memory();
- return EXIT_FAILURE;
- }
- if (import_from_csv(&database, args[it]) == false) {
- print_error("Failed to import CSV file.");
- free_memory();
- return EXIT_FAILURE;
- }
- if (store_database(&database, db_file_path) == false) {
- print_error("Failed to store database.");
- free_memory();
- return EXIT_FAILURE;
- }
- reset_database(&database);
+ // TODO Implement
+// if (load_database(&database, db_file_path) == false) {
+// print_error("Failed to load database.");
+// return;
+// }
+// if (import_from_csv(&database, args[it]) == false) {
+// print_error("Failed to import CSV file.");
+// return;
+// }
+// if (store_database(&database, db_file_path) == false) {
+// print_error("Failed to store database.");
+// return;
+// }
+// reset_database(&database);
is_exit_requested = true;
continue;
}
- if (is_equal_to_any(args[it], "--export-csv", "-e")) {
- idx++;
- if (idx >= argc) {
+ if is_equal_to_any(args[it], "--export-csv", "-e") {
+ it += 1;
+ if it >= args.count {
print_error("Missing CSV file path to export.");
- free_memory();
- return EXIT_FAILURE;
- }
- if (load_database(&database, db_file_path) == false) {
- print_error("Failed to load database.");
- free_memory();
- return EXIT_FAILURE;
+ return;
}
- if (export_to_csv(&database, args[it]) == false) {
- print_error("Failed to export CSV file.");
- free_memory();
- return EXIT_FAILURE;
- }
- reset_database(&database);
+ // TODO Implement
+// if (load_database(&database, db_file_path) == false) {
+// print_error("Failed to load database.");
+// return;
+// }
+// if (export_to_csv(&database, args[it]) == false) {
+// print_error("Failed to export CSV file.");
+// return;
+// }
+// reset_database(&database);
is_exit_requested = true;
continue;
}
- if (is_equal_to_any(args[it], "--no-autosave", "-n")) {
+ if is_equal_to_any(args[it], "--no-autosave", "-n") {
is_autosave_enabled = false;
continue;
}
- print_error("%s: invalid option '%s'.\nTry '%s --help' for more information.", argv[0], args[it], argv[0]);
- free_memory();
- return EXIT_FAILURE;
+ print_error("%: invalid option '%'.\nTry '% --help' for more information.", args[0], args[it], args[0]);
+ return;
}
- if (is_exit_requested) {
- free_memory();
- return EXIT_SUCCESS;
+ if is_exit_requested {
+ return;
}
}
+ /*
if (load_database(&database, db_file_path) == false) {
print_error("Failed to load database.");
- free_memory();
- return EXIT_FAILURE;
+ return;
}
initialize_tui();
@@ -1823,7 +1795,6 @@ main :: () {
}
endwin();
- free_memory();
return error_saving ? EXIT_FAILURE : EXIT_SUCCESS;
*/
}