From e97e1060011707bf4e2ba4addbe9002bb13abf1e Mon Sep 17 00:00:00 2001 From: Zvonimir Rudinski Date: Mon, 22 Dec 2025 04:03:26 +0100 Subject: [PATCH] add csv export --- engine/todo.c | 17 +++++++++++++++++ engine/todo.h | 3 +++ main.c | 15 ++++++++++++++- 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/engine/todo.c b/engine/todo.c index 3c60c66..a69ff94 100644 --- a/engine/todo.c +++ b/engine/todo.c @@ -55,3 +55,20 @@ char *todo_item_serialize(TodoItem *item, int *buffer_size_out) { return buffer; } + +// Exports +void todo_export_as_csv(TodoItem *items, int item_count, const char *filename) { + FILE *file = fopen(filename, "w"); + if (file == NULL) { + perror("Failed to open file for writing"); + return; + } + + // Write CSV header + fprintf(file, "Title,Completed\n"); + for (int i = 0; i < item_count; i++) { + fprintf(file, "\"%s\",%s\n", items[i].title, + items[i].completed ? "Yes" : "No"); + } + fclose(file); +} diff --git a/engine/todo.h b/engine/todo.h index 82419c6..84e98d5 100644 --- a/engine/todo.h +++ b/engine/todo.h @@ -20,4 +20,7 @@ void todo_print_item(TodoItem *item); // Serialization char *todo_item_serialize(TodoItem *item, int *buffer_size_out); + +// Exports +void todo_export_as_csv(TodoItem *items, int item_count, const char *filename); #endif diff --git a/main.c b/main.c index c2ce0b7..378b691 100644 --- a/main.c +++ b/main.c @@ -17,7 +17,7 @@ #define COMPLETED_PAIR 3 #include "3rd-party/stb-ds.h" -#define KEY_BINDINGS " [a]dd [v]view [m]ark [d]elete [w]rite [l]oad [q]uit" +#define KEY_BINDINGS " [a]dd [v]view [m]ark [d]elete [e]xport csv [w]rite [l]oad [q]uit" bool dirty = false; char SAVE_FILE[MAX_PATH_LENGTH] = ""; @@ -32,6 +32,7 @@ enum Command { WRITE_TO_FILE = 'w', LOAD_FROM_FILE = 'l', + EXPORT_AS_CSV = 'e', UP = 'k', DOWN = 'j', @@ -346,6 +347,14 @@ void load_from_file_handler(void) { dirty = false; } +void export_as_csv_handler(void) { + // create the csv file path + char csv_file_path[MAX_PATH_LENGTH + 5]; // +5 for .csv extension + snprintf(csv_file_path, sizeof(csv_file_path), "%s.csv", SAVE_FILE); + + todo_export_as_csv(todos, arrlen(todos), csv_file_path); +} + int main(int argc, char **argv) { int terminal_width = 0; int terminal_height = 0; @@ -390,6 +399,10 @@ int main(int argc, char **argv) { write_to_file_handler(); alert("Saved!", terminal_width, terminal_height); break; + case EXPORT_AS_CSV: + export_as_csv_handler(); + alert("Exported as CSV!", terminal_width, terminal_height); + break; case ADD: add_command_handler(terminal_width, terminal_height); break;