Skip to content

Instantly share code, notes, and snippets.

@jgrar
Created December 22, 2015 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgrar/d3e2238fdff59c8f42a9 to your computer and use it in GitHub Desktop.
Save jgrar/d3e2238fdff59c8f42a9 to your computer and use it in GitHub Desktop.
modified dwm-6-1-statuscolors patch
diff --git a/config.h b/config.h
index 7054c06..9199a1f 100644
--- a/config.h
+++ b/config.h
@@ -5,12 +5,13 @@ static const char *fonts[] = {
"monospace:size=10"
};
static const char dmenufont[] = "monospace:size=10";
-static const char normbordercolor[] = "#444444";
-static const char normbgcolor[] = "#222222";
-static const char normfgcolor[] = "#bbbbbb";
-static const char selbordercolor[] = "#005577";
-static const char selbgcolor[] = "#005577";
-static const char selfgcolor[] = "#eeeeee";
+#define NUMCOLORS 2
+static const char colors[NUMCOLORS][MAXCOLORS][8] = {
+// border foreground background
+ { "#444444", "#bbbbbb", "#222222" }, // normal
+ { "#005577", "#eeeeee", "#005577" }, // selected
+ // add more here
+};
static const unsigned int borderpx = 1; /* border pixel of windows */
static const unsigned int snap = 32; /* snap pixel */
static const int showbar = 1; /* 0 means no bar */
@@ -54,7 +55,7 @@ static const Layout layouts[] = {
/* commands */
static char dmenumon[2] = "0"; /* component of dmenucmd, manipulated in spawn() */
-static const char *dmenucmd[] = { "dmenu_run", "-m", dmenumon, "-fn", dmenufont, "-nb", normbgcolor, "-nf", normfgcolor, "-sb", selbgcolor, "-sf", selfgcolor, NULL };
+static const char *dmenucmd[] = { "dmenu_run", "-m", dmenumon, "-fn", dmenufont, "-nb", colors[0][2], "-nf", colors[0][1], "-sb", colors[1][2], "-sf", colors[1][1], NULL };
static const char *termcmd[] = { "st", NULL };
static Key keys[] = {
diff --git a/drw.c b/drw.c
index f49200b..2ea3ae4 100644
--- a/drw.c
+++ b/drw.c
@@ -206,6 +206,68 @@ drw_setscheme(Drw *drw, ClrScheme *scheme)
drw->scheme = scheme;
}
+int
+drw_get_width(Drw *drw, int numcolors, const char *text)
+{
+ int i;
+ Fnt *curfont = drw->fonts[0];
+ int w = drw_text(drw, 0, 0, 0, 0, text, 0) + curfont->h;
+
+ 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 -= curfont->xfont->max_advance_width;
+
+ if (i == 0 || i + 1 == strlen(text)) {
+ /* we're on the first or the last character of the string
+ * drw_text already added one character's height (divided by 2) as padding to the beginning and end
+ * we don't want to double this padding, so we skip this character */
+ continue;
+ }
+
+ if (text[i - 1] > 0 && text[i - 1] <= numcolors) {
+ /* the previous character was also a color code
+ * we already added padding in the previous iteration
+ * we don't want to double this padding, so we skip this character */
+ continue;
+ }
+
+ /* we are somewhere in the middle of the string and the color has changed
+ * we want to add one character's height (divided by 2) as padding to the end of the previous colored text
+ * and to the beginning of the new colored text */
+ w += curfont->h;
+ }
+ }
+
+ return w;
+}
+
+void
+drw_colored_text(Drw *drw, ClrScheme *scheme, int numcolors, int x, int y, unsigned int w, unsigned int h, char *text)
+{
+ if (!drw || !drw->fontcount || !drw->scheme)
+ return;
+
+ char *buf = text, *ptr = buf, c = 1;
+ int i;
+
+ while (*ptr) {
+ for (i = 0; *ptr < 0 || *ptr > numcolors; i++, ptr++);
+ if (!*ptr)
+ break;
+ c = *ptr;
+ *ptr = 0;
+ if (i)
+ x = drw_text(drw, x, y, w, h, buf, 0) + drw->fonts[0]->h;
+ *ptr = c;
+ drw_setscheme(drw, &scheme[c-1]);
+ buf = ++ptr;
+ }
+ drw_text(drw, x, y, w, h, buf, 0);
+}
+
void
drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int empty, int invert)
{
diff --git a/drw.h b/drw.h
index e3b8515..c51f6cd 100644
--- a/drw.h
+++ b/drw.h
@@ -67,6 +67,8 @@ void drw_setfont(Drw *, Fnt *);
void drw_setscheme(Drw *, ClrScheme *);
/* Drawing functions */
+int drw_get_width(Drw *, int, const char *);
+void drw_colored_text(Drw *, ClrScheme *, int, int, int, unsigned int, unsigned int, char *);
void drw_rect(Drw *, int, int, unsigned int, unsigned int, int, int, int);
int drw_text(Drw *, int, int, unsigned int, unsigned int, const char *, int);
diff --git a/dwm.c b/dwm.c
index 0362114..4a9b2ff 100644
--- a/dwm.c
+++ b/dwm.c
@@ -51,6 +51,7 @@
* MAX(0, MIN((y)+(h),(m)->wy+(m)->wh) - MAX((y),(m)->wy)))
#define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags]))
#define LENGTH(X) (sizeof X / sizeof X[0])
+#define MAXCOLORS 9
#define MOUSEMASK (BUTTONMASK|PointerMotionMask)
#define WIDTH(X) ((X)->w + 2 * (X)->bw)
#define HEIGHT(X) ((X)->h + 2 * (X)->bw)
@@ -261,7 +262,7 @@ static void (*handler[LASTEvent]) (XEvent *) = {
static Atom wmatom[WMLast], netatom[NetLast];
static int running = 1;
static Cur *cursor[CurLast];
-static ClrScheme scheme[SchemeLast];
+static ClrScheme scheme[MAXCOLORS];
static Display *dpy;
static Drw *drw;
static Monitor *mons, *selmon;
@@ -718,35 +719,35 @@ drawbar(Monitor *m)
x = 0;
for (i = 0; i < LENGTH(tags); i++) {
w = TEXTW(tags[i]);
- drw_setscheme(drw, m->tagset[m->seltags] & 1 << i ? &scheme[SchemeSel] : &scheme[SchemeNorm]);
- drw_text(drw, x, 0, w, bh, tags[i], urg & 1 << i);
+ drw_setscheme(drw, &scheme[(m->tagset[m->seltags] & 1 << i) ? 1 : (urg & 1 << i ? 2 : 0)]);
+ drw_text(drw, x, 0, w, bh, tags[i], 0);
drw_rect(drw, x + 1, 1, dx, dx, m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
- occ & 1 << i, urg & 1 << i);
+ occ & 1 << i, 0);
x += w;
}
w = blw = TEXTW(m->ltsymbol);
- drw_setscheme(drw, &scheme[SchemeNorm]);
+ drw_setscheme(drw, &scheme[0]);
drw_text(drw, x, 0, w, bh, m->ltsymbol, 0);
x += w;
xx = x;
if (m == selmon) { /* status is only drawn on selected monitor */
- w = TEXTW(stext);
+ w = drw_get_width(drw, NUMCOLORS, stext);
x = m->ww - w;
if (x < xx) {
x = xx;
w = m->ww - xx;
}
- drw_text(drw, x, 0, w, bh, stext, 0);
+ drw_colored_text(drw, scheme, NUMCOLORS, x, 0, w, bh, stext);
} else
x = m->ww;
if ((w = x - xx) > bh) {
x = xx;
if (m->sel) {
- drw_setscheme(drw, m == selmon ? &scheme[SchemeSel] : &scheme[SchemeNorm]);
+ drw_setscheme(drw, &scheme[m == selmon ? 1 : 0]);
drw_text(drw, x, 0, w, bh, m->sel->name, 0);
drw_rect(drw, x + 1, 1, dx, dx, m->sel->isfixed, m->sel->isfloating, 0);
} else {
- drw_setscheme(drw, &scheme[SchemeNorm]);
+ drw_setscheme(drw, &scheme[0]);
drw_rect(drw, x, 0, w, bh, 1, 0, 1);
}
}
@@ -807,7 +808,7 @@ focus(Client *c)
detachstack(c);
attachstack(c);
grabbuttons(c, 1);
- XSetWindowBorder(dpy, c->win, scheme[SchemeSel].border->pix);
+ XSetWindowBorder(dpy, c->win, scheme[1].border->pix);
setfocus(c);
} else {
XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
@@ -1065,7 +1066,7 @@ manage(Window w, XWindowAttributes *wa)
wc.border_width = c->bw;
XConfigureWindow(dpy, w, CWBorderWidth, &wc);
- XSetWindowBorder(dpy, w, scheme[SchemeNorm].border->pix);
+ XSetWindowBorder(dpy, w, scheme[0].border->pix);
configure(c); /* propagates border_width, if size doesn't change */
updatewindowtype(c);
updatesizehints(c);
@@ -1580,12 +1581,12 @@ setup(void)
cursor[CurResize] = drw_cur_create(drw, XC_sizing);
cursor[CurMove] = drw_cur_create(drw, XC_fleur);
/* init appearance */
- scheme[SchemeNorm].border = drw_clr_create(drw, normbordercolor);
- scheme[SchemeNorm].bg = drw_clr_create(drw, normbgcolor);
- scheme[SchemeNorm].fg = drw_clr_create(drw, normfgcolor);
- scheme[SchemeSel].border = drw_clr_create(drw, selbordercolor);
- scheme[SchemeSel].bg = drw_clr_create(drw, selbgcolor);
- scheme[SchemeSel].fg = drw_clr_create(drw, selfgcolor);
+ for(int i = 0; i < NUMCOLORS; i++){
+ scheme[i].border = drw_clr_create(drw, colors[i][0]);
+ scheme[i].fg = drw_clr_create(drw, colors[i][1]);
+ scheme[i].bg = drw_clr_create(drw, colors[i][2]);
+ }
+
/* init bars */
updatebars();
updatestatus();
@@ -1745,7 +1746,7 @@ unfocus(Client *c, int setfocus)
if (!c)
return;
grabbuttons(c, 0);
- XSetWindowBorder(dpy, c->win, scheme[SchemeNorm].border->pix);
+ XSetWindowBorder(dpy, c->win, scheme[0].border->pix);
if (setfocus) {
XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment