From 7526a9e6e5c06bb461ac5a4f0a8bd88ecf44052f Mon Sep 17 00:00:00 2001 From: nova Date: Tue, 12 Aug 2025 21:59:34 +0200 Subject: [PATCH] implementation of jump_to_dir --- config.h | 81 ++++++++++++++++++++++++++------------------------ interactions.c | 53 ++++++++++++++++++++++++++++++--- 2 files changed, 91 insertions(+), 43 deletions(-) diff --git a/config.h b/config.h index 0ccdbe1..27b255e 100644 --- a/config.h +++ b/config.h @@ -35,53 +35,56 @@ static const binding key_binding[] = { * 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 */ + { "B", enter_shell, "$SHELL", "enter $SHELL shell" }, + { "/", not_implemented, NULL, "search" }, - { "h", move_left, NULL, "move left" }, /* moves one dir up */ - { "t", move_down, NULL, "move down" }, - { "n", move_up, NULL, "move up" }, - { "s", move_right, NULL, "move right" }, /* if a dir is hovered, cd into it, if a file is selected, see mimetype_default_cmd */ + { "h", move_left, NULL, "move left" }, /* moves one dir up */ + { "t", move_down, NULL, "move down" }, + { "n", move_up, NULL, "move up" }, + { "s", move_right, NULL, "move right" }, /* 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 */ - { "dD", 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 */ + { "\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 */ + { "dD", 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 first file in dir" }, - { "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" }, + { "G", jump_bottom, NULL, "jump to last file in dir" }, + { "gg", jump_top, NULL, "jump to first file in dir" }, + { "gh", jump_to_dir, "$HOME", "jump to $HOME" }, + { "gs", jump_to_dir, "$START_PATH", "jump to $START_PATH" }, + { "gD", jump_to_dir, "$HOME/Downloads", "jump to $HOME/Downloads" }, + { "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", 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" }, + { "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", order_by, sort_size, "order size" }, - { "ot", order_by, sort_type, "order type" }, - { "oz", order_by, sort_random, "order random" }, - { "oa", order_by, sort_alpha, "order alphabetically" }, + { "on", order_by, sort_natural, "order natural" }, + { "or", not_implemented, "", "order reverse" }, + { "oe", not_implemented, "", "order extension" }, + { "os", order_by, sort_size, "order size" }, + { "ot", order_by, sort_type, "order type" }, + { "oz", order_by, sort_random, "order random" }, + { "oa", order_by, sort_alpha, "order alphabetically" }, - { "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" }, - { "\x7F", toggle_hidden_files, NULL, "toggle hidden files" }, /* backspace/delete key */ + { "a", toggle_hidden_files, NULL, "toggle hidden files" }, + { "\x7F", toggle_hidden_files, NULL, "toggle hidden files" }, /* backspace key */ }; static const unsigned long binding_count = sizeof(key_binding) / sizeof(binding); static const unsigned long mimetype_default_count = sizeof(mimetype_default_cmd) / sizeof(mimetype); static const unsigned long file_extension_default_count = sizeof(file_extension_default_cmd) / sizeof(extension); + diff --git a/interactions.c b/interactions.c index a716dad..f15b63e 100644 --- a/interactions.c +++ b/interactions.c @@ -519,11 +519,56 @@ void not_implemented(int passes, int index){ mvaddstr(terminal_height-1, strlen(key_binding[index].comment) + strlen("\t"), "is not yet implemented"); } void jump_to_dir(int passes, int index){ - if ((char*)key_binding[index].black_magic) { - if (chdir(getenv((char*)key_binding[index].black_magic+1)) != 0) { - FAIL("jump_to_dir", "jumping to black_magic in config.h failed"); + char *ch = (char*)key_binding[index].black_magic; + char slash = 0; + unsigned int env_len = 0; + while (*ch != '\0') { + if (*ch == '/') { + slash = 1; + break; } - } + env_len++; + ch++; + } + char *env_str = NULL; + char *env_parsed = NULL; + char *path = NULL; + ch = (char*)key_binding[index].black_magic; + if (*ch == '/') { + path = malloc(strlen((char*)key_binding[index].black_magic)); + strcpy(path, (char*)key_binding[index].black_magic); + } else if (slash) { + env_str = malloc(env_len * sizeof(char)); + memcpy(env_str, (char*)key_binding[index].black_magic +1, env_len); + env_str[env_len-1] = '\0'; + env_parsed = getenv(env_str); + if (env_parsed) { + path = concat(env_parsed, (char*)key_binding[index].black_magic + env_len); + } else { + path = malloc(strlen((char*)key_binding[index].black_magic)); + strcpy(path, (char*)key_binding[index].black_magic); + } + } else { + env_parsed = getenv((char*)key_binding[index].black_magic +1); + if (env_parsed) { + path = malloc(strlen(env_parsed)+1); + strcpy(path, env_parsed); + } else { + path = malloc(strlen((char*)key_binding[index].black_magic)); + strcpy(path, (char*)key_binding[index].black_magic); + } + } + if (chdir(path) != 0) { + FAIL("jump_to_dir", "jumping to black_magic in config.h failed"); + } + + /*env_parsed shall not be modified (read: free'd) - the man page*/ + if (env_str) { + free(env_str); + } + if(path) { + free(path); + } status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY); } void order_by(int passes, int index){