Merge (#45) from tomasriveral/vault-fzf-search

feat: fzf search inside vaults
This commit is contained in:
2026-06-02 13:51:45 +02:00
committed by GitHub
2 changed files with 28 additions and 56 deletions
+13 -5
View File
@@ -644,12 +644,13 @@ backup_config_end:
altDebug("└------------------------------\n"); altDebug("└------------------------------\n");
} }
// adds options // adds options
int extraNotesOptions = 4; int extraNotesOptions = 5;
filesArray = realloc(filesArray, (filesCount + extraNotesOptions) * sizeof(char *)); // resize filesArray to fit the extra options filesArray = realloc(filesArray, (filesCount + extraNotesOptions) * sizeof(char *)); // resize filesArray to fit the extra options
filesArray[filesCount] = "Create new note"; filesArray[filesCount] = "Create new note";
filesArray[filesCount + 1] = "Back to vault selection"; filesArray[filesCount + 1] = "Search inside the vault";
filesArray[filesCount + 2] = "Delete vault"; filesArray[filesCount + 2] = "Back to vault selection";
filesArray[filesCount + 3] = "Quit (Ctrl+C)"; filesArray[filesCount + 3] = "Delete vault";
filesArray[filesCount + 4] = "Quit (Ctrl+C)";
char *noteSelected; char *noteSelected;
// if we set to bypass the note selector // if we set to bypass the note selector
if (bypassSelectionNote) { if (bypassSelectionNote) {
@@ -680,7 +681,7 @@ backup_config_end:
free(filesArray); free(filesArray);
debug("Selected note: %s", noteSelected); debug("Selected note: %s", noteSelected);
if (strcmp(noteSelected, "Create new note") != 0 && strcmp(noteSelected, "Back to vault selection") != 0 && strcmp(noteSelected, "Delete vault") != 0 && if (strcmp(noteSelected, "Create new note") != 0 && strcmp(noteSelected, "Back to vault selection") != 0 && strcmp(noteSelected, "Delete vault") != 0 &&
strcmp(noteSelected, "Quit (Ctrl+C)") != 0) { strcmp(noteSelected, "Quit (Ctrl+C)") != 0 && strcmp(noteSelected, "Search inside the vault") != 0) {
open_note: open_note:
bypassSelectionNote = 0; // we must reset bypassSelectionNote to avoid getting into an infinite bypassSelectionNote = 0; // we must reset bypassSelectionNote to avoid getting into an infinite
// loop of bypassing the note selection // loop of bypassing the note selection
@@ -717,6 +718,13 @@ backup_config_end:
noteSelected = createNewNote(notesDirectoryString, vaultSelected, bypassSelectionNote, bypassSelectionNoteValue, journalRegex, shouldDebug); noteSelected = createNewNote(notesDirectoryString, vaultSelected, bypassSelectionNote, bypassSelectionNoteValue, journalRegex, shouldDebug);
// we can just go back to open_note // we can just go back to open_note
goto open_note; goto open_note;
} else if (strcmp(noteSelected, "Search inside the vault") == 0) {
char *vaultPath = malloc(PATH_MAX);
snprintf(vaultPath, PATH_MAX, "%s/%s/", notesDirectoryString, vaultSelected);
char *result = fzfSelect(vaultPath, "Input text to be searched", shouldDebug);
bypassSelectionNote = 1;
bypassSelectionNoteValue = result;
free(vaultPath);
} else if (strcmp(noteSelected, "Back to vault selection") == 0) { } else if (strcmp(noteSelected, "Back to vault selection") == 0) {
shouldChangeVault = 1; shouldChangeVault = 1;
} else if (strcmp(noteSelected, "Delete vault") == 0) { } else if (strcmp(noteSelected, "Delete vault") == 0) {
+15 -51
View File
@@ -179,13 +179,7 @@ char *fzfSelect(char *pathToFiles, char *selectText, int shouldDebug) {
error(fd == -1, "program", "mkstemp failed"); error(fd == -1, "program", "mkstemp failed");
close(fd); close(fd);
/*
* STEP 1:
* 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); debug("INDEX CMD: %s", command);
if (system(command) != 0) { if (system(command) != 0) {
@@ -193,45 +187,6 @@ char *fzfSelect(char *pathToFiles, char *selectText, int shouldDebug) {
error(1, "program", "rg indexing failed"); error(1, "program", "rg indexing failed");
} }
/*
* STEP 2:
* Use fzf on the index file (NO rg anymore)
*/
/*
EXPLANATION:
cat %s
→ feeds prebuilt index file (format: file:line:content) into fzf
fzf --delimiter ':'
→ splits each line into fields:
{1} = file path
{2} = line number
{3} = content
--prompt
→ UI prompt text shown in fzf
--preview
→ runs a shell command for selected item:
file={1} → current file
line={2} → line number of match
nl -ba "$file"
→ prints file with line numbers
sed -n "$((line-5)),$((line+5))p"
→ extracts a window of 5 lines above and below match
sed "... -> ..."
→ highlights the matched line (middle of window) with red arrow
--preview-window=right:60%:hidden
→ preview appears on right, 60% width, initially hidden
--bind 'change:show-preview'
→ preview only appears after first selection change (not at startup)
*/
snprintf(command, sizeof(command), snprintf(command, sizeof(command),
"cat %s | fzf --delimiter ':' --prompt='%s' " "cat %s | fzf --delimiter ':' --prompt='%s' "
"--preview 'file={1}; line={2}; nl -ba \"$file\" | sed -n \"$((line-5)),$((line+5))p\" | " "--preview 'file={1}; line={2}; nl -ba \"$file\" | sed -n \"$((line-5)),$((line+5))p\" | "
@@ -254,16 +209,25 @@ char *fzfSelect(char *pathToFiles, char *selectText, int shouldDebug) {
buffer[strcspn(buffer, "\n")] = '\0'; buffer[strcspn(buffer, "\n")] = '\0';
result = strdup(buffer); result = strdup(buffer);
} }
debug("Buffer: %s", buffer);
debug("result: %s", result);
// cut at first ':' // cut at first ':'
result = strtok(result, ":"); result = strtok(result, ":");
// cut at / (because we need the full paths before and we only need to return the journal entry) // we need to cut out the whole pathToFiles
char *token = strtok(result, "/"); int pathToFilesLen = strlen(pathToFiles);
result = NULL; int cutOut = 0;
while (token != NULL) { for (int i = 0; i < pathToFilesLen; i++) {
result = token; if (pathToFiles[i] == result[i]) {
token = strtok(NULL, "/"); // Passing NULL means “dont start a new string, resume cutOut = i;
}
} }
result += cutOut + 1; // the + 1 cuts the first /
// in case we select a journal, we won't open the entry just yet. We only need to return the journal name (everything before [now the first] /
result = strtok(result, "/");
debug("Cleaned note: %s", result);
// clean up // clean up
pclose(fzfPipe); pclose(fzfPipe);