mirror of
https://github.com/tomasriveral/NoteWrapper.git
synced 2026-08-12 02:18:38 +02:00
Feat: Button to select a random journal entry
This commit is contained in:
@@ -106,7 +106,7 @@ Change `~/.config/notewrapper/config.json`. If it does not exist. On building, i
|
|||||||
- [x] Port vivify.vim to nixpkgs
|
- [x] Port vivify.vim to nixpkgs
|
||||||
- [ ] Add a way to have vaults in different directories
|
- [ ] Add a way to have vaults in different directories
|
||||||
- [ ] some kind of FZF search for notes
|
- [ ] some kind of FZF search for notes
|
||||||
- [ ] A button to randomly select a note or an entry in a journal
|
- [x] A button to randomly select a note or an entry in a journal
|
||||||
- [x] Fix crash when the window is resized
|
- [x] Fix crash when the window is resized
|
||||||
- [x] Actually open vivify when opening nvim
|
- [x] Actually open vivify when opening nvim
|
||||||
- [x] Write the journaling code (separate files or one big journal files)
|
- [x] Write the journaling code (separate files or one big journal files)
|
||||||
@@ -121,5 +121,4 @@ Change `~/.config/notewrapper/config.json`. If it does not exist. On building, i
|
|||||||
- [ ] Fixe all the small stuff marked //(TODO LATER) in the code
|
- [ ] Fixe all the small stuff marked //(TODO LATER) in the code
|
||||||
- [x] 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
|
||||||
- [ ] With journals, a way to open random entry (not today's)
|
|
||||||
|
|
||||||
|
|||||||
+18
-5
@@ -156,13 +156,17 @@ char *updateJournal(char *path, char *journal, char *timeFormat, int shouldDebug
|
|||||||
strncpy(path, temp, PATH_MAX);
|
strncpy(path, temp, PATH_MAX);
|
||||||
DIR *dividedJournalDirectory = opendir(path);
|
DIR *dividedJournalDirectory = opendir(path);
|
||||||
error(dividedJournalDirectory==NULL, "program", "Could not open directory %s", path);
|
error(dividedJournalDirectory==NULL, "program", "Could not open directory %s", path);
|
||||||
char **entryArray = NULL; // will contain all the entries of the divided journal
|
/*char **entryArray = NULL; // will contain all the entries of the divided journal
|
||||||
// This time it will be different. We must add "invert" the extraOptions to the options so that the Create new entry in journal is on top
|
// This time it will be different. We must add "invert" the extraOptions to the options so that the Create new entry in journal is on top
|
||||||
entryArray = realloc(entryArray, sizeof(char*));
|
entryArray = realloc(entryArray, sizeof(char*));*/
|
||||||
|
// simpler to just malloc 2 and later realloc
|
||||||
|
const int extraOptions = 2;
|
||||||
|
char **entryArray = malloc(extraOptions*sizeof(char*));
|
||||||
char *createEntryMessage = malloc(PATH_MAX);
|
char *createEntryMessage = malloc(PATH_MAX);
|
||||||
snprintf(createEntryMessage, PATH_MAX, "Create new entry for the journal %s", journal);
|
snprintf(createEntryMessage, PATH_MAX, "Create new entry for the journal %s", journal);
|
||||||
entryArray[0] = createEntryMessage;
|
entryArray[0] = createEntryMessage;
|
||||||
size_t entryCount = 1; // we need to count how many dirs there is to always readjust how many memory we alloc
|
entryArray[1] = "Open random entry";
|
||||||
|
size_t entryCount = extraOptions; // 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()
|
||||||
debug("┌------------------------------\n Detected files and dirs from %s:", path);
|
debug("┌------------------------------\n Detected files and dirs from %s:", path);
|
||||||
@@ -185,10 +189,10 @@ char *updateJournal(char *path, char *journal, char *timeFormat, int shouldDebug
|
|||||||
altDebug("└------------------------------\n");
|
altDebug("└------------------------------\n");
|
||||||
// free's some used memory
|
// free's some used memory
|
||||||
closedir(dividedJournalDirectory);
|
closedir(dividedJournalDirectory);
|
||||||
qsort(entryArray + 1, entryCount - 1, sizeof(const char *), reverseCompareString); // sorts the journals entries alphabetically. // the + 1 - 1 is to not sort "Create new entry for the journal" which is the first element
|
qsort(entryArray + extraOptions, entryCount - extraOptions, sizeof(const char *), reverseCompareString); // sorts the journals entries alphabetically. // the + extraOptions - extraOptions is to not sort "Create new entry for the journal" or Open random entry which is the first element
|
||||||
|
|
||||||
// we must now select to create new entry or to enter in old one
|
// we must now select to create new entry or to enter in old one
|
||||||
char *selectedOption = ncursesSelect(entryArray, "Create new entry or acces old entry (Use arrows or WASD, Enter to select):", 1, entryCount -1, " ", " ", "", shouldDebug); // the "Create new entry for the journal %s" will be the only options. All other will be extraOptions. This is made so that "Create [...] %s" will always be on top
|
char *selectedOption = ncursesSelect(entryArray, "Create new entry or acces old entry (Use arrows or WASD, Enter to select):", extraOptions, entryCount - extraOptions, " ", " ", "", shouldDebug); // the "Create new entry for the journal %s" will be the only options. All other will be extraOptions. This is made so that "Create [...] %s" will always be on top
|
||||||
debug("Selected option from journal entry selection: %s", selectedOption);
|
debug("Selected option from journal entry selection: %s", selectedOption);
|
||||||
if (strcmp(selectedOption, createEntryMessage) == 0) { // create new entry
|
if (strcmp(selectedOption, createEntryMessage) == 0) { // create new entry
|
||||||
char temp[PATH_MAX];
|
char temp[PATH_MAX];
|
||||||
@@ -208,6 +212,15 @@ char *updateJournal(char *path, char *journal, char *timeFormat, int shouldDebug
|
|||||||
} else {
|
} else {
|
||||||
debug("Today's entry (%s) already exist. We won't create a new one.", dateWithExtension);
|
debug("Today's entry (%s) already exist. We won't create a new one.", dateWithExtension);
|
||||||
}
|
}
|
||||||
|
} else if (strcmp(selectedOption, entryArray[1]) == 0) { // if we choosed to open a random entry
|
||||||
|
// setting up the seed for rand()
|
||||||
|
srand(time(NULL)); // not totaly random, just pseudorandom
|
||||||
|
int randomEntry = extraOptions + (rand() % (entryCount - extraOptions)); // we get a random number between extraOptions and entryCount (so all journal entries and not extraOptions)
|
||||||
|
char temp[PATH_MAX];
|
||||||
|
snprintf(temp, PATH_MAX, "%s/%s", path, entryArray[randomEntry]); // reconstruct the path
|
||||||
|
strncpy(path, temp, PATH_MAX);
|
||||||
|
debug("Selected random entry %s. It's path is %s.", entryArray[randomEntry], path);
|
||||||
|
//
|
||||||
} else { // we just recreate the path to the selected entry
|
} else { // we just recreate the path to the selected entry
|
||||||
// snprintf does not like to have the same variable as input and output so we use a buffer
|
// snprintf does not like to have the same variable as input and output so we use a buffer
|
||||||
char temp[PATH_MAX];
|
char temp[PATH_MAX];
|
||||||
|
|||||||
Reference in New Issue
Block a user