diff --git a/README.md b/README.md index 236f60a..0428bb6 100644 --- a/README.md +++ b/README.md @@ -16,8 +16,8 @@ some way to specify the browser things to do: -1. port vivify.vim to nixos - +1. port vivify.vim to nixos (got problems see `https://discourse.nixos.org/t/help-with-adding-a-vim-plugin-in-nixpkgs/76682`) +2. i'm gonna work on the TUI for now. diff --git a/a.out b/a.out new file mode 100755 index 0000000..dd386a0 Binary files /dev/null and b/a.out differ diff --git a/config.json b/config.json new file mode 100644 index 0000000..f988c52 --- /dev/null +++ b/config.json @@ -0,0 +1,6 @@ +{ + "directory": "/home/tomasr/Documents/Notes/" + + + +} diff --git a/shell.nix b/shell.nix index f9a4a4a..3b317dd 100644 --- a/shell.nix +++ b/shell.nix @@ -3,5 +3,7 @@ pkgs.mkShell { buildInputs = [ pkgs.vivify + pkgs.cjson + pkgs.pkg-config ]; } diff --git a/startup.c b/startup.c new file mode 100644 index 0000000..45d39d2 --- /dev/null +++ b/startup.c @@ -0,0 +1,78 @@ +#include +#include +#include +#include +#include + +int main() { + // Read config file + FILE *f = fopen("./config.json", "r"); + if (!f) { + fprintf(stderr, "Could not open config.json\n"); + return 1; + } + + fseek(f, 0, SEEK_END); + long size = ftell(f); + rewind(f); + + char *data = malloc(size + 1); + size_t read_bytes = fread(data, 1, size, f); + if (read_bytes != size) { + fprintf(stderr, "Failed to read file\n"); + free(data); + fclose(f); + return 1; + } + data[size] = 0; + fclose(f); + + // Parse JSON + cJSON *json = cJSON_Parse(data); + if (!json) { + fprintf(stderr, "JSON parse error\n"); + free(data); + return 1; + } + + // Get "directory" + cJSON *dirJsonFormat = cJSON_GetObjectItem(json, "directory"); + + char *notesDirectoryString = NULL; + if (dirJsonFormat && cJSON_IsString(dirJsonFormat) && dirJsonFormat->valuestring != NULL) { + notesDirectoryString = strdup(dirJsonFormat->valuestring); // independent string + } else { + notesDirectoryString = "~/Documents/Notes/"; // default + } + + // (TODO LATER) it might be a good idea to check if these directories exist + // (TODO LATER) expand ~ as it does not work with opendir() + printf("Opening %s\n", notesDirectoryString); + + //--------------------------------------------------------------------------------------- + // https://www.geeksforgeeks.org/c/c-program-list-files-sub-directories-directory/ + struct dirent *notesDirectoryEntry; + DIR *notesDirectory = opendir(notesDirectoryString); + if (notesDirectory == NULL) // opendir returns NULL if couldn't open directory + { + printf("Could not open current directory" ); + return 1; + } + // Refer https://pubs.opengroup.org/onlinepubs/7990989775/xsh/readdir.html + // for readdir() + while ((notesDirectoryEntry = readdir(notesDirectory)) != NULL) + printf("%s\n", notesDirectoryEntry->d_name); + + + //-------------------------------------------------------------------------------------- + + // list all the directories (except hidden ones). They are like obsidian vaults + + // Cleanup + if (dirJsonFormat && notesDirectoryString != dirJsonFormat->valuestring) free(notesDirectoryString); // only free strdup + closedir(notesDirectory); + cJSON_Delete(json); + free(data); + + return 0; +}