From 96a96f83568c5365663b9266c856a671b9ab4ca2 Mon Sep 17 00:00:00 2001 From: Gravemind Date: Tue, 29 May 2018 18:20:41 +0200 Subject: [PATCH] Add default color and minor color enhancements The significant code change was to replace the `START_COLOR` and `END_COLOR` macros by an enum and a variable `outcolor` used similarly to `outwalk` (see `OUTPUT_FULL_TEXT`). A few improvements have been made from there: 1. allow any `color_*` config to be set to an empty string to disable it (i.e. force remove the color from the output) (also useful for 2.). 2. add a `color_default` config to general and to all modules/sections, so the default/normal color can be customized everywhere (color of `tztime`, "normal" color of `memory`, etc...). Unspecified by default, so same as old behavior (no color output). 3. fix per-section color emission for `output_format` other than `i3bar`. --- i3status.c | 26 +++++++++++-- include/i3status.h | 48 +++++++++++------------- man/i3status.man | 21 ++++++----- src/output.c | 73 +++++++++++++++++++++++++------------ src/print_battery_info.c | 13 +++---- src/print_cpu_temperature.c | 10 +---- src/print_cpu_usage.c | 11 ++---- src/print_ddate.c | 1 + src/print_disk_info.c | 8 +--- src/print_eth_info.c | 8 ++-- src/print_file_contents.c | 6 +-- src/print_ipv6_addr.c | 7 ++-- src/print_load.c | 8 +--- src/print_mem.c | 16 +++----- src/print_path_exists.c | 4 +- src/print_run_watch.c | 4 +- src/print_time.c | 1 + src/print_volume.c | 24 +++++------- src/print_wireless_info.c | 12 +++--- 19 files changed, 154 insertions(+), 147 deletions(-) diff --git a/i3status.c b/i3status.c index 0898da31..1d53dbbd 100644 --- a/i3status.c +++ b/i3status.c @@ -44,9 +44,12 @@ #define CFG_CUSTOM_ALIGN_OPT \ CFG_STR_CB("align", NULL, CFGF_NONE, parse_align) +#define CFG_CUSTOM_DEFAULT_COLOR_OPT \ + CFG_STR("color_default", NULL, CFGF_NONE) + #define CFG_COLOR_OPTS(good, degraded, bad) \ - CFG_STR("color_good", good, CFGF_NONE) \ - , \ + CFG_CUSTOM_DEFAULT_COLOR_OPT, \ + CFG_STR("color_good", good, CFGF_NONE), \ CFG_STR("color_degraded", degraded, CFGF_NONE), \ CFG_STR("color_bad", bad, CFGF_NONE) @@ -78,6 +81,8 @@ output_format_t output_format; char *pct_mark; +bool enable_colors; + /* * Set the exit_upon_signal flag, because one cannot do anything in a safe * manner in a signal handler (e.g. fprintf, which we really want to do for @@ -175,10 +180,14 @@ static int parse_min_width(cfg_t *context, cfg_opt_t *option, const char *value, } /* - * Validates a color in "#RRGGBB" format + * Validates a color in "#RRGGBB" format, or empty string * */ static int valid_color(const char *value) { + /* allow empty */ + if (value == NULL || value[0] == '\0') + return 1; + const int len = strlen(value); if (output_format == O_LEMONBAR) { @@ -389,6 +398,7 @@ int main(int argc, char *argv[]) { cfg_opt_t time_opts[] = { CFG_STR("format", "%Y-%m-%d %H:%M:%S", CFGF_NONE), CFG_CUSTOM_ALIGN_OPT, + CFG_CUSTOM_DEFAULT_COLOR_OPT, CFG_CUSTOM_MIN_WIDTH_OPT, CFG_CUSTOM_SEPARATOR_OPT, CFG_CUSTOM_SEP_BLOCK_WIDTH_OPT, @@ -401,6 +411,7 @@ int main(int argc, char *argv[]) { CFG_STR("format_time", NULL, CFGF_NONE), CFG_BOOL("hide_if_equals_localtime", false, CFGF_NONE), CFG_CUSTOM_ALIGN_OPT, + CFG_CUSTOM_DEFAULT_COLOR_OPT, CFG_CUSTOM_MIN_WIDTH_OPT, CFG_CUSTOM_SEPARATOR_OPT, CFG_CUSTOM_SEP_BLOCK_WIDTH_OPT, @@ -409,6 +420,7 @@ int main(int argc, char *argv[]) { cfg_opt_t ddate_opts[] = { CFG_STR("format", "%{%a, %b %d%}, %Y%N - %H", CFGF_NONE), CFG_CUSTOM_ALIGN_OPT, + CFG_CUSTOM_DEFAULT_COLOR_OPT, CFG_CUSTOM_MIN_WIDTH_OPT, CFG_CUSTOM_SEPARATOR_OPT, CFG_CUSTOM_SEP_BLOCK_WIDTH_OPT, @@ -626,7 +638,13 @@ int main(int argc, char *argv[]) { if (strcasecmp(separator, "default") == 0) separator = get_default_separator(); - 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"))) + enable_colors = cfg_getbool(cfg_general, "colors"); + + if (!valid_color(cfg_getstr(cfg_general, "color_default")) || + !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"); char *markup_str = cfg_getstr(cfg_general, "markup"); diff --git a/include/i3status.h b/include/i3status.h index 217376a1..850dc15c 100644 --- a/include/i3status.h +++ b/include/i3status.h @@ -17,6 +17,14 @@ typedef enum { } markup_format_t; extern markup_format_t markup_format; +typedef enum { + COLOR_DEFAULT, + COLOR_GOOD, + COLOR_BAD, + COLOR_DEGRADED, + COLOR_SEPARATOR, +} output_color_t; + extern char *pct_mark; #include @@ -87,14 +95,22 @@ extern char *pct_mark; /* Terminate the output buffer here in any case, so that it’s \ * not forgotten in the module */ \ *outwalk = '\0'; \ + const char *colorstr = begin_color_str(outcolor, true); \ if (output_format == O_I3BAR) { \ + if (colorstr) { \ + yajl_gen_string(json_gen, (const unsigned char *)"color", strlen("color")); \ + yajl_gen_string(json_gen, (const unsigned char *)colorstr, strlen(colorstr)); \ + } \ char *_markup = cfg_getstr(cfg_general, "markup"); \ yajl_gen_string(json_gen, (const unsigned char *)"markup", strlen("markup")); \ yajl_gen_string(json_gen, (const unsigned char *)_markup, strlen(_markup)); \ yajl_gen_string(json_gen, (const unsigned char *)"full_text", strlen("full_text")); \ yajl_gen_string(json_gen, (const unsigned char *)text, strlen(text)); \ } else { \ - printf("%s", text); \ + if (colorstr) \ + printf("%s%s%s", colorstr, text, end_color_str()); \ + else \ + printf("%s", text); \ } \ } while (0) @@ -143,30 +159,6 @@ extern char *pct_mark; } \ } while (0) -#define START_COLOR(colorstr) \ - do { \ - if (cfg_getbool(cfg_general, "colors")) { \ - const char *_val = NULL; \ - if (cfg_section) \ - _val = cfg_getstr(cfg_section, colorstr); \ - if (!_val) \ - _val = cfg_getstr(cfg_general, colorstr); \ - if (output_format == O_I3BAR) { \ - yajl_gen_string(json_gen, (const unsigned char *)"color", strlen("color")); \ - yajl_gen_string(json_gen, (const unsigned char *)_val, strlen(_val)); \ - } else { \ - outwalk += sprintf(outwalk, "%s", color(colorstr)); \ - } \ - } \ - } while (0) - -#define END_COLOR \ - do { \ - if (cfg_getbool(cfg_general, "colors") && output_format != O_I3BAR) { \ - outwalk += sprintf(outwalk, "%s", endcolor()); \ - } \ - } while (0) - #define INSTANCE(instance) \ do { \ if (output_format == O_I3BAR) { \ @@ -193,8 +185,8 @@ bool slurp(const char *filename, char *destination, int size); /* src/output.c */ void print_separator(const char *separator); -char *color(const char *colorstr); -char *endcolor() __attribute__((pure)); +const char *begin_color_str(output_color_t outcolor, bool try_cfg_section); +const char *end_color_str() __attribute__((pure)); void reset_cursor(void); void maybe_escape_markup(char *text, char **buffer); @@ -236,6 +228,8 @@ void print_file_contents(yajl_gen json_gen, char *buffer, const char *title, con /* socket file descriptor for general purposes */ extern int general_socket; +extern bool enable_colors; + extern cfg_t *cfg, *cfg_general, *cfg_section; extern void **cur_instance; diff --git a/man/i3status.man b/man/i3status.man index 70ba71a9..e19d3374 100644 --- a/man/i3status.man +++ b/man/i3status.man @@ -133,12 +133,13 @@ read_file uptime { === General The +colors+ directive will disable all colors if you set it to +false+. You can -also specify the colors that will be used to display "good", "degraded" or "bad" -values using the +color_good+, +color_degraded+ or +color_bad+ directives, -respectively. Those directives are only used if color support is not disabled by -the +colors+ directive. The input format for color values is the canonical RGB -hexadecimal triplet (with no separators between the colors), prefixed by a hash -character ("#"). +also specify the colors that will be used to display "default", "good", +"degraded" or "bad" values using the +color_default+, +color_good+, ++color_degraded+ or +color_bad+ directives, respectively. Those directives are +only used if color support is not disabled by the +colors+ directive. The input +format for color values is the canonical RGB hexadecimal triplet (with no +separators between the colors) prefixed by a hash character ("#"), or an empty +string to disable the color. *Example configuration*: ------------------------------------------------------------- @@ -182,10 +183,10 @@ none:: Does not use any color codes. Separates values by the pipe symbol by default. This should be used with i3bar and can be used for custom scripts. -It's also possible to use the color_good, color_degraded, color_bad directives -to define specific colors per module. If one of these directives is defined -in a module section its value will override the value defined in the general -section just for this module. +It's also possible to use the color_default, color_good, color_degraded, +color_bad directives to define specific colors per module. If one of these +directives is defined in a module section its value will override the value +defined in the general section just for this module. If you don't fancy the vertical separators between modules i3status/i3bar uses by default, you can employ the +separator+ directive to configure how diff --git a/src/output.c b/src/output.c index 937fa602..220b4565 100644 --- a/src/output.c +++ b/src/output.c @@ -12,34 +12,67 @@ #include "i3status.h" /* - * Returns the correct color format for dzen (^fg(color)), xmobar () - * or lemonbar (%{Fcolor}) + * Returns the correct color format for i3bar (color), dzen (^fg(color)), xmobar + * (), lemonbar (%{Fcolor}) or terminal (escape-sequence) * */ -char *color(const char *colorstr) { - static char colorbuf[32]; - if (!cfg_getbool(cfg_general, "colors")) { - colorbuf[0] = '\0'; - return colorbuf; +const char *begin_color_str(output_color_t outcolor, bool try_cfg_section) { + if (!enable_colors || output_format == O_NONE) { + return NULL; + } + + const char *color_key = NULL; + switch (outcolor) { + case COLOR_DEFAULT: + color_key = "color_default"; + break; + case COLOR_GOOD: + color_key = "color_good"; + break; + case COLOR_BAD: + color_key = "color_bad"; + break; + case COLOR_DEGRADED: + color_key = "color_degraded"; + break; + case COLOR_SEPARATOR: + color_key = "color_separator"; + break; } + if (!color_key) + return NULL; + + const char *color = NULL; + if (try_cfg_section && cfg_section) + color = cfg_getstr(cfg_section, color_key); + if (!color) + color = cfg_getstr(cfg_general, color_key); + if (!color || color[0] == '\0') + return NULL; + + if (output_format == O_I3BAR) + return color; + + static char colorbuf[32]; if (output_format == O_DZEN2) - (void)snprintf(colorbuf, sizeof(colorbuf), "^fg(%s)", cfg_getstr(cfg_general, colorstr)); + (void)snprintf(colorbuf, sizeof(colorbuf), "^fg(%s)", color); else if (output_format == O_XMOBAR) - (void)snprintf(colorbuf, sizeof(colorbuf), "", cfg_getstr(cfg_general, colorstr)); + (void)snprintf(colorbuf, sizeof(colorbuf), "", color); else if (output_format == O_LEMONBAR) - (void)snprintf(colorbuf, sizeof(colorbuf), "%%{F%s}", cfg_getstr(cfg_general, colorstr)); + (void)snprintf(colorbuf, sizeof(colorbuf), "%%{F%s}", color); else if (output_format == O_TERM) { /* The escape-sequence for color is ;1m (bright/bold * output), where col is a 3-bit rgb-value with b in the * least-significant bit. We round the given color to the * nearist 3-bit-depth color and output the escape-sequence */ - char *str = cfg_getstr(cfg_general, colorstr); - int col = strtol(str + 1, NULL, 16); + int col = strtol(color + 1, NULL, 16); int r = (col & (0xFF << 0)) / 0x80; int g = (col & (0xFF << 8)) / 0x8000; int b = (col & (0xFF << 16)) / 0x800000; col = (r << 2) | (g << 1) | b; (void)snprintf(colorbuf, sizeof(colorbuf), "\033[3%d;1m", col); + } else { + return NULL; } return colorbuf; } @@ -48,7 +81,7 @@ char *color(const char *colorstr) { * Some color formats (xmobar) require to terminate colors again * */ -char *endcolor(void) { +const char *end_color_str(void) { if (output_format == O_XMOBAR) return ""; else if (output_format == O_TERM) @@ -60,16 +93,10 @@ char *endcolor(void) { void print_separator(const char *separator) { if (output_format == O_I3BAR || strlen(separator) == 0) return; - - if (output_format == O_DZEN2) - printf("^fg(%s)%s^fg()", cfg_getstr(cfg_general, "color_separator"), separator); - else if (output_format == O_XMOBAR) - printf("%s", cfg_getstr(cfg_general, "color_separator"), separator); - else if (output_format == O_LEMONBAR) - printf("%%{F%s}%s%%{F-}", cfg_getstr(cfg_general, "color_separator"), separator); - else if (output_format == O_TERM) - printf("%s%s%s", color("color_separator"), separator, endcolor()); - else if (output_format == O_NONE) + const char *colorstr = begin_color_str(COLOR_SEPARATOR, false); + if (colorstr) + printf("%s%s%s", colorstr, separator, end_color_str()); + else printf("%s", separator); } diff --git a/src/print_battery_info.c b/src/print_battery_info.c index 1c516242..36f16bc7 100644 --- a/src/print_battery_info.c +++ b/src/print_battery_info.c @@ -132,6 +132,7 @@ static void add_battery_info(struct battery_info *acc, const struct battery_info static bool slurp_battery_info(struct battery_info *batt_info, yajl_gen json_gen, char *buffer, int number, const char *path, const char *format_down) { char *outwalk = buffer; + output_color_t outcolor = COLOR_DEFAULT; #if defined(__linux__) char buf[1024]; @@ -472,6 +473,7 @@ static bool slurp_battery_info(struct battery_info *batt_info, yajl_gen json_gen static bool slurp_all_batteries(struct battery_info *batt_info, yajl_gen json_gen, char *buffer, const char *path, const char *format_down) { #if defined(__linux__) char *outwalk = buffer; + output_color_t outcolor = COLOR_DEFAULT; bool is_found = false; char *placeholder; @@ -530,6 +532,7 @@ static bool slurp_all_batteries(struct battery_info *batt_info, yajl_gen json_ge void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char *path, const char *format, const char *format_down, const char *status_chr, const char *status_bat, const char *status_unk, const char *status_full, int low_threshold, char *threshold_type, bool last_full_capacity, bool integer_battery_capacity, bool hide_seconds) { const char *walk; char *outwalk = buffer; + output_color_t outcolor = COLOR_DEFAULT; struct battery_info batt_info = { .full_design = -1, .full_last = -1, @@ -539,7 +542,6 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char .percentage_remaining = -1, .status = CS_UNKNOWN, }; - bool colorful_output = false; #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__OpenBSD__) /* These OSes report battery stats in whole percent. */ @@ -600,11 +602,9 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char if (batt_info.status == CS_DISCHARGING && low_threshold > 0) { if (batt_info.percentage_remaining >= 0 && strcasecmp(threshold_type, "percentage") == 0 && batt_info.percentage_remaining < low_threshold) { - START_COLOR("color_bad"); - colorful_output = true; + outcolor = COLOR_BAD; } else if (batt_info.seconds_remaining >= 0 && strcasecmp(threshold_type, "time") == 0 && batt_info.seconds_remaining < 60 * low_threshold) { - START_COLOR("color_bad"); - colorful_output = true; + outcolor = COLOR_BAD; } } @@ -698,8 +698,5 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char } } - if (colorful_output) - END_COLOR; - OUTPUT_FULL_TEXT(buffer); } diff --git a/src/print_cpu_temperature.c b/src/print_cpu_temperature.c index 01744a14..5e0a2d64 100644 --- a/src/print_cpu_temperature.c +++ b/src/print_cpu_temperature.c @@ -211,10 +211,10 @@ static int read_temperature(char *thermal_zone, temperature_t *temperature) { */ void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const char *path, const char *format, const char *format_above_threshold, int max_threshold) { char *outwalk = buffer; + output_color_t outcolor = COLOR_DEFAULT; #ifdef THERMAL_ZONE const char *selected_format = format; const char *walk; - bool colorful_output = false; char *thermal_zone; temperature_t temperature; temperature.raw_value = 0; @@ -242,8 +242,7 @@ void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const goto error; if (temperature.raw_value >= max_threshold) { - START_COLOR("color_bad"); - colorful_output = true; + outcolor = COLOR_BAD; if (format_above_threshold != NULL) selected_format = format_above_threshold; } @@ -261,11 +260,6 @@ void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const } } - if (colorful_output) { - END_COLOR; - colorful_output = false; - } - free(thermal_zone); OUTPUT_FULL_TEXT(buffer); diff --git a/src/print_cpu_usage.c b/src/print_cpu_usage.c index a5021bfc..107704e4 100644 --- a/src/print_cpu_usage.c +++ b/src/print_cpu_usage.c @@ -59,9 +59,9 @@ void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format, const const char *selected_format = format; const char *walk; char *outwalk = buffer; + output_color_t outcolor = COLOR_DEFAULT; struct cpu_usage curr_all = {0, 0, 0, 0, 0}; int diff_idle, diff_total, diff_usage; - bool colorful_output = false; #if defined(__linux__) @@ -145,13 +145,11 @@ void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format, const #endif if (diff_usage >= max_threshold) { - START_COLOR("color_bad"); - colorful_output = true; + outcolor = COLOR_BAD; if (format_above_threshold != NULL) selected_format = format_above_threshold; } else if (diff_usage >= degraded_threshold) { - START_COLOR("color_degraded"); - colorful_output = true; + outcolor = COLOR_DEGRADED; if (format_above_degraded_threshold != NULL) selected_format = format_above_degraded_threshold; } @@ -196,9 +194,6 @@ void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format, const prev_cpus = curr_cpus; curr_cpus = temp_cpus; - if (colorful_output) - END_COLOR; - OUTPUT_FULL_TEXT(buffer); return; error: diff --git a/src/print_ddate.c b/src/print_ddate.c index 93e652d6..78e5a0ef 100644 --- a/src/print_ddate.c +++ b/src/print_ddate.c @@ -202,6 +202,7 @@ struct disc_time *get_ddate(struct tm *current_tm) { void print_ddate(yajl_gen json_gen, char *buffer, const char *format, time_t t) { char *outwalk = buffer; + output_color_t outcolor = COLOR_DEFAULT; static char *form = NULL; struct tm current_tm; struct disc_time *dt; diff --git a/src/print_disk_info.c b/src/print_disk_info.c index 5f12e930..5043f9b4 100644 --- a/src/print_disk_info.c +++ b/src/print_disk_info.c @@ -115,7 +115,7 @@ void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const ch const char *selected_format = format; const char *walk; char *outwalk = buffer; - bool colorful_output = false; + output_color_t outcolor = COLOR_DEFAULT; bool mounted = false; INSTANCE(path); @@ -171,8 +171,7 @@ void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const ch format_not_mounted = ""; selected_format = format_not_mounted; } else if (low_threshold > 0 && below_threshold(buf, prefix_type, threshold_type, low_threshold)) { - START_COLOR("color_bad"); - colorful_output = true; + outcolor = COLOR_BAD; if (format_below_threshold != NULL) selected_format = format_below_threshold; } @@ -218,9 +217,6 @@ void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const ch } } - if (colorful_output) - END_COLOR; - *outwalk = '\0'; OUTPUT_FULL_TEXT(buffer); } diff --git a/src/print_eth_info.c b/src/print_eth_info.c index 81e74a79..8b8d28b0 100644 --- a/src/print_eth_info.c +++ b/src/print_eth_info.c @@ -139,6 +139,7 @@ void print_eth_info(yajl_gen json_gen, char *buffer, const char *interface, cons const char *walk; char *outwalk = buffer; + output_color_t outcolor = COLOR_DEFAULT; INSTANCE(interface); @@ -159,7 +160,7 @@ void print_eth_info(yajl_gen json_gen, char *buffer, const char *interface, cons bool prefer_ipv4 = true; if (ipv4_address == NULL) { if (ipv6_address == NULL) { - START_COLOR("color_bad"); + outcolor = COLOR_BAD; goto out; } else { prefer_ipv4 = false; @@ -172,9 +173,9 @@ void print_eth_info(yajl_gen json_gen, char *buffer, const char *interface, cons const char *ip_address = (prefer_ipv4) ? ipv4_address : ipv6_address; if (BEGINS_WITH(ip_address, "no IP")) { - START_COLOR("color_degraded"); + outcolor = COLOR_DEGRADED; } else { - START_COLOR("color_good"); + outcolor = COLOR_GOOD; } out: @@ -198,7 +199,6 @@ void print_eth_info(yajl_gen json_gen, char *buffer, const char *interface, cons *(outwalk++) = '%'; } } - END_COLOR; free(ipv4_address); free(ipv6_address); OUTPUT_FULL_TEXT(buffer); diff --git a/src/print_file_contents.c b/src/print_file_contents.c index afbe3e3c..e08ca477 100644 --- a/src/print_file_contents.c +++ b/src/print_file_contents.c @@ -23,6 +23,7 @@ static void *scalloc(size_t size) { void print_file_contents(yajl_gen json_gen, char *buffer, const char *title, const char *path, const char *format, const char *format_bad, const int max_chars) { const char *walk = format; char *outwalk = buffer; + output_color_t outcolor = COLOR_DEFAULT; char *buf = scalloc(max_chars * sizeof(char)); int n = -1; @@ -36,10 +37,10 @@ void print_file_contents(yajl_gen json_gen, char *buffer, const char *title, con buf[n] = '\0'; } (void)close(fd); - START_COLOR("color_good"); + outcolor = COLOR_GOOD; } else if (errno != 0) { walk = format_bad; - START_COLOR("color_bad"); + outcolor = COLOR_BAD; } for (; *walk != '\0'; walk++) { @@ -68,6 +69,5 @@ void print_file_contents(yajl_gen json_gen, char *buffer, const char *title, con free(buf); - END_COLOR; OUTPUT_FULL_TEXT(buffer); } diff --git a/src/print_ipv6_addr.c b/src/print_ipv6_addr.c index 5a0c38d9..fc8e755c 100644 --- a/src/print_ipv6_addr.c +++ b/src/print_ipv6_addr.c @@ -121,16 +121,16 @@ void print_ipv6_info(yajl_gen json_gen, char *buffer, const char *format_up, con const char *walk; char *addr_string = get_ipv6_addr(); char *outwalk = buffer; + output_color_t outcolor = COLOR_DEFAULT; if (addr_string == NULL) { - START_COLOR("color_bad"); + outcolor = COLOR_BAD; outwalk += sprintf(outwalk, "%s", format_down); - END_COLOR; OUTPUT_FULL_TEXT(buffer); return; } - START_COLOR("color_good"); + outcolor = COLOR_GOOD; for (walk = format_up; *walk != '\0'; walk++) { if (*walk != '%') { *(outwalk++) = *walk; @@ -143,6 +143,5 @@ void print_ipv6_info(yajl_gen json_gen, char *buffer, const char *format_up, con *(outwalk++) = '%'; } } - END_COLOR; OUTPUT_FULL_TEXT(buffer); } diff --git a/src/print_load.c b/src/print_load.c index 5d97a2ca..5c75ea11 100644 --- a/src/print_load.c +++ b/src/print_load.c @@ -9,20 +9,19 @@ void print_load(yajl_gen json_gen, char *buffer, const char *format, const char *format_above_threshold, const float max_threshold) { char *outwalk = buffer; + output_color_t outcolor = COLOR_DEFAULT; /* Get load */ #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(linux) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__) || defined(sun) || defined(__DragonFly__) double loadavg[3]; const char *selected_format = format; const char *walk; - bool colorful_output = false; if (getloadavg(loadavg, 3) == -1) goto error; if (loadavg[0] >= max_threshold) { - START_COLOR("color_bad"); - colorful_output = true; + outcolor = COLOR_BAD; if (format_above_threshold != NULL) selected_format = format_above_threshold; } @@ -48,9 +47,6 @@ void print_load(yajl_gen json_gen, char *buffer, const char *format, const char } } - if (colorful_output) - END_COLOR; - *outwalk = '\0'; OUTPUT_FULL_TEXT(buffer); diff --git a/src/print_mem.c b/src/print_mem.c index 840f997e..b590047e 100644 --- a/src/print_mem.c +++ b/src/print_mem.c @@ -75,11 +75,11 @@ static long memory_absolute(const long mem_total, const char *size) { void print_memory(yajl_gen json_gen, char *buffer, const char *format, const char *format_degraded, const char *threshold_degraded, const char *threshold_critical, const char *memory_used_method) { char *outwalk = buffer; + output_color_t outcolor = COLOR_DEFAULT; #if defined(linux) const char *selected_format = format; const char *walk; - const char *output_color = NULL; long ram_total = -1; long ram_free = -1; @@ -139,22 +139,19 @@ void print_memory(yajl_gen json_gen, char *buffer, const char *format, const cha if (threshold_degraded) { long abs = memory_absolute(ram_total, threshold_degraded); if (ram_available < abs) { - output_color = "color_degraded"; + outcolor = COLOR_DEGRADED; } } if (threshold_critical) { long abs = memory_absolute(ram_total, threshold_critical); if (ram_available < abs) { - output_color = "color_bad"; + outcolor = COLOR_BAD; } } - if (output_color) { - START_COLOR(output_color); - - if (format_degraded) - selected_format = format_degraded; + if (outcolor != COLOR_DEFAULT && format_degraded) { + selected_format = format_degraded; } for (walk = selected_format; *walk != '\0'; walk++) { @@ -202,9 +199,6 @@ void print_memory(yajl_gen json_gen, char *buffer, const char *format, const cha } } - if (output_color) - END_COLOR; - *outwalk = '\0'; OUTPUT_FULL_TEXT(buffer); diff --git a/src/print_path_exists.c b/src/print_path_exists.c index 87601b43..fce72a09 100644 --- a/src/print_path_exists.c +++ b/src/print_path_exists.c @@ -10,6 +10,7 @@ void print_path_exists(yajl_gen json_gen, char *buffer, const char *title, const char *path, const char *format, const char *format_down) { const char *walk; char *outwalk = buffer; + output_color_t outcolor = COLOR_DEFAULT; struct stat st; const bool exists = (stat(path, &st) == 0); @@ -21,7 +22,7 @@ void print_path_exists(yajl_gen json_gen, char *buffer, const char *title, const INSTANCE(path); - START_COLOR((exists ? "color_good" : "color_bad")); + outcolor = (exists ? COLOR_GOOD : COLOR_BAD); for (; *walk != '\0'; walk++) { if (*walk != '%') { @@ -40,6 +41,5 @@ void print_path_exists(yajl_gen json_gen, char *buffer, const char *title, const } } - END_COLOR; OUTPUT_FULL_TEXT(buffer); } diff --git a/src/print_run_watch.c b/src/print_run_watch.c index f0a0746b..1e39b0e0 100644 --- a/src/print_run_watch.c +++ b/src/print_run_watch.c @@ -10,6 +10,7 @@ void print_run_watch(yajl_gen json_gen, char *buffer, const char *title, const c bool running = process_runs(pidfile); const char *walk; char *outwalk = buffer; + output_color_t outcolor = COLOR_DEFAULT; if (running || format_down == NULL) { walk = format; @@ -19,7 +20,7 @@ void print_run_watch(yajl_gen json_gen, char *buffer, const char *title, const c INSTANCE(pidfile); - START_COLOR((running ? "color_good" : "color_bad")); + outcolor = (running ? COLOR_GOOD : COLOR_BAD); for (; *walk != '\0'; walk++) { if (*walk != '%') { @@ -38,6 +39,5 @@ void print_run_watch(yajl_gen json_gen, char *buffer, const char *title, const c } } - END_COLOR; OUTPUT_FULL_TEXT(buffer); } diff --git a/src/print_time.c b/src/print_time.c index 5133c510..91fd3443 100644 --- a/src/print_time.c +++ b/src/print_time.c @@ -37,6 +37,7 @@ void set_timezone(const char *tz) { void print_time(yajl_gen json_gen, char *buffer, const char *title, const char *format, const char *tz, const char *locale, const char *format_time, bool hide_if_equals_localtime, time_t t) { const char *walk; + output_color_t outcolor = COLOR_DEFAULT; char *outwalk = buffer; struct tm local_tm, tm; char timebuf[1024]; diff --git a/src/print_volume.c b/src/print_volume.c index 91e8ce28..708e2021 100644 --- a/src/print_volume.c +++ b/src/print_volume.c @@ -41,10 +41,11 @@ } #define ALSA_MUTE_SWITCH(channel) \ - if ((err = snd_mixer_selem_get_##channel##_switch(elem, 0, &pbval)) < 0) \ + int value = 1; \ + if ((err = snd_mixer_selem_get_##channel##_switch(elem, 0, &value)) < 0) \ fprintf(stderr, "i3status: ALSA: " #channel "_switch: %s\n", snd_strerror(err)); \ - if (!pbval) { \ - START_COLOR("color_degraded"); \ + if (!value) { \ + outcolor = COLOR_DEGRADED; \ fmt = fmt_muted; \ } @@ -76,7 +77,7 @@ static char *apply_volume_format(const char *fmt, char *outwalk, int ivolume, co void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char *fmt_muted, const char *device, const char *mixer, int mixer_idx) { char *outwalk = buffer; - int pbval = 1; + output_color_t outcolor = COLOR_DEFAULT; /* Printing volume works with ALSA and PulseAudio at the moment */ if (output_format == O_I3BAR) { @@ -112,8 +113,7 @@ void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char * int ivolume = DECOMPOSE_VOLUME(cvolume); bool muted = DECOMPOSE_MUTED(cvolume); if (muted) { - START_COLOR("color_degraded"); - pbval = 0; + outcolor = COLOR_DEGRADED; } /* negative result means error, stick to 0 */ @@ -133,8 +133,7 @@ void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char * bool muted = DECOMPOSE_MUTED(cvolume); if (ivolume >= 0 && success) { if (muted) { - START_COLOR("color_degraded"); - pbval = 0; + outcolor = COLOR_DEGRADED; } outwalk = apply_volume_format(muted ? fmt_muted : fmt, outwalk, @@ -253,7 +252,6 @@ void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char * char defaultmixer[] = "/dev/mixer"; int mixfd, vol, devmask = 0; const char *devicename = "UNSUPPORTED"; /* TODO: implement support for this */ - pbval = 1; if (mixer_idx > 0) asprintf(&mixerpath, "/dev/mixer%d", mixer_idx); @@ -333,9 +331,8 @@ void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char * goto out; if (master_mute_idx != -1 && vinfo.un.ord) { - START_COLOR("color_degraded"); + outcolor = COLOR_DEGRADED; fmt = fmt_muted; - pbval = 0; } #else @@ -349,8 +346,7 @@ void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char * } if (((vol & 0x7f) == 0) && (((vol >> 8) & 0x7f) == 0)) { - START_COLOR("color_degraded"); - pbval = 0; + outcolor = COLOR_DEGRADED; } #endif @@ -360,7 +356,5 @@ void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char * out: *outwalk = '\0'; - if (!pbval) - END_COLOR; OUTPUT_FULL_TEXT(buffer); } diff --git a/src/print_wireless_info.c b/src/print_wireless_info.c index 8b2d2106..b13f0fd9 100644 --- a/src/print_wireless_info.c +++ b/src/print_wireless_info.c @@ -483,6 +483,7 @@ static int get_wireless_info(const char *interface, wireless_info_t *info) { void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down, const char *format_quality) { const char *walk; char *outwalk = buffer; + output_color_t outcolor = COLOR_DEFAULT; wireless_info_t info; INSTANCE(interface); @@ -504,7 +505,7 @@ void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, bool prefer_ipv4 = true; if (ipv4_address == NULL) { if (ipv6_address == NULL) { - START_COLOR("color_bad"); + outcolor = COLOR_BAD; outwalk += sprintf(outwalk, "%s", format_down); goto out; } else { @@ -517,16 +518,16 @@ void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, const char *ip_address = (prefer_ipv4) ? ipv4_address : ipv6_address; if (!get_wireless_info(interface, &info)) { walk = format_down; - START_COLOR("color_bad"); + outcolor = COLOR_BAD; } else { walk = format_up; if (info.flags & WIRELESS_INFO_FLAG_HAS_QUALITY) - START_COLOR((info.quality < info.quality_average ? "color_degraded" : "color_good")); + outcolor = (info.quality < info.quality_average ? COLOR_DEGRADED : COLOR_GOOD); else { if (BEGINS_WITH(ip_address, "no IP")) { - START_COLOR("color_degraded"); + outcolor = COLOR_DEGRADED; } else { - START_COLOR("color_good"); + outcolor = COLOR_GOOD; } } } @@ -604,7 +605,6 @@ void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, } out: - END_COLOR; free(ipv4_address); free(ipv6_address); OUTPUT_FULL_TEXT(buffer);