Added some flags

This commit is contained in:
Tomas Rivera
2026-04-04 12:55:55 +02:00
parent e2e338b5e4
commit 21842880b2
4 changed files with 40 additions and 24 deletions
+1 -1
View File
@@ -47,5 +47,5 @@ Change `$/.config/notewrapper/config.json`. If it does not exist. On building, i
- [x] Port to vim - [x] Port to vim
- [ ] Fixe all the small stuff marked //(TODO LATER) in the code - [ ] Fixe all the small stuff marked //(TODO LATER) in the code
- [ ] Add a way to delete notes - [ ] Add a way to delete notes
- [ ] Comply with GPL-3 notice (add info about no waranty, etc.) - [x] Comply with GPL-3 notice (add info about no waranty, etc.)
- [ ] A converter for both type of journals - [ ] A converter for both type of journals
+4 -3
View File
@@ -164,7 +164,7 @@ int main(int argc, char *argv[]) {
printf(" -r, --render Renders the note with Vivify.\n"); printf(" -r, --render Renders the note with Vivify.\n");
printf(" -R, --no-render Do not render.\n"); printf(" -R, --no-render Do not render.\n");
printf(" -v, --vault <vault's name> Specify the vault.\n"); printf(" -v, --vault <vault's name> Specify the vault.\n");
printf(" --version Display the program version.\n"); printf(" --version Display the program version and the GPL3 notice.\n");
printf(" -V, --verbose Show debug information.\n"); printf(" -V, --verbose Show debug information.\n");
return 1; return 1;
} else if (strcmp(argv[i], "-e") == 0 || strcmp(argv[i], "--editor") == 0) { } else if (strcmp(argv[i], "-e") == 0 || strcmp(argv[i], "--editor") == 0) {
@@ -191,8 +191,10 @@ int main(int argc, char *argv[]) {
error(i+1==argc, "user", "Missing argument. Pleaase use -v <vault's name> or --vault <vault's name>"); error(i+1==argc, "user", "Missing argument. Pleaase use -v <vault's name> or --vault <vault's name>");
bypassVaultSelection = argv[i+1]; // (TODO LATER) Add security checks pass ti strndup. and if vault don't exist create one. SEE (TODO LATER) where bypassVaultSelection is checked (TODO LATER) Add check if vault exist bypassVaultSelection = argv[i+1]; // (TODO LATER) Add security checks pass ti strndup. and if vault don't exist create one. SEE (TODO LATER) where bypassVaultSelection is checked (TODO LATER) Add check if vault exist
} else if (strcmp(argv[i], "--version") == 0) { } else if (strcmp(argv[i], "--version") == 0) {
printf("There is still no released version\n"); printf("There is still no released version\n\n Copyright (C) 2026 Tomás Rivera\n License GPLv3: GNU GPL version 3 <https://gnu.org/licenses/gpl.html>.\n This is free software: you are free to change and redistribute it.\n There is NO WARRANTY, to the extent permitted by law.\n\n Written by Tomás Rivera.\n");
return 0; return 0;
} else if (argv[i][0] == '-') {
error(1, "user", "unexpected argument \"%s\" found\nFor more information, try \"--help\".", argv[i]);
} }
} }
// if -n or --note is set but note -v or --vaults // if -n or --note is set but note -v or --vaults
@@ -219,7 +221,6 @@ int main(int argc, char *argv[]) {
// adds "create a new vault" into the vaultsArray // adds "create a new vault" into the vaultsArray
const int extraOptions = 3; const int extraOptions = 3;
vaultsArray = realloc(vaultsArray, (vaultsCount + extraOptions)*sizeof(char*)); // resize vaultsArray to fit the extra options 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] = "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)"; vaultsArray[vaultsCount+2] = "Quit (Ctrl+C)";
+33 -18
View File
@@ -3,8 +3,6 @@
const char *supportedEditor[] = {"neovim", "vim"}; const char *supportedEditor[] = {"neovim", "vim"};
const int numEditors = 2; const int numEditors = 2;
//(TODO LATER) We should write a debug() function and an error(function)
int compareString(const void *a, const void *b) { int compareString(const void *a, const void *b) {
const char *str1 = *(const char **)a; const char *str1 = *(const char **)a;
const char *str2 = *(const char **)b; const char *str2 = *(const char **)b;
@@ -114,14 +112,34 @@ int isStringInFile(const char *path, const char *string, const int shouldDebug)
debug("The string %s is not inside %s", string, path); debug("The string %s is not inside %s", string, path);
return 0; return 0;
} }
void appendToFile(const char *path, const char *string, const int shouldDebug) { void appendToFile(const char *path, const char *string, const int shouldDebug) {
// (TODO LATER) we should check if it is not already at the end and not append. (Usefull for \n) FILE *file = fopen(path, "r");
FILE *file = fopen(path, "a"); char lastLine[1024] = {0};
error(file == NULL, "program", "could not open %s", path); error(file == NULL, "program", "could not open %s", path);
fprintf(file, "%s", string); char buffer[BUFFER_SIZE]; // it does not matter if we have a small buffer. string is relatively small (most time \n or the name of the file). So it is under BUFFER_SIZE. If the last line is more than BUFFER_SIZE. It can't be equal to string
fclose(file);
debug("%s was appended to %s succesfully", string, path); // Read file line by line to get the last one
while (fgets(buffer, sizeof(buffer), file) != NULL) {
strncpy(lastLine, buffer, sizeof(lastLine) - 1);
}
fclose(file);
// Compare last line with string
if (strcmp(lastLine, string) == 0) {
if (shouldDebug) {
debug("Skipping append: last line already matches \"%s\"", string);
}
return;
}
// Append since it's different
file = fopen(path, "a");
error(file == NULL, "program", "could not open %s", path);
fprintf(file, "%s", string);
fclose(file);
debug("\"%s\" was appended to %s successfully", string, path);
} }
void sanitize(char *string) { void sanitize(char *string) {
@@ -151,18 +169,16 @@ int rmrf(char *path) {
} }
int openEditor(char *path, char *editor, int render, int endOfFile, int shouldDebug) { int openEditor(char *path, char *editor, int render, int shouldJumpToEndOfFile, int shouldDebug) {
// (TODO LATER) Bug app breaks if browser was not already launched before vivify // (TODO LATER) Bug app breaks if browser was not already launched before vivify
// (TODO LATER) find better name for endOfFile // (TODO LATER) for nvim and vim we should check if there is swap files or recovery files and handle that
//(TODO LATER) for nvim and vim we should check if there is swap files or recovery files and handle that
pid_t pid = fork(); // this forking allows the programs to return when nvim is closed pid_t pid = fork(); // this forking allows the programs to return when nvim is closed
error(pid<0, "program", "fork() failed."); error(pid<0, "program", "fork() failed.");
if (pid == 0) { if (pid == 0) {
// Child process: replace with editor of choice // Child process: replace with editor of choice
if (strcmp(editor, "neovim") == 0) { // opens with Neovim if (strcmp(editor, "neovim") == 0) { // opens with Neovim
//(TODO LATER) we should (with a config option) append a new line every time it opens
if (render) { // don't render using vivify if (render) { // don't render using vivify
if (endOfFile) { // goes to the end of the file on opening. (TODO LATER) find a better way to do this loops. Maybe an array of args and if () we add the arg to the array and we pass the whole array to execlp if (shouldJumpToEndOfFile) { // goes to the end of the file on opening.
// :$ goes to the end of the file. :Vivify runs vivify // :$ goes to the end of the file. :Vivify runs vivify
debug("Running nvim +:$ +:Vivify %s", path); debug("Running nvim +:$ +:Vivify %s", path);
execlp("nvim", "nvim", "+:$", "+:Vivify", path, NULL); execlp("nvim", "nvim", "+:$", "+:Vivify", path, NULL);
@@ -173,7 +189,7 @@ int openEditor(char *path, char *editor, int render, int endOfFile, int shouldDe
error(1, "program", "execlp() failed."); error(1, "program", "execlp() failed.");
} }
} else { // don't render using vivify } else { // don't render using vivify
if (endOfFile) { // go to end of the file on opening if (shouldJumpToEndOfFile) { // go to end of the file on opening
debug("Running nvim +:$ %s", path); debug("Running nvim +:$ %s", path);
execlp("nvim", "nvim", "+:$", path, NULL); execlp("nvim", "nvim", "+:$", path, NULL);
error(1, "program", "execlp() failed."); error(1, "program", "execlp() failed.");
@@ -184,9 +200,8 @@ int openEditor(char *path, char *editor, int render, int endOfFile, int shouldDe
} }
} }
} else if (strcmp(editor, "vim") == 0) { // opens with Vim // see comments for neovim for explanations } else if (strcmp(editor, "vim") == 0) { // opens with Vim // see comments for neovim for explanations
//(TODO LATER) we should (with a config option) append a new line every time it opens
if (render) { if (render) {
if (endOfFile) { if (shouldJumpToEndOfFile) {
debug("Running vim +:$ +:Vivify %s", path); debug("Running vim +:$ +:Vivify %s", path);
execlp("vim", "vim", "+:$", "+:Vivify", path, NULL); execlp("vim", "vim", "+:$", "+:Vivify", path, NULL);
error(1, "program", "execlp() failed."); error(1, "program", "execlp() failed.");
@@ -196,7 +211,7 @@ int openEditor(char *path, char *editor, int render, int endOfFile, int shouldDe
error(1, "program", "execlp() failed."); error(1, "program", "execlp() failed.");
} }
} else { } else {
if (endOfFile) { if (shouldJumpToEndOfFile) {
debug("Running vim +:$ %s", path); debug("Running vim +:$ %s", path);
execlp("vim", "vim", "+:$", path, NULL); execlp("vim", "vim", "+:$", path, NULL);
error(1, "program", "execlp() failed."); error(1, "program", "execlp() failed.");
+2 -2
View File
@@ -56,10 +56,10 @@ void sanitize(char *string);
int rmrf(char *path); int rmrf(char *path);
//from https://stackoverflow.com/a/5467788 //from https://stackoverflow.com/a/5467788
//deletes an entire directory. Use with parsimony and carefullness //deletes an entire directory. Use with parsimony and carefullness
int openEditor(char *path, char *editor, int render, int endOfFile, int debug); int openEditor(char *path, char *editor, int render, int shouldJumpToEndOfFile, int debug);
// Inputs are the path to the file, the editor to open and some rendering option // Inputs are the path to the file, the editor to open and some rendering option
// render: if we render the .md file with Vivify // render: if we render the .md file with Vivify
// endOfFile: if we put the cursor at the end of the file when opening // shouldJumpToEndOfFile: if we put the cursor at the end of the file when opening
// The program resumes when the editor is closed // The program resumes when the editor is closed
char *getFormatedTime(char *format, int shouldDebug); char *getFormatedTime(char *format, int shouldDebug);
// see https://pubs.opengroup.org/onlinepubs/7908799/xsh/strftime.html? for formats // see https://pubs.opengroup.org/onlinepubs/7908799/xsh/strftime.html? for formats