]> Skullheadx's Git Forge - dmenu.git/commitdiff
fuzzyhighlight: highlight fuzzy matches
authorJustinas Grigas <dev@jstnas.com>
Sat, 15 Jun 2024 23:38:53 +0000 (00:38 +0100)
committerSkullheadx <admonty1@protonmail.com>
Fri, 1 May 2026 13:06:44 +0000 (09:06 -0400)
This patch actually fixes the highlighted character offset

config.def.h
dmenu.1
dmenu.c

index b0adc55c2d6ea8ef58654f19044254fc9d40ab26..2a032421ad299565f17e618d7b3ac4eae1cc234d 100644 (file)
@@ -12,6 +12,8 @@ static const char *colors[SchemeLast][2] = {
        /*     fg         bg       */
        [SchemeNorm] = { "#bbbbbb", "#222222" },
        [SchemeSel] = { "#eeeeee", "#005577" },
+       [SchemeSelHighlight] = { "#ffc978", "#005577" },
+       [SchemeNormHighlight] = { "#ffc978", "#222222" },
        [SchemeOut] = { "#000000", "#00ffff" },
 };
 /* -l option; if nonzero, dmenu uses vertical list with given number of lines */
diff --git a/dmenu.1 b/dmenu.1
index 5a7b0e18b45fa60e637fbe8aa844403a9d89be51..a5e8190d6062da74edc9b0609d40f2bae8abb143 100644 (file)
--- a/dmenu.1
+++ b/dmenu.1
@@ -23,6 +23,13 @@ dmenu \- dynamic menu
 .RB [ \-ob
 .IR color ]
 .RB [ \-of
+.RB [ \-nhb
+.IR color ]
+.RB [ \-nhf
+.IR color ]
+.RB [ \-shb
+.IR color ]
+.RB [ \-shf
 .IR color ]
 .RB [ \-w
 .IR windowid ]
diff --git a/dmenu.c b/dmenu.c
index 08c020373c87f79b8ddae6399f3ff52fa165f9f3..bce8bc2cc27ca3c9c93353faa86fee54520f1c76 100644 (file)
--- a/dmenu.c
+++ b/dmenu.c
@@ -26,7 +26,9 @@
 #define TEXTW(X)              (drw_fontset_getwidth(drw, (X)) + lrpad)
 
 /* enums */
-enum { SchemeNorm, SchemeSel, SchemeOut, SchemeLast }; /* color schemes */
+enum { SchemeNorm, SchemeSel, SchemeNormHighlight, SchemeSelHighlight,
+       SchemeOut, SchemeLast }; /* color schemes */
+
 
 struct item {
        char *text;
@@ -131,9 +133,47 @@ cistrstr(const char *h, const char *n)
        return NULL;
 }
 
+static void
+drawhighlights(struct item *item, int x, int y, int maxw)
+{
+       int i, indent;
+       char c, *highlight;
+
+       if (!(strlen(item->text) && strlen(text)))
+               return;
+
+       drw_setscheme(drw, scheme[item == sel
+                          ? SchemeSelHighlight
+                          : SchemeNormHighlight]);
+       for (i = 0, highlight = item->text; *highlight && text[i];) {
+               if (!fstrncmp(highlight, &text[i], 1)) {
+                       /* get indentation */
+                       c = *highlight;
+                       *highlight = '\0';
+                       indent = TEXTW(item->text);
+                       *highlight = c;
+
+                       /* highlight character */
+                       c = highlight[1];
+                       highlight[1] = '\0';
+                       drw_text(
+                               drw,
+                               x + indent - (lrpad / 2.),
+                               y,
+                               MIN(maxw - indent, TEXTW(highlight) - lrpad),
+                               bh, 0, highlight, 0
+                       );
+                       highlight[1] = c;
+                       ++i;
+               }
+               ++highlight;
+       }
+}
+
 static int
 drawitem(struct item *item, int x, int y, int w)
 {
+       int r;
        if (item == sel)
                drw_setscheme(drw, scheme[SchemeSel]);
        else if (item->out)
@@ -141,7 +181,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);
+       r = drw_text(drw, x, y, w, bh, lrpad / 2, item->text, 0);
+       drawhighlights(item, x, y, w);
+       return r;
 }
 
 static void
@@ -800,8 +842,9 @@ setup(void)
 static void
 usage(void)
 {
-       die("usage: dmenu [-bFfiv] [-l lines] [-p prompt] [-fn font] [-m monitor]\n"
-           "             [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]");
+       die("usage: dmenu [-bfiv] [-l lines] [-p prompt] [-fn font] [-m monitor]\n"
+           "             [-nb color] [-nf color] [-sb color] [-sf color]\n"
+           "             [-nhb color] [-nhf color] [-shb color] [-shf color] [-w windowid]");
 }
 
 int
@@ -847,6 +890,14 @@ main(int argc, char *argv[])
                        colors[SchemeOut][ColBg] = argv[++i];
                else if (!strcmp(argv[i], "-of"))  /* outline foreground color */
                        colors[SchemeOut][ColFg] = argv[++i];
+               else if (!strcmp(argv[i], "-nhb")) /* normal hi background color */
+                       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], "-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], "-w"))   /* embedding window id */
                        embed = argv[++i];
                else