Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
BasedOnStyle: LLVM
IndentWidth: 4
ColumnLimit: 100
ColumnLimit: 210
195 changes: 70 additions & 125 deletions src/main.c

Large diffs are not rendered by default.

136 changes: 48 additions & 88 deletions src/notes.c

Large diffs are not rendered by default.

16 changes: 5 additions & 11 deletions src/notes.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,28 @@
#include "ui.h"
#include "utils.h"
// find which directory contains targetVault
char *getDirectoryFromVault(char *targetVault, char **vaultsArray, int vaultTotalNumber,
int *vaultNumberPerDirectory, char **directoryArray,
int directoryNumber, int shouldDebug);
char *getDirectoryFromVault(char *targetVault, char **vaultsArray, int vaultTotalNumber, int *vaultNumberPerDirectory, char **directoryArray, int directoryNumber, int shouldDebug);
// gets the journals from the vault.
// to distinguish journals from note we use regex.
// Note: we must handle them separatly as journals can either be a single file (which will treat
// specially) or a directory.
char **getJournalsFromVault(char *pathToVault, char *vault, char *journalRegex, int *count,
int shouldDebug);
char **getJournalsFromVault(char *pathToVault, char *vault, char *journalRegex, int *count, int shouldDebug);
// this function is inputed a path to a vault (which was selected before) and outpus all the
// suitable notes (so not the hidden ones). journalRegex is the regex code for the journals. If a
// note matches this code, it is treated as a journal and it is not outputed from this function.
char **getNotesFromVault(char *pathToVault, char *vault, char *journalRegex, int *count,
int shouldDebug);
char **getNotesFromVault(char *pathToVault, char *vault, char *journalRegex, int *count, int shouldDebug);
// function gets as input an array of directoryNumber strings. Which are the directories the
// function will search for vaults. it returns an array of vaults. the vaults are organized in order
// per directory. vaultsPerDirectoryNumber and count should be initiallized before calling the
// function and the address be inputed. count is the total number of vaults.
// vaultsPerDirectoryNumber gives how many vaults each directory has.
char **getVaultsFromDirectories(char **directoryStringArray, int directoryNumber,
int *vaultsPerDirectoryNumber, int *count, int shouldDebug);
char **getVaultsFromDirectories(char **directoryStringArray, int directoryNumber, int *vaultsPerDirectoryNumber, int *count, int shouldDebug);
// path is the path to the file.
// journal is the name of the file.
// journalWasUpdated will be set to 1 if a new entry was created
// handles both type of journal (divided and unified).
// creates new entry with date.
// for divided select if we want to acces to a new entry or a old one.
// returns the path to the file that needs to be opened.
char *updateJournal(char *path, char *journal, char *timeFormat, int *journalWasUpdated,
int shouldDebug);
char *updateJournal(char *path, char *journal, char *timeFormat, int *journalWasUpdated, int shouldDebug);
#endif
58 changes: 22 additions & 36 deletions src/ui.c
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
#include "ui.h"
#include "utils.h"

