Compare commits
1 Commits
6862af407e
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
e97e106001
|
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
15
main.c
15
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;
|
||||
|
||||
Reference in New Issue
Block a user