fe
terminal file explorer and picker
git clone https://9o.is/git/fe.git
commit 7d7ce9facbf8e50672ea54217bcdd47be6b94c9c parent 4882dc745c37072a2e2afcbfc853b89c23e0fc13 Author: Jul <jul@9o.is> Date: Mon, 19 Jan 2026 01:20:44 -0500 move actions to separate file Diffstat:
| M | Makefile | | | 1 | + |
| A | actions.c | | | 177 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | actions.h | | | 16 | ++++++++++++++++ |
| M | config.def.h | | | 1 | + |
| M | tty_interface.c | | | 183 | +------------------------------------------------------------------------------ |
| M | tty_interface.h | | | 18 | +----------------- |
6 files changed, 197 insertions(+), 199 deletions(-)
diff --git a/Makefile b/Makefile @@ -19,6 +19,7 @@ OBJS = fe.o \ entries.o \ tty_interface.o \ tty.o \ + actions.o \ spawn.o \ compats.o diff --git a/actions.c b/actions.c @@ -0,0 +1,177 @@ +#include <stdlib.h> +#include <string.h> +#include "spawn.h" + +static void clear(tty_interface_t *state) { + tty_t *tty = state->tty; + + tty_setcol(tty, 0); + size_t line = 0; + while (line++ < state->options->num_files) { + tty_newline(tty); + } + tty_clearline(tty); + if (state->options->num_files > 0) { + tty_moveup(tty, line - 1); + } + tty_unhide_cursor(tty); + tty_title(tty, ""); + tty_flush(tty); +} + +void action_ignore(tty_interface_t *state, const char *argv) { + (void)state; + (void)argv; +} + +void action_select(tty_interface_t *state, const char *argv) { + (void)argv; + int done = entries_select(state->entries); + + if (!done) { + draw(state); + } else { + clear(state); + tty_close(state->tty); + const struct entry *selection = entries_selected(state->entries); + + if (state->options->run == NULL) { + printf("%s/%s\n", state->entries->path, selection->name); + state->exit = EXIT_SUCCESS; + } else { + char fullpath[2*PATH_MAX]; + snprintf(fullpath, sizeof(fullpath), "%s/%s", state->entries->path, selection->name); + + char *const sargv[] = {(char *)state->options->run, fullpath, NULL}; + x_spawn(state->options->run, sargv, EXEC_REPLACE, NULL, 0); + state->exit = EXIT_FAILURE; + } + } +} + +void action_parent(tty_interface_t *state, const char *argv) { + (void)argv; + entries_parent(state->entries); + draw(state); +} + +void action_prev(tty_interface_t *state, const char *argv) { + (void)argv; + entries_prev(state->entries); + draw(state); +} + +void action_next(tty_interface_t *state, const char *argv) { + (void)argv; + entries_next(state->entries); + draw(state); +} + +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); + + draw(state); +} + +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); + + draw(state); +} + +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); + + draw(state); +} + +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); + + draw(state); +} + +void action_first(tty_interface_t *state, const char *argv) { + (void)argv; + entries_position(state->entries, 0); + draw(state); +} + +void action_last(tty_interface_t *state, const char *argv) { + (void)argv; + entries_position(state->entries, state->entries->size - 1); + draw(state); +} + +void action_home(tty_interface_t *state, const char *argv) { + (void)argv; + entries_setpath(state->entries, getenv("HOME")); + draw(state); +} + +void action_togglehidden(tty_interface_t *state, const char *argv) { + (void)argv; + entries_togglehidden(); + draw(state); +} + +void action_run(tty_interface_t *state, const char *argv) { + (void)argv; + + const struct entry *entry = entries_item(state->entries, state->entries->selection); + if (!entry) return; + + size_t input_len = strlen(argv) + PATH_MAX; + char *input = malloc(input_len); + + if (input == NULL) { + perror("Failed to allocate memory"); + return; + } + + snprintf(input, input_len, argv, state->entries->path, entry->name); + + clear(state); + x_spawn(input, NULL, EXEC_SHELL, NULL, 0); + + free(input); + tty_hide_cursor(state->tty); + draw(state); +} + +void action_setpath(tty_interface_t *state, const char *argv) { + size_t input_len = strlen(argv) + PATH_MAX; + char *input = malloc(input_len); + + if (input == NULL) { + perror("Failed to allocate memory"); + return; + } + + snprintf(input, input_len, argv, state->entries->path); + + char output[PATH_MAX]; + clear(state); + + if (0 == x_spawn(input, NULL, EXEC_SHELL | EXEC_CAPTURE, output, sizeof(output))) + entries_setpath(state->entries, output); + + free(input); + tty_hide_cursor(state->tty); + draw(state); +} + +void action_exit(tty_interface_t *state, const char *argv) { + (void)argv; + clear(state); + tty_close(state->tty); + state->exit = EXIT_FAILURE; +} diff --git a/actions.h b/actions.h @@ -0,0 +1,16 @@ +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); diff --git a/config.def.h b/config.def.h @@ -1,5 +1,6 @@ #include "tty.h" #include "tty_interface.h" +#include "actions.h" #define COLOR_DIRECTORY TTY_COLOR_BLUE #define COLOR_LINK TTY_COLOR_CYAN diff --git a/tty_interface.c b/tty_interface.c @@ -1,30 +1,6 @@ -#include <ctype.h> -#include <stdio.h> -#include <stdlib.h> #include <string.h> -#include <math.h> -#include "tty_interface.h" -#include "spawn.h" - -static void clear(tty_interface_t *state) { - tty_t *tty = state->tty; - - tty_setcol(tty, 0); - size_t line = 0; - while (line++ < state->options->num_files) { - tty_newline(tty); - } - tty_clearline(tty); - if (state->options->num_files > 0) { - tty_moveup(tty, line - 1); - } - tty_unhide_cursor(tty); - tty_title(tty, ""); - tty_flush(tty); -} - -static void draw(tty_interface_t *state) { +void draw(tty_interface_t *state) { tty_t *tty = state->tty; entries_t *entries = state->entries; const options_t *options = state->options; @@ -76,163 +52,6 @@ static void draw(tty_interface_t *state) { tty_flush(tty); } -void action_ignore(tty_interface_t *state, const char *argv) { - (void)state; - (void)argv; -} - -void action_select(tty_interface_t *state, const char *argv) { - (void)argv; - int done = entries_select(state->entries); - - if (!done) { - draw(state); - } else { - clear(state); - tty_close(state->tty); - const struct entry *selection = entries_selected(state->entries); - - if (state->options->run == NULL) { - printf("%s/%s\n", state->entries->path, selection->name); - state->exit = EXIT_SUCCESS; - } else { - char fullpath[2*PATH_MAX]; - snprintf(fullpath, sizeof(fullpath), "%s/%s", state->entries->path, selection->name); - - char *const sargv[] = {(char *)state->options->run, fullpath, NULL}; - x_spawn(state->options->run, sargv, EXEC_REPLACE, NULL, 0); - state->exit = EXIT_FAILURE; - } - } -} - -void action_parent(tty_interface_t *state, const char *argv) { - (void)argv; - entries_parent(state->entries); - draw(state); -} - -void action_prev(tty_interface_t *state, const char *argv) { - (void)argv; - entries_prev(state->entries); - draw(state); -} - -void action_next(tty_interface_t *state, const char *argv) { - (void)argv; - entries_next(state->entries); - draw(state); -} - -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); - - draw(state); -} - -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); - - draw(state); -} - -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); - - draw(state); -} - -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); - - draw(state); -} - -void action_first(tty_interface_t *state, const char *argv) { - (void)argv; - entries_position(state->entries, 0); - draw(state); -} - -void action_last(tty_interface_t *state, const char *argv) { - (void)argv; - entries_position(state->entries, state->entries->size - 1); - draw(state); -} - -void action_home(tty_interface_t *state, const char *argv) { - (void)argv; - entries_setpath(state->entries, getenv("HOME")); - draw(state); -} - -void action_togglehidden(tty_interface_t *state, const char *argv) { - (void)argv; - entries_togglehidden(); - draw(state); -} - -void action_run(tty_interface_t *state, const char *argv) { - (void)argv; - - const struct entry *entry = entries_item(state->entries, state->entries->selection); - if (!entry) return; - - size_t input_len = strlen(argv) + PATH_MAX; - char *input = malloc(input_len); - - if (input == NULL) { - perror("Failed to allocate memory"); - return; - } - - snprintf(input, input_len, argv, state->entries->path, entry->name); - - clear(state); - x_spawn(input, NULL, EXEC_SHELL, NULL, 0); - - free(input); - tty_hide_cursor(state->tty); - draw(state); -} - -void action_setpath(tty_interface_t *state, const char *argv) { - size_t input_len = strlen(argv) + PATH_MAX; - char *input = malloc(input_len); - - if (input == NULL) { - perror("Failed to allocate memory"); - return; - } - - snprintf(input, input_len, argv, state->entries->path); - - char output[PATH_MAX]; - clear(state); - - if (0 == x_spawn(input, NULL, EXEC_SHELL | EXEC_CAPTURE, output, sizeof(output))) - entries_setpath(state->entries, output); - - free(input); - tty_hide_cursor(state->tty); - draw(state); -} - -void action_exit(tty_interface_t *state, const char *argv) { - (void)argv; - clear(state); - tty_close(state->tty); - state->exit = EXIT_FAILURE; -} - void tty_interface_init(tty_interface_t *state, tty_t *tty, entries_t *entries, options_t *options) { if (options->num_files + 1 > tty_getheight(tty)) { options->num_files = tty_getheight(tty) - 3; diff --git a/tty_interface.h b/tty_interface.h @@ -16,22 +16,6 @@ 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); +void draw(tty_interface_t *state); #endif