void createNewVault(char **directoriesArray, int directoryCount, char **vaultsArray, int vaultCount,
int bypass, char *bypassvalue, int shouldDebug) {
void createNewVault(char **directoriesArray, int directoryCount, char **vaultsArray, int vaultCount, int bypass, char *bypassvalue, int shouldDebug) {
int duplicateWarning = 0; // set to 1 later if the vault you tried to create already existed
int emptyWarning = 0; // set to 1 later if inpted empty name
input_screen:
// choose in which directory the vault will be created
char *dirToVault = ncursesSelect(
directoriesArray,
"Select a directory, in which the vault will remain (Use arrows or WASD, Enter to select)",
directoryCount, 0, "", "", " ", shouldDebug);
char *dirToVault = ncursesSelect(directoriesArray, "Select a directory, in which the vault will remain (Use arrows or WASD, Enter to select)", directoryCount, 0, "", "", " ", shouldDebug);
char *vaultName = malloc(PATH_MAX);
if (!bypass) { // if won't bypass (if -v or --vault weren't set)
echo();
Expand Down Expand Up @@ -69,8 +65,7 @@ void createNewVault(char **directoriesArray, int directoryCount, char **vaultsAr
free(vaultName);
}

char *createNewNote(char dirToVault[PATH_MAX], char *vaultFromDir, int bypass, char *bypassvalue,
char *journalRegex, int shouldDebug) {
char *createNewNote(char dirToVault[PATH_MAX], char *vaultFromDir, int bypass, char *bypassvalue, char *journalRegex, int shouldDebug) {
// input from user for the name
char *fileName = malloc(BUFFER_SIZE);
if (!bypass) { // if we don't bypass. (if -n or --note weren't set.)
Expand All @@ -96,10 +91,9 @@ char *createNewNote(char dirToVault[PATH_MAX], char *vaultFromDir, int bypass, c
strncpy(fileName, bypassvalue, BUFFER_SIZE);
fileName[BUFFER_SIZE - 1] = '\0';
}
error(
strcmp(fileName, "") == 0, "user",
"fileName is empty"); // replace this with a warning and add a warning if duplicate file and
// handle case where multiple warnings (if such case is possible)
error(strcmp(fileName, "") == 0, "user",
"fileName is empty"); // replace this with a warning and add a warning if duplicate file and
// handle case where multiple warnings (if such case is possible)
// check/sanitize the input
debug("Inputed fileName=%s", fileName);
sanitize(fileName);
Expand Down Expand Up @@ -138,11 +132,8 @@ char *createNewNote(char dirToVault[PATH_MAX], char *vaultFromDir, int bypass, c
char *fileFullPath = malloc(PATH_MAX);
snprintf(fileFullPath, PATH_MAX, "%s/%s/%s", dirToVault, vaultFromDir, fileName);
int len = strlen(fileFullPath);
if (fileFullPath[len - 3] != '.' || fileFullPath[len - 2] != 'm' ||
fileFullPath[len - 1] != 'd') { // there might be a cleaner way to do this
error(len > PATH_MAX - 3, "user",
"%s is too big (greater than PATH_MAX-3) and we can't append .md",
fileFullPath);
if (fileFullPath[len - 3] != '.' || fileFullPath[len - 2] != 'm' || fileFullPath[len - 1] != 'd') { // there might be a cleaner way to do this
error(len > PATH_MAX - 3, "user", "%s is too big (greater than PATH_MAX-3) and we can't append .md", fileFullPath);
strncat(fileFullPath, ".md", PATH_MAX);
// we checked before if fileName didn't already exist.
// we must redo it as we add a .mode
Expand All @@ -161,10 +152,9 @@ char *createNewNote(char dirToVault[PATH_MAX], char *vaultFromDir, int bypass, c
debug("%s does not match with %s treating it as a note", fileName, journalRegex);
// if there is no .md add an .md
int len = strlen(fileName);
if (fileName[len - 3] != '.' || fileName[len - 2] != 'm' ||
fileName[len - 1] != 'd') { // there might be a cleaner way to do this
strcat(fileName, ".md"); // this should not cause an overflow issue as we get at most
// 252 chars (+'.'+'m'+'d'+'\0' makes it to 256) with wgetnstr
if (fileName[len - 3] != '.' || fileName[len - 2] != 'm' || fileName[len - 1] != 'd') { // there might be a cleaner way to do this
strcat(fileName, ".md"); // this should not cause an overflow issue as we get at most
// 252 chars (+'.'+'m'+'d'+'\0' makes it to 256) with wgetnstr
}
char *fileFullPath = malloc(PATH_MAX); // this dinamically allocated because we use it in
// the main function to call openEditor
Expand Down Expand Up @@ -194,8 +184,7 @@ char *fzfSelect(char *pathToFiles, char *selectText, int shouldDebug) {
* Build index once into temp file
* format: file:line:content
*/
snprintf(command, sizeof(command), "rg --line-number --no-heading --color=never . \"%s\" > %s",
pathToFiles, indexFile);
snprintf(command, sizeof(command), "rg --line-number --no-heading --color=never . \"%s\" > %s", pathToFiles, indexFile);

debug("INDEX CMD: %s", command);

Expand Down Expand Up @@ -243,14 +232,13 @@ char *fzfSelect(char *pathToFiles, char *selectText, int shouldDebug) {
--bind 'change:show-preview'
→ preview only appears after first selection change (not at startup)
*/
snprintf(
command, sizeof(command),
"cat %s | fzf --delimiter ':' --prompt='%s' "
"--preview 'file={1}; line={2}; nl -ba \"$file\" | sed -n \"$((line-5)),$((line+5))p\" | "
"sed \"$((line-$(($((line-5))))+1))s/^/\\x1b[31m-> \\x1b[0m/\"' "
"--preview-window=right:60%%:hidden "
"--bind 'change:show-preview'",
indexFile, selectText ? selectText : "> ");
snprintf(command, sizeof(command),
"cat %s | fzf --delimiter ':' --prompt='%s' "
"--preview 'file={1}; line={2}; nl -ba \"$file\" | sed -n \"$((line-5)),$((line+5))p\" | "
"sed \"$((line-$(($((line-5))))+1))s/^/\\x1b[31m-> \\x1b[0m/\"' "
"--preview-window=right:60%%:hidden "
"--bind 'change:show-preview'",
indexFile, selectText ? selectText : "> ");
debug("FZF CMD: %s", command);

FILE *fzfPipe = popen(command, "r");
Expand Down Expand Up @@ -284,11 +272,9 @@ char *fzfSelect(char *pathToFiles, char *selectText, int shouldDebug) {
return result;
}

char *ncursesSelect(
char **options, char *optionsText, int optionsNumber, int extraOptionsNumber, char *bottomText,
char *middleText, char *topText,
int shouldDebug) { // TODO we should see how it handles larges strings (like large directories)
int highlight = 0; // curently highlighted option
char *ncursesSelect(char **options, char *optionsText, int optionsNumber, int extraOptionsNumber, char *bottomText, char *middleText, char *topText,
int shouldDebug) { // TODO we should see how it handles larges strings (like large directories)
int highlight = 0; // curently highlighted option
int key;

cbreak(); // disable line buffering
Expand Down
9 changes: 3 additions & 6 deletions src/ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,14 @@ also can create a journal if the name matches with journalRegex.
returns the path to the note.
If you want to bypass the input TUI set bypass to 1 and bypassvalue to the note name
*/
char *createNewNote(char dirToVault[PATH_MAX], char *vaultFromDir, int bypass, char *bypassvalue,
char *journalRegex, int debug);
char *createNewNote(char dirToVault[PATH_MAX], char *vaultFromDir, int bypass, char *bypassvalue, char *journalRegex, int debug);
/*
Chooses in which directory it want to create the vault.
Uses ncurses to get an input from the user.
Creates a new vault with this input.
If vault already exists, prints a warning.
returns nothing. */
void createNewVault(char **directoriesArray, int directoryCount, char **vaultsArray, int vaultCount,
int bypass, char *bypassvalue, int debug);
void createNewVault(char **directoriesArray, int directoryCount, char **vaultsArray, int vaultCount, int bypass, char *bypassvalue, int debug);
// uses fzf and ripgrep to search inside the files to choose between all the options.
char *fzfSelect(char *pathToFiles, char *selectText, int shouldDebug);
/*this function is used multiple times let the user select one options from many with ncurses in a
Expand All @@ -42,6 +40,5 @@ extraOptions are options that are special and have a special color (for ex: "Del
options text and the start of options. middleText (usally \n) is printed between options and
extraOptions. bottomText is printed bellow. topText and middle must be exactly one line. If you want
empty lines use " " and not "". returns the selected option. */
char *ncursesSelect(char **options, char *optionsText, int optionsNumber, int extraOptionsNumber,
char *bottomText, char *middleText, char *topText, int debug);
char *ncursesSelect(char **options, char *optionsText, int optionsNumber, int extraOptionsNumber, char *bottomText, char *middleText, char *topText, int debug);
#endif
Loading
Loading