diff --git a/a.out b/a.out index 5c6468f..1402e55 100755 Binary files a/a.out and b/a.out differ diff --git a/startup.c b/startup.c index e4c6c55..fa6f9c0 100644 --- a/startup.c +++ b/startup.c @@ -9,26 +9,34 @@ #include #include -char** getVaultsFromDirectory(char *dirString, size_t *count) { +int compareString(const void *a, const void *b) { // this will be the function used by qsort + const char *str1 = *(const char **)a; + const char *str2 = *(const char **)b; + return strcmp(str1, str2); // strcmp returns <0, 0, >0 +} + + +char** getVaultsFromDirectory(char *dirString, size_t *count, int debug) { // (TODO LATER) it might be a good idea to check if these directories exist // (TODO LATER) expand ~ as it does not work with opendir() // this function is inputed a path to a directory (which comes usually from the config file) and outpus all the suitable directories (so not the hidden ones) which will serve as separate vaults for notes - printf("Opening %s aka the directory of vaults\n", dirString); + if (debug) {printf("\e[0;32m[DEBUG]\e[0m Opening %s aka the directory of vaults\n", dirString);} // originally from https://www.geeksforgeeks.org/c/c-program-list-files-sub-directories-directory/ struct dirent *vaultsDirectoryEntry; DIR *vaultsDirectory = opendir(dirString); if (vaultsDirectory == NULL) // opendir returns NULL if couldn't open directory { - printf("Could not open current directory" ); + printf("\e[0;31mERROR: Could not open current directory\e[0m\n" ); exit(1); //something is fucked up } char **dirsArray = NULL; // will contain all the dirs/vaults size_t dirsCount = 0; // we need to count how many dirs there is to always readjust how many memory we alloc // Refer https://pubs.opengroup.org/onlinepubs/7990989775/xsh/readdir.html // for readdir() + if (debug) {printf("┌------------------------------\n\e[0;32m[DEBUG]\e[0m Files and dirs from the directory\n");} while ((vaultsDirectoryEntry = readdir(vaultsDirectory)) != NULL) { - printf("%s\n", vaultsDirectoryEntry->d_name); // debugs every file/directory + if (debug) {printf("%s\n", vaultsDirectoryEntry->d_name);} // debugs every file/directory if (vaultsDirectoryEntry->d_name[0] != '.') { // if the entry don't start with a dot (so hidden dirs and hidden files) char fullPathEntry[PATH_MAX]; // creates a string of size of the maximum path lenght snprintf(fullPathEntry, sizeof(fullPathEntry), "%s/%s", dirString, vaultsDirectoryEntry->d_name); // sets the full absolute path to fullPathEntry @@ -41,6 +49,7 @@ char** getVaultsFromDirectory(char *dirString, size_t *count) { } } } + if (debug) {printf("└------------------------------\n");} // (TODO LATER) Alphabetically sort them // free's some used memory @@ -49,38 +58,29 @@ char** getVaultsFromDirectory(char *dirString, size_t *count) { return dirsArray; } -int openNvim(char *path) { - printf("Opening with command nvim +:NvimTreeOpen %s\n", path); - execlp("nvim", - "nvim", // we can specify each argument for nvim by passing more args to execlp - path, - NULL); - perror("execlp failed"); - return 1; -} - - -char** getNotesFromVault(char *pathToVault, char *vault, int *count) { +char** getNotesFromVault(char *pathToVault, char *vault, int *count, int debug) { // this function is inputed a path to a vault (which was selected before) and outpus all the suitable notes (so not the hidden ones) - printf("Opening %s aka the vault\n", vault); + if (debug) {printf("\e[0;32m[DEBUG]\e[0m Opening %s aka the vault\n", vault);} // originally from https://www.geeksforgeeks.org/c/c-program-list-files-sub-directories-directory/ struct dirent *notesDirectoryEntry; char tempPath[PATH_MAX]; snprintf(tempPath, sizeof(tempPath), "%s/%s", pathToVault, vault); // sets the full absolute path to fullPathEntry DIR *vaultDirectory = opendir(tempPath); - if (vaultDirectory == NULL) // opendir returns NULL if couldn't open directory - { - printf("Could not open current directory" ); - exit(1); //something is fucked up + if (vaultDirectory == NULL) { // opendir returns NULL if couldn't open directory + printf("\e[0;31mERROR: Could not open current directory\e[0m\n" ); + exit(1); //something is fucked up } char **notesArray = NULL; // will contain all the notes size_t notesCount = 0; // we need to count how many notes there is to always readjust how many memory we alloc // Refer https://pubs.opengroup.org/onlinepubs/7990989775/xsh/readdir.html // for readdir() + if (debug) {printf("┌------------------------------\n\e[0;32m[DEBUG]\e[0m Files and dirs from the vault:\n");} while ((notesDirectoryEntry = readdir(vaultDirectory)) != NULL) { - printf("%s\n", notesDirectoryEntry->d_name); // debugs every note + + if (debug) {printf("%s\n", notesDirectoryEntry->d_name);} + if (notesDirectoryEntry->d_name[0] != '.') { // if the entry don't start with a dot (so hidden dirs and hidden files) char fullPathEntry[PATH_MAX]; // creates a string of size of the maximum path lenght snprintf(fullPathEntry, sizeof(fullPathEntry), "%s/%s/%s", pathToVault, vault, notesDirectoryEntry->d_name); // sets the full absolute path to fullPathEntry @@ -93,6 +93,7 @@ char** getNotesFromVault(char *pathToVault, char *vault, int *count) { } } } + if (debug) {printf("└ ------------------------------\n");} // (TODO LATER) Alphabetically sort them // free's some used memory closedir(vaultDirectory); @@ -100,7 +101,7 @@ char** getNotesFromVault(char *pathToVault, char *vault, int *count) { return notesArray; } -char* ncursesSelect(char **options, char *optionsType, size_t optionsNumber) { // this fonction make a TUI to select one of multiple options. +char* ncursesSelect(char **options, char *optionsType, size_t optionsNumber, int debug) { // this fonction make a TUI to select one of multiple options. int highlight = 0; //curently highlighted option int choice = 1; //selected index int key; @@ -152,18 +153,35 @@ char* ncursesSelect(char **options, char *optionsType, size_t optionsNumber) { / return options[choice]; } -int compareString(const void *a, const void *b) { // this will be the function used by qsort - const char *str1 = *(const char **)a; - const char *str2 = *(const char **)b; - return strcmp(str1, str2); // strcmp returns <0, 0, >0 -} + +int openNvim(char *path, int debug) { + if (debug) {printf("\e[0;32m[DEBUG]\e[0m Opening with command:\nnvim +:NvimTreeOpen %s\n", path);} + execlp("nvim", + "nvim", // we can specify each argument for nvim by passing more args to execlp + path, + NULL); + perror("\e[0;31mERROR: execlp failed. Nvim might be not installed or not in path.\e[0m\n"); + return 1; +} -int main() { + + + +int main(int argc, char *argv[]) { + + int debug = 0; + for (int i = 1; i < argc; i++) { + if (strcmp(argv[i], "-V") == 0) { + debug = 0; + } else if (strcmp(argv[i], "-v") == 0) { + debug = 1; + } + } // Read config file FILE *f = fopen("./config.json", "r"); if (!f) { - fprintf(stderr, "Could not open config.json\n"); + fprintf(stderr, "\e[0;31mERROR: Could not open config.json\e[0m\n"); return 1; } @@ -174,7 +192,7 @@ int main() { 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"); + fprintf(stderr, "\e[0;31mERROR: Failed to read file\e[0m\n"); free(data); fclose(f); return 1; @@ -185,7 +203,7 @@ int main() { // Parse JSON cJSON *json = cJSON_Parse(data); if (!json) { - fprintf(stderr, "JSON parse error\n"); + fprintf(stderr, "\e[0;31mERROR: JSON parse error\[0m\n"); free(data); return 1; } @@ -204,11 +222,14 @@ int main() { while(!shouldExit) { // this loop is the vault selector size_t vaultsCount = 0; - char **vaultsArray = getVaultsFromDirectory(notesDirectoryString, &vaultsCount); + char **vaultsArray = getVaultsFromDirectory(notesDirectoryString, &vaultsCount, debug); qsort(vaultsArray, vaultsCount, sizeof(const char *), compareString); // sorts the vaults alphabetically - printf("Available vaults:\n"); - for (size_t i = 0; i < vaultsCount; i++) { - printf("%s\n", vaultsArray[i]); + if (debug) { + printf("┌------------------------------\n\e[0;32m[DEBUG]\e[0m Available vaults:\n"); + for (size_t i = 0; i < vaultsCount; i++) { + printf("%s\n", vaultsArray[i]); + } + printf("└ ------------------------------\n"); } // adds "create a new vault" into the vaultsArray @@ -219,20 +240,23 @@ int main() { vaultsArray[vaultsCount+1] = "Settings"; vaultsArray[vaultsCount+2] = "Quit (Ctrl+C)"; // select a vault - char *vaultSelected = ncursesSelect(vaultsArray, "vault", vaultsCount + extraOptions); - printf("Selected vault:%s\n", vaultSelected); + char *vaultSelected = ncursesSelect(vaultsArray, "vault", vaultsCount + extraOptions, debug); + if (debug) {printf("\e[0;32m[DEBUG]\e[0m Selected vault:%s\n", vaultSelected);} if (vaultSelected != "Create a new vault" && vaultSelected != "Settings" && vaultSelected != "Quit (Ctrl+C)") { int shouldChangeVault = 0; while (!shouldExit && !shouldChangeVault) { // this loop is the note selector int filesCount = 0; - char **filesArray = getNotesFromVault(notesDirectoryString, vaultSelected, &filesCount); + char **filesArray = getNotesFromVault(notesDirectoryString, vaultSelected, &filesCount, debug); qsort(filesArray, filesCount, sizeof(const char *), compareString); // sorts the notes alphabetically - // - printf("Available notes:\n"); - for (size_t i = 0; i < filesCount; i++) { - printf("%s\n", filesArray[i]); + + if (debug) { + printf("┌------------------------------\n\e[0;32m[DEBUG]\e[0m Available notes:\n"); + for (size_t i = 0; i < filesCount; i++) { + printf("%s\n", filesArray[i]); + } + printf("└ ------------------------------\n"); } // adds options int extraNotesOptions = 3; @@ -240,18 +264,19 @@ int main() { filesArray[filesCount] = "Create new note"; filesArray[filesCount+1] = "Back to vault selection"; filesArray[filesCount+2] = "Quit (Ctrl+C)"; - char *noteSelected = ncursesSelect(filesArray, "note", filesCount + extraNotesOptions); - printf("Selected note: %s\n", noteSelected); + char *noteSelected = ncursesSelect(filesArray, "note", filesCount + extraNotesOptions, debug); + if (debug) {printf("\e[0;32m[DEBUG]\e[0m Selected note: %s\n", noteSelected);} + if (noteSelected != "Create new note" && noteSelected != "Back to vault selection" && noteSelected != "Quit (Ctrl+C)") { char fullPath[PATH_MAX]; sprintf(fullPath, "%s/%s/%s", notesDirectoryString, vaultSelected, noteSelected); - openNvim(fullPath); + openNvim(fullPath, debug); } else if (noteSelected == "Create new note") { } else if (noteSelected == "Back to vault selection") { shouldChangeVault = 1; } else if (noteSelected == "Quit (Ctrl+C)") { - printf("The program was exited,\n"); + if (debug) {printf("\e[0;32m[DEBUG]\e[0m The program was exited,\n");} shouldExit = 1; } } @@ -261,7 +286,7 @@ int main() { } else if (vaultSelected == "Settings") { //(TODO LATER) add a way to change the config.json from the app or at least show all the options } else if (vaultSelected == "Quit (Ctrl+C)") { - printf("The program was exited.\n"); + if (debug) {printf("\e[0;32m[DEBUG]\e[0m The program was exited.\n");} shouldExit = 1; } }