worked the base logic

This commit is contained in:
Tomas Rivera
2026-03-30 12:30:33 +02:00
parent 9a5e01f4b0
commit 86d0464219
4 changed files with 82 additions and 34 deletions
+4
View File
@@ -28,4 +28,8 @@ per vault config inside the json file
a tui configurator that writes to the json a tui configurator that writes to the json
an option or save in another part for backups (like periodically just put the directory you want to save in config) an option or save in another part for backups (like periodically just put the directory you want to save in config)
a lot of options. via the config.json or via flags.
a option/flag to not render.
later to port it to Nix later to port it to Nix
BIN
View File
Binary file not shown.
+1
View File
@@ -6,5 +6,6 @@ pkgs.mkShell {
pkgs.cjson pkgs.cjson
pkgs.ncurses pkgs.ncurses
pkgs.pkg-config pkgs.pkg-config
pkgs.gdb
]; ];
} }
+50 -7
View File
@@ -7,6 +7,7 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <limits.h> // for PATH_MAX #include <limits.h> // for PATH_MAX
#include <ncurses.h> #include <ncurses.h>
#include <unistd.h>
char** getVaultsFromDirectory(char *dirString, size_t *count) { char** getVaultsFromDirectory(char *dirString, size_t *count) {
// (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
@@ -48,6 +49,18 @@ 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) { 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); printf("Opening %s aka the vault\n", vault);
@@ -186,6 +199,10 @@ int main() {
} else { } else {
notesDirectoryString = "~/Documents/Notes/"; // default notesDirectoryString = "~/Documents/Notes/"; // default
} }
int shouldExit = 0;
while(!shouldExit) {
// this loop is the vault selector
size_t vaultsCount = 0; size_t vaultsCount = 0;
char **vaultsArray = getVaultsFromDirectory(notesDirectoryString, &vaultsCount); char **vaultsArray = getVaultsFromDirectory(notesDirectoryString, &vaultsCount);
qsort(vaultsArray, vaultsCount, sizeof(const char *), compareString); // sorts the vaults alphabetically qsort(vaultsArray, vaultsCount, sizeof(const char *), compareString); // sorts the vaults alphabetically
@@ -195,17 +212,20 @@ int main() {
} }
// adds "create a new vault" into the vaultsArray // adds "create a new vault" into the vaultsArray
// (TODO LATER) maybe add an option for setting const int extraOptions = 3;
const int extraOptions = 2; vaultsArray = realloc(vaultsArray, (vaultsCount + extraOptions)*sizeof(char*)); // resize vaultsArray to fit the extra options
vaultsArray = realloc(vaultsArray, (vaultsCount + extraOptions)*sizeof(char*)); // resize dirsArray so that
// (TODO LATER) add a way to Colorize the extraOptions // (TODO LATER) add a way to Colorize the extraOptions
vaultsArray[vaultsCount] = "Create a new vault"; // some more options that are not vaults vaultsArray[vaultsCount] = "Create a new vault"; // some more options that are not vaults
vaultsArray[vaultsCount+1] = "Settings"; vaultsArray[vaultsCount+1] = "Settings";
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);
printf("Selected vault:%s\n", vaultSelected); printf("Selected vault:%s\n", vaultSelected);
if (vaultSelected != "Create a new vault" || vaultSelected != "Settings") { 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; int filesCount = 0;
char **filesArray = getNotesFromVault(notesDirectoryString, vaultSelected, &filesCount); char **filesArray = getNotesFromVault(notesDirectoryString, vaultSelected, &filesCount);
qsort(filesArray, filesCount, sizeof(const char *), compareString); // sorts the notes alphabetically qsort(filesArray, filesCount, sizeof(const char *), compareString); // sorts the notes alphabetically
@@ -214,18 +234,41 @@ int main() {
for (size_t i = 0; i < filesCount; i++) { for (size_t i = 0; i < filesCount; i++) {
printf("%s\n", filesArray[i]); printf("%s\n", filesArray[i]);
} }
char *noteSelected = ncursesSelect(filesArray, "note", filesCount); // adds options
printf("Selected note: %s", noteSelected); int extraNotesOptions = 3;
filesArray = realloc(filesArray, (filesCount + extraNotesOptions)*sizeof(char*)); // resize filesArray to fit the extra options
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);
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);
} 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");
shouldExit = 1;
}
}
} else if (vaultSelected == "Create a new vault") { } else if (vaultSelected == "Create a new vault") {
} 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)") {
printf("The program was exited.\n");
shouldExit = 1;
}
} }
// Cleanup // Cleanup
if (dirJsonFormat && notesDirectoryString != dirJsonFormat->valuestring) free(notesDirectoryString); // only free strdup if (dirJsonFormat && notesDirectoryString != dirJsonFormat->valuestring) free(notesDirectoryString); // only free strdup
cJSON_Delete(json); cJSON_Delete(json);
free(data); free(data);
return 0; return 0;
} }