Fixed crash when resizing the window

This commit is contained in:
Tomas Rivera
2026-04-09 18:00:34 +02:00
parent c658453994
commit 2b0996e6d5
5 changed files with 108 additions and 80 deletions
+2
View File
@@ -2,3 +2,5 @@ a.out
nvimnotes nvimnotes
notewrapper notewrapper
nw nw
error.log
output.log
+2
View File
@@ -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
+2
View File
@@ -268,6 +268,8 @@ int main(int argc, char *argv[]) {
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) {
// this loop is the vault selector // this loop is the vault selector
+6 -4
View File
@@ -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
+25 -5
View File
@@ -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,6 +257,7 @@ 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) {
// this ensures that ncurses won't affect the editor behaviour
pid_t editor_pid = fork(); pid_t editor_pid = fork();
error(editor_pid < 0, "program", "fork() failed."); error(editor_pid < 0, "program", "fork() failed.");
@@ -283,11 +296,14 @@ int openEditor(char *path, char *editor, int render, int shouldJumpToEndOfFile,
// If render enabled → spawn viv in parallel // If render enabled → spawn viv in parallel
if (render) { if (render) {
debug("Running the editor...");
pid_t viv_pid = fork(); pid_t viv_pid = fork();
error(viv_pid < 0, "program", "fork() failed."); error(viv_pid < 0, "program", "fork() failed.");
if (viv_pid == 0) { if (viv_pid == 0) {
// GRANDCHILD → viv // GRANDCHILD → viv
char viv_path[PATH_MAX]; char viv_path[PATH_MAX];
strncpy(viv_path, path, PATH_MAX - 1); strncpy(viv_path, path, PATH_MAX - 1);
viv_path[PATH_MAX - 1] = '\0'; viv_path[PATH_MAX - 1] = '\0';
@@ -326,7 +342,11 @@ int openEditor(char *path, char *editor, int render, int shouldJumpToEndOfFile,
// PARENT: wait ONLY editor // PARENT: wait ONLY editor
// ========================= // =========================
int status; int status;
waitpid(editor_pid, &status, 0); 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; return 0;
} }