sites

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

dmenu-dynamicoptions-20200526-9585ad8.diff (4723B)


      1 From 2b7c611d792da9653e3dbb284f7624ce46bc9f65 Mon Sep 17 00:00:00 2001
      2 From: Tiago Teles <tiago.sequeira.teles@gmail.com>
      3 Date: Tue, 26 May 2020 11:56:29 +0100
      4 Subject: [PATCH 1/2] '-dy commandhere' option added, where said command will
      5  be ran every time input changes, with the current output as the first
      6  argumentand dmenu options will be changed accordingly
      7 
      8 ---
      9  config.def.h |  1 +
     10  dmenu.c      | 38 +++++++++++++++++++++++++++++++-------
     11  2 files changed, 32 insertions(+), 7 deletions(-)
     12 
     13 diff --git a/config.def.h b/config.def.h
     14 index 1edb647..035b877 100644
     15 --- a/config.def.h
     16 +++ b/config.def.h
     17 @@ -7,6 +7,7 @@ static const char *fonts[] = {
     18  	"monospace:size=10"
     19  };
     20  static const char *prompt      = NULL;      /* -p  option; prompt to the left of input field */
     21 +static const char *dynamic     = NULL;      /* -dy option; dynamic command to run on input change */
     22  static const char *colors[SchemeLast][2] = {
     23  	/*     fg         bg       */
     24  	[SchemeNorm] = { "#bbbbbb", "#222222" },
     25 diff --git a/dmenu.c b/dmenu.c
     26 index 6b8f51b..6a0eb01 100644
     27 --- a/dmenu.c
     28 +++ b/dmenu.c
     29 @@ -210,9 +210,28 @@ grabkeyboard(void)
     30  	die("cannot grab keyboard");
     31  }
     32  
     33 +static void readstdin(FILE* stream);
     34 +
     35 +static void
     36 +refreshoptions(){
     37 +  int dynlen = strlen(dynamic);
     38 +  char cmd[dynlen + strlen(text)];
     39 +  strcpy(cmd, dynamic);
     40 +  cmd[dynlen] = ' ';
     41 +  strcpy(&cmd[dynlen] + 1, text);
     42 +  FILE *stream = popen(cmd, "r");
     43 +  readstdin(stream);
     44 +  pclose(stream);
     45 +  curr = sel = items;
     46 +}
     47 +
     48  static void
     49  match(void)
     50  {
     51 +	if(dynamic && *dynamic){
     52 +		refreshoptions();
     53 +	}
     54 +
     55  	static char **tokv = NULL;
     56  	static int tokn = 0;
     57  
     58 @@ -234,7 +253,7 @@ match(void)
     59  		for (i = 0; i < tokc; i++)
     60  			if (!fstrstr(item->text, tokv[i]))
     61  				break;
     62 -		if (i != tokc) /* not all tokens match */
     63 +		if (i != tokc && !(dynamic && *dynamic)) /* not all tokens match */
     64  			continue;
     65  		/* exact matches go first, then prefixes, then substrings */
     66  		if (!tokc || !fstrncmp(text, item->text, textsize))
     67 @@ -519,14 +538,14 @@ paste(void)
     68  }
     69  
     70  static void
     71 -readstdin(void)
     72 +readstdin(FILE* stream)
     73  {
     74  	char buf[sizeof text], *p;
     75  	size_t i, imax = 0, size = 0;
     76  	unsigned int tmpmax = 0;
     77  
     78  	/* read each line from stdin and add it to the item list */
     79 -	for (i = 0; fgets(buf, sizeof buf, stdin); i++) {
     80 +	for (i = 0; fgets(buf, sizeof buf, stream); i++) {
     81  		if (i + 1 >= size / sizeof *items)
     82  			if (!(items = realloc(items, (size += BUFSIZ))))
     83  				die("cannot realloc %u bytes:", size);
     84 @@ -544,7 +563,8 @@ readstdin(void)
     85  	if (items)
     86  		items[i].text = NULL;
     87  	inputw = items ? TEXTW(items[imax].text) : 0;
     88 -	lines = MIN(lines, i);
     89 +	if (!dynamic || !*dynamic)
     90 +		lines = MIN(lines, i);
     91  }
     92  
     93  static void
     94 @@ -683,7 +703,7 @@ static void
     95  usage(void)
     96  {
     97  	fputs("usage: dmenu [-bfiv] [-l lines] [-p prompt] [-fn font] [-m monitor]\n"
     98 -	      "             [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]\n", stderr);
     99 +	      "             [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]\n" "[-dy command]\n", stderr);
    100  	exit(1);
    101  }
    102  
    103 @@ -726,6 +746,8 @@ main(int argc, char *argv[])
    104  			colors[SchemeSel][ColFg] = argv[++i];
    105  		else if (!strcmp(argv[i], "-w"))   /* embedding window id */
    106  			embed = argv[++i];
    107 +		else if (!strcmp(argv[i], "-dy"))  /* dynamic command to run */
    108 +			dynamic = argv[++i];
    109  		else
    110  			usage();
    111  
    112 @@ -754,9 +776,11 @@ main(int argc, char *argv[])
    113  
    114  	if (fast && !isatty(0)) {
    115  		grabkeyboard();
    116 -		readstdin();
    117 +		if(!(dynamic && *dynamic))
    118 +			readstdin(stdin);
    119  	} else {
    120 -		readstdin();
    121 +		if(!(dynamic && *dynamic))
    122 +			readstdin(stdin);
    123  		grabkeyboard();
    124  	}
    125  	setup();
    126 -- 
    127 2.26.2
    128 
    129 
    130 From 9585ad8921f5fa17c9da0d613ce5fbedf98e18ef Mon Sep 17 00:00:00 2001
    131 From: Tiago Teles <tiago.sequeira.teles@gmail.com>
    132 Date: Tue, 26 May 2020 17:08:58 +0100
    133 Subject: [PATCH 2/2] slight modifications and errors added
    134 
    135 ---
    136  dmenu.c | 21 ++++++++++++---------
    137  1 file changed, 12 insertions(+), 9 deletions(-)
    138 
    139 diff --git a/dmenu.c b/dmenu.c
    140 index 6a0eb01..b7798e7 100644
    141 --- a/dmenu.c
    142 +++ b/dmenu.c
    143 @@ -214,15 +214,18 @@ static void readstdin(FILE* stream);
    144  
    145  static void
    146  refreshoptions(){
    147 -  int dynlen = strlen(dynamic);
    148 -  char cmd[dynlen + strlen(text)];
    149 -  strcpy(cmd, dynamic);
    150 -  cmd[dynlen] = ' ';
    151 -  strcpy(&cmd[dynlen] + 1, text);
    152 -  FILE *stream = popen(cmd, "r");
    153 -  readstdin(stream);
    154 -  pclose(stream);
    155 -  curr = sel = items;
    156 +	int dynlen = strlen(dynamic);
    157 +	char* cmd= malloc(dynlen + strlen(text)+2);
    158 +	sprintf(cmd,"%s %s",dynamic, text);
    159 +	FILE *stream = popen(cmd, "r");
    160 +	if(!stream)
    161 +		die("popen(%s):",cmd);
    162 +	readstdin(stream);
    163 +	int pc = pclose(stream);
    164 +	if(pc == -1)
    165 +		die("pclose:");
    166 +	free(cmd);
    167 +	curr = sel = items;
    168  }
    169  
    170  static void
    171 -- 
    172 2.26.2
    173