From 3eb1c0f93eb5c69ac1f9f78437862dd65a5fafed Mon Sep 17 00:00:00 2001 From: nova Date: Sun, 22 Jun 2025 01:28:59 +0200 Subject: [PATCH] implemented rename_hovered, custom input string loop --- config.h | 4 +- defines.h | 4 +- interactions.c | 112 ++++++++++++++++++++++++++++++++++++++++--------- interactions.h | 1 + main.c | 7 ++-- 5 files changed, 103 insertions(+), 25 deletions(-) diff --git a/config.h b/config.h index e5dd03e..d599db5 100644 --- a/config.h +++ b/config.h @@ -15,7 +15,9 @@ static binding key_binding[] = { { "t", move_down }, { "n", move_up }, { "s", move_left }, /* if a dir is hovered, cd into it, if a file is selected, see mimetype_default_cmd */ - { "\n", open_with }, /* enter/return/new line/whatever you call it */ + + { "\n", open_with }, /* opens the hovered file with an arbitrary command */ + { "r", rename_hovered }, /* renames currently hovered file/directory */ { "gg", jump_top }, { "G", jump_bottom }, diff --git a/defines.h b/defines.h index 70b06c0..fc67743 100644 --- a/defines.h +++ b/defines.h @@ -7,7 +7,9 @@ #define STATUS_UPDATE_SCREEN_RESIZE 16 #define STATUS_UPDATE_SCREEN_RELOAD_FULL 32 #define STATUS_USER_ROOT 64 -#define STATUS_OPEN_WITH 128 +#define STATUS_INTERACTIONS_MASK 384 +#define STATUS_INTERACTIONS_OPEN_WITH 128 +#define STATUS_INTERACTIONS_RENAME 256 #define SETTINGS_HAS_COLOR 1 diff --git a/interactions.c b/interactions.c index df67ac6..beedc55 100644 --- a/interactions.c +++ b/interactions.c @@ -25,12 +25,19 @@ extern char *btm_buffer; extern unsigned int status; void open_with_pass_2(); +void rename_hovered_pass_2(); +int read_string(char *str); void user_interactions(char *input, WINDOW *win_b) { void (*func_ptr)(); unsigned long i = 0; - if (status & STATUS_OPEN_WITH) { - open_with_pass_2(win_b); + if (status & STATUS_INTERACTIONS_MASK) { + if (status & STATUS_INTERACTIONS_OPEN_WITH) { + open_with_pass_2(win_b); + } else if (status & STATUS_INTERACTIONS_RENAME) { + rename_hovered_pass_2(win_b); + } + } for (i = 0; i < binding_count; i++) { if (*input == key_binding[i].key[0]) { @@ -105,11 +112,39 @@ void jump_top(){ pthread_mutex_unlock(&mutex_selection); } -void open_with(){ +int read_string(char *str){ echo(); + curs_set(1); + + timeout(-1); /* negative numbers block until enter is pressed */ + + unsigned int pass = 0; + char ch; + char err = 0; + while(1) { + ch = getch(); + if (ch == '\n') { + err = 0; + break; + } else if (ch == 27) { /* esc key */ + err = 1; + break; + } + str[pass] = ch; + pass++; + } + str[pass] = '\0'; + + timeout(10); + noecho(); + curs_set(0); + + return err; +} +void open_with(){ btm_buffer = concat("open \"", file_current.file_name); btm_buffer = concat(btm_buffer, "\" with:"); - status |= (STATUS_UPDATE_SCREEN_0 | STATUS_OPEN_WITH); + status |= (STATUS_UPDATE_SCREEN_0 | STATUS_INTERACTIONS_OPEN_WITH); } void open_with_pass_2(WINDOW *win_b){ @@ -118,28 +153,65 @@ void open_with_pass_2(WINDOW *win_b){ unsigned long local_height; getmaxyx(win_b, local_height, local_width); - echo(); - curs_set(1); - char *str = malloc(50); - timeout(-1); /* negative numbers block until enter is pressed */ - mvwgetstr(win_b, local_height-1, 0, str); - timeout(10); + /* TODO(2025-06-22T01:24:36) fix fixed buffer size */ + char *str = malloc(255); + memset(str, ' ', 255); + int err = read_string(str); - char *cmd = concat(str, " ./\""); - cmd = concat(cmd, file_current.file_name); - cmd = concat(cmd, "\""); + if (!err) { + char *cmd = concat(str, " ./\""); + cmd = concat(cmd, file_current.file_name); + cmd = concat(cmd, "\""); - system(cmd); + system(cmd); - noecho(); - curs_set(0); - status &= ~(STATUS_OPEN_WITH); + free(btm_buffer); + btm_buffer = cmd; + + } + + status &= ~(STATUS_INTERACTIONS_MASK); status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL); - free(btm_buffer); - btm_buffer = cmd; - + free(str); +} + +void rename_hovered(WINDOW *win_b){ + + btm_buffer = concat("rename \"", file_current.file_name); + btm_buffer = concat(btm_buffer, "\" to:"); + status |= (STATUS_UPDATE_SCREEN_0 | STATUS_INTERACTIONS_RENAME); + +} + +void rename_hovered_pass_2(WINDOW *win_b){ + + unsigned long local_width; + unsigned long local_height; + getmaxyx(win_b, local_height, local_width); + + /* TODO(2025-06-22T01:24:30) fix fixed buffer size */ + char *str = malloc(255); + memset(str, ' ', 255); + int err = read_string(str); + + + if (!err) { + char *cmd = concat("mv ./\"", file_current.file_name); + cmd = concat(cmd, "\" ./\""); + cmd = concat(cmd, str); + cmd = concat(cmd, "\""); + + system(cmd); + btm_buffer = cmd; + } + + status &= ~(STATUS_INTERACTIONS_MASK); + status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL); + + free(btm_buffer); + free(str); } diff --git a/interactions.h b/interactions.h index f9b49ff..6d98402 100644 --- a/interactions.h +++ b/interactions.h @@ -16,3 +16,4 @@ void jump_bottom(); void jump_top(); void toggle_hidden_files(); void open_with(); +void rename_hovered(); diff --git a/main.c b/main.c index d4eb23b..7f54371 100644 --- a/main.c +++ b/main.c @@ -112,7 +112,7 @@ int main(){ void render_pass(WINDOW *win_t, WINDOW *win_b, WINDOW *win_l, WINDOW *win_m, WINDOW *win_r){ - if (status & (STATUS_UPDATE_SCREEN_RESIZE | STATUS_OPEN_WITH)) { + if (status & (STATUS_UPDATE_SCREEN_RESIZE | STATUS_INTERACTIONS_MASK)) { if (status & STATUS_UPDATE_SCREEN_RELOAD_FULL) { clear(); status &= ~STATUS_UPDATE_SCREEN_RELOAD_FULL; @@ -130,7 +130,7 @@ void render_pass(WINDOW *win_t, WINDOW *win_b, WINDOW *win_l, WINDOW *win_m, WIN wresize(win_l, terminal_height-2, terminal_width/8); wresize(win_m, terminal_height-2, (terminal_width/2)-(terminal_width/8)); wresize(win_r, terminal_height-2, terminal_width/2); - if(status & STATUS_OPEN_WITH) { + if(status & STATUS_INTERACTIONS_MASK) { wresize(win_b, 5, terminal_width/3); /*the div3 just looks cool*/ } else { wresize(win_b, 1, terminal_width); @@ -141,7 +141,7 @@ void render_pass(WINDOW *win_t, WINDOW *win_b, WINDOW *win_l, WINDOW *win_m, WIN mvwin(win_l, 1, 0); mvwin(win_m, 1, (terminal_width/8)); mvwin(win_r, 1, ((terminal_width/2))); - if(status & STATUS_OPEN_WITH) { + if(status & STATUS_INTERACTIONS_MASK) { mvwin(win_b, terminal_height-6, 0); } else { mvwin(win_b, terminal_height-1, 0); @@ -182,6 +182,7 @@ void init() { threading_init(); /* found in threading.c */ colors_init(); + ESCDELAY = 10; }