From cbd479ff4fa7319ca747b93dbed2452518ac55f7 Mon Sep 17 00:00:00 2001 From: nova Date: Sun, 19 Oct 2025 23:13:26 +0200 Subject: [PATCH] various changes to functions modifying btm_buffer --- defines.h | 2 + interactions.c | 174 +++++++++++++++++++++++++------------------------ main.c | 7 +- window.c | 6 +- window.h | 2 +- 5 files changed, 98 insertions(+), 93 deletions(-) diff --git a/defines.h b/defines.h index 56776ab..b049c88 100644 --- a/defines.h +++ b/defines.h @@ -56,6 +56,8 @@ #define YANK_CUT 2 #define YANK_COPY 4 +#define BTM_WINDOW_HEIGHT_ON_STR_INTERACTION 5 + #ifndef STRUCT_GUARD #define STRUCT_GUARD /* complex types are good actually */ diff --git a/interactions.c b/interactions.c index 548d6ef..1eb525e 100644 --- a/interactions.c +++ b/interactions.c @@ -48,6 +48,7 @@ yank yank_files = { 0 }; int read_string(WINDOW *win, int y, int x, char *str); extern void render_pass(); +void window_btm(WINDOW *win, char force_render); extern int (*order_func)(); @@ -323,26 +324,25 @@ void jump_top(){ void open_with(){ pthread_mutex_lock(&mutex_btm); + + char *btm_buffer_tmp = btm_buffer; + werase(win_b); + mvwin(win_b, terminal_height-6, 0); + wresize(win_b, BTM_WINDOW_HEIGHT_ON_STR_INTERACTION, terminal_width/3); /*the div3 just looks cool*/ + btm_buffer = concat("open \"", mid_content[selected_file_current].file_name); btm_buffer = concat(btm_buffer, "\" with:"); - status |= STATUS_UPDATE_SCREEN_0; - werase(win_b); - mvwin(win_b, terminal_height-6, 0); - wresize(win_b, 5, terminal_width/3); /*the div3 just looks cool*/ - - render_pass(); - unsigned long local_height; - local_height = getmaxy(win_b); + window_btm(win_b, 1); /* TODO(2025-06-22T01:24:36) fix fixed buffer size */ char *str = malloc(255); memset(str, ' ', 255); - int err = read_string(win_b, local_height - 1, 0 , str); + int err = read_string(win_b, BTM_WINDOW_HEIGHT_ON_STR_INTERACTION - 1, 0 , str); - if (!err) { + if (err == 0) { char *cmd = concat(str, " ./\""); cmd = concat(cmd, mid_content[selected_file_current].file_name); cmd = concat(cmd, "\""); @@ -354,39 +354,34 @@ void open_with(){ if (system(cmd) == -1) { FAIL("open_with", "creating subcommand failed unhandled"); } - - free(btm_buffer); - btm_buffer = cmd; - } status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL); + free(btm_buffer); + btm_buffer = btm_buffer_tmp; + pthread_mutex_unlock(&mutex_btm); + free(str); } void rename_hovered(){ pthread_mutex_lock(&mutex_btm); + char *btm_buffer_tmp = btm_buffer; + werase(win_b); + mvwin(win_b, terminal_height-6, 0); + wresize(win_b, BTM_WINDOW_HEIGHT_ON_STR_INTERACTION, terminal_width/3); /*the div3 just looks cool*/ + btm_buffer = concat("rename \"", mid_content[selected_file_current].file_name); btm_buffer = concat(btm_buffer, "\" to:"); - status |= STATUS_UPDATE_SCREEN_0; - werase(win_b); - mvwin(win_b, terminal_height-6, 0); - wresize(win_b, 5, terminal_width/3); /*the div3 just looks cool*/ - - pthread_mutex_unlock(&mutex_btm); - render_pass(); - pthread_mutex_lock(&mutex_btm); - - unsigned long local_height; - local_height = getmaxy(win_b); + window_btm(win_b, 1); /* TODO(2025-06-22T01:24:30) fix fixed buffer size */ char *str = malloc(255); memset(str, ' ', 255); - int err = read_string(win_b, local_height - 1, 0, str); + int err = read_string(win_b, BTM_WINDOW_HEIGHT_ON_STR_INTERACTION - 1, 0, str); if (!err) { @@ -400,17 +395,21 @@ void rename_hovered(){ }; btm_buffer = cmd; } - pthread_mutex_unlock(&mutex_btm); + free(str); + free(btm_buffer); + btm_buffer = btm_buffer_tmp; status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL); + pthread_mutex_unlock(&mutex_btm); - free(str); } void delete(){ pthread_mutex_lock(&mutex_btm); + char *btm_buffer_tmp = btm_buffer; + unsigned int i = 0; unsigned int hits = 0; char *file_str = " "; @@ -423,32 +422,46 @@ void delete(){ } } - if (hits) { - btm_buffer = concat("delete:", file_str); - } else { - btm_buffer = concat("delete: \"", mid_content[selected_file_current].file_name); - btm_buffer = concat(btm_buffer, "\""); - } - - btm_buffer = concat(btm_buffer, "?"); - btm_buffer = concat(btm_buffer, "\n\n"); - btm_buffer = concat(btm_buffer, "(y/N)"); - - status |= STATUS_UPDATE_SCREEN_0; werase(win_b); mvwin(win_b, terminal_height-6, 0); - wresize(win_b, 5, terminal_width/3); /*the div3 just looks cool*/ + if (strlen(file_str) < (BTM_WINDOW_HEIGHT_ON_STR_INTERACTION-1) * (terminal_width/3)) { + wresize(win_b, BTM_WINDOW_HEIGHT_ON_STR_INTERACTION, terminal_width/3); /*the div3 just looks cool*/ + btm_buffer = malloc(BTM_WINDOW_HEIGHT_ON_STR_INTERACTION * (terminal_width/3)); + memset(btm_buffer, ' ', (BTM_WINDOW_HEIGHT_ON_STR_INTERACTION * (terminal_width/3))); - pthread_mutex_unlock(&mutex_btm); - render_pass(); - pthread_mutex_lock(&mutex_btm); + memcpy(btm_buffer, "delete: ",strlen("delete: ")); + if (hits) { + memcpy(btm_buffer + strlen("delete: "), file_str, strlen(file_str)); + } else { + btm_buffer[strlen("delete: ")] = '"'; + memcpy(btm_buffer + strlen("delete: ") + sizeof(char), mid_content[selected_file_current].file_name, strlen(mid_content[selected_file_current].file_name)-1); + btm_buffer[strlen("delete: ") + sizeof(char) + strlen(mid_content[selected_file_current].file_name)] = '"'; + } + + memcpy(btm_buffer + (BTM_WINDOW_HEIGHT_ON_STR_INTERACTION * (terminal_width/3) - (terminal_width/3)) , "(y/N)", strlen("(y/N)")); + btm_buffer[BTM_WINDOW_HEIGHT_ON_STR_INTERACTION * (terminal_width/3)] = '\0'; + + } else { + wresize(win_b, BTM_WINDOW_HEIGHT_ON_STR_INTERACTION, terminal_width); /*since more data is present than can be represented using div3, we do the uncool thing)*/ + btm_buffer = malloc(BTM_WINDOW_HEIGHT_ON_STR_INTERACTION * terminal_width); + memset(btm_buffer, ' ', BTM_WINDOW_HEIGHT_ON_STR_INTERACTION * terminal_width); + + memcpy(btm_buffer, "delete: ",strlen("delete: ")); + memcpy(btm_buffer + strlen("delete: "), file_str, (strlen(file_str) > ((BTM_WINDOW_HEIGHT_ON_STR_INTERACTION * terminal_width) - terminal_width) ? ((BTM_WINDOW_HEIGHT_ON_STR_INTERACTION * terminal_width) - terminal_width) : strlen(file_str))); + + memcpy(btm_buffer + (BTM_WINDOW_HEIGHT_ON_STR_INTERACTION * terminal_width - terminal_width) , "(y/N)", strlen("(y/N)")); + btm_buffer[BTM_WINDOW_HEIGHT_ON_STR_INTERACTION * terminal_width] = '\0'; + } + + + + window_btm(win_b, 1); timeout(-1); /* negative numbers block until enter is pressed */ /* TODO(2025-06-22T01:24:30) fix fixed buffer size */ char ch = wgetch(win_b); if (ch == 'y' || ch == 'Y') { - /* TODO(2025-06-30T02:27:06) IMPORTANT: this really fucks up when the file has a quotation mark in its name */ if (hits) { for (i = 0; i < mid_file_count; i++) { if (mid_content[i].status & FILE_STATUS_SELECTED) { @@ -456,54 +469,45 @@ void delete(){ } } free(btm_buffer); - btm_buffer = concat("deleted: ", file_str); } else { free(btm_buffer); if (mid_content[selected_file_current].file_type & FILE_TYPE_DIR) { recursive_delete(mid_content[selected_file_current]); } remove(mid_content[selected_file_current].file_name); - btm_buffer = concat("deleted: \"", mid_content[selected_file_current].file_name); - btm_buffer = concat(btm_buffer, "\""); } - /*system(cmd);*/ - - } else { - free(btm_buffer); - btm_buffer = "cancled deletion"; + } + free(btm_buffer); + btm_buffer = btm_buffer_tmp; + if (hits) { + free(file_str); } timeout(10); status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL); - if (hits) { - free(file_str); - } pthread_mutex_unlock(&mutex_btm); } void makedir(){ pthread_mutex_lock(&mutex_btm); + + char *btm_buffer_tmp = btm_buffer; werase(win_b); mvwin(win_b, terminal_height-6, 0); - wresize(win_b, 5, terminal_width/3); /*the div3 just looks cool*/ - memset(btm_buffer, ' ', terminal_width); - memcpy(btm_buffer, "create dir: ", strlen("create dir: ")); - status |= STATUS_UPDATE_SCREEN_0; + wresize(win_b, BTM_WINDOW_HEIGHT_ON_STR_INTERACTION, terminal_width/3); /*the div3 just looks cool*/ - pthread_mutex_unlock(&mutex_btm); - render_pass(); - pthread_mutex_lock(&mutex_btm); + btm_buffer = "create dir: "; - unsigned long local_height; - local_height = getmaxy(win_b); + window_btm(win_b, 1); /* TODO(2025-07-03T01:19:55) fix fixed buffer size */ char *str = malloc(255); memset(str, ' ', 255); - int err = read_string(win_b, local_height - 1, 0, str); + int err = read_string(win_b, BTM_WINDOW_HEIGHT_ON_STR_INTERACTION - 1, 0, str); + if (!err) { btm_buffer = concat(btm_buffer, str); mode_t mask = umask(0); @@ -511,29 +515,29 @@ void makedir(){ umask(mask); } free(str); + free(btm_buffer); + btm_buffer = btm_buffer_tmp; + status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL); pthread_mutex_unlock(&mutex_btm); } void makefile(){ pthread_mutex_lock(&mutex_btm); - memcpy(btm_buffer, "create file: ", strlen("create file: ")); - status |= STATUS_UPDATE_SCREEN_0; + char *btm_buffer_tmp = btm_buffer; werase(win_b); mvwin(win_b, terminal_height-6, 0); - wresize(win_b, 5, terminal_width/3); /*the div3 just looks cool*/ + wresize(win_b, BTM_WINDOW_HEIGHT_ON_STR_INTERACTION, terminal_width/3); /*the div3 just looks cool*/ - pthread_mutex_unlock(&mutex_btm); - render_pass(); - pthread_mutex_lock(&mutex_btm); + btm_buffer = "create file: "; - unsigned long local_height; - local_height = getmaxy(win_b); + window_btm(win_b, 1); /* TODO(2025-07-03T01:19:49) fix fixed buffer size */ char *str = malloc(255); memset(str, ' ', 255); - int err = read_string(win_b, local_height - 1, 0, str); + int err = read_string(win_b, BTM_WINDOW_HEIGHT_ON_STR_INTERACTION - 1, 0, str); + if (!err) { btm_buffer = concat(btm_buffer, str); FILE *fp; @@ -541,8 +545,11 @@ void makefile(){ fclose(fp); } free(str); - pthread_mutex_unlock(&mutex_btm); + free(btm_buffer); + btm_buffer = btm_buffer_tmp; + status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL); + pthread_mutex_unlock(&mutex_btm); } void update(){ @@ -640,6 +647,7 @@ void cmd_on_selected(unsigned long passes, int index){ (void)passes; pthread_mutex_lock(&mutex_btm); + char *btm_buffer_tmp = btm_buffer; unsigned int i = 0; unsigned int hits = 0; char *file_str = " "; @@ -664,15 +672,11 @@ void cmd_on_selected(unsigned long passes, int index){ btm_buffer = concat(btm_buffer, "\n\n"); btm_buffer = concat(btm_buffer, "(y/N)"); - status |= STATUS_UPDATE_SCREEN_0; werase(win_b); mvwin(win_b, terminal_height-6, 0); - wresize(win_b, 5, terminal_width/3); /*the div3 just looks cool*/ - - pthread_mutex_unlock(&mutex_btm); - render_pass(); - pthread_mutex_lock(&mutex_btm); + wresize(win_b, BTM_WINDOW_HEIGHT_ON_STR_INTERACTION, terminal_width/3); /*the div3 just looks cool*/ + window_btm(win_b, 1); timeout(-1); /* negative numbers block until enter is pressed */ /* TODO(2025-07-06T07:22:49) fix fixed buffer size */ @@ -715,6 +719,8 @@ void cmd_on_selected(unsigned long passes, int index){ free(btm_buffer); memcpy(btm_buffer, "cancled deletion", strlen("cancled deletion")); } + free(btm_buffer); + btm_buffer = btm_buffer_tmp; timeout(10); status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL); @@ -844,9 +850,7 @@ void search(){ local_height = getmaxy(win_b); memset(search_buffer, '\0', 255); - pthread_mutex_unlock(&mutex_btm); - render_pass(); - pthread_mutex_lock(&mutex_btm); + window_btm(win_b, 1); curs_set(1); diff --git a/main.c b/main.c index 432a1e1..0df7c71 100644 --- a/main.c +++ b/main.c @@ -145,11 +145,6 @@ void render_pass(){ /*TODO: check if deallocation of window and reallocation is faster than this or not */ - wclear(win_t); - wclear(win_b); - wclear(win_l); - wclear(win_m); - wclear(win_r); wresize(win_t, 1, terminal_width); wresize(win_l, terminal_height-2, terminal_width/8); @@ -174,7 +169,7 @@ void render_pass(){ window_lft(win_l); window_mid(win_m); window_rgt(win_r); - window_btm(win_b); + window_btm(win_b, 0); wrefresh(win_t); wrefresh(win_l); wrefresh(win_m); diff --git a/window.c b/window.c index 2cd26ec..e328a4d 100644 --- a/window.c +++ b/window.c @@ -48,7 +48,7 @@ void window_top(WINDOW *win){ status |= STATUS_UPDATE_SCREEN_0; } } -void window_btm(WINDOW *win){ +void window_btm(WINDOW *win, char force_render){ werase(win); if (pthread_mutex_trylock(&mutex_btm) == 0) { @@ -58,6 +58,10 @@ void window_btm(WINDOW *win){ pthread_mutex_unlock(&mutex_btm); /*the printing of the input char is done in user_interactions*/ /*the printing of all possible inputs are done in user_interactions */ + } else if (force_render) { + /*force_render is used in stuff like open_with, search, and other functions that take over win_b, + * while needing to avoid possible conflicts like thread_btm itself*/ + mvwprintw(win, 0, 0, "%s", btm_buffer); } else { mvwaddstr(win, 0, terminal_width/2, "LOADING"); status |= STATUS_UPDATE_SCREEN_0; diff --git a/window.h b/window.h index f1ce2df..7abda83 100644 --- a/window.h +++ b/window.h @@ -1,7 +1,7 @@ #include "window.c" void window_top(WINDOW *win); -void window_btm(WINDOW *win); +void window_btm(WINDOW *win, char force_render); void window_lft(WINDOW *win); void window_mid(WINDOW *win); void window_rgt(WINDOW *win);