dmenu-noxz

[fork] suckless dmenu - personal fork
git clone https://noxz.tech/git/dmenu-noxz.git
dmenu-noxz

commit: 1870969df21484059f080269cbfcf90aff563034
parent: 7de4c55668023b91770caae1601dcad1d5787a77
author: Chris Noxz <chris@noxz.tech>
date:   Fri, 15 Dec 2023 15:18:53 +0100
Ability to specify centering, border color and odd color
Mconfig.def.h10+-
Mdmenu.13+
Mdmenu.c97++++++++++++++------
3 files changed, 82 insertions(+), 28 deletions(-)
diff --git a/config.def.h b/config.def.h
@@ -1,8 +1,13 @@
 /* See LICENSE file for copyright and license details. */
 /* Default settings; can be overriden by command line. */
 
-static int topbar = 1;                      /* -b  option; if 0, dmenu appears at bottom     */
+static int centered = 0;                    /* -c option; if 0, dneby aooears at top or bottom */
 static int fuzzy = 1;                       /* -F  option; if 0, dmenu doesn't use fuzzy matching     */
+static int topbar = 1;                      /* -b  option; if 0, dmenu appears at bottom     */
+static int min_width = 800;                 /* the minimum width if centered */
+static int bw = 0;                          /* window border width */
+static int im = 2;                          /* input bottom margin */
+
 /* -fn option overrides fonts[0]; default X11 font or font set */
 static const char *fonts[] = {
 	"hack:size=9"
@@ -11,10 +16,13 @@ static const char *prompt      = NULL;      /* -p  option; prompt to the left of
 static const char *colors[SchemeLast][2] = {
 	/*                          fg         bg       */
 	[SchemeNorm]            = { "#bbbbbb", "#222222" },
+	[SchemeOdd]             = { "#bbbbbb", "#000000" },
 	[SchemeSel]             = { "#eeeeee", "#005577" },
 	[SchemeSelHighlight]    = { "#ffc978", "#005577" },
+	[SchemeOddHighlight]    = { "#ffc978", "#000000" },
 	[SchemeNormHighlight]   = { "#ffc978", "#222222" },
 	[SchemeOut]             = { "#000000", "#00ffff" },
+	[SchemeBorder]          = { "#000000", "#00ffff" },
 };
 /* -l option; if nonzero, dmenu uses vertical list with given number of lines */
 static unsigned int lines      = 0;
diff --git a/dmenu.1 b/dmenu.1
@@ -48,6 +48,9 @@ which lists programs in the user's $PATH and runs the result in their $SHELL.
 .B \-b
 dmenu appears at the bottom of the screen.
 .TP
+.B \-c
+dmenu appears centered on the screen.
+.TP
 .B \-f
 dmenu grabs the keyboard before reading stdin if not reading from a tty. This
 is faster, but will lock up X until stdin reaches end\-of\-file.
diff --git a/dmenu.c b/dmenu.c
@@ -26,8 +26,9 @@
 #define TEXTW(X)              (drw_fontset_getwidth(drw, (X)) + lrpad)
 
 /* enums */
-enum { SchemeNorm, SchemeSel, SchemeNormHighlight, SchemeSelHighlight,
-       SchemeOut, SchemeLast }; /* color schemes */
+enum { SchemeNorm, SchemeOdd, SchemeSel, SchemeNormHighlight,
+       SchemeOddHighlight, SchemeSelHighlight, SchemeOut, SchemeBorder,
+       SchemeLast }; /* color schemes */
 
 struct item {
 	char *text;
@@ -101,6 +102,15 @@ calcoffsets(void)
 			break;
 }
 
+static int
+max_textw(void)
+{
+	int len = 0;
+	for (struct item *item = items; item && item->text; item++)
+		len = MAX(TEXTW(item->text), len);
+	return len;
+}
+
 static void
 cleanup(void)
 {
@@ -136,7 +146,7 @@ cistrstr(const char *h, const char *n)
 }
 
 static void
