mirror of
https://github.com/tomasriveral/NoteWrapper.git
synced 2026-08-12 02:18:38 +02:00
Fixed crash when resizing the window
This commit is contained in:
@@ -2,3 +2,5 @@ a.out
|
|||||||
nvimnotes
|
nvimnotes
|
||||||
notewrapper
|
notewrapper
|
||||||
nw
|
nw
|
||||||
|
error.log
|
||||||
|
output.log
|
||||||
|
|||||||
@@ -106,6 +106,8 @@ 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] 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)
|
||||||
- [x] Adapt createNewNote with journals
|
- [x] Adapt createNewNote with journals
|
||||||
|
|||||||
@@ -267,6 +267,8 @@ int main(int argc, char *argv[]) {
|
|||||||
if (doesBackup) { // (TODO LATER) when implementing multiple directories for vault we should verifiy this works.
|
if (doesBackup) { // (TODO LATER) when implementing multiple directories for vault we should verifiy this works.
|
||||||
handleBackups(notesDirectoryString, pathToBackup, homedir, interval, shouldDebug);
|
handleBackups(notesDirectoryString, pathToBackup, homedir, interval, shouldDebug);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
initscr(); //initialize ncurses
|
||||||
|
|
||||||
int shouldExit = 0;
|
int shouldExit = 0;
|
||||||
while(!shouldExit) {
|
while(!shouldExit) {
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
#include "ui.h"
|
#include "ui.h"
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
void createNewVault(char *dirToVault, int bypass, char *bypassvalue, int shouldDebug) {
|
void createNewVault(char *dirToVault, int bypass, char *bypassvalue, int shouldDebug) {
|
||||||
// (TODO LATER) warn if it matches the regex for the journal
|
// (TODO LATER) warn if it matches the regex for the journal
|
||||||
@@ -8,7 +7,6 @@ void createNewVault(char *dirToVault, int bypass, char *bypassvalue, int shouldD
|
|||||||
input_screen:
|
input_screen:
|
||||||
char *vaultName = malloc(PATH_MAX);
|
char *vaultName = malloc(PATH_MAX);
|
||||||
if (!bypass) { // if won't bypass (if -v or --vault weren't set)
|
if (!bypass) { // if won't bypass (if -v or --vault weren't set)
|
||||||
initscr();
|
|
||||||
echo();
|
echo();
|
||||||
keypad(stdscr, FALSE);
|
keypad(stdscr, FALSE);
|
||||||
// color code from https://stackoverflow.com/a/73396575
|
// color code from https://stackoverflow.com/a/73396575
|
||||||
@@ -30,6 +28,9 @@ input_screen:
|
|||||||
wgetnstr(stdscr, vaultName, sizeof(vaultName)-1);
|
wgetnstr(stdscr, vaultName, sizeof(vaultName)-1);
|
||||||
refresh();
|
refresh();
|
||||||
endwin();
|
endwin();
|
||||||
|
reset_shell_mode();
|
||||||
|
fflush(stdout);
|
||||||
|
fflush(stderr);
|
||||||
} else {
|
} else {
|
||||||
strncpy(vaultName, bypassvalue, PATH_MAX -2); // -2 (and later -1) because indexing
|
strncpy(vaultName, bypassvalue, PATH_MAX -2); // -2 (and later -1) because indexing
|
||||||
vaultName[PATH_MAX-1] = '\0'; // (TODO LATER) if bypassvalue << PATH_MAX, we loose a lot of space. maybe check strlen(bypassvalue) and append there a \0
|
vaultName[PATH_MAX-1] = '\0'; // (TODO LATER) if bypassvalue << PATH_MAX, we loose a lot of space. maybe check strlen(bypassvalue) and append there a \0
|
||||||
@@ -59,7 +60,6 @@ char *createNewNote(char dirToVault[PATH_MAX], char *vaultFromDir, int bypass, c
|
|||||||
// input from user for the name
|
// input from user for the name
|
||||||
char *fileName = malloc(BUFFER_SIZE);
|
char *fileName = malloc(BUFFER_SIZE);
|
||||||
if (!bypass) { // if we don't bypass. (if -n or --note weren't set.)
|
if (!bypass) { // if we don't bypass. (if -n or --note weren't set.)
|
||||||
initscr();
|
|
||||||
echo();
|
echo();
|
||||||
keypad(stdscr, FALSE);
|
keypad(stdscr, FALSE);
|
||||||
clear();
|
clear();
|
||||||
@@ -70,6 +70,9 @@ char *createNewNote(char dirToVault[PATH_MAX], char *vaultFromDir, int bypass, c
|
|||||||
wgetnstr(stdscr, fileName, BUFFER_SIZE-4); //limits the buffer to prevent overflow (-4 to account indexing and from ".md" in case we need to add it later)
|
wgetnstr(stdscr, fileName, BUFFER_SIZE-4); //limits the buffer to prevent overflow (-4 to account indexing and from ".md" in case we need to add it later)
|
||||||
refresh();
|
refresh();
|
||||||
endwin();
|
endwin();
|
||||||
|
reset_shell_mode();
|
||||||
|
fflush(stdout);
|
||||||
|
fflush(stderr);
|
||||||
} else { // bypasses user input if we bypass is set to 1
|
} else { // bypasses user input if we bypass is set to 1
|
||||||
strncpy(fileName, bypassvalue, BUFFER_SIZE-1); // (TODO LATER) Maybe add a warning if string is too big. It gets truncated
|
strncpy(fileName, bypassvalue, BUFFER_SIZE-1); // (TODO LATER) Maybe add a warning if string is too big. It gets truncated
|
||||||
fileName[BUFFER_SIZE-1] = '\0';
|
fileName[BUFFER_SIZE-1] = '\0';
|
||||||
@@ -146,7 +149,6 @@ char* ncursesSelect(char **options, char *optionsText, size_t optionsNumber, siz
|
|||||||
int highlight = 0; //curently highlighted option
|
int highlight = 0; //curently highlighted option
|
||||||
int key;
|
int key;
|
||||||
|
|
||||||
initscr(); //initialize ncurses
|
|
||||||
cbreak(); // disable line buffering
|
cbreak(); // disable line buffering
|
||||||
noecho(); // don't echo key presses
|
noecho(); // don't echo key presses
|
||||||
keypad(stdscr, TRUE); // enable arrow keys
|
keypad(stdscr, TRUE); // enable arrow keys
|
||||||
|
|||||||
+96
-76
@@ -9,12 +9,22 @@ int compareString(const void *a, const void *b) {
|
|||||||
return strcmp(str1, str2); // strcmp returns <0, 0, >0
|
return strcmp(str1, str2); // strcmp returns <0, 0, >0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void getCurrentTime(int *hour, int *minute, int *second) {
|
||||||
|
time_t now = time(NULL); // Get current time in seconds since epoch
|
||||||
|
struct tm *local = localtime(&now); // Convert to local time structure
|
||||||
|
|
||||||
|
*hour = local->tm_hour; // Extract hour
|
||||||
|
*minute = local->tm_min; // Extract minutes
|
||||||
|
*second = local->tm_sec; // Extract seconds
|
||||||
|
}
|
||||||
|
|
||||||
void _debug(const int d, const char *file, const int line, const char *function, const char *message, ...) { // use for formatted debug
|
void _debug(const int d, const char *file, const int line, const char *function, const char *message, ...) { // use for formatted debug
|
||||||
if (d) {
|
if (d) {
|
||||||
va_list args; //variadic function stuff
|
va_list args; //variadic function stuff
|
||||||
va_start(args, message);
|
va_start(args, message);
|
||||||
|
int h, m, s;
|
||||||
fprintf(stderr, "\e[0;32m[DEBUG] From file %s line %d function %s:\e[0m\n", file, line, function);
|
getCurrentTime(&h, &m, &s);
|
||||||
|
fprintf(stderr, "\e[0;32m[DEBUG -- %d:%d:%d] From file %s line %d function %s:\e[0m\n", h, m, s, file, line, function);
|
||||||
vfprintf(stderr, message, args);
|
vfprintf(stderr, message, args);
|
||||||
printf("\e[0m\n");
|
printf("\e[0m\n");
|
||||||
va_end(args);
|
va_end(args);
|
||||||
@@ -31,7 +41,9 @@ void _altDebug(const int d, const char *message, ...) { // use for less formal d
|
|||||||
|
|
||||||
void _error(const int shouldDebug, const int condition, const char *type, const char *file, const int line, const char *function, const char *message, ...) { // used for formatted errors
|
void _error(const int shouldDebug, const int condition, const char *type, const char *file, const int line, const char *function, const char *message, ...) { // used for formatted errors
|
||||||
if (condition) {
|
if (condition) {
|
||||||
fprintf(stderr, "\e[0;31m[%s ERROR] From file %s line %d function %s:\n", type, file, line, function);
|
int h, m, s;
|
||||||
|
getCurrentTime(&h, &m, &s);
|
||||||
|
fprintf(stderr, "\e[0;31m[%s ERROR -- %d:%d:%d] From file %s line %d function %s:\n", type, h, m, s, file, line, function);
|
||||||
if (errno != 0) {
|
if (errno != 0) {
|
||||||
fprintf(stderr, " (System-level error message: %s)\n", strerror(errno));
|
fprintf(stderr, " (System-level error message: %s)\n", strerror(errno));
|
||||||
} else {
|
} else {
|
||||||
@@ -245,88 +257,96 @@ int rmrf(char *path) {
|
|||||||
|
|
||||||
int openEditor(char *path, char *editor, int render, int shouldJumpToEndOfFile, int shouldDebug) {
|
int openEditor(char *path, char *editor, int render, int shouldJumpToEndOfFile, int shouldDebug) {
|
||||||
|
|
||||||
pid_t editor_pid = fork();
|
// this ensures that ncurses won't affect the editor behaviour
|
||||||
error(editor_pid < 0, "program", "fork() failed.");
|
pid_t editor_pid = fork();
|
||||||
|
error(editor_pid < 0, "program", "fork() failed.");
|
||||||
|
|
||||||
if (editor_pid == 0) {
|
if (editor_pid == 0) {
|
||||||
// =========================
|
// =========================
|
||||||
// CHILD: launch editor
|
// CHILD: launch editor
|
||||||
// =========================
|
// =========================
|
||||||
|
|
||||||
// ---- NEOVIM / VIM ----
|
// ---- NEOVIM / VIM ----
|
||||||
if (strcmp(editor, "neovim") == 0 || strcmp(editor, "vim") == 0) {
|
if (strcmp(editor, "neovim") == 0 || strcmp(editor, "vim") == 0) {
|
||||||
const char *bin = (strcmp(editor, "neovim") == 0) ? "nvim" : "vim";
|
const char *bin = (strcmp(editor, "neovim") == 0) ? "nvim" : "vim";
|
||||||
|
|
||||||
if (render) {
|
if (render) {
|
||||||
if (shouldJumpToEndOfFile) {
|
|
||||||
debug("Running %s +:$ +:Vivify %s", bin, path);
|
|
||||||
execlp(bin, bin, "+:$", "+:Vivify", path, NULL);
|
|
||||||
} else {
|
|
||||||
debug("Running %s +:Vivify %s", bin, path);
|
|
||||||
execlp(bin, bin, "+:Vivify", path, NULL);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (shouldJumpToEndOfFile) {
|
|
||||||
debug("Running %s +:$ %s", bin, path);
|
|
||||||
execlp(bin, bin, "+:$", path, NULL);
|
|
||||||
} else {
|
|
||||||
debug("Running %s %s", bin, path);
|
|
||||||
execlp(bin, bin, path, NULL);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
error(1, "program", "execlp() failed.");
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---- NANO ----
|
|
||||||
else if (strcmp(editor, "nano") == 0) {
|
|
||||||
|
|
||||||
// If render enabled → spawn viv in parallel
|
|
||||||
if (render) {
|
|
||||||
pid_t viv_pid = fork();
|
|
||||||
error(viv_pid < 0, "program", "fork() failed.");
|
|
||||||
|
|
||||||
if (viv_pid == 0) {
|
|
||||||
// GRANDCHILD → viv
|
|
||||||
char viv_path[PATH_MAX];
|
|
||||||
strncpy(viv_path, path, PATH_MAX - 1);
|
|
||||||
viv_path[PATH_MAX - 1] = '\0';
|
|
||||||
|
|
||||||
if (shouldJumpToEndOfFile) {
|
|
||||||
strncat(viv_path, ":99999",
|
|
||||||
PATH_MAX - strlen(viv_path) - 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
debug("Running viv %s", viv_path);
|
|
||||||
execlp("viv", "viv", viv_path, NULL);
|
|
||||||
error(1, "program", "execlp() failed.");
|
|
||||||
}
|
|
||||||
// IMPORTANT: do NOT wait for viv
|
|
||||||
}
|
|
||||||
|
|
||||||
// Now run nano (this replaces the child process)
|
|
||||||
if (shouldJumpToEndOfFile) {
|
if (shouldJumpToEndOfFile) {
|
||||||
debug("Running nano + %s", path);
|
debug("Running %s +:$ +:Vivify %s", bin, path);
|
||||||
execlp("nano", "nano", "+", path, NULL);
|
execlp(bin, bin, "+:$", "+:Vivify", path, NULL);
|
||||||
} else {
|
} else {
|
||||||
debug("Running nano %s", path);
|
debug("Running %s +:Vivify %s", bin, path);
|
||||||
execlp("nano", "nano", path, NULL);
|
execlp(bin, bin, "+:Vivify", path, NULL);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (shouldJumpToEndOfFile) {
|
||||||
|
debug("Running %s +:$ %s", bin, path);
|
||||||
|
execlp(bin, bin, "+:$", path, NULL);
|
||||||
|
} else {
|
||||||
|
debug("Running %s %s", bin, path);
|
||||||
|
execlp(bin, bin, path, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
error(1, "program", "execlp() failed.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---- UNKNOWN EDITOR ----
|
error(1, "program", "execlp() failed.");
|
||||||
else {
|
|
||||||
error(1, "program", "Unknown editor.");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// =========================
|
// ---- NANO ----
|
||||||
// PARENT: wait ONLY editor
|
else if (strcmp(editor, "nano") == 0) {
|
||||||
// =========================
|
|
||||||
int status;
|
|
||||||
waitpid(editor_pid, &status, 0);
|
|
||||||
|
|
||||||
return 0;
|
// If render enabled → spawn viv in parallel
|
||||||
|
if (render) {
|
||||||
|
debug("Running the editor...");
|
||||||
|
pid_t viv_pid = fork();
|
||||||
|
error(viv_pid < 0, "program", "fork() failed.");
|
||||||
|
|
||||||
|
if (viv_pid == 0) {
|
||||||
|
// GRANDCHILD → viv
|
||||||
|
|
||||||
|
|
||||||
|
char viv_path[PATH_MAX];
|
||||||
|
strncpy(viv_path, path, PATH_MAX - 1);
|
||||||
|
viv_path[PATH_MAX - 1] = '\0';
|
||||||
|
|
||||||
|
if (shouldJumpToEndOfFile) {
|
||||||
|
strncat(viv_path, ":99999",
|
||||||
|
PATH_MAX - strlen(viv_path) - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
debug("Running viv %s", viv_path);
|
||||||
|
execlp("viv", "viv", viv_path, NULL);
|
||||||
|
error(1, "program", "execlp() failed.");
|
||||||
|
}
|
||||||
|
// IMPORTANT: do NOT wait for viv
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now run nano (this replaces the child process)
|
||||||
|
if (shouldJumpToEndOfFile) {
|
||||||
|
debug("Running nano + %s", path);
|
||||||
|
execlp("nano", "nano", "+", path, NULL);
|
||||||
|
} else {
|
||||||
|
debug("Running nano %s", path);
|
||||||
|
execlp("nano", "nano", path, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
error(1, "program", "execlp() failed.");
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- UNKNOWN EDITOR ----
|
||||||
|
else {
|
||||||
|
error(1, "program", "Unknown editor.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// =========================
|
||||||
|
// PARENT: wait ONLY editor
|
||||||
|
// =========================
|
||||||
|
int status;
|
||||||
|
while (waitpid(editor_pid, &status, 0) == -1) { // we can't just use waitpid(). Because when resizing the terminal, waitpid() is returned so we loop to see if there is not a problem
|
||||||
|
if (errno != EINTR) {
|
||||||
|
perror("waitpid");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user