Hi,
I do small cleanup of drawtext():
- buf[] is not needed to be global
- there's no need to end buf[] with zero byte, as all function take length
parameter
- w > dc.w can be true iff length of printed text is zero
-Ph
--
Premysl "Anydot" Hruby, http://www.redrum.cz/
diff -r 042cef9c6ace dwm.c
--- a/dwm.c Fri Apr 25 00:04:27 2008 +0200
+++ b/dwm.c Sun Apr 27 18:15:02 2008 +0200
@@ -210,7 +210,7 @@
void zoom(const char *arg);
/* variables */
-char stext[256], buf[256];
+char stext[256];
int screen, sx, sy, sw, sh;
int (*xerrorxlib)(Display *, XErrorEvent *);
int bx, by, bw, bh, blw, bgw, mx, my, mw, mh, mox, moy, mow, moh, tx, ty, tw, th, wx, wy, ww, wh;
@@ -600,22 +600,23 @@
int x, y, w, h;
unsigned int len, olen;
XRectangle r = { dc.x, dc.y, dc.w, dc.h };
+ char buf[256];
XSetForeground(dpy, dc.gc, col[invert ? ColFG : ColBG]);
XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
if(!text)
return;
+ olen = strlen(text);
+ len = MIN(olen, sizeof buf);
+ memcpy(buf, text, len);
w = 0;
- olen = strlen(text);
- len = MIN(olen, sizeof buf - 1);
- memcpy(buf, text, len);
- buf[len] = 0;
h = dc.font.ascent + dc.font.descent;
y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
x = dc.x + (h / 2);
/* shorten text if necessary */
- while(len && (w = textnw(buf, len)) > dc.w - h)
- buf[--len] = 0;
+ for(; len && (w = textnw(buf, len)) > dc.w - h; len--);
+ if (!len)
+ return;
if(len < olen) {
if(len > 1)
buf[len - 1] = '.';
@@ -624,8 +625,6 @@
if(len > 3)
buf[len - 3] = '.';
}
- if(w > dc.w)
- return; /* too long */
XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]);
if(dc.font.set)
XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);