Use simpler highlighting

This commit is contained in:
Jacob 2023-08-07 18:59:13 +02:00
parent 9cd846ebef
commit 495401e153

View file

@ -1,44 +1,51 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
void drawhighlights(struct item *item, int x, int y, int w, int p, char *itemtext) { void drawhighlights(struct item *item, int x, int y, int w, int p, char *itemtext) {
int i, indent; char restorechar, text[sizeof tx.text], *highlight, *ctext;
char *highlight; int indent, highlightlen;
char c;
// limitation in order to prevent highlighting from drawing when the text isn't visible if ((columns > 5 && lines > 1) || (!(strlen(itemtext) && strlen(tx.text))) || strstr(itemtext, "</") || is_selected(item->index)) {
if (columns > 5 && lines > 1)
return; return;
}
if (!(strlen(itemtext) && strlen(tx.text))) strcpy(text, tx.text);
return;
if (strstr(itemtext, "</")) for (ctext = text; ctext; ctext = NULL) {
return; highlight = fstrstr(itemtext, ctext);
for (i = 0, highlight = itemtext; *highlight && tx.text[i];) { while (highlight) {
if (((fuzzy && !fstrncmp(&(*highlight), &tx.text[i], 1)) || (!fuzzy && *highlight == tx.text[i]))) { highlightlen = highlight - itemtext;
c = *highlight; restorechar = *highlight;
*highlight = '\0';
indent = TEXTW(itemtext) - sp.lrpad;
*highlight = c;
// highlight character itemtext[highlightlen] = '\0';
c = highlight[1];
highlight[1] = '\0'; indent = TEXTW(itemtext);
draw_text( itemtext[highlightlen] = restorechar;
restorechar = highlight[strlen(ctext)];
highlight[strlen(ctext)] = '\0';
if (indent - (sp.lrpad / 2) - 1 < w) {
draw_text(
draw, draw,
x + indent + (p), x + indent - (sp.lrpad / 2) - 1,
y, y,
MIN(w - indent - sp.lrpad, TEXTW(highlight) - sp.lrpad), MIN(w - indent, TEXTW(highlight) - sp.lrpad),
sp.bh, 0, highlight, 0, False, sp.bh, 0, highlight, 0, False,
item == selecteditem ? col_hlselfg : col_hlnormfg, item == selecteditem ? col_hlselfg : col_hlnormfg,
item == selecteditem ? col_hlselbg : col_hlnormbg, item == selecteditem ? col_hlselbg : col_hlnormbg,
item == selecteditem ? alpha_hlselfg : alpha_hlnormfg, item == selecteditem ? alpha_hlselfg : alpha_hlnormfg,
item == selecteditem ? alpha_hlselbg : alpha_hlnormbg); item == selecteditem ? alpha_hlselbg : alpha_hlnormbg
highlight[1] = c; );
i++; }
}
highlight++; highlight[strlen(ctext)] = restorechar;
}
if (strlen(highlight) - strlen(ctext) < strlen(ctext))
break;
highlight = fstrstr(highlight + strlen(ctext), ctext);
}
}
} }
char* get_text_n_sgr(struct item *item) { char* get_text_n_sgr(struct item *item) {
@ -80,7 +87,6 @@ int drawitemtext(struct item *item, int x, int y, int w) {
int bg = 0; // background int bg = 0; // background
int bgfg = 0; // both int bgfg = 0; // both
int ignore = 0; // ignore colors int ignore = 0; // ignore colors
int selitem = 0;
int priitem = 0; int priitem = 0;
char *bgcol; char *bgcol;
char *fgcol; char *fgcol;
@ -99,7 +105,6 @@ int drawitemtext(struct item *item, int x, int y, int w) {
// memcpy the correct scheme // memcpy the correct scheme
if (item == selecteditem) { if (item == selecteditem) {
selitem = 1;
bgcol = col_itemselbg; bgcol = col_itemselbg;
fgcol = col_itemselfg; fgcol = col_itemselfg;
bga = alpha_itemselbg; bga = alpha_itemselbg;
@ -136,7 +141,6 @@ int drawitemtext(struct item *item, int x, int y, int w) {
} }
if (is_selected(item->index)) { if (is_selected(item->index)) {
selitem = (lines ? 1 : selitem);
bgcol = col_itemmarkedbg; bgcol = col_itemmarkedbg;
fgcol = col_itemmarkedfg; fgcol = col_itemmarkedfg;
fga = alpha_itemmarkedfg; fga = alpha_itemmarkedfg;
@ -144,7 +148,7 @@ int drawitemtext(struct item *item, int x, int y, int w) {
} }
// apply extra padding // apply extra padding
if ((selitem && !priitem) && lines) { if ((!priitem) && lines) {
leftpadding += selitempadding; leftpadding += selitempadding;
} else if (priitem && lines) { } else if (priitem && lines) {
leftpadding += priitempadding; leftpadding += priitempadding;
@ -166,7 +170,7 @@ int drawitemtext(struct item *item, int x, int y, int w) {
obgcol = bgcol; obgcol = bgcol;
oleftpadding = leftpadding; oleftpadding = leftpadding;
if (!hidepowerline && powerlineitems && selitem) { if (!hidepowerline && powerlineitems) {
if (itempwlstyle == 2) { if (itempwlstyle == 2) {
draw_circle(draw, x - sp.plw, y, sp.plw, sp.bh, 0, col_menu, bgcol, alpha_menu, bga); draw_circle(draw, x - sp.plw, y, sp.plw, sp.bh, 0, col_menu, bgcol, alpha_menu, bga);
} else { } else {
@ -200,11 +204,9 @@ int drawitemtext(struct item *item, int x, int y, int w) {
apply_fribidi(buffer); apply_fribidi(buffer);
draw_text(draw, x, y, w, sp.bh, leftpadding, isrtl ? fribidi_text : buffer, 0, pango_item ? True : False, fgcol, bgcol, fga, bga); draw_text(draw, x, y, w, sp.bh, leftpadding, isrtl ? fribidi_text : buffer, 0, pango_item ? True : False, fgcol, bgcol, fga, bga);
// position and width
x += MIN(w, TEXTW(buffer) - sp.lrpad + leftpadding); x += MIN(w, TEXTW(buffer) - sp.lrpad + leftpadding);
w -= MIN(w, TEXTW(buffer) - sp.lrpad + leftpadding); w -= MIN(w, TEXTW(buffer) - sp.lrpad + leftpadding);
// no highlighting if colored text
leftpadding = 0; leftpadding = 0;
char *c_character = item->text + escape + 1; // current character char *c_character = item->text + escape + 1; // current character
@ -310,7 +312,7 @@ int drawitemtext(struct item *item, int x, int y, int w) {
if (!hidehighlight) if (!hidehighlight)
drawhighlights(item, ox, oy, ow, oleftpadding, item->nsgrtext); drawhighlights(item, ox, oy, ow, oleftpadding, item->nsgrtext);
if (!hidepowerline && powerlineitems && selitem) { if (!hidepowerline && powerlineitems) {
if (itempwlstyle == 2) { if (itempwlstyle == 2) {
draw_circle(draw, ret, y, sp.plw, sp.bh, 1, col_menu, obgcol, alpha_menu, obga); draw_circle(draw, ret, y, sp.plw, sp.bh, 1, col_menu, obgcol, alpha_menu, obga);
} else { } else {