fe

terminal file explorer and picker

git clone https://9o.is/git/fe.git

commit 4882dc745c37072a2e2afcbfc853b89c23e0fc13
parent 6fab37bc5e639365d6a96064b4e6d16dfa1e34f3
Author: Jul <jul@9o.is>
Date:   Mon, 19 Jan 2026 01:07:51 -0500

move keybindings to config.h

Diffstat:
Mconfig.def.h | 45+++++++++++++++++++++++++++++++++++++++++++++
Mtty_interface.c | 75++++++++++++++++-----------------------------------------------------------
Mtty_interface.h | 17+++++++++++++++++
3 files changed, 78 insertions(+), 59 deletions(-)

diff --git a/config.def.h b/config.def.h @@ -1,4 +1,5 @@ #include "tty.h" +#include "tty_interface.h" #define COLOR_DIRECTORY TTY_COLOR_BLUE #define COLOR_LINK TTY_COLOR_CYAN @@ -15,3 +16,47 @@ #define SORT_ICASE 0; #define SORT_MTIME 0; #define SHOW_HIDDEN 0; + +typedef struct { + const char *key; + void (*action)(tty_interface_t *, const char *); + const char *argv; +} keybinding_t; + +#define KEY(key) ((const char[]){key, '\0'}) +#define KEY_CTRL(key) ((const char[]){((key) - ('@')), '\0'}) + +static const keybinding_t keybindings[] = { + {"\x1b", action_exit, NULL}, /* ESC */ + {KEY_CTRL('C'), action_exit, NULL}, /* C-C */ + {KEY_CTRL('M'), action_select, NULL}, /* CR */ + {KEY_CTRL('P'), action_prev, NULL}, /* C-P */ + {KEY_CTRL('N'), action_next, NULL}, /* C-N */ + {KEY_CTRL('K'), action_prev, NULL}, /* C-K */ + {KEY_CTRL('J'), action_next, NULL}, /* C-J */ + {KEY_CTRL('U'), action_halfpageup, NULL}, /* C-U */ + {KEY_CTRL('D'), action_halfpagedown, NULL}, /* C-D */ + {KEY('g'), action_first, NULL}, /* g */ + {KEY('G'), action_last, NULL}, /* G */ + {KEY('~'), action_home, NULL}, /* ~ */ + {KEY('.'), action_togglehidden, NULL}, /* . */ + {KEY('p'), action_run, "less %s/%s"}, /* p (preview) */ + {KEY('t'), action_run, "echo \"create 'vis %s/%s'\" > $DVTM_CMD_FIFO"}, /* edit new tab */ + {KEY('f'), action_setpath, "find %s | fzy"}, /* s (search) */ + {KEY('-'), action_parent, NULL}, /* - */ + {"\x1bOD", action_parent, NULL}, /* LEFT */ + {"\x1b[D", action_parent, NULL}, /* LEFT */ + {"\x1bOC", action_select, NULL}, /* RIGHT */ + {"\x1b[C", action_select, NULL}, /* RIGHT */ + {"\x1b[A", action_prev, NULL}, /* UP */ + {"\x1bOA", action_prev, NULL}, /* UP */ + {"\x1b[B", action_next, NULL}, /* DOWN */ + {"\x1bOB", action_next, NULL}, /* DOWN */ + {"\x1b[5~", action_pageup, NULL}, + {"\x1b[6~", action_pagedown, NULL}, + {"\x1b[200~", action_ignore, NULL}, + {"\x1b[201~", action_ignore, NULL}, + {NULL, NULL, NULL}}; + +#undef KEY_CTRL +#undef KEY diff --git a/tty_interface.c b/tty_interface.c @@ -76,12 +76,12 @@ static void draw(tty_interface_t *state) { tty_flush(tty); } -static void action_ignore(tty_interface_t *state, const char *argv) { +void action_ignore(tty_interface_t *state, const char *argv) { (void)state; (void)argv; } -static void action_select(tty_interface_t *state, const char *argv) { +void action_select(tty_interface_t *state, const char *argv) { (void)argv; int done = entries_select(state->entries); @@ -106,25 +106,25 @@ static void action_select(tty_interface_t *state, const char *argv) { } } -static void action_parent(tty_interface_t *state, const char *argv) { +void action_parent(tty_interface_t *state, const char *argv) { (void)argv; entries_parent(state->entries); draw(state); } -static void action_prev(tty_interface_t *state, const char *argv) { +void action_prev(tty_interface_t *state, const char *argv) { (void)argv; entries_prev(state->entries); draw(state); } -static void action_next(tty_interface_t *state, const char *argv) { +void action_next(tty_interface_t *state, const char *argv) { (void)argv; entries_next(state->entries); draw(state); } -static void action_halfpageup(tty_interface_t *state, const char *argv) { +void action_halfpageup(tty_interface_t *state, const char *argv) { (void)argv; for (size_t i = 0; i < (state->options->num_files / 2) && state->entries->selection > 0; i++) entries_prev(state->entries); @@ -132,7 +132,7 @@ static void action_halfpageup(tty_interface_t *state, const char *argv) { draw(state); } -static void action_halfpagedown(tty_interface_t *state, const char *argv) { +void action_halfpagedown(tty_interface_t *state, const char *argv) { (void)argv; for (size_t i = 0; i < (state->options->num_files / 2) && state->entries->selection < state->entries->size - 1; i++) entries_next(state->entries); @@ -140,7 +140,7 @@ static void action_halfpagedown(tty_interface_t *state, const char *argv) { draw(state); } -static void action_pageup(tty_interface_t *state, const char *argv) { +void action_pageup(tty_interface_t *state, const char *argv) { (void)argv; for (size_t i = 0; i < state->options->num_files && state->entries->selection > 0; i++) entries_prev(state->entries); @@ -148,7 +148,7 @@ static void action_pageup(tty_interface_t *state, const char *argv) { draw(state); } -static void action_pagedown(tty_interface_t *state, const char *argv) { +void action_pagedown(tty_interface_t *state, const char *argv) { (void)argv; for (size_t i = 0; i < state->options->num_files && state->entries->selection < state->entries->size - 1; i++) entries_next(state->entries); @@ -156,31 +156,31 @@ static void action_pagedown(tty_interface_t *state, const char *argv) { draw(state); } -static void action_first(tty_interface_t *state, const char *argv) { +void action_first(tty_interface_t *state, const char *argv) { (void)argv; entries_position(state->entries, 0); draw(state); } -static void action_last(tty_interface_t *state, const char *argv) { +void action_last(tty_interface_t *state, const char *argv) { (void)argv; entries_position(state->entries, state->entries->size - 1); draw(state); } -static void action_home(tty_interface_t *state, const char *argv) { +void action_home(tty_interface_t *state, const char *argv) { (void)argv; entries_setpath(state->entries, getenv("HOME")); draw(state); } -static void action_togglehidden(tty_interface_t *state, const char *argv) { +void action_togglehidden(tty_interface_t *state, const char *argv) { (void)argv; entries_togglehidden(); draw(state); } -static void action_run(tty_interface_t *state, const char *argv) { +void action_run(tty_interface_t *state, const char *argv) { (void)argv; const struct entry *entry = entries_item(state->entries, state->entries->selection); @@ -204,7 +204,7 @@ static void action_run(tty_interface_t *state, const char *argv) { draw(state); } -static void action_setpath(tty_interface_t *state, const char *argv) { +void action_setpath(tty_interface_t *state, const char *argv) { size_t input_len = strlen(argv) + PATH_MAX; char *input = malloc(input_len); @@ -226,7 +226,7 @@ static void action_setpath(tty_interface_t *state, const char *argv) { draw(state); } -static void action_exit(tty_interface_t *state, const char *argv) { +void action_exit(tty_interface_t *state, const char *argv) { (void)argv; clear(state); tty_close(state->tty); @@ -249,49 +249,6 @@ void tty_interface_init(tty_interface_t *state, tty_t *tty, entries_t *entries, draw(state); } -typedef struct { - const char *key; - void (*action)(tty_interface_t *, const char *); - const char *argv; -} keybinding_t; - -#define KEY(key) ((const char[]){key, '\0'}) -#define KEY_CTRL(key) ((const char[]){((key) - ('@')), '\0'}) - -static const keybinding_t keybindings[] = { - {"\x1b", action_exit, NULL}, /* ESC */ - {KEY_CTRL('C'), action_exit, NULL}, /* C-C */ - {KEY_CTRL('M'), action_select, NULL}, /* CR */ - {KEY_CTRL('P'), action_prev,NULL}, /* C-P */ - {KEY_CTRL('N'), action_next,NULL}, /* C-N */ - {KEY_CTRL('K'), action_prev,NULL}, /* C-K */ - {KEY_CTRL('J'), action_next,NULL}, /* C-J */ - {KEY_CTRL('U'), action_halfpageup,NULL}, /* C-U */ - {KEY_CTRL('D'), action_halfpagedown,NULL}, /* C-D */ - {KEY('g'), action_first,NULL}, /* g */ - {KEY('G'), action_last,NULL}, /* G */ - {KEY('~'), action_home,NULL}, /* ~ */ - {KEY('.'), action_togglehidden,NULL}, /* . */ - {KEY('p'), action_run, "less %s/%s"}, /* p (preview) */ - {KEY('t'), action_run, "echo \"create 'vis %s/%s'\" > $DVTM_CMD_FIFO"}, /* edit new tab */ - {KEY('f'), action_setpath, "find %s | fzf"}, /* s (search) */ - {KEY('-'), action_parent,NULL}, /* - */ - {"\x1bOD", action_parent,NULL}, /* LEFT */ - {"\x1b[D", action_parent,NULL}, /* LEFT */ - {"\x1bOC", action_select,NULL}, /* RIGHT */ - {"\x1b[C", action_select,NULL}, /* RIGHT */ - {"\x1b[A", action_prev,NULL}, /* UP */ - {"\x1bOA", action_prev,NULL}, /* UP */ - {"\x1b[B", action_next,NULL}, /* DOWN */ - {"\x1bOB", action_next,NULL}, /* DOWN */ - {"\x1b[5~", action_pageup,NULL}, - {"\x1b[6~", action_pagedown,NULL}, - {"\x1b[200~", action_ignore,NULL}, - {"\x1b[201~", action_ignore,NULL}, - {NULL, NULL, NULL}}; - -#undef KEY_CTRL - static void handle_input(tty_interface_t *state, const char *s, int handle_ambiguous_key) { state->ambiguous_key_pending = 0; diff --git a/tty_interface.h b/tty_interface.h @@ -17,4 +17,21 @@ typedef struct { void tty_interface_init(tty_interface_t *state, tty_t *tty, entries_t *entries, options_t *options); int tty_interface_run(tty_interface_t *state); +void action_ignore(tty_interface_t *state, const char *argv); +void action_select(tty_interface_t *state, const char *argv); +void action_parent(tty_interface_t *state, const char *argv); +void action_prev(tty_interface_t *state, const char *argv); +void action_next(tty_interface_t *state, const char *argv); +void action_halfpageup(tty_interface_t *state, const char *argv); +void action_halfpagedown(tty_interface_t *state, const char *argv); +void action_pageup(tty_interface_t *state, const char *argv); +void action_pagedown(tty_interface_t *state, const char *argv); +void action_first(tty_interface_t *state, const char *argv); +void action_last(tty_interface_t *state, const char *argv); +void action_home(tty_interface_t *state, const char *argv); +void action_togglehidden(tty_interface_t *state, const char *argv); +void action_run(tty_interface_t *state, const char *argv); +void action_setpath(tty_interface_t *state, const char *argv); +void action_exit(tty_interface_t *state, const char *argv); + #endif