dmenu-fuzzyhighlight-5.3.diff (4734B)
1 From dd0327d33a3c5269ed128e0903787365625bd507 Mon Sep 17 00:00:00 2001 2 From: Justinas Grigas <dev@jstnas.com> 3 Date: Sun, 16 Jun 2024 00:38:53 +0100 4 Subject: [PATCH] fuzzyhighlight: highlight fuzzy matches 5 6 This patch actually fixes the highlighted character offset 7 --- 8 config.def.h | 2 ++ 9 dmenu.1 | 20 ++++++++++++++++++ 10 dmenu.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++--- 11 3 files changed, 76 insertions(+), 3 deletions(-) 12 13 diff --git a/config.def.h b/config.def.h 14 index 1edb647..64eab2a 100644 15 --- a/config.def.h 16 +++ b/config.def.h 17 @@ -11,6 +11,8 @@ static const char *colors[SchemeLast][2] = { 18 /* fg bg */ 19 [SchemeNorm] = { "#bbbbbb", "#222222" }, 20 [SchemeSel] = { "#eeeeee", "#005577" }, 21 + [SchemeSelHighlight] = { "#ffc978", "#005577" }, 22 + [SchemeNormHighlight] = { "#ffc978", "#222222" }, 23 [SchemeOut] = { "#000000", "#00ffff" }, 24 }; 25 /* -l option; if nonzero, dmenu uses vertical list with given number of lines */ 26 diff --git a/dmenu.1 b/dmenu.1 27 index 323f93c..472b179 100644 28 --- a/dmenu.1 29 +++ b/dmenu.1 30 @@ -20,6 +20,14 @@ dmenu \- dynamic menu 31 .IR color ] 32 .RB [ \-sf 33 .IR color ] 34 +.RB [ \-nhb 35 +.IR color ] 36 +.RB [ \-nhf 37 +.IR color ] 38 +.RB [ \-shb 39 +.IR color ] 40 +.RB [ \-shf 41 +.IR color ] 42 .RB [ \-w 43 .IR windowid ] 44 .P 45 @@ -75,6 +83,18 @@ defines the selected background color. 46 .BI \-sf " color" 47 defines the selected foreground color. 48 .TP 49 +.BI \-nhb " color" 50 +defines the normal highlight background color. 51 +.TP 52 +.BI \-nhf " color" 53 +defines the normal highlight foreground color. 54 +.TP 55 +.BI \-shb " color" 56 +defines the selected highlight background color. 57 +.TP 58 +.BI \-shf " color" 59 +defines the selected highlight foreground color. 60 +.TP 61 .B \-v 62 prints version information to stdout, then exits. 63 .TP 64 diff --git a/dmenu.c b/dmenu.c 65 index 40f93e0..662131a 100644 66 --- a/dmenu.c 67 +++ b/dmenu.c 68 @@ -25,7 +25,9 @@ 69 #define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad) 70 71 /* enums */ 72 -enum { SchemeNorm, SchemeSel, SchemeOut, SchemeLast }; /* color schemes */ 73 +enum { SchemeNorm, SchemeSel, SchemeNormHighlight, SchemeSelHighlight, 74 + SchemeOut, SchemeLast }; /* color schemes */ 75 + 76 77 struct item { 78 char *text; 79 @@ -129,9 +131,47 @@ cistrstr(const char *h, const char *n) 80 return NULL; 81 } 82 83 +static void 84 +drawhighlights(struct item *item, int x, int y, int maxw) 85 +{ 86 + int i, indent; 87 + char c, *highlight; 88 + 89 + if (!(strlen(item->text) && strlen(text))) 90 + return; 91 + 92 + drw_setscheme(drw, scheme[item == sel 93 + ? SchemeSelHighlight 94 + : SchemeNormHighlight]); 95 + for (i = 0, highlight = item->text; *highlight && text[i];) { 96 + if (!fstrncmp(highlight, &text[i], 1)) { 97 + /* get indentation */ 98 + c = *highlight; 99 + *highlight = '\0'; 100 + indent = TEXTW(item->text); 101 + *highlight = c; 102 + 103 + /* highlight character */ 104 + c = highlight[1]; 105 + highlight[1] = '\0'; 106 + drw_text( 107 + drw, 108 + x + indent - (lrpad / 2.), 109 + y, 110 + MIN(maxw - indent, TEXTW(highlight) - lrpad), 111 + bh, 0, highlight, 0 112 + ); 113 + highlight[1] = c; 114 + ++i; 115 + } 116 + ++highlight; 117 + } 118 +} 119 + 120 static int 121 drawitem(struct item *item, int x, int y, int w) 122 { 123 + int r; 124 if (item == sel) 125 drw_setscheme(drw, scheme[SchemeSel]); 126 else if (item->out) 127 @@ -139,7 +179,9 @@ drawitem(struct item *item, int x, int y, int w) 128 else 129 drw_setscheme(drw, scheme[SchemeNorm]); 130 131 - return drw_text(drw, x, y, w, bh, lrpad / 2, item->text, 0); 132 + r = drw_text(drw, x, y, w, bh, lrpad / 2, item->text, 0); 133 + drawhighlights(item, x, y, w); 134 + return r; 135 } 136 137 static void 138 @@ -716,7 +758,8 @@ static void 139 usage(void) 140 { 141 die("usage: dmenu [-bfiv] [-l lines] [-p prompt] [-fn font] [-m monitor]\n" 142 - " [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]"); 143 + " [-nb color] [-nf color] [-sb color] [-sf color]\n" 144 + " [-nhb color] [-nhf color] [-shb color] [-shf color] [-w windowid]"); 145 } 146 147 int 148 @@ -756,6 +799,14 @@ main(int argc, char *argv[]) 149 colors[SchemeSel][ColBg] = argv[++i]; 150 else if (!strcmp(argv[i], "-sf")) /* selected foreground color */ 151 colors[SchemeSel][ColFg] = argv[++i]; 152 + else if (!strcmp(argv[i], "-nhb")) /* normal hi background color */ 153 + colors[SchemeNormHighlight][ColBg] = argv[++i]; 154 + else if (!strcmp(argv[i], "-nhf")) /* normal hi foreground color */ 155 + colors[SchemeNormHighlight][ColFg] = argv[++i]; 156 + else if (!strcmp(argv[i], "-shb")) /* selected hi background color */ 157 + colors[SchemeSelHighlight][ColBg] = argv[++i]; 158 + else if (!strcmp(argv[i], "-shf")) /* selected hi foreground color */ 159 + colors[SchemeSelHighlight][ColFg] = argv[++i]; 160 else if (!strcmp(argv[i], "-w")) /* embedding window id */ 161 embed = argv[++i]; 162 else 163 -- 164 2.45.2 165