diff --git a/drw.c b/drw.c index 8fd1ca4..68c1830 100644 --- a/drw.c +++ b/drw.c @@ -247,6 +247,50 @@ drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int XDrawRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w - 1, h - 1); } +int +drw_get_width(Drw *drw, int numcolors, const char *text) +{ + int i; + int w = drw_text(drw, 0, 0, 0, 0, 0, text, 0); + + for (i = 0; i < strlen(text); i++) + if (text[i] > 0 && text[i] <= numcolors) + /* we found a color code + * drw_text counted it as a normal character and added one character's width + * we aren't going to render this character, so we remove one character's width */ + w -= drw->fonts->xfont->max_advance_width; + + return w; +} + +int +drw_color_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lpad, const char *text, int invert, int numcolors, Clr **scheme) +{ + int render = x || y || w || h; + char *buf = (char*)text, *ptr = buf, c = 1; + int i; + + if (!drw || (render && !drw->scheme) || !text || !drw->fonts) + return 0; + + while (*ptr) { + for (i = 0; *ptr < 0 || *ptr > numcolors; i++, ptr++); + if (!*ptr) + break; + c = *ptr; + *ptr = 0; + if (i) { + unsigned int sw = drw_fontset_getwidth(drw, buf); + x = drw_text(drw, x, y, sw, h, lpad, buf, invert); + } + + *ptr = c; + drw_setscheme(drw, scheme[c-1]); + buf = ++ptr; + } + return drw_text(drw, x, y, w, h, lpad, buf, invert); +} + int drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lpad, const char *text, int invert) { diff --git a/drw.h b/drw.h index 4bcd5ad..2675971 100644 --- a/drw.h +++ b/drw.h @@ -55,3 +55,7 @@ int drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned in /* Map functions */ void drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h); + +/* Drawing color text functions */ +int drw_get_width(Drw *drw, int numcolors, const char *text); +int drw_color_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lpad, const char *text, int invert, int numcolors, Clr **scheme); diff --git a/dwm.c b/dwm.c index 4465af1..8b77209 100644 --- a/dwm.c +++ b/dwm.c @@ -704,8 +704,8 @@ drawbar(Monitor *m) /* draw status first so it can be overdrawn by tags later */ if (m == selmon) { /* status is only drawn on selected monitor */ drw_setscheme(drw, scheme[SchemeNorm]); - sw = TEXTW(stext) - lrpad + 2; /* 2px right padding */ - drw_text(drw, m->ww - sw, 0, sw, bh, 0, stext, 0); + sw = drw_get_width(drw, LENGTH(colors), stext) + 2; /*2px right padding */ + drw_color_text(drw, m->ww - sw, 0, sw, bh, 0, stext, 0, LENGTH(colors), scheme); } for (c = m->clients; c; c = c->next) {