From 494b7fe74a8dc334a5b0345c84814ec3d8316999 Mon Sep 17 00:00:00 2001 From: Sam Chudnick Date: Sat, 25 Feb 2023 22:08:18 -0500 Subject: Apply highlight patch --- config.h | 3 +++ dmenu | Bin 44432 -> 0 bytes dmenu.c | 44 +++++++++++++++++++++++++++++++++++++++++--- stest | Bin 17680 -> 0 bytes 4 files changed, 44 insertions(+), 3 deletions(-) delete mode 100755 dmenu delete mode 100755 stest diff --git a/config.h b/config.h index 5893d0b..bf22598 100644 --- a/config.h +++ b/config.h @@ -13,7 +13,10 @@ static const char *colors[SchemeLast][2] = { /* fg bg */ [SchemeNorm] = { "#bbbbbb", "#222222" }, [SchemeSel] = { "#eeeeee", "#005577" }, + [SchemeSelHighlight] = { "#ffc978", "#005577" }, + [SchemeNormHighlight] = { "#ffc978", "#222222" }, [SchemeOut] = { "#000000", "#00ffff" }, + [SchemeOutHighlight] = { "#ffc978", "#00ffff" }, }; static const unsigned int alphas[SchemeLast][2] = { diff --git a/dmenu b/dmenu deleted file mode 100755 index 63f1aea..0000000 Binary files a/dmenu and /dev/null differ diff --git a/dmenu.c b/dmenu.c index 9cfb1c4..635f204 100644 --- a/dmenu.c +++ b/dmenu.c @@ -30,8 +30,7 @@ #define OPAQUE 0xffU /* enums */ -enum { SchemeNorm, SchemeSel, SchemeOut, SchemeLast }; /* color schemes */ - +enum { SchemeNorm, SchemeSel, SchemeOut, SchemeNormHighlight, SchemeSelHighlight, SchemeOutHighlight, SchemeLast }; /* color schemes */ struct item { char *text; struct item *left, *right; @@ -141,6 +140,43 @@ cistrstr(const char *h, const char *n) return NULL; } +static void +drawhighlights(struct item *item, int x, int y, int maxw) +{ + char restorechar, tokens[sizeof text], *highlight, *token; + int indentx, highlightlen; + + drw_setscheme(drw, scheme[item == sel ? SchemeSelHighlight : item->out ? SchemeOutHighlight : SchemeNormHighlight]); + strcpy(tokens, text); + for (token = strtok(tokens, " "); token; token = strtok(NULL, " ")) { + highlight = fstrstr(item->text, token); + while (highlight) { + // Move item str end, calc width for highlight indent, & restore + highlightlen = highlight - item->text; + restorechar = *highlight; + item->text[highlightlen] = '\0'; + indentx = TEXTW(item->text); + item->text[highlightlen] = restorechar; + + // Move highlight str end, draw highlight, & restore + restorechar = highlight[strlen(token)]; + highlight[strlen(token)] = '\0'; + if (indentx - (lrpad / 2) - 1 < maxw) + drw_text( + drw, + x + indentx - (lrpad / 2) - 1, + y, + MIN(maxw - indentx, TEXTW(highlight) - lrpad), + bh, 0, highlight, 0 + ); + highlight[strlen(token)] = restorechar; + + if (strlen(highlight) - strlen(token) < strlen(token)) break; + highlight = fstrstr(highlight + strlen(token), token); + } + } +} + static int drawitem(struct item *item, int x, int y, int w) { @@ -151,7 +187,9 @@ drawitem(struct item *item, int x, int y, int w) else drw_setscheme(drw, scheme[SchemeNorm]); - return drw_text(drw, x, y, w, bh, lrpad / 2, item->text, 0); + int r = drw_text(drw, x, y, w, bh, lrpad / 2, item->text, 0); + drawhighlights(item, x, y, w); + return r; } static void diff --git a/stest b/stest deleted file mode 100755 index d2d6ca3..0000000 Binary files a/stest and /dev/null differ -- cgit v1.2.3