dmenu-highlight-20201211-fcdc159.diff (3148B)
1 From fcdc1593ed418166f20b7e691a49b1e6eefc116e Mon Sep 17 00:00:00 2001 2 From: Nathaniel Evan <nathanielevan@zohomail.com> 3 Date: Fri, 11 Dec 2020 11:08:12 +0700 4 Subject: [PATCH] Highlight matched text in a different color scheme 5 6 --- 7 config.def.h | 3 +++ 8 dmenu.c | 44 +++++++++++++++++++++++++++++++++++++++++--- 9 2 files changed, 44 insertions(+), 3 deletions(-) 10 11 diff --git a/config.def.h b/config.def.h 12 index 1edb647..79be73a 100644 13 --- a/config.def.h 14 +++ b/config.def.h 15 @@ -11,7 +11,10 @@ static const char *colors[SchemeLast][2] = { 16 /* fg bg */ 17 [SchemeNorm] = { "#bbbbbb", "#222222" }, 18 [SchemeSel] = { "#eeeeee", "#005577" }, 19 + [SchemeSelHighlight] = { "#ffc978", "#005577" }, 20 + [SchemeNormHighlight] = { "#ffc978", "#222222" }, 21 [SchemeOut] = { "#000000", "#00ffff" }, 22 + [SchemeOutHighlight] = { "#ffc978", "#00ffff" }, 23 }; 24 /* -l option; if nonzero, dmenu uses vertical list with given number of lines */ 25 static unsigned int lines = 0; 26 diff --git a/dmenu.c b/dmenu.c 27 index 65f25ce..cce1ad1 100644 28 --- a/dmenu.c 29 +++ b/dmenu.c 30 @@ -26,8 +26,7 @@ 31 #define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad) 32 33 /* enums */ 34 -enum { SchemeNorm, SchemeSel, SchemeOut, SchemeLast }; /* color schemes */ 35 - 36 +enum { SchemeNorm, SchemeSel, SchemeOut, SchemeNormHighlight, SchemeSelHighlight, SchemeOutHighlight, SchemeLast }; /* color schemes */ 37 struct item { 38 char *text; 39 struct item *left, *right; 40 @@ -113,6 +112,43 @@ cistrstr(const char *s, const char *sub) 41 return NULL; 42 } 43 44 +static void 45 +drawhighlights(struct item *item, int x, int y, int maxw) 46 +{ 47 + char restorechar, tokens[sizeof text], *highlight, *token; 48 + int indentx, highlightlen; 49 + 50 + drw_setscheme(drw, scheme[item == sel ? SchemeSelHighlight : item->out ? SchemeOutHighlight : SchemeNormHighlight]); 51 + strcpy(tokens, text); 52 + for (token = strtok(tokens, " "); token; token = strtok(NULL, " ")) { 53 + highlight = fstrstr(item->text, token); 54 + while (highlight) { 55 + // Move item str end, calc width for highlight indent, & restore 56 + highlightlen = highlight - item->text; 57 + restorechar = *highlight; 58 + item->text[highlightlen] = '\0'; 59 + indentx = TEXTW(item->text); 60 + item->text[highlightlen] = restorechar; 61 + 62 + // Move highlight str end, draw highlight, & restore 63 + restorechar = highlight[strlen(token)]; 64 + highlight[strlen(token)] = '\0'; 65 + if (indentx - (lrpad / 2) - 1 < maxw) 66 + drw_text( 67 + drw, 68 + x + indentx - (lrpad / 2) - 1, 69 + y, 70 + MIN(maxw - indentx, TEXTW(highlight) - lrpad), 71 + bh, 0, highlight, 0 72 + ); 73 + highlight[strlen(token)] = restorechar; 74 + 75 + if (strlen(highlight) - strlen(token) < strlen(token)) break; 76 + highlight = fstrstr(highlight + strlen(token), token); 77 + } 78 + } 79 +} 80 + 81 static int 82 drawitem(struct item *item, int x, int y, int w) 83 { 84 @@ -123,7 +159,9 @@ drawitem(struct item *item, int x, int y, int w) 85 else 86 drw_setscheme(drw, scheme[SchemeNorm]); 87 88 - return drw_text(drw, x, y, w, bh, lrpad / 2, item->text, 0); 89 + int r = drw_text(drw, x, y, w, bh, lrpad / 2, item->text, 0); 90 + drawhighlights(item, x, y, w); 91 + return r; 92 } 93 94 static void 95 -- 96 2.29.2 97