mirror of
https://github.com/tomasriveral/NoteWrapper.git
synced 2026-08-12 10:20:46 +02:00
cleaned debug logic and debug messages
This commit is contained in:
@@ -9,26 +9,34 @@
|
|||||||
#include <ncurses.h>
|
#include <ncurses.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
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) it might be a good idea to check if these directories exist
|
||||||
// (TODO LATER) expand ~ as it does not work with opendir()
|
// (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
|
// 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/
|
// originally from https://www.geeksforgeeks.org/c/c-program-list-files-sub-directories-directory/
|
||||||
struct dirent *vaultsDirectoryEntry;
|
struct dirent *vaultsDirectoryEntry;
|
||||||
DIR *vaultsDirectory = opendir(dirString);
|
DIR *vaultsDirectory = opendir(dirString);
|
||||||
if (vaultsDirectory == NULL) // opendir returns NULL if couldn't open directory
|
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
|
exit(1); //something is fucked up
|
||||||
}
|
}
|
||||||
char **dirsArray = NULL; // will contain all the dirs/vaults
|
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
|
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
|
// Refer https://pubs.opengroup.org/onlinepubs/7990989775/xsh/readdir.html
|
||||||
// for readdir()
|
// for readdir()
|
||||||
|
if (debug) {printf("┌------------------------------\n\e[0;32m[DEBUG]\e[0m Files and dirs from the directory\n");}
|
||||||
while ((vaultsDirectoryEntry = readdir(vaultsDirectory)) != NULL) {
|
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)
|
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
|
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
|
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
|
// (TODO LATER) Alphabetically sort them
|
||||||
|
|
||||||
// free's some used memory
|
// free's some used memory
|
||||||
@@ -49,38 +58,29 @@ char** getVaultsFromDirectory(char *dirString, size_t *count) {
|
|||||||
return dirsArray;
|
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, int debug) {
|
||||||
|
|
||||||
char** getNotesFromVault(char *pathToVault, char *vault, int *count) {
|
|
||||||
// this function is inputed a path to a vault (which was selected before) and outpus all the suitable notes (so not the hidden ones)
|
// 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/
|
// originally from https://www.geeksforgeeks.org/c/c-program-list-files-sub-directories-directory/
|
||||||
struct dirent *notesDirectoryEntry;
|
struct dirent *notesDirectoryEntry;
|
||||||
char tempPath[PATH_MAX];
|
char tempPath[PATH_MAX];
|
||||||
snprintf(tempPath, sizeof(tempPath), "%s/%s", pathToVault, vault); // sets the full absolute path to fullPathEntry
|
snprintf(tempPath, sizeof(tempPath), "%s/%s", pathToVault, vault); // sets the full absolute path to fullPathEntry
|
||||||
DIR *vaultDirectory = opendir(tempPath);
|
DIR *vaultDirectory = opendir(tempPath);
|
||||||
if (vaultDirectory == NULL) // opendir returns NULL if couldn't open directory
|
if (vaultDirectory == NULL) { // opendir returns NULL if couldn't open directory
|
||||||
{
|
printf("\e[0;31mERROR: Could not open current directory\e[0m\n" );
|
||||||
printf("Could not open current directory" );
|
exit(1); //something is fucked up
|
||||||
exit(1); //something is fucked up
|
|
||||||
}
|
}
|
||||||
char **notesArray = NULL; // will contain all the notes
|
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
|
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
|
// Refer https://pubs.opengroup.org/onlinepubs/7990989775/xsh/readdir.html
|
||||||
// for readdir()
|
// for readdir()
|
||||||
|
if (debug) {printf("┌------------------------------\n\e[0;32m[DEBUG]\e[0m Files and dirs from the vault:\n");}
|
||||||
while ((notesDirectoryEntry = readdir(vaultDirectory)) != NULL) {
|
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)
|
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
|
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
|
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
|
// (TODO LATER) Alphabetically sort them
|
||||||
// free's some used memory
|
// free's some used memory
|
||||||
closedir(vaultDirectory);
|
closedir(vaultDirectory);
|
||||||
@@ -100,7 +101,7 @@ char** getNotesFromVault(char *pathToVault, char *vault, int *count) {
|
|||||||
return notesArray;
|
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 highlight = 0; //curently highlighted option
|
||||||
int choice = 1; //selected index
|
int choice = 1; //selected index
|
||||||
int key;
|
int key;
|
||||||
@@ -152,18 +153,35 @@ char* ncursesSelect(char **options, char *optionsType, size_t optionsNumber) { /
|
|||||||
return options[choice];
|
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;
|
int openNvim(char *path, int debug) {
|
||||||
const char *str2 = *(const char **)b;
|
if (debug) {printf("\e[0;32m[DEBUG]\e[0m Opening with command:\nnvim +:NvimTreeOpen %s\n", path);}
|
||||||
return strcmp(str1, str2); // strcmp returns <0, 0, >0
|
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
|
// Read config file
|
||||||
FILE *f = fopen("./config.json", "r");
|
FILE *f = fopen("./config.json", "r");
|
||||||
if (!f) {
|
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;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -174,7 +192,7 @@ int main() {
|
|||||||
char *data = malloc(size + 1);
|
char *data = malloc(size + 1);
|
||||||
size_t read_bytes = fread(data, 1, size, f);
|
size_t read_bytes = fread(data, 1, size, f);
|
||||||
if (read_bytes != size) {
|
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);
|
free(data);
|
||||||
fclose(f);
|
fclose(f);
|
||||||
return 1;
|
return 1;
|
||||||
@@ -185,7 +203,7 @@ int main() {
|
|||||||
// Parse JSON
|
// Parse JSON
|
||||||
cJSON *json = cJSON_Parse(data);
|
cJSON *json = cJSON_Parse(data);
|
||||||
if (!json) {
|
if (!json) {
|
||||||
fprintf(stderr, "JSON parse error\n");
|
fprintf(stderr, "\e[0;31mERROR: JSON parse error\[0m\n");
|
||||||
free(data);
|
free(data);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -204,11 +222,14 @@ int main() {
|
|||||||
while(!shouldExit) {
|
while(!shouldExit) {
|
||||||
// this loop is the vault selector
|
// this loop is the vault selector
|
||||||
size_t vaultsCount = 0;
|
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
|
qsort(vaultsArray, vaultsCount, sizeof(const char *), compareString); // sorts the vaults alphabetically
|
||||||
printf("Available vaults:\n");
|
if (debug) {
|
||||||
for (size_t i = 0; i < vaultsCount; i++) {
|
printf("┌------------------------------\n\e[0;32m[DEBUG]\e[0m Available vaults:\n");
|
||||||
printf("%s\n", vaultsArray[i]);
|
for (size_t i = 0; i < vaultsCount; i++) {
|
||||||
|
printf("%s\n", vaultsArray[i]);
|
||||||
|
}
|
||||||
|
printf("└ ------------------------------\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
// adds "create a new vault" into the vaultsArray
|
// adds "create a new vault" into the vaultsArray
|
||||||
@@ -219,20 +240,23 @@ int main() {
|
|||||||
vaultsArray[vaultsCount+1] = "Settings";
|
vaultsArray[vaultsCount+1] = "Settings";
|
||||||
vaultsArray[vaultsCount+2] = "Quit (Ctrl+C)";
|
vaultsArray[vaultsCount+2] = "Quit (Ctrl+C)";
|
||||||
// select a vault
|
// select a vault
|
||||||
char *vaultSelected = ncursesSelect(vaultsArray, "vault", vaultsCount + extraOptions);
|
char *vaultSelected = ncursesSelect(vaultsArray, "vault", vaultsCount + extraOptions, debug);
|
||||||
printf("Selected vault:%s\n", vaultSelected);
|
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)") {
|
if (vaultSelected != "Create a new vault" && vaultSelected != "Settings" && vaultSelected != "Quit (Ctrl+C)") {
|
||||||
int shouldChangeVault = 0;
|
int shouldChangeVault = 0;
|
||||||
while (!shouldExit && !shouldChangeVault) {
|
while (!shouldExit && !shouldChangeVault) {
|
||||||
// this loop is the note selector
|
// this loop is the note selector
|
||||||
int filesCount = 0;
|
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
|
qsort(filesArray, filesCount, sizeof(const char *), compareString); // sorts the notes alphabetically
|
||||||
//
|
|
||||||
printf("Available notes:\n");
|
if (debug) {
|
||||||
for (size_t i = 0; i < filesCount; i++) {
|
printf("┌------------------------------\n\e[0;32m[DEBUG]\e[0m Available notes:\n");
|
||||||
printf("%s\n", filesArray[i]);
|
for (size_t i = 0; i < filesCount; i++) {
|
||||||
|
printf("%s\n", filesArray[i]);
|
||||||
|
}
|
||||||
|
printf("└ ------------------------------\n");
|
||||||
}
|
}
|
||||||
// adds options
|
// adds options
|
||||||
int extraNotesOptions = 3;
|
int extraNotesOptions = 3;
|
||||||
@@ -240,18 +264,19 @@ int main() {
|
|||||||
filesArray[filesCount] = "Create new note";
|
filesArray[filesCount] = "Create new note";
|
||||||
filesArray[filesCount+1] = "Back to vault selection";
|
filesArray[filesCount+1] = "Back to vault selection";
|
||||||
filesArray[filesCount+2] = "Quit (Ctrl+C)";
|
filesArray[filesCount+2] = "Quit (Ctrl+C)";
|
||||||
char *noteSelected = ncursesSelect(filesArray, "note", filesCount + extraNotesOptions);
|
char *noteSelected = ncursesSelect(filesArray, "note", filesCount + extraNotesOptions, debug);
|
||||||
printf("Selected note: %s\n", noteSelected);
|
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)") {
|
if (noteSelected != "Create new note" && noteSelected != "Back to vault selection" && noteSelected != "Quit (Ctrl+C)") {
|
||||||
char fullPath[PATH_MAX];
|
char fullPath[PATH_MAX];
|
||||||
sprintf(fullPath, "%s/%s/%s", notesDirectoryString, vaultSelected, noteSelected);
|
sprintf(fullPath, "%s/%s/%s", notesDirectoryString, vaultSelected, noteSelected);
|
||||||
openNvim(fullPath);
|
openNvim(fullPath, debug);
|
||||||
} else if (noteSelected == "Create new note") {
|
} else if (noteSelected == "Create new note") {
|
||||||
|
|
||||||
} else if (noteSelected == "Back to vault selection") {
|
} else if (noteSelected == "Back to vault selection") {
|
||||||
shouldChangeVault = 1;
|
shouldChangeVault = 1;
|
||||||
} else if (noteSelected == "Quit (Ctrl+C)") {
|
} 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;
|
shouldExit = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -261,7 +286,7 @@ int main() {
|
|||||||
} else if (vaultSelected == "Settings") {
|
} else if (vaultSelected == "Settings") {
|
||||||
//(TODO LATER) add a way to change the config.json from the app or at least show all the options
|
//(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)") {
|
} 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;
|
shouldExit = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user