commit: c3858adfb40ec7755caa418b4d93d28a2ee25c75
parent: 7f7712d66a81dd3dd9579d15987312de3ae82276
author: Chris Noxz <chris@noxz.tech>
date: Wed, 14 Dec 2022 10:52:33 +0100
Add ability to return selected index
1 file changed, 11 insertions(+), 3 deletions(-)
diff --git a/dmenu.c b/dmenu.c
@@ -34,6 +34,7 @@ struct item {
char *text;
struct item *left, *right;
int out;
+ int idx;
double distance;
};
@@ -48,6 +49,7 @@ static struct item *items = NULL;
static struct item *matches, *matchend;
static struct item *prev, *curr, *next, *sel;
static int mon = -1, screen;
+static int print_idx;
static Atom clip, utf8;
static Display *dpy;
@@ -633,7 +635,10 @@ insert:
break;
case XK_Return:
case XK_KP_Enter:
- puts((sel && !(ev->state & ShiftMask)) ? sel->text : text);
+ if (print_idx)
+ printf("%d\n", (sel && !(ev->state & ShiftMask)) ? sel->idx : -1);
+ else
+ puts((sel && !(ev->state & ShiftMask)) ? sel->text : text);
if (!(ev->state & ControlMask)) {
cleanup();
exit(0);
@@ -711,6 +716,7 @@ readstdin(void)
if (!(items[i].text = strdup(buf)))
die("cannot strdup %u bytes:", strlen(buf) + 1);
items[i].out = 0;
+ items[i].idx = i;
drw_font_getexts(drw->fonts, buf, strlen(buf), &tmpmax, NULL);
if (tmpmax > inputw) {
inputw = tmpmax;
@@ -893,9 +899,11 @@ main(int argc, char *argv[])
else if (!strcmp(argv[i], "-i")) { /* case-insensitive item matching */
fstrncmp = strncasecmp;
fstrstr = cistrstr;
- } else if (!strcmp(argv[i], "-P")) {
+ } else if (!strcmp(argv[i], "-P")) /* doesn't show typed text */
passwd = 1;
- } else if (i + 1 == argc)
+ else if (!strcmp(argv[i], "-I")) /* returns selected index */
+ print_idx = 1;
+ else if (i + 1 == argc)
usage();
/* these options take one argument */
else if (!strcmp(argv[i], "-l")) /* number of lines in vertical list */