sites

public wiki contents of suckless.org
git clone git://git.suckless.org/sites
Log | Files | Refs

dmenu-separator-20210904-d78ff08.diff (3089B)


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