[hackers] [dmenu][PATCH] '-dy commandhere' option added, where said command will be ran every time input changes, with the current output as the first argumentand dmenu options will be changed accordingly

From: Tiago Teles <tiago.sequeira.teles_AT_gmail.com>
Date: Tue, 26 May 2020 13:47:14 +0100

---
 config.def.h |  1 +
 dmenu.c      | 38 +++++++++++++++++++++++++++++++-------
 2 files changed, 32 insertions(+), 7 deletions(-)
diff --git a/config.def.h b/config.def.h
index 1edb647..035b877 100644
--- a/config.def.h
+++ b/config.def.h
_AT_@ -7,6 +7,7 @@ static const char *fonts[] = {
 	"monospace:size=10"
 };
 static const char *prompt      = NULL;      /* -p  option; prompt to the left of input field */
+static const char *dynamic     = NULL;      /* -dy option; dynamic command to run on input change */
 static const char *colors[SchemeLast][2] = {
 	/*     fg         bg       */
 	[SchemeNorm] = { "#bbbbbb", "#222222" },
diff --git a/dmenu.c b/dmenu.c
index 6b8f51b..6a0eb01 100644
--- a/dmenu.c
+++ b/dmenu.c
_AT_@ -210,9 +210,28 @@ grabkeyboard(void)
 	die("cannot grab keyboard");
 }
 
+static void readstdin(FILE* stream);
+
+static void
+refreshoptions(){
+  int dynlen = strlen(dynamic);
+  char cmd[dynlen + strlen(text)];
+  strcpy(cmd, dynamic);
+  cmd[dynlen] = ' ';
+  strcpy(&cmd[dynlen] + 1, text);
+  FILE *stream = popen(cmd, "r");
+  readstdin(stream);
+  pclose(stream);
+  curr = sel = items;
+}
+
 static void
 match(void)
 {
+	if(dynamic && *dynamic){
+		refreshoptions();
+	}
+
 	static char **tokv = NULL;
 	static int tokn = 0;
 
_AT_@ -234,7 +253,7 @@ match(void)
 		for (i = 0; i < tokc; i++)
 			if (!fstrstr(item->text, tokv[i]))
 				break;
-		if (i != tokc) /* not all tokens match */
+		if (i != tokc && !(dynamic && *dynamic)) /* not all tokens match */
 			continue;
 		/* exact matches go first, then prefixes, then substrings */
 		if (!tokc || !fstrncmp(text, item->text, textsize))
_AT_@ -519,14 +538,14 @@ paste(void)
 }
 
 static void
-readstdin(void)
+readstdin(FILE* stream)
 {
 	char buf[sizeof text], *p;
 	size_t i, imax = 0, size = 0;
 	unsigned int tmpmax = 0;
 
 	/* read each line from stdin and add it to the item list */
-	for (i = 0; fgets(buf, sizeof buf, stdin); i++) {
+	for (i = 0; fgets(buf, sizeof buf, stream); i++) {
 		if (i + 1 >= size / sizeof *items)
 			if (!(items = realloc(items, (size += BUFSIZ))))
 				die("cannot realloc %u bytes:", size);
_AT_@ -544,7 +563,8 @@ readstdin(void)
 	if (items)
 		items[i].text = NULL;
 	inputw = items ? TEXTW(items[imax].text) : 0;
-	lines = MIN(lines, i);
+	if (!dynamic || !*dynamic)
+		lines = MIN(lines, i);
 }
 
 static void
_AT_@ -683,7 +703,7 @@ static void
 usage(void)
 {
 	fputs("usage: dmenu [-bfiv] [-l lines] [-p prompt] [-fn font] [-m monitor]\n"
-	      "             [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]\n", stderr);
+	      "             [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]\n" "[-dy command]\n", stderr);
 	exit(1);
 }
 
_AT_@ -726,6 +746,8 @@ main(int argc, char *argv[])
 			colors[SchemeSel][ColFg] = argv[++i];
 		else if (!strcmp(argv[i], "-w"))   /* embedding window id */
 			embed = argv[++i];
+		else if (!strcmp(argv[i], "-dy"))  /* dynamic command to run */
+			dynamic = argv[++i];
 		else
 			usage();
 
_AT_@ -754,9 +776,11 @@ main(int argc, char *argv[])
 
 	if (fast && !isatty(0)) {
 		grabkeyboard();
-		readstdin();
+		if(!(dynamic && *dynamic))
+			readstdin(stdin);
 	} else {
-		readstdin();
+		if(!(dynamic && *dynamic))
+			readstdin(stdin);
 		grabkeyboard();
 	}
 	setup();
-- 
2.26.2
Received on Tue May 26 2020 - 14:47:14 CEST

This archive was generated by hypermail 2.3.0 : Tue May 26 2020 - 15:00:38 CEST