fe

terminal file explorer and picker

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

commit 438a963fa83b8baced20b1f32f64d940426d3a10
parent 26ea0b8c37c551238e897c2709c09a96d1894ce0
Author: Jul <jul@9o.is>
Date:   Thu,  7 Aug 2025 23:50:05 -0400

add run flag

Diffstat:
Moptions.c | 8+++++++-
Moptions.h | 1+
Mspawn.c | 11+++++++++++
Mspawn.h | 1+
Mtty_interface.c | 14+++++++++++---
5 files changed, 31 insertions(+), 4 deletions(-)

diff --git a/options.c b/options.c @@ -12,6 +12,7 @@ static const char *usage_str = "Usage: fe [OPTION] [path]\n" " -n, --num-files=LINES Specify how many files to display\n" " -t, --tty=TTY Specify file to use as TTY device (default /dev/tty)\n" + " -r, --run=file Run program when file is selected\n" " -d, --sort-directory Sort by directories first\n" " -m, --sort-mtime Sort by time modified\n" " -i, --sort-icase Sort case insensitively\n" @@ -26,6 +27,7 @@ static void usage(const char *argv0) { static struct option longopts[] = { {"num-files", required_argument, NULL, 'l'}, {"tty", required_argument, NULL, 't'}, + {"run", required_argument, NULL, 'r'}, {"sort-directory", no_argument, NULL, 'd'}, {"sort-mtime", no_argument, NULL, 'm'}, {"sort-icase", no_argument, NULL, 'i'}, @@ -42,14 +44,18 @@ void options_parse(options_t *options, int argc, char *argv[]) { options->sort_mtime = SORT_MTIME; options->sort_icase = SORT_ICASE; options->show_hidden = SHOW_HIDDEN; + options->run = NULL; options->path = NULL; int c; - while ((c = getopt_long(argc, argv, "vhadmi:n:t:", longopts, NULL)) != -1) { + while ((c = getopt_long(argc, argv, "vhadmr:i:n:t:", longopts, NULL)) != -1) { switch (c) { case 't': options->tty_filename = optarg; break; + case 'r': + options->run = optarg; + break; case 'n': { int n; if (!strcmp(optarg, "max")) { diff --git a/options.h b/options.h @@ -8,6 +8,7 @@ extern "C" { typedef struct { unsigned int num_files; const char *tty_filename; + const char *run; const char *path; int sort_dir; int sort_icase; diff --git a/spawn.c b/spawn.c @@ -8,6 +8,17 @@ #include <stdio.h> #include <string.h> +void execute_program(const char *program, char *path) { + char *prog = strdup(program); + char *const argv[] = {prog, path, NULL}; + + if (execvp(prog, argv) == -1) { + perror("execvp failed"); + } + + free(prog); +} + int execute_command(const char *command) { pid_t pid; diff --git a/spawn.h b/spawn.h @@ -1,2 +1,3 @@ +void execute_program(const char *program, char *path); int execute_command_output(const char *command, char *output_buffer, size_t buffer_size); int execute_command(const char *command); diff --git a/tty_interface.c b/tty_interface.c @@ -92,8 +92,17 @@ static void action_select(tty_interface_t *state, char *argv) { clear(state); tty_close(state->tty); struct entry *selection = entries_selected(state->entries); - printf("%s/%s\n", state->entries->path, selection->name); - state->exit = EXIT_SUCCESS; + + 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); + + execute_program(state->options->run, fullpath); + state->exit = EXIT_FAILURE; + } } } @@ -234,7 +243,6 @@ void tty_interface_init(tty_interface_t *state, tty_t *tty, entries_t *entries, state->options = options; state->ambiguous_key_pending = 0; state->exit = -1; - strcpy(state->input, ""); tty_hide_cursor(tty);