From 322a1495c0c705bb2015ccfc22d0b9b2960272e2 Mon Sep 17 00:00:00 2001 From: nova Date: Sun, 6 Jul 2025 07:03:45 +0200 Subject: [PATCH] added blackmagic (funcptr/string modifiers on actions) --- backend.c | 5 +++- config.h | 75 ++++++++++++++++++++++++++------------------------ defines.h | 1 + interactions.c | 20 ++++++++++++-- interactions.h | 2 ++ 5 files changed, 64 insertions(+), 39 deletions(-) diff --git a/backend.c b/backend.c index e59853e..975d87b 100644 --- a/backend.c +++ b/backend.c @@ -14,6 +14,8 @@ extern unsigned int file_modifiers; extern unsigned int color_count; extern color *colors; unsigned long file_offset; +int (*order_func)() = sort_natural; + char* concat(const char *s1, const char *s2){ const size_t len1 = strlen(s1); @@ -128,7 +130,8 @@ void get_dir_content(char *path, unsigned long *dir_file_count, file *dir_conten free(file); } } - qsort(dir_content, *dir_file_count, sizeof(file), sort_natural); + + qsort(dir_content, *dir_file_count, sizeof(file), order_func); for (i = 0; i < *dir_file_count; i++) { free(entry[i]); diff --git a/config.h b/config.h index 14d9a29..55bfb99 100644 --- a/config.h +++ b/config.h @@ -1,5 +1,6 @@ #include "defines.h" #include "interactions.h" +#include "sorting.h" static mimetype mimetype_default_cmd[] = { /* mimetype shell command @@ -17,54 +18,56 @@ static mimetype mimetype_default_cmd[] = { }; static binding key_binding[] = { - /*key action 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 */ - { "q", quit_program, "quit" }, - { " ", toggle_selection, "toggle file selection" }, /* on hovered file/directory */ - { "e", update, "rerun all backend" }, /* executes the entire backend and redrawing of the screen */ - { "/", not_implemented, "search" }, + /* 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" }, - { "h", move_right, "move right" }, /* moves one dir up */ - { "t", move_down, "move down" }, - { "n", move_up, "move up" }, - { "s", move_left, "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, "open \"open with\" dialog" }, /* opens the hovered file with an arbitrary command */ - { "r", rename_hovered, "rename hovered file" }, /* renames currently hovered file/directory */ - { "d", delete, "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, "jump to last file in dir" }, - { "gg", jump_top, "jump to file 0" }, - { "gh", not_implemented, "jump to ~/" }, - { "gd", not_implemented, "jump to /dev" }, - { "ge", not_implemented, "jump to /etc" }, - { "gm", not_implemented, "jump to /mnt" }, - { "go", not_implemented, "jump to /opt" }, - { "gt", not_implemented, "jump to /tmp" }, - { "gv", not_implemented, "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, "~/", "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" }, - { "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", 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" }, - { "on", not_implemented, "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" }, + { "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" }, + { "oa", order_by, sort_alpha, "order random" }, - { "mk", makedir, "create directory" }, - { "mf", makefile, "create file" }, + { "mk", makedir, NULL, "create directory" }, + { "mf", makefile, NULL, "create file" }, - { "a", toggle_hidden_files, "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/defines.h b/defines.h index ed3cdbc..6a06765 100644 --- a/defines.h +++ b/defines.h @@ -73,6 +73,7 @@ typedef struct Mimetype { typedef struct Binding { char* key; void (*func)(); + void* black_magic; char* comment; } binding; diff --git a/interactions.c b/interactions.c index c569d00..52e529f 100644 --- a/interactions.c +++ b/interactions.c @@ -41,6 +41,8 @@ extern char *terminal_width_empty_line; int read_string(WINDOW *win, int y, int x, char *str); int strcmp_offset(char *in0, char *in1, char offset); extern void render_pass(); +extern int (*order_func)(); + void user_interactions() { @@ -74,7 +76,7 @@ void user_interactions() { - void (*func_ptr)(int); + void (*func_ptr)(int, int); int number_length = 0; if (!binding_pass) { parsed_input_number = 0; @@ -95,7 +97,7 @@ void user_interactions() { if (strcmp(input + number_length, key_binding[i].key) == 0) { func_ptr = key_binding[i].func; - func_ptr(parsed_input_number); + func_ptr(parsed_input_number, i); memset(input, 0, 255); input_pass = 0; @@ -464,3 +466,17 @@ void not_implemented(){ mvaddstr(terminal_height-1, strlen(input), "\t"); mvaddstr(terminal_height-1, strlen(input) + 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")); + } + /* 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); +} +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); +} diff --git a/interactions.h b/interactions.h index b3bd865..f1f1a24 100644 --- a/interactions.h +++ b/interactions.h @@ -23,3 +23,5 @@ void makedir(); void makefile(); void update(); void not_implemented(); +void jump_to_dir(int passes, int index); +void order_by(int passes, int index);