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
]; ];
} }
+75 -32
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,46 +199,76 @@ int main() {
} else { } else {
notesDirectoryString = "~/Documents/Notes/"; // default notesDirectoryString = "~/Documents/Notes/"; // default
} }
size_t vaultsCount = 0;
char **vaultsArray = getVaultsFromDirectory(notesDirectoryString, &vaultsCount);
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]);
}
// adds "create a new vault" into the vaultsArray int shouldExit = 0;
// (TODO LATER) maybe add an option for setting while(!shouldExit) {
const int extraOptions = 2; // this loop is the vault selector
vaultsArray = realloc(vaultsArray, (vaultsCount + extraOptions)*sizeof(char*)); // resize dirsArray so that size_t vaultsCount = 0;
// (TODO LATER) add a way to Colorize the extraOptions char **vaultsArray = getVaultsFromDirectory(notesDirectoryString, &vaultsCount);
vaultsArray[vaultsCount] = "Create a new vault"; // some more options that are not vaults qsort(vaultsArray, vaultsCount, sizeof(const char *), compareString); // sorts the vaults alphabetically
vaultsArray[vaultsCount+1] = "Settings"; printf("Available vaults:\n");
// select a vault for (size_t i = 0; i < vaultsCount; i++) {
char *vaultSelected = ncursesSelect(vaultsArray, "vault", vaultsCount + extraOptions); printf("%s\n", vaultsArray[i]);
printf("Selected vault:%s\n", vaultSelected);
if (vaultSelected != "Create a new vault" || vaultSelected != "Settings") {
int filesCount = 0;
char **filesArray = getNotesFromVault(notesDirectoryString, vaultSelected, &filesCount);
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]);
} }
char *noteSelected = ncursesSelect(filesArray, "note", filesCount);
printf("Selected note: %s", noteSelected);
} else if (vaultSelected == "Create a new vault") {
} else if (vaultSelected == "Settings") { // adds "create a new vault" into the vaultsArray
//(TODO LATER) add a way to change the config.json from the app or at least show all the options const int extraOptions = 3;
vaultsArray = realloc(vaultsArray, (vaultsCount + extraOptions)*sizeof(char*)); // resize vaultsArray to fit the extra options
// (TODO LATER) add a way to Colorize the extraOptions
vaultsArray[vaultsCount] = "Create a new vault"; // some more options that are not vaults
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);
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);
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]);
}
// adds options
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 == "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");
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;
} }