summaryrefslogtreecommitdiff
path: root/patches/dmenu-separator-20210904-d78ff08.diff
blob: 25e1fb3e02c188ff0a7aec77ef4228be09051b6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
diff --git a/dmenu.1 b/dmenu.1
index f9e0db7..af3a6f5 100644
--- a/dmenu.1
+++ b/dmenu.1
@@ -22,6 +22,10 @@ dmenu \- dynamic menu
 .IR color ]
 .RB [ \-w
 .IR windowid ]
+.RB [ \-d
+.IR separator ]
+.RB [ \-D
+.IR separator ]
 .P
 .BR dmenu_run " ..."
 .SH DESCRIPTION
@@ -83,6 +87,14 @@ prints version information to stdout, then exits.
 .TP
 .BI \-w " windowid"
 embed into windowid.
+.TP
+.BI \-d " separator"
+separate the input into two halves on the first occurrence of the given charcter.
+Display only the first half in dmenu and print the second half to stdout upon selection.
+Appending '|' to the separator reverses the display/printing order.
+.TP
+.BI \-D " separator"
+same as \-d but separate based on the last occurrence.
 .SH USAGE
 dmenu is completely controlled by the keyboard.  Items are selected using the
 arrow keys, page up, page down, home, and end.
diff --git a/dmenu.c b/dmenu.c
index 98507d9..2ac2c9a 100644
--- a/dmenu.c
+++ b/dmenu.c
@@ -30,12 +30,16 @@ enum { SchemeNorm, SchemeSel, SchemeOut, SchemeLast }; /* color schemes */
 
 struct item {
 	char *text;
+	char *text_output;
 	struct item *left, *right;
 	int out;
 };
 
 static char text[BUFSIZ] = "";
 static char *embed;
+static char separator;
+static int separator_greedy;
+static int separator_reverse;
 static int bh, mw, mh;
 static int inputw = 0, promptw;
 static int lrpad; /* sum of left and right padding */
@@ -473,7 +477,7 @@ insert:
 		break;
 	case XK_Return:
 	case XK_KP_Enter:
-		puts((sel && !(ev->state & ShiftMask)) ? sel->text : text);
+		puts((sel && !(ev->state & ShiftMask)) ? sel->text_output : text);
 		if (!(ev->state & ControlMask)) {
 			cleanup();
 			exit(0);
@@ -545,6 +549,18 @@ readstdin(void)
 			*p = '\0';
 		if (!(items[i].text = strdup(buf)))
 			die("cannot strdup %u bytes:", strlen(buf) + 1);
+		if (separator && (p = (separator_greedy) ?
+			strrchr(items[i].text, separator) : strchr(items[i].text, separator))) {
+			*p = '\0';
+			items[i].text_output = ++p;
+		} else {
+			items[i].text_output = items[i].text;
+		}
+		if (separator_reverse) {
+			char *tmp = items[i].text;
+			items[i].text = items[i].text_output;
+			items[i].text_output = tmp;
+		}
 		items[i].out = 0;
 		drw_font_getexts(drw->fonts, buf, strlen(buf), &tmpmax, NULL);
 		if (tmpmax > inputw) {
@@ -700,8 +716,9 @@ setup(void)
 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);
+	fputs("usage: dmenu [-bfinv] [-l lines] [-p prompt] [-fn font] [-m monitor]\n"
+	      "             [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]\n"
+	      "             [-d separator] [-D separator]\n", stderr);
 	exit(1);
 }
 
@@ -744,6 +761,11 @@ 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], "-d") || !strcmp(argv[i], "-D")) { /* field separator */
+			separator_reverse = (*(argv[i+1]+1) == '|');
+			separator_greedy = !strcmp(argv[i], "-D");
+			separator = *argv[++i];
+		}
 		else
 			usage();