From f87c249d213fe36be3fc487ce85352e9b1ae7f60 Mon Sep 17 00:00:00 2001 From: "Timothy M. Schaeffer" Date: Thu, 30 Oct 2014 21:58:19 -0400 Subject: [PATCH 1/3] Allow colors in the form #RRGGBB (ie w/o alpha). --- bar.c | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/bar.c b/bar.c index bec8a15..1b3b4cb 100644 --- a/bar.c +++ b/bar.c @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -168,19 +169,24 @@ parse_color (const char *str, char **end, const uint32_t def) unsigned int g = (tmp&0x0000ff00) >> 8; unsigned int b = (tmp&0x000000ff); - if (a) { - r = (r * 255) / a; - g = (g * 255) / a; - b = (b * 255) / a; + ptrdiff_t len = *end - str; + if (len == 8) { + if (a == 0) { + r = g = b = 0; + } else { + r = (r * 255) / a; + g = (g * 255) / a; + b = (b * 255) / a; - /* Clamp on overflow */ - if (r > 255) r = 255; - if (g > 255) g = 255; - if (b > 255) b = 255; - } else - r = g = b = 0; + /* Clamp on overflow */ + if (r > 255) r = 255; + if (g > 255) g = 255; + if (b > 255) b = 255; + } + } - return a << 24 | r << 16 | g << 8 | b; + uint32_t c = a << 24 | r << 16 | g << 8 | b; + return c; } /* Actual color name, resolve it */ From f07aa2db6f1945c13f2a74a7a14cc47c48af5a6c Mon Sep 17 00:00:00 2001 From: "Timothy M. Schaeffer" Date: Thu, 30 Oct 2014 21:59:51 -0400 Subject: [PATCH 2/3] On input, ignore % w/o a { after. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This allows eg “Battery 88%” in the input w/o messing up the colors. --- bar.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bar.c b/bar.c index 1b3b4cb..d7e4c56 100644 --- a/bar.c +++ b/bar.c @@ -362,7 +362,8 @@ parse (char *text) if (*p == '\0' || *p == '\n') return; - if (*p == '%' && p++ && *p == '{' && (end = strchr(p++, '}'))) { + if (*p == '%' && *(p+1) == '{' && (end = strchr(p+2, '}'))) { + p += 2; while (p < end) { while (isspace(*p)) p++; From 7d17fa5040b0f1ea0a75bb315272b49a9356bc7d Mon Sep 17 00:00:00 2001 From: "Timothy M. Schaeffer" Date: Thu, 30 Oct 2014 22:00:24 -0400 Subject: [PATCH 3/3] Avoid compiler warning about ignore return from write. --- bar.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/bar.c b/bar.c index d7e4c56..047b6bd 100644 --- a/bar.c +++ b/bar.c @@ -1141,8 +1141,9 @@ main (int argc, char **argv) area_t *area = area_get(press_ev->event, press_ev->event_x); /* Respond to the click */ if (area && area->button == press_ev->detail) { - write(STDOUT_FILENO, area->cmd, strlen(area->cmd)); - write(STDOUT_FILENO, "\n", 1); + ssize_t n; + n = write(STDOUT_FILENO, area->cmd, strlen(area->cmd)); + n = write(STDOUT_FILENO, "\n", 1); } } break;