]> git.sur5r.net Git - i3/i3status/commitdiff
Support customized colors.
authorFernando Tarlá Cardoso Lemos <fernandotcl@gmail.com>
Tue, 29 Jun 2010 23:59:03 +0000 (20:59 -0300)
committerMichael Stapelberg <michael@stapelberg.de>
Sun, 4 Jul 2010 19:52:43 +0000 (21:52 +0200)
i3status.c
src/output.c
src/print_eth_info.c
src/print_run_watch.c
src/print_wireless_info.c

index 64fac76997138321866cf87c8c7b708bea705cf6..eabcd6c68cbf79469ca105e0013673da42ad3718 100644 (file)
@@ -65,12 +65,33 @@ static char *file_exists(char *path) {
         return full_path;
 }
 
+/*
+ * Validates a color in "#RRGGBB" format
+ *
+ */
+static int valid_color(const char *value)
+{
+        if (strlen(value) != 7) return 0;
+        if (value[0] != '#') return 0;
+        for (int i = 1; i < 7; ++i) {
+                if (value[i] >= '0' && value[i] <= '9') continue;
+                if (value[i] >= 'a' && value[i] <= 'f') continue;
+                if (value[i] >= 'A' && value[i] <= 'F') continue;
+                return 0;
+        }
+        return 1;
+}
+
 int main(int argc, char *argv[]) {
         unsigned int j;
 
         cfg_opt_t general_opts[] = {
                 CFG_STR("output_format", "dzen2", CFGF_NONE),
                 CFG_BOOL("colors", 1, CFGF_NONE),
+                CFG_STR("color_good", "#00FF00", CFGF_NONE),
+                CFG_STR("color_degraded", "#FFFF00", CFGF_NONE),
+                CFG_STR("color_bad", "#FF0000", CFGF_NONE),
+                CFG_STR("color_separator", "#333333", CFGF_NONE),
                 CFG_INT("interval", 1, CFGF_NONE),
                 CFG_END()
         };
@@ -194,6 +215,12 @@ int main(int argc, char *argv[]) {
                 output_format = O_NONE;
         else die("Unknown output format: \"%s\"\n", output_str);
 
+        if (!valid_color(cfg_getstr(cfg_general, "color_good"))
+                        || !valid_color(cfg_getstr(cfg_general, "color_degraded"))
+                        || !valid_color(cfg_getstr(cfg_general, "color_bad"))
+                        || !valid_color(cfg_getstr(cfg_general, "color_separator")))
+               die("Bad color format");
+
         if ((general_socket = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
                 die("Could not create socket\n");
 
index 3d6666e1409c494baa1829151538e22f5d6dc562..c0c1480bcbed785bf6f0ee03ac9327379df7b9a4 100644 (file)
@@ -21,9 +21,9 @@ char *color(const char *colorstr) {
                 return colorbuf;
         }
         if (output_format == O_DZEN2)
-                (void)snprintf(colorbuf, sizeof(colorbuf), "^fg(%s)", colorstr);
+                (void)snprintf(colorbuf, sizeof(colorbuf), "^fg(%s)", cfg_getstr(cfg_general, colorstr));
         else if (output_format == O_XMOBAR)
-                (void)snprintf(colorbuf, sizeof(colorbuf), "<fc=%s>", colorstr);
+                (void)snprintf(colorbuf, sizeof(colorbuf), "<fc=%s>", cfg_getstr(cfg_general, colorstr));
 
         return colorbuf;
 }
@@ -40,9 +40,9 @@ char *endcolor() {
 
 void print_seperator() {
         if (output_format == O_DZEN2)
-                printf("^fg(#333333)^p(5;-2)^ro(2)^p()^fg()^p(5)");
+                printf("^fg(%s)^p(5;-2)^ro(2)^p()^fg()^p(5)", cfg_getstr(cfg_general, "color_separator"));
         else if (output_format == O_XMOBAR)
-                printf("<fc=#333333> | </fc>");
+                printf("<fc=%s> | </fc>", cfg_getstr(cfg_general, "color_separator"));
         else if (output_format == O_NONE)
                 printf(" | ");
 }
index 9ad9b922e135e1c82144d9369c065c109ea76595..28ba6c1aef1dd67a319c8bfac298b44ac0faff35 100644 (file)
@@ -79,12 +79,12 @@ void print_eth_info(const char *interface, const char *format_up, const char *fo
         const char *ip_address = get_ip_addr(interface);
 
         if (ip_address == NULL) {
-                printf("%s", color("#FF0000"));
+                printf("%s", color("color_bad"));
                 printf("%s", format_down);
                 (void)printf("%s", endcolor());
                 return;
         } else {
-                printf("%s", color("#00FF00"));
+                printf("%s", color("color_good"));
         }
 
         for (walk = format_up; *walk != '\0'; walk++) {
index 807e322b07f4989f65dfbbb7bce31575834d0c85..455130e91bab049ce1c024e42f75f1b061e5a355 100644 (file)
@@ -6,7 +6,7 @@ void print_run_watch(const char *title, const char *pidfile, const char *format)
        bool running = process_runs(pidfile);
        const char *walk;
 
-       printf("%s", (running ? color("#00FF00") : color("#FF0000")));
+       printf("%s", (running ? color("color_good") : color("color_bad")));
 
         for (walk = format; *walk != '\0'; walk++) {
                 if (*walk != '%') {
index 22a9c3191f8dfa69fd6f1768070e6a641b18e65d..60eb90644454a408a232a065a76e4593ea149ae2 100644 (file)
@@ -143,11 +143,11 @@ void print_wireless_info(const char *interface, const char *format_up, const cha
         if (get_wireless_info(interface, &info)) {
                 walk = format_up;
                 if (info.flags & WIRELESS_INFO_FLAG_HAS_QUALITY)
-                        printf("%s", info.quality < info.quality_average ? color("#FFFF00") : color("#00FF00"));
+                        printf("%s", info.quality < info.quality_average ? color("color_degraded") : color("color_good"));
         }
         else {
                 walk = format_down;
-                printf("%s", color("#FF0000"));
+                printf("%s", color("color_bad"));
         }
 
         for (; *walk != '\0'; walk++) {