changeset: 189:449dd7029c10
tag: tip
user: dinkumator_AT_gmail.com
date: Sat Dec 06 13:22:10 2008 -0500
files: dwm/patches/colored_status_text.md dwm/patches/dwm-5.3.1-statuscolors.diff dwm/patches/dwm-5.3.1-statuscolors.png
description:
colored status text patch page
diff -r 777abcdf53d7 -r 449dd7029c10 dwm/patches/colored_status_text.md
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/dwm/patches/colored_status_text.md Sat Dec 06 13:22:10 2008 -0500
_AT_@ -0,0 +1,66 @@
+# COLORED STATUS TEXT #
+
+## Description ##
+
+This patch enables colored text in the status bar. It changes the way colors are defined in config.h, allowing the user to define multiple color combinations for use in their status script.
+
+## Configuration ##
+
+Download the patch and apply it according to the [general instructions](.).
+
+Modify the colors definition in 'config.h' to suit your needs. Make sure to define at least 3 colors as they will be used for 'normal', 'selected', and 'urgent' windows, respectively.
+
+## Usage ##
+
+Add code to your status script to output the raw characters '\x03' to switch to the 'urgent' color, or '\x04' to switch to the 4th defined color, etc. Note that the color indices in the status text are +1 from the definition in 'config.h' (because '\0' is the string terminator). To switch back to the normal text color use '\x01'.
+
+### Example ###
+
+The following definition in 'config.h':
+
+ #define NUMCOLORS 4
+ static const char colors[NUMCOLORS][ColLast][8] = {
+ // border foreground background
+ { "#000033", "#dddddd", "#000033" }, // normal
+ { "#000088", "#ffffff", "#000088" }, // selected
+ { "#ff0000", "#000000", "#ffff00" }, // urgent/warning (black on yellow)
+ { "#ff0000", "#ffffff", "#ff0000" }, // error (white on red)
+ // add more here
+ };
+
+Coupled with a matching status script produces the following:
+ ![Example Colored Status Text][1]
+
+A really silly example:
+
+ echo -e "normal text \x01 selected text \x03 warning/urgent text \x04 error text \x01 back to normal text" | dwm
+
+An example status script snippet to take advantage of the colors:
+
+ status=""
+ if [$batperc -le 10]; then
+ # use "warning" color
+ status+="\x03 BAT: $batperc"
+ elif [$batperc -le 5]; then
+ # use "error" color
+ status+="\x04 BAT: $batperc"
+ else
+ # default is normal color
+ status+="BAT: $batperc"
+ fi
+
+ # switch back to normal color for date
+ status+="\x01| "+$(date)
+
+ echo -e $status
+
+## Download ##
+
+ * [dwm-5.3.1-statuscolors.diff][2]
+
+## Comment ##
+If you find any bugs or may improve the code, feel free to do so and push the changes to the wiki.
+
+[1]:
http://www.suckless.org/dwm/patches/dwm-5.3.1-statuscolors.diff
+[2]:
http://www.suckless.org/dwm/patches/dwm-5.3.1-statuscolors.png
+
diff -r 777abcdf53d7 -r 449dd7029c10 dwm/patches/dwm-5.3.1-statuscolors.diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/dwm/patches/dwm-5.3.1-statuscolors.diff Sat Dec 06 13:22:10 2008 -0500
_AT_@ -0,0 +1,228 @@
+diff -r 94032e7d3943 config.def.h
+--- a/config.def.h Sat Dec 06 11:22:30 2008 +0000
++++ b/config.def.h Sat Dec 06 11:24:26 2008 -0500
+_AT_@ -1,13 +1,16 @@
+ /* See LICENSE file for copyright and license details. */
+
+ /* appearance */
++#define NUMCOLORS 4 // need at least 3
++static const char colors[NUMCOLORS][ColLast][8] = {
++ // border foreground background
++ { "#cccccc", "#000000", "#cccccc" }, // 0 = normal
++ { "#0066ff", "#ffffff", "#0066ff" }, // 1 = selected
++ { "#0066ff", "#0066ff", "#ffffff" }, // 2 = urgent/warning
++ { "#ff0000", "#ffffff", "#ff0000" }, // 3 = error
++ // add more here
++};
+ static const char font[] = "-*-terminus-medium-r-normal-*-14-*-*-*-*-*-*-*";
+-static const char normbordercolor[] = "#cccccc";
+-static const char normbgcolor[] = "#cccccc";
+-static const char normfgcolor[] = "#000000";
+-static const char selbordercolor[] = "#0066ff";
+-static const char selbgcolor[] = "#0066ff";
+-static const char selfgcolor[] = "#ffffff";
+ static unsigned int borderpx = 1; /* border pixel of windows */
+ static unsigned int snap = 32; /* snap pixel */
+ static Bool showbar = True; /* False means no bar */
+_AT_@ -49,7 +52,7 @@
+ #define SHCMD(cmd) { .v = (const char*[]){ "/bin/sh", "-c", cmd, NULL } }
+
+ /* commands */
+-static const char *dmenucmd[] = { "dmenu_run", "-fn", font, "-nb", normbgcolor, "-nf", normfgcolor, "-sb", selbgcolor, "-sf", selfgcolor, NULL };
++static const char *dmenucmd[] = { "dmenu_run", "-fn", font, "-nb", colors[0][ColBG], "-nf", colors[0][ColFG], "-sb", colors[1][ColBG], "-sf", colors[1][ColFG], NULL };
+ static const char *termcmd[] = { "uxterm", NULL };
+
+ static Key keys[] = {
+diff -r 94032e7d3943 dwm.c
+--- a/dwm.c Sat Dec 06 11:22:30 2008 +0000
++++ b/dwm.c Sat Dec 06 11:24:26 2008 -0500
+_AT_@ -52,6 +52,7 @@
+ #define MAX(a, b) ((a) > (b) ? (a) : (b))
+ #define MIN(a, b) ((a) < (b) ? (a) : (b))
+ #define MAXTAGLEN 16
++#define MAXCOLORS 8
+ #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
+ #define WIDTH(x) ((x)->w + 2 * (x)->bw)
+ #define HEIGHT(x) ((x)->h + 2 * (x)->bw)
+_AT_@ -97,8 +98,7 @@
+
+ typedef struct {
+ int x, y, w, h;
+- unsigned long norm[ColLast];
+- unsigned long sel[ColLast];
++ unsigned long colors[MAXCOLORS][ColLast];
+ Drawable drawable;
+ GC gc;
+ struct {
+_AT_@ -147,8 +147,9 @@
+ static void detachstack(Client *c);
+ static void die(const char *errstr, ...);
+ static void drawbar(void);
+-static void drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]);
+-static void drawtext(const char *text, unsigned long col[ColLast], Bool invert);
++static void drawcoloredtext(char *text);
++static void drawsquare(Bool filled, Bool empty, unsigned long col[ColLast]);
++static void drawtext(const char *text, unsigned long col[ColLast], Bool pad);
+ static void enternotify(XEvent *e);
+ static void expose(XEvent *e);
+ static void focus(Client *c);
+_AT_@ -501,14 +502,14 @@
+ dc.x = 0;
+ for(i = 0; i < LENGTH(tags); i++) {
+ dc.w = TEXTW(tags[i]);
+- col = tagset[seltags] & 1 << i ? dc.sel : dc.norm;
+- drawtext(tags[i], col, urg & 1 << i);
+- drawsquare(sel && sel->tags & 1 << i, occ & 1 << i, urg & 1 << i, col);
++ col = dc.colors[ (tagset[seltags] & 1 << i ? 1 : (urg & 1 << i ? 2:0)) ];
++ drawtext(tags[i], col, True);
++ drawsquare(sel && sel->tags & 1 << i, occ & 1 << i, col);
+ dc.x += dc.w;
+ }
+ if(blw > 0) {
+ dc.w = blw;
+- drawtext(lt[sellt]->symbol, dc.norm, False);
++ drawtext(lt[sellt]->symbol, dc.colors[0], True);
+ x = dc.x + dc.w;
+ }
+ else
+_AT_@ -519,27 +520,57 @@
+ dc.x = x;
+ dc.w = ww - x;
+ }
+- drawtext(stext, dc.norm, False);
++ drawcoloredtext(stext);
+ if((dc.w = dc.x - x) > bh) {
+ dc.x = x;
+ if(sel) {
+- drawtext(sel->name, dc.sel, False);
+- drawsquare(sel->isfixed, sel->isfloating, False, dc.sel);
++ drawtext(sel->name, dc.colors[1], True);
++ drawsquare(sel->isfixed, sel->isfloating, dc.colors[1]);
+ }
+ else
+- drawtext(NULL, dc.norm, False);
++ drawtext(NULL, dc.colors[0], True);
+ }
+ XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, ww, bh, 0, 0);
+ XSync(dpy, False);
+ }
+
+ void
+-drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]) {
++drawcoloredtext(char *text) {
++ Bool first=True;
++ char *buf = text, *ptr = buf, c = 1;
++ unsigned long *col = dc.colors[0];
++ int i, ox = dc.x;
++
++ while( *ptr ) {
++ for( i = 0; *ptr < 0 || *ptr > NUMCOLORS; i++, ptr++);
++ if( !*ptr ) break;
++ c=*ptr;
++ *ptr=0;
++ if( i ) {
++ dc.w = ww - dc.x;
++ drawtext(buf, col, first);
++ dc.x += textnw(buf, i) + textnw(&c,1);
++ if( first ) dc.x += ( dc.font.ascent + dc.font.descent ) / 2;
++ first = False;
++ } else if( first ) {
++ ox = dc.x += textnw(&c,1);
++ }
++ *ptr = c;
++ col = dc.colors[ c-1 ];
++ buf = ++ptr;
++ }
++ if( !first ) dc.x-=(dc.font.ascent+dc.font.descent)/2;
++ drawtext(buf, col, True);
++ dc.x = ox;
++}
++
++void
++drawsquare(Bool filled, Bool empty, unsigned long col[ColLast]) {
+ int x;
+ XGCValues gcv;
+ XRectangle r = { dc.x, dc.y, dc.w, dc.h };
+
+- gcv.foreground = col[invert ? ColBG : ColFG];
++ gcv.foreground = col[ ColFG ];
+ XChangeGC(dpy, dc.gc, GCForeground, &gcv);
+ x = (dc.font.ascent + dc.font.descent + 2) / 4;
+ r.x = dc.x + 1;
+_AT_@ -555,18 +586,18 @@
+ }
+
+ void
+-drawtext(const char *text, unsigned long col[ColLast], Bool invert) {
++drawtext(const char *text, unsigned long col[ColLast], Bool pad) {
+ char buf[256];
+ int i, x, y, h, len, olen;
+ XRectangle r = { dc.x, dc.y, dc.w, dc.h };
+
+- XSetForeground(dpy, dc.gc, col[invert ? ColFG : ColBG]);
++ XSetForeground(dpy, dc.gc, col[ ColBG ]);
+ XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
+ if(!text)
+ return;
+ olen = strlen(text);
+- h = dc.font.ascent + dc.font.descent;
+- y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
++ h = pad ? (dc.font.ascent + dc.font.descent) : 0;
++ y = dc.y + ((dc.h + dc.font.ascent - dc.font.descent) / 2);
+ x = dc.x + (h / 2);
+ /* shorten text if necessary */
+ for(len = MIN(olen, sizeof buf); len && textnw(text, len) > dc.w - h; len--);
+_AT_@ -575,7 +606,7 @@
+ memcpy(buf, text, len);
+ if(len < olen)
+ for(i = len; i && i > len - 3; buf[--i] = '.');
+- XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]);
++ XSetForeground(dpy, dc.gc, col[ ColFG ]);
+ if(dc.font.set)
+ XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
+ else
+_AT_@ -609,7 +640,7 @@
+ for(c = stack; c && !ISVISIBLE(c); c = c->snext);
+ if(sel && sel != c) {
+ grabbuttons(sel, False);
+- XSetWindowBorder(dpy, sel->win, dc.norm[ColBorder]);
++ XSetWindowBorder(dpy, sel->win, dc.colors[0][ColBorder]);
+ }
+ if(c) {
+ if(c->isurgent)
+_AT_@ -617,7 +648,7 @@
+ detachstack(c);
+ attachstack(c);
+ grabbuttons(c, True);
+- XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
++ XSetWindowBorder(dpy, c->win, dc.colors[1][ColBorder]);
+ XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
+ }
+ else
+_AT_@ -878,7 +909,7 @@
+
+ wc.border_width = c->bw;
+ XConfigureWindow(dpy, w, CWBorderWidth, &wc);
+- XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
++ XSetWindowBorder(dpy, w, dc.colors[0][ColBorder]);
+ configure(c); /* propagates border_width, if size doesn't change */
+ updatesizehints(c);
+ XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
+_AT_@ -1329,12 +1360,11 @@
+ cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
+
+ /* init appearance */
+- dc.norm[ColBorder] = getcolor(normbordercolor);
+- dc.norm[ColBG] = getcolor(normbgcolor);
+- dc.norm[ColFG] = getcolor(normfgcolor);
+- dc.sel[ColBorder] = getcolor(selbordercolor);
+- dc.sel[ColBG] = getcolor(selbgcolor);
+- dc.sel[ColFG] = getcolor(selfgcolor);
++ for(i=0; i<NUMCOLORS; i++) {
++ dc.colors[i][ColBorder] = getcolor( colors[i][ColBorder] );
++ dc.colors[i][ColFG] = getcolor( colors[i][ColFG] );
++ dc.colors[i][ColBG] = getcolor( colors[i][ColBG] );
++ }
+ dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
+ dc.gc = XCreateGC(dpy, root, 0, 0);
+ XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
diff -r 777abcdf53d7 -r 449dd7029c10 dwm/patches/dwm-5.3.1-statuscolors.png
Binary file dwm/patches/dwm-5.3.1-statuscolors.png has changed
Received on Sat Dec 06 2008 - 19:22:48 CET