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:
+18
-5
@@ -156,13 +156,17 @@ char *updateJournal(char *path, char *journal, char *timeFormat, int shouldDebug
|
||||
strncpy(path, temp, PATH_MAX);
|
||||
DIR *dividedJournalDirectory = opendir(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
|
||||
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);
|
||||
snprintf(createEntryMessage, PATH_MAX, "Create new entry for the journal %s", journal);
|
||||
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
|
||||
// for readdir()
|
||||
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");
|
||||
// free's some used memory
|
||||
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
|
||||
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);
|
||||
if (strcmp(selectedOption, createEntryMessage) == 0) { // create new entry
|
||||
char temp[PATH_MAX];
|
||||
@@ -208,6 +212,15 @@ char *updateJournal(char *path, char *journal, char *timeFormat, int shouldDebug
|
||||
} else {
|
||||
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
|
||||
// snprintf does not like to have the same variable as input and output so we use a buffer
|
||||
char temp[PATH_MAX];
|
||||
|
||||
Reference in New Issue
Block a user