patch: fuzzyhighlight

This commit is contained in:
frosty 2024-08-20 01:52:25 +00:00
parent 2955bed332
commit 33361d3def
3 changed files with 76 additions and 3 deletions

View file

@ -12,6 +12,8 @@ static const char *colors[SchemeLast][2] = {
/* fg bg */ /* fg bg */
[SchemeNorm] = { "#bbbbbb", "#222222" }, [SchemeNorm] = { "#bbbbbb", "#222222" },
[SchemeSel] = { "#eeeeee", "#005577" }, [SchemeSel] = { "#eeeeee", "#005577" },
[SchemeSelHighlight] = { "#ffc978", "#005577" },
[SchemeNormHighlight] = { "#ffc978", "#222222" },
[SchemeOut] = { "#000000", "#00ffff" }, [SchemeOut] = { "#000000", "#00ffff" },
}; };
/* -l option; if nonzero, dmenu uses vertical list with given number of lines */ /* -l option; if nonzero, dmenu uses vertical list with given number of lines */

20
dmenu.1
View file

@ -20,6 +20,14 @@ dmenu \- dynamic menu
.IR color ] .IR color ]
.RB [ \-sf .RB [ \-sf
.IR color ] .IR color ]
.RB [ \-nhb
.IR color ]
.RB [ \-nhf
.IR color ]
.RB [ \-shb
.IR color ]
.RB [ \-shf
.IR color ]
.RB [ \-w .RB [ \-w
.IR windowid ] .IR windowid ]
.P .P
@ -78,6 +86,18 @@ defines the selected background color.
.BI \-sf " color" .BI \-sf " color"
defines the selected foreground color. defines the selected foreground color.
.TP .TP
.BI \-nhb " color"
defines the normal highlight background color.
.TP
.BI \-nhf " color"
defines the normal highlight foreground color.
.TP
.BI \-shb " color"
defines the selected highlight background color.
.TP
.BI \-shf " color"
defines the selected highlight foreground color.
.TP
.B \-v .B \-v
prints version information to stdout, then exits. prints version information to stdout, then exits.
.TP .TP

57
dmenu.c
View file

@ -26,7 +26,9 @@
#define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad) #define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad)
/* enums */ /* enums */
enum { SchemeNorm, SchemeSel, SchemeOut, SchemeLast }; /* color schemes */ enum { SchemeNorm, SchemeSel, SchemeNormHighlight, SchemeSelHighlight,
SchemeOut, SchemeLast }; /* color schemes */
struct item { struct item {
char *text; char *text;
@ -131,9 +133,47 @@ cistrstr(const char *h, const char *n)
return NULL; 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 static int
drawitem(struct item *item, int x, int y, int w) drawitem(struct item *item, int x, int y, int w)
{ {
int r;
if (item == sel) if (item == sel)
drw_setscheme(drw, scheme[SchemeSel]); drw_setscheme(drw, scheme[SchemeSel]);
else if (item->out) else if (item->out)
@ -141,7 +181,9 @@ drawitem(struct item *item, int x, int y, int w)
else else
drw_setscheme(drw, scheme[SchemeNorm]); 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 static void
@ -802,7 +844,8 @@ static void
usage(void) usage(void)
{ {
die("usage: dmenu [-bFfiv] [-l lines] [-p prompt] [-fn font] [-m monitor]\n" die("usage: dmenu [-bFfiv] [-l lines] [-p prompt] [-fn font] [-m monitor]\n"
" [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]"); " [-nb color] [-nf color] [-sb color] [-sf color]\n"
" [-nhb color] [-nhf color] [-shb color] [-shf color] [-w windowid]");
} }
int int
@ -844,6 +887,14 @@ main(int argc, char *argv[])
colors[SchemeSel][ColBg] = argv[++i]; colors[SchemeSel][ColBg] = argv[++i];
else if (!strcmp(argv[i], "-sf")) /* selected foreground color */ else if (!strcmp(argv[i], "-sf")) /* selected foreground color */
colors[SchemeSel][ColFg] = argv[++i]; colors[SchemeSel][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 */ else if (!strcmp(argv[i], "-w")) /* embedding window id */
embed = argv[++i]; embed = argv[++i];
else else