commit 31f39b1b2d6464dc59b5b45c7883073e765e3589
parent 00593807c8609425c47bc9052360bb945a4327b1
Author: Louis Burda <quent.burda@gmail.com>
Date: Fri, 14 Jun 2024 02:11:42 +0200
Add run flag to run program on select
Diffstat:
M | tquery.c | | | 49 | +++++++++++++++++++++++++++++++++++++++++++++---- |
1 file changed, 45 insertions(+), 4 deletions(-)
diff --git a/tquery.c b/tquery.c
@@ -3,7 +3,6 @@
#include <sys/wait.h>
#include <sys/poll.h>
#include <sys/fcntl.h>
-#include <limits.h>
#include <signal.h>
#include <dirent.h>
#include <unistd.h>
@@ -42,6 +41,10 @@ static char hook_key_arg[6] = { 0 };
static char *hook_argv[4] = { NULL, hook_key_arg, NULL, NULL };
static bool hook_io = false;
+static char *run_argv[4] = { NULL, NULL, NULL };
+static bool run_cmd = false;
+static bool run_io = false;
+
static char *loadbuf = NULL;
static char *output = NULL;
@@ -297,7 +300,7 @@ spawn(const char *query)
argv[argc++] = strndup(tok, (size_t) (end - tok));
tok = ntok + 1;
}
- } else if (argc < 64 && query && *query) {
+ } else if (argc < 64 && query) {
argv[argc++] = query;
}
@@ -316,6 +319,33 @@ spawn(const char *query)
}
static void
+run(char *input)
+{
+ if (run_io) {
+ def_prog_mode();
+ endwin();
+ }
+
+ int status, pid;
+ run_argv[1] = input;
+ invoke((const char **)run_argv, &pid, NULL, run_io, run_io, errlog);
+ do {
+ int rc = waitpid(pid, &status, 0);
+ if (rc != pid) die("waitpid:");
+ } while (!WIFEXITED(status) && !WIFSIGNALED(status));
+
+ if (oneshot) exit(0);
+
+ if (run_io) {
+ reset_prog_mode();
+ }
+
+ if (WIFEXITED(status) && WEXITSTATUS(status) == 3)
+ spawn(inputbuf);
+ update();
+}
+
+static void
hook(uint8_t key, const char *line)
{
int status, rc;
@@ -415,8 +445,12 @@ input(void)
break;
case '\n':
if (output_nav.max > 0 && output_nav.sel >= 0) {
- puts(entry((size_t) output_nav.sel));
- if (oneshot) exit(0);
+ if (run_cmd) {
+ run(entry((size_t) output_nav.sel));
+ } else {
+ puts(entry((size_t) output_nav.sel));
+ if (oneshot) exit(0);
+ }
}
break;
default:
@@ -481,6 +515,13 @@ parse(int argc, char **argv)
split_args = true;
} else if (!strcmp(*arg, "-d") || !strcmp(*arg, "--delim")) {
delim = **++arg;
+ } else if (!strcmp(*arg, "-r") || !strcmp(*arg, "--run")) {
+ run_argv[0] = *++arg;
+ run_cmd = true;
+ } else if (!strcmp(*arg, "-R") || !strcmp(*arg, "--run-io")) {
+ run_argv[0] = *++arg;
+ run_cmd = true;
+ run_io = true;
} else if (!strcmp(*arg, "-x") || !strcmp(*arg, "--hook")) {
hook_argv[0] = *++arg;
} else if (!strcmp(*arg, "-X") || !strcmp(*arg, "--hook-io")) {