diff --git a/README.md b/README.md index 00ec41d..89e5666 100644 --- a/README.md +++ b/README.md @@ -47,5 +47,5 @@ Change `$/.config/notewrapper/config.json`. If it does not exist. On building, i - [x] Port to vim - [ ] Fixe all the small stuff marked //(TODO LATER) in the code - [ ] 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 diff --git a/src/main.c b/src/main.c index 7c4dd02..c489b38 100644 --- a/src/main.c +++ b/src/main.c @@ -164,7 +164,7 @@ int main(int argc, char *argv[]) { printf(" -r, --render Renders the note with Vivify.\n"); printf(" -R, --no-render Do not render.\n"); printf(" -v, --vault 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"); return 1; } 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 or --vault "); 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) { - 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 .\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; + } 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 @@ -219,7 +221,6 @@ int main(int argc, char *argv[]) { // adds "create a new vault" into the vaultsArray 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)"; diff --git a/src/utils.c b/src/utils.c index 7b60f9b..b7d4d4c 100644 --- a/src/utils.c +++ b/src/utils.c @@ -3,8 +3,6 @@ const char *supportedEditor[] = {"neovim", "vim"}; const int numEditors = 2; -//(TODO LATER) We should write a debug() function and an error(function) - int compareString(const void *a, const void *b) { const char *str1 = *(const char **)a; 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); return 0; } - 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, "a"); - error(file == NULL, "program", "could not open %s", path); - fprintf(file, "%s", string); - fclose(file); - debug("%s was appended to %s succesfully", string, path); + FILE *file = fopen(path, "r"); + char lastLine[1024] = {0}; + error(file == NULL, "program", "could not open %s", path); + 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 + + // 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) { @@ -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) 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 error(pid<0, "program", "fork() failed."); if (pid == 0) { // Child process: replace with editor of choice 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 (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 debug("Running nvim +:$ +:Vivify %s", path); 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."); } } 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); execlp("nvim", "nvim", "+:$", path, NULL); 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 - //(TODO LATER) we should (with a config option) append a new line every time it opens if (render) { - if (endOfFile) { + if (shouldJumpToEndOfFile) { debug("Running vim +:$ +:Vivify %s", path); execlp("vim", "vim", "+:$", "+:Vivify", path, NULL); 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."); } } else { - if (endOfFile) { + if (shouldJumpToEndOfFile) { debug("Running vim +:$ %s", path); execlp("vim", "vim", "+:$", path, NULL); error(1, "program", "execlp() failed."); diff --git a/src/utils.h b/src/utils.h index a4d1a2e..fd34356 100644 --- a/src/utils.h +++ b/src/utils.h @@ -56,10 +56,10 @@ void sanitize(char *string); int rmrf(char *path); //from https://stackoverflow.com/a/5467788 //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 // 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 char *getFormatedTime(char *format, int shouldDebug); // see https://pubs.opengroup.org/onlinepubs/7908799/xsh/strftime.html? for formats