-drawhighlights(struct item *item, int x, int y, int maxw)
+drawhighlights(struct item *item, int x, int y, int maxw, int n)
 {
 	int i, indent;
 	char *highlight;
@@ -145,9 +155,11 @@ drawhighlights(struct item *item, int x, int y, int maxw)
 	if (!(strlen(item->text) && strlen(text)))
 		return;
 
-	drw_setscheme(drw, scheme[item == sel
-	                   ? SchemeSelHighlight
-	                   : SchemeNormHighlight]);
+	drw_setscheme(drw, scheme[
+	                     item == sel ? SchemeSelHighlight
+	           /* TODO : item->out   ? SchemeOutHighlight*/
+	                   : n & 1       ? SchemeOddHighlight
+	                   :               SchemeNormHighlight]);
 	for (i = 0, highlight = item->text; *highlight && text[i];) {
 		if (!fstrncmp(&(*highlight), &text[i], 1)) {
 			/* get indentation */
@@ -174,18 +186,17 @@ drawhighlights(struct item *item, int x, int y, int maxw)
 }
 
 static int
-drawitem(struct item *item, int x, int y, int w)
+drawitem(struct item *item, int x, int y, int w, int n)
 {
 	int r;
-	if (item == sel)
-		drw_setscheme(drw, scheme[SchemeSel]);
-	else if (item->out)
-		drw_setscheme(drw, scheme[SchemeOut]);
-	else
-		drw_setscheme(drw, scheme[SchemeNorm]);
+	drw_setscheme(drw, scheme[
+	                     item == sel ? SchemeSel
+	                   : item->out   ? SchemeOut
+	                   : n & 1       ? SchemeOdd
+	                   :               SchemeNorm]);
 
 	r = drw_text(drw, x, y, w, bh, lrpad / 2, item->text, 0);
-	drawhighlights(item, x, y, w);
+	drawhighlights(item, x, y, w, n);
 	return r;
 }
 
@@ -205,7 +216,7 @@ drawmenu(void)
 {
 	unsigned int curpos;
 	struct item *item;
-	int x = 0, y = 0, fh = drw->fonts->h, w;
+	int n = 1, x = 0, y = 0, fh = drw->fonts->h, w;
 	char *censort;
 
 	drw_setscheme(drw, scheme[SchemeNorm]);
@@ -234,10 +245,11 @@ drawmenu(void)
 	}
 
 	recalculatematched();
+	drw_rect(drw, 0, (y += im) + bh, mw, im, 1, 1);
 	if (lines > 0) {
 		/* draw vertical list */
 		for (item = curr; item != next; item = item->right)
-			drawitem(item, x, y += bh, mw - x);
+			drawitem(item, 0, y += bh, mw, n++);
 	} else if (matches) {
 		/* draw horizontal list */
 		x += inputw;
@@ -248,7 +260,7 @@ drawmenu(void)
 		}
 		x += w;
 		for (item = curr; item != next; item = item->right)
-			x = drawitem(item, x, 0, textw_clamp(item->text, mw - x - TEXTW(">") - TEXTW(matched)));
+			x = drawitem(item, x, 0, textw_clamp(item->text, mw - x - TEXTW(">") - TEXTW(matched)), 0);
 		if (next) {
 			w = TEXTW(">");
 			drw_setscheme(drw, scheme[SchemeNorm]);
@@ -796,9 +808,10 @@ setup(void)
 
 	/* calculate menu geometry */
 	bh = drw->fonts->h + 2;
-	bh = MAX(bh,lineheight);	/* make a menu line AT LEAST 'lineheight' tall */
+	bh = MAX(bh,lineheight);    /* make a menu line AT LEAST 'lineheight' tall */
 	lines = MAX(lines, 0);
-	mh = (lines + 1) * bh;
+	mh = (lines + 1) * bh + im;
+	promptw = (prompt && *prompt) ? TEXTW(prompt) - lrpad / 4 : 0;
 #ifdef XINERAMA
 	i = 0;
 	if (parentwin == root && (info = XineramaQueryScreens(dpy, &n))) {
@@ -825,9 +838,16 @@ setup(void)
 				if (INTERSECT(x, y, 1, 1, info[i]) != 0)
 					break;
 
-		x = info[i].x_org;
-		y = info[i].y_org + (topbar ? 0 : info[i].height - mh);
-		mw = info[i].width;
+		if (centered) {
+			mw = MIN(MAX(max_textw() + promptw, min_width), info[i].width);
+			x = info[i].x_org + ((info[i].width  - mw - bw) / 2);
+			y = info[i].y_org + ((info[i].height - mh) / 2);
+		} else {
+			x = info[i].x_org;
+			y = info[i].y_org + (topbar ? 0 : info[i].height - mh);
+			mw = info[i].width;
+		}
+
 		XFree(info);
 	} else
 #endif
@@ -835,21 +855,30 @@ setup(void)
 		if (!XGetWindowAttributes(dpy, parentwin, &wa))
 			die("could not get embedding window attributes: 0x%lx",
 			    parentwin);
-		x = 0;
-		y = topbar ? 0 : wa.height - mh;
-		mw = wa.width;
+
+		if (centered) {
+			mw = MIN(MAX(max_textw() + promptw, min_width), wa.width);
+			x = (wa.width  - mw - bw) / 2;
+			y = (wa.height - mh) / 2;
+		} else {
+			x = 0;
+			y = topbar ? 0 : wa.height - mh;
+			mw = wa.width;
+		}
 	}
 	promptw = (prompt && *prompt) ? TEXTW(prompt) - lrpad / 4 : 0;
-	inputw = mw / 3; /* input width: ~33% of monitor width */
+	inputw = mw / 6; /* input width: ~1666..% of monitor width */
 	match();
 
 	/* create menu window */
 	swa.override_redirect = True;
 	swa.background_pixel = scheme[SchemeNorm][ColBg].pixel;
 	swa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
-	win = XCreateWindow(dpy, root, x, y, mw, mh, 0,
+	win = XCreateWindow(dpy, root, x, y, mw, mh, bw,
 	                    CopyFromParent, CopyFromParent, CopyFromParent,
 	                    CWOverrideRedirect | CWBackPixel | CWEventMask, &swa);
+	if (bw)
+		XSetWindowBorder(dpy, win, scheme[SchemeBorder][ColBg].pixel);
 	XSetClassHint(dpy, win, &ch);
 
 
@@ -898,6 +927,8 @@ main(int argc, char *argv[])
 			exit(0);
 		} else if (!strcmp(argv[i], "-b")) /* appears at the bottom of the screen */
 			topbar = 0;
+		else if (!strcmp(argv[i], "-c"))   /* center on screen */
+			centered = 1;
 		else if (!strcmp(argv[i], "-f"))   /* grabs keyboard before reading stdin */
 			fast = 1;
 		else if (!strcmp(argv[i], "-F"))   /* grabs keyboard before reading stdin */
@@ -928,6 +959,10 @@ main(int argc, char *argv[])
 			colors[SchemeNorm][ColBg] = argv[++i];
 		else if (!strcmp(argv[i], "-nf"))  /* normal foreground color */
 			colors[SchemeNorm][ColFg] = argv[++i];
+		else if (!strcmp(argv[i], "-ob"))  /* odd background color */
+			colors[SchemeOdd][ColBg] = argv[++i];
+		else if (!strcmp(argv[i], "-of"))  /* odd foreground color */
+			colors[SchemeOdd][ColFg] = argv[++i];
 		else if (!strcmp(argv[i], "-sb"))  /* selected background color */
 			colors[SchemeSel][ColBg] = argv[++i];
 		else if (!strcmp(argv[i], "-sf"))  /* selected foreground color */
@@ -936,12 +971,20 @@ main(int argc, char *argv[])
 			colors[SchemeNormHighlight][ColBg] = argv[++i];
 		else if (!strcmp(argv[i], "-nhf")) /* normal hi foreground color */
 			colors[SchemeNormHighlight][ColFg] = argv[++i];
+		else if (!strcmp(argv[i], "-ohb")) /* odd hi background color */
+			colors[SchemeOddHighlight][ColBg] = argv[++i];
+		else if (!strcmp(argv[i], "-ohf")) /* odd hi foreground color */
+			colors[SchemeOddHighlight][ColFg] = argv[++i];
 		else if (!strcmp(argv[i], "-shb")) /* selected hi background color */
 			colors[SchemeSelHighlight][ColBg] = argv[++i];
 		else if (!strcmp(argv[i], "-shf")) /* selected hi foreground color */
 			colors[SchemeSelHighlight][ColFg] = argv[++i];
+		else if (!strcmp(argv[i], "-bb"))  /* border background color */
+			colors[SchemeBorder][ColBg] = argv[++i];
 		else if (!strcmp(argv[i], "-w"))   /* embedding window id */
 			embed = argv[++i];
+		else if (!strcmp(argv[i], "-bw"))  /* border width */
+			bw = atoi(argv[++i]);
 		else if (!strcmp(argv[i], "-it") && ++i)  /* insert initial text */
 			insert(argv[i], strlen(argv[i]));
 		else