diff --git a/config.h b/config.h index 55bfb99..e605e4b 100644 --- a/config.h +++ b/config.h @@ -18,56 +18,57 @@ static mimetype mimetype_default_cmd[] = { }; static binding key_binding[] = { - /*key action blackmagic comment*/ + /*key action blackmagic comment*/ /*you cannot add bindings that include other bindings in its entirety * possible: mk, mf * not possible: gg, ggg * trying to use ggg will always fail as it will execute gg first instead, resetting the input buffer, thus never reaching ggg */ /* blackmagic holds a modifier of an action, either as string or as function pointer depending on the action */ - { "q", quit_program, NULL, "quit" }, - { " ", toggle_selection, NULL, "toggle file selection" }, /* on hovered file/directory */ - { "e", update, NULL, "rerun all backend" }, /* executes the entire backend and redrawing of the screen */ - { "/", not_implemented, NULL, "search" }, + { "q", quit_program, NULL, "quit" }, + { " ", toggle_selection, NULL, "toggle file selection" }, /* on hovered file/directory */ + { "e", update, NULL, "rerun all backend" }, /* executes the entire backend and redrawing of the screen */ + { "/", not_implemented, NULL, "search" }, - { "h", move_right, NULL, "move right" }, /* moves one dir up */ - { "t", move_down, NULL, "move down" }, - { "n", move_up, NULL ,"move up" }, - { "s", move_left, NULL, "move left" }, /* if a dir is hovered, cd into it, if a file is selected, see mimetype_default_cmd */ + { "h", move_right, NULL, "move right" }, /* moves one dir up */ + { "t", move_down, NULL, "move down" }, + { "n", move_up, NULL, "move up" }, + { "s", move_left, NULL, "move left" }, /* if a dir is hovered, cd into it, if a file is selected, see mimetype_default_cmd */ - { "\n", open_with, NULL, "open \"open with\" dialog" }, /* opens the hovered file with an arbitrary command */ - { "r", rename_hovered, NULL, "rename hovered file" }, /* renames currently hovered file/directory */ - { "d", delete, NULL, "delete file" }, /* deletes currently hovered OR selected file/directory + { "\n", open_with, NULL, "open \"open with\" dialog" }, /* opens the hovered file with an arbitrary command */ + { "r", rename_hovered, NULL, "rename hovered file" }, /* renames currently hovered file/directory */ + { "d", delete, NULL, "delete file" }, /* deletes currently hovered OR selected file/directory * this means that it does not delete the hovered files if files are already selected */ - { "G", jump_bottom, NULL, "jump to last file in dir" }, - { "gg", jump_top, NULL, "jump to file 0" }, - { "gh", jump_to_dir, "~/", "jump to ~/" }, - { "gd", jump_to_dir, "/dev", "jump to /dev" }, - { "ge", jump_to_dir, "/etc", "jump to /etc" }, - { "gm", jump_to_dir, "/mnt", "jump to /mnt" }, - { "go", jump_to_dir, "/opt", "jump to /opt" }, - { "gt", jump_to_dir, "/tmp", "jump to /tmp" }, - { "gv", jump_to_dir, "/var", "jump to /var" }, + { "G", jump_bottom, NULL, "jump to last file in dir" }, + { "gg", jump_top, NULL, "jump to file 0" }, + { "gh", jump_to_dir, "$HOME", "jump to $HOME" }, + { "gs", jump_to_dir, "$START_PATH", "jump to $START_PATH" }, + { "gd", jump_to_dir, "/dev", "jump to /dev" }, + { "ge", jump_to_dir, "/etc", "jump to /etc" }, + { "gm", jump_to_dir, "/mnt", "jump to /mnt" }, + { "go", jump_to_dir, "/opt", "jump to /opt" }, + { "gt", jump_to_dir, "/tmp", "jump to /tmp" }, + { "gv", jump_to_dir, "/var", "jump to /var" }, - { "u7", not_implemented, "", "unzip 7z" }, - { "ub", not_implemented, "", "unzip bz2" }, - { "ur", not_implemented, "", "unzip rar" }, - { "ut", not_implemented, "", "unzip tar" }, - { "ut", not_implemented, "", "unzip gzip" }, - { "uz", not_implemented, "", "unzip zip" }, + { "u7", cmd_on_selected, "7z x", "unzip 7z" }, + { "ub", cmd_on_selected, "tar -xvf", "unzip bz2" }, + { "ur", cmd_on_selected, "unrar x", "unzip rar" }, + { "ut", cmd_on_selected, "tar -xvf", "unzip tar" }, + { "ut", cmd_on_selected, "gzip -d", "unzip gzip" }, + { "uz", cmd_on_selected, "unzip ", "unzip zip" }, { "on", order_by, sort_natural, "order natural" }, - { "or", not_implemented, "", "order reverse" }, - { "oe", not_implemented, "", "order extension" }, - { "os", not_implemented, "", "order size" }, - { "ot", not_implemented, "", "order type" }, - { "oz", not_implemented, "", "order random" }, + { "or", not_implemented, "", "order reverse" }, + { "oe", not_implemented, "", "order extension" }, + { "os", not_implemented, "", "order size" }, + { "ot", not_implemented, "", "order type" }, + { "oz", not_implemented, "", "order random" }, { "oa", order_by, sort_alpha, "order random" }, - { "mk", makedir, NULL, "create directory" }, - { "mf", makefile, NULL, "create file" }, + { "mk", makedir, NULL, "create directory" }, + { "mf", makefile, NULL, "create file" }, - { "a", toggle_hidden_files, NULL, "toggle hidden files" }, + { "a", toggle_hidden_files, NULL, "toggle hidden files" }, }; static unsigned long binding_count = sizeof(key_binding) / sizeof(binding); static unsigned long mimetype_default_count = sizeof(mimetype_default_cmd) / sizeof(mimetype); diff --git a/interactions.c b/interactions.c index 52e529f..cf2feb0 100644 --- a/interactions.c +++ b/interactions.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -37,6 +38,7 @@ extern char *input; unsigned int input_pass; int parsed_input_number; extern char *terminal_width_empty_line; +extern char *start_path; int read_string(WINDOW *win, int y, int x, char *str); int strcmp_offset(char *in0, char *in1, char offset); @@ -461,22 +463,99 @@ void makefile(){ void update(){ status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL); } -void not_implemented(){ - mvaddstr(terminal_height-1, 0, input); - mvaddstr(terminal_height-1, strlen(input), "\t"); - mvaddstr(terminal_height-1, strlen(input) + strlen("\t"), "is not yet implemented"); +void not_implemented(int passes, int index){ + mvaddstr(terminal_height-1, 0, key_binding[index].comment); + mvaddstr(terminal_height-1, strlen(key_binding[index].comment), "\t"); + mvaddstr(terminal_height-1, strlen(key_binding[index].comment) + strlen("\t"), "is not yet implemented"); } void jump_to_dir(int passes, int index){ - if (strcmp((char*)key_binding[index].black_magic, "~/") || strcmp((char*)key_binding[index].black_magic, "$HOME")) { - chdir(getenv("HOME")); + if ((char*)key_binding[index].black_magic) { + chdir(getenv((char*)key_binding[index].black_magic+1)); + } else { + chdir((char*)key_binding[index].black_magic); } - /* for some reason putting this in a else does not work */ - chdir((char*)key_binding[index].black_magic); - status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL); + status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY); } void order_by(int passes, int index){ order_func = key_binding[index].black_magic; status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY); } +void cmd_on_selected(int passes, int index){ + unsigned int i = 0; + unsigned int hits = 0; + char *file_str = " "; + for (i = 0; i < mid_file_count; i++) { + if (mid_content[i].status & FILE_STATUS_SELECTED) { + file_str = concat(file_str, "\""); + file_str = concat(file_str, mid_content[i].file_name); + file_str = concat(file_str, "\" "); + hits++; + } + } + + if (hits) { + btm_buffer = concat(key_binding[index].black_magic, file_str); + } else { + btm_buffer = concat(key_binding[index].black_magic, "\""); + btm_buffer = concat(btm_buffer, 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*/ + + render_pass(); + + + timeout(-1); /* negative numbers block until enter is pressed */ + /* TODO(2025-07-06T07:22:49) fix fixed buffer size */ + char ch = wgetch(win_b); + + if (ch == 'y' || ch == 'Y') { + /* the second loop is used to add "./", wich is not being printed" */ + char *cmd; + /* TODO(2025-07-06T07:23:05) 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) { + cmd = concat((char*)key_binding[index].black_magic, " \""); + cmd = concat(cmd, mid_content[i].file_name); + cmd = concat(cmd, "\""); + system(cmd); + free(cmd); + } + } + free(btm_buffer); + btm_buffer = concat("completed: ", key_binding[index].black_magic); + } else { + free(btm_buffer); + cmd = concat((char*)key_binding[index].black_magic, " \""); + cmd = concat(cmd, file_current->file_name); + cmd = concat(cmd, "\""); + system(cmd); + mvaddstr(10,10, cmd); + + } + /*system(cmd);*/ + free(cmd); + + } else { + free(btm_buffer); + btm_buffer = "cancled deletion"; + } + + timeout(10); + status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL); + + if (hits) { + free(file_str); + } +} diff --git a/interactions.h b/interactions.h index f1f1a24..c93f0a4 100644 --- a/interactions.h +++ b/interactions.h @@ -22,6 +22,7 @@ void delete(); void makedir(); void makefile(); void update(); -void not_implemented(); +void not_implemented(int passes, int index); void jump_to_dir(int passes, int index); void order_by(int passes, int index); +void cmd_on_selected(int passes, int index); diff --git a/main.c b/main.c index ee6a82c..662a288 100644 --- a/main.c +++ b/main.c @@ -17,6 +17,7 @@ unsigned int temp_width = 0; unsigned int settings; unsigned int file_modifiers; unsigned int status; +char *start_path; WINDOW *win_t; WINDOW *win_b; @@ -90,6 +91,7 @@ int main(){ } threading_free(); + free(start_path); pthread_join(thread_l, NULL); pthread_join(thread_r, NULL); @@ -178,5 +180,6 @@ void init() { colors_init(); ESCDELAY = 10; + setenv("START_PATH", getcwd(NULL, 0), 0); }