From 7340a539d51c9408ab7e0fe11a088ec052de1dfc Mon Sep 17 00:00:00 2001 From: Gravemind Date: Tue, 29 May 2018 18:20:41 +0200 Subject: [PATCH] Color config enhancements Replace the macros `START_COLOR` and `END_COLOR` by an enum and a variable `outcolor` (similar to `outwalk`, see `OUTPUT_FULL_TEXT`). This allowed for the addition of a couple user-side features: Add a new `color_default` config to the general and to all modules. So the default color can now be customized per-module. An unspecified or empty value means no color (same as old behavior). Any `color_*` config (in general or modules) can now be set to an empty string to disable it (no color emitted). --- i3status.c | 26 +++++++++++-- include/i3status.h | 48 +++++++++++------------- man/i3status.man | 21 ++++++----- src/output.c | 74 +++++++++++++++++++++++++------------ src/print_battery_info.c | 14 +++---- 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 | 13 ++----- src/print_path_exists.c | 4 +- src/print_run_watch.c | 4 +- src/print_time.c | 1 + src/print_volume.c | 20 +++------- src/print_wireless_info.c | 13 +++---- 19 files changed, 151 insertions(+), 146 deletions(-) diff --git a/i3status.c b/i3status.c index 2f60b4e5..f1e008d1 100644 --- a/i3status.c +++ b/i3status.c @@ -37,9 +37,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) @@ -71,6 +74,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 @@ -153,10 +158,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) { @@ -339,6 +348,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, @@ -351,6 +361,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, @@ -359,6 +370,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, @@ -584,7 +596,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 1bc2c85f..efae106f 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(ctx->json_gen, (const unsigned char *)"color", strlen("color")); \ + yajl_gen_string(ctx->json_gen, (const unsigned char *)colorstr, strlen(colorstr)); \ + } \ char *_markup = cfg_getstr(cfg_general, "markup"); \ yajl_gen_string(ctx->json_gen, (const unsigned char *)"markup", strlen("markup")); \ yajl_gen_string(ctx->json_gen, (const unsigned char *)_markup, strlen(_markup)); \ yajl_gen_string(ctx->json_gen, (const unsigned char *)"full_text", strlen("full_text")); \ yajl_gen_string(ctx->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(ctx->json_gen, (const unsigned char *)"color", strlen("color")); \ - yajl_gen_string(ctx->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) { \ @@ -201,8 +193,8 @@ char *sstrdup(const char *str); /* 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); @@ -445,6 +437,8 @@ void print_file_contents(file_contents_ctx_t *ctx); /* 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 3cd231cb..ab18f17b 100644 --- a/man/i3status.man +++ b/man/i3status.man @@ -144,12 +144,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*: ------------------------------------------------------------- @@ -193,10 +194,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 9a180497..4eec0232 100644 --- a/src/output.c +++ b/src/output.c @@ -13,34 +13,68 @@ #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). Returns + * NULL if the color is disabled. * */ -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; } @@ -49,7 +83,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) @@ -61,16 +95,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 8864978b..445a8851 100644 --- a/src/print_battery_info.c +++ b/src/print_battery_info.c @@ -145,6 +145,7 @@ static void add_battery_info(struct battery_info *acc, const struct battery_info static bool slurp_battery_info(battery_info_ctx_t *ctx, 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]; @@ -529,6 +530,7 @@ static bool slurp_battery_info(battery_info_ctx_t *ctx, struct battery_info *bat static bool slurp_all_batteries(battery_info_ctx_t *ctx, 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; @@ -586,6 +588,7 @@ static bool slurp_all_batteries(battery_info_ctx_t *ctx, struct battery_info *ba void print_battery_info(battery_info_ctx_t *ctx) { char *outwalk = ctx->buf; + output_color_t outcolor = COLOR_DEFAULT; struct battery_info batt_info = { .full_design = -1, .full_last = -1, @@ -595,7 +598,6 @@ void print_battery_info(battery_info_ctx_t *ctx) { .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. */ @@ -658,11 +660,9 @@ void print_battery_info(battery_info_ctx_t *ctx) { if (batt_info.status == CS_DISCHARGING && ctx->low_threshold > 0) { if (batt_info.percentage_remaining >= 0 && strcasecmp(ctx->threshold_type, "percentage") == 0 && batt_info.percentage_remaining < ctx->low_threshold) { - START_COLOR("color_bad"); - colorful_output = true; + outcolor = COLOR_BAD; } else if (batt_info.seconds_remaining >= 0 && strcasecmp(ctx->threshold_type, "time") == 0 && batt_info.seconds_remaining < 60 * ctx->low_threshold) { - START_COLOR("color_bad"); - colorful_output = true; + outcolor = COLOR_BAD; } } @@ -732,9 +732,5 @@ void print_battery_info(battery_info_ctx_t *ctx) { free(formatted); free(untrimmed); - if (colorful_output) { - END_COLOR; - } - OUTPUT_FULL_TEXT(ctx->buf); } diff --git a/src/print_cpu_temperature.c b/src/print_cpu_temperature.c index 9c9bbf0e..95abfb7e 100644 --- a/src/print_cpu_temperature.c +++ b/src/print_cpu_temperature.c @@ -213,9 +213,9 @@ static int read_temperature(char *thermal_zone, temperature_t *temperature) { */ void print_cpu_temperature_info(cpu_temperature_ctx_t *ctx) { char *outwalk = ctx->buf; + output_color_t outcolor = COLOR_DEFAULT; #ifdef THERMAL_ZONE const char *selected_format = ctx->format; - bool colorful_output = false; char *thermal_zone; temperature_t temperature; temperature.raw_value = 0; @@ -243,8 +243,7 @@ void print_cpu_temperature_info(cpu_temperature_ctx_t *ctx) { goto error; if (temperature.raw_value >= ctx->max_threshold) { - START_COLOR("color_bad"); - colorful_output = true; + outcolor = COLOR_BAD; if (ctx->format_above_threshold != NULL) selected_format = ctx->format_above_threshold; } @@ -259,11 +258,6 @@ void print_cpu_temperature_info(cpu_temperature_ctx_t *ctx) { OUTPUT_FORMATTED; free(formatted); - if (colorful_output) { - END_COLOR; - colorful_output = false; - } - free(thermal_zone); OUTPUT_FULL_TEXT(ctx->buf); diff --git a/src/print_cpu_usage.c b/src/print_cpu_usage.c index a7ee2e46..cc366161 100644 --- a/src/print_cpu_usage.c +++ b/src/print_cpu_usage.c @@ -73,6 +73,7 @@ void print_cpu_usage(cpu_usage_ctx_t *ctx) { const char *selected_format = ctx->format; const char *walk; char *outwalk = ctx->buf; + output_color_t outcolor = COLOR_DEFAULT; struct cpu_usage curr_all = {0, 0, 0, 0, 0}; #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) long diff_idle, diff_total; @@ -80,7 +81,6 @@ void print_cpu_usage(cpu_usage_ctx_t *ctx) { int diff_idle, diff_total; #endif int diff_usage; - bool colorful_output = false; #if defined(__linux__) @@ -185,13 +185,11 @@ void print_cpu_usage(cpu_usage_ctx_t *ctx) { #endif if (diff_usage >= ctx->max_threshold) { - START_COLOR("color_bad"); - colorful_output = true; + outcolor = COLOR_BAD; if (ctx->format_above_threshold != NULL) selected_format = ctx->format_above_threshold; } else if (diff_usage >= ctx->degraded_threshold) { - START_COLOR("color_degraded"); - colorful_output = true; + outcolor = COLOR_DEGRADED; if (ctx->format_above_degraded_threshold != NULL) selected_format = ctx->format_above_degraded_threshold; } @@ -231,9 +229,6 @@ void print_cpu_usage(cpu_usage_ctx_t *ctx) { prev_cpus = curr_cpus; curr_cpus = temp_cpus; - if (colorful_output) - END_COLOR; - OUTPUT_FULL_TEXT(ctx->buf); return; error: diff --git a/src/print_ddate.c b/src/print_ddate.c index d9cafd37..d53b1fed 100644 --- a/src/print_ddate.c +++ b/src/print_ddate.c @@ -94,6 +94,7 @@ struct disc_time *get_ddate(struct tm *current_tm) { void print_ddate(ddate_ctx_t *ctx) { char *outwalk = ctx->buf; + output_color_t outcolor = COLOR_DEFAULT; struct tm current_tm; struct disc_time *dt; set_timezone(NULL); /* Use local time. */ diff --git a/src/print_disk_info.c b/src/print_disk_info.c index 8be8deef..e6e8f861 100644 --- a/src/print_disk_info.c +++ b/src/print_disk_info.c @@ -127,7 +127,7 @@ static bool below_threshold(struct statvfs buf, const char *prefix_type, const c void print_disk_info(disk_info_ctx_t *ctx) { const char *selected_format = ctx->format; char *outwalk = ctx->buf; - bool colorful_output = false; + output_color_t outcolor = COLOR_DEFAULT; bool mounted = false; INSTANCE(ctx->path); @@ -183,8 +183,7 @@ void print_disk_info(disk_info_ctx_t *ctx) { ctx->format_not_mounted = ""; selected_format = ctx->format_not_mounted; } else if (ctx->low_threshold > 0 && below_threshold(buf, ctx->prefix_type, ctx->threshold_type, ctx->low_threshold)) { - START_COLOR("color_bad"); - colorful_output = true; + outcolor = COLOR_BAD; if (ctx->format_below_threshold != NULL) selected_format = ctx->format_below_threshold; } @@ -229,9 +228,6 @@ void print_disk_info(disk_info_ctx_t *ctx) { OUTPUT_FORMATTED; free(formatted); - if (colorful_output) - END_COLOR; - *outwalk = '\0'; OUTPUT_FULL_TEXT(ctx->buf); } diff --git a/src/print_eth_info.c b/src/print_eth_info.c index 9e2e22fe..f141d33c 100644 --- a/src/print_eth_info.c +++ b/src/print_eth_info.c @@ -147,6 +147,7 @@ void print_eth_info(eth_info_ctx_t *ctx) { const char *format = ctx->format_down; // default format char *outwalk = ctx->buf; + output_color_t outcolor = COLOR_DEFAULT; size_t num = 0; INSTANCE(ctx->interface); @@ -168,7 +169,7 @@ void print_eth_info(eth_info_ctx_t *ctx) { 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; @@ -181,9 +182,9 @@ void print_eth_info(eth_info_ctx_t *ctx) { 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; } char string_ip[STRING_SIZE]; @@ -203,7 +204,6 @@ out : { OUTPUT_FORMATTED; free(formatted); - END_COLOR; free(ipv4_address); free(ipv6_address); OUTPUT_FULL_TEXT(ctx->buf); diff --git a/src/print_file_contents.c b/src/print_file_contents.c index 486d91b1..e9035860 100644 --- a/src/print_file_contents.c +++ b/src/print_file_contents.c @@ -17,6 +17,7 @@ void print_file_contents(file_contents_ctx_t *ctx) { const char *walk = ctx->format; char *outwalk = ctx->buf; + output_color_t outcolor = COLOR_DEFAULT; char *buf = scalloc(ctx->max_chars * sizeof(char) + 1); if (ctx->path == NULL) { @@ -38,10 +39,10 @@ void print_file_contents(file_contents_ctx_t *ctx) { buf[n] = '\0'; } (void)close(fd); - START_COLOR("color_good"); + outcolor = COLOR_GOOD; } else if (errno != 0) { walk = ctx->format_bad; - START_COLOR("color_bad"); + outcolor = COLOR_BAD; } // remove newline chars @@ -70,6 +71,5 @@ void print_file_contents(file_contents_ctx_t *ctx) { free(formatted); free(buf); - END_COLOR; OUTPUT_FULL_TEXT(ctx->buf); } diff --git a/src/print_ipv6_addr.c b/src/print_ipv6_addr.c index c0d174e8..85849b4e 100644 --- a/src/print_ipv6_addr.c +++ b/src/print_ipv6_addr.c @@ -183,16 +183,16 @@ void print_ipv6_info(ipv6_info_ctx_t *ctx) { ? get_iface_addr(addr_string) : ""; char *outwalk = ctx->buf; + output_color_t outcolor = COLOR_DEFAULT; if (addr_string == NULL) { - START_COLOR("color_bad"); + outcolor = COLOR_BAD; outwalk += sprintf(outwalk, "%s", ctx->format_down); - END_COLOR; OUTPUT_FULL_TEXT(ctx->buf); return; } - START_COLOR("color_good"); + outcolor = COLOR_GOOD; placeholder_t placeholders[] = { {.name = "%ip", .value = addr_string}, @@ -203,6 +203,5 @@ void print_ipv6_info(ipv6_info_ctx_t *ctx) { OUTPUT_FORMATTED; free(formatted); - END_COLOR; OUTPUT_FULL_TEXT(ctx->buf); } diff --git a/src/print_load.c b/src/print_load.c index 080e5f16..1983954d 100644 --- a/src/print_load.c +++ b/src/print_load.c @@ -13,19 +13,18 @@ void print_load(load_ctx_t *ctx) { char *outwalk = ctx->buf; + 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 = ctx->format; - bool colorful_output = false; if (getloadavg(loadavg, 3) == -1) goto error; if (loadavg[0] >= ctx->max_threshold) { - START_COLOR("color_bad"); - colorful_output = true; + outcolor = COLOR_BAD; if (ctx->format_above_threshold != NULL) selected_format = ctx->format_above_threshold; } @@ -48,9 +47,6 @@ void print_load(load_ctx_t *ctx) { OUTPUT_FORMATTED; free(formatted); - if (colorful_output) - END_COLOR; - *outwalk = '\0'; OUTPUT_FULL_TEXT(ctx->buf); diff --git a/src/print_mem.c b/src/print_mem.c index 76a70b90..df5a254e 100644 --- a/src/print_mem.c +++ b/src/print_mem.c @@ -88,10 +88,10 @@ static unsigned long memory_absolute(const char *mem_amount, const unsigned long void print_memory(memory_ctx_t *ctx) { char *outwalk = ctx->buf; + output_color_t outcolor = COLOR_DEFAULT; #if defined(__linux__) const char *selected_format = ctx->format; - const char *output_color = NULL; int unread_fields = 6; unsigned long ram_total; @@ -151,20 +151,18 @@ void print_memory(memory_ctx_t *ctx) { if (ctx->threshold_degraded) { const unsigned long threshold = memory_absolute(ctx->threshold_degraded, ram_total); if (ram_available < threshold) { - output_color = "color_degraded"; + outcolor = COLOR_DEGRADED; } } if (ctx->threshold_critical) { const unsigned long threshold = memory_absolute(ctx->threshold_critical, ram_total); if (ram_available < threshold) { - output_color = "color_bad"; + outcolor = COLOR_BAD; } } - if (output_color) { - START_COLOR(output_color); - + if (outcolor != COLOR_DEFAULT) { if (ctx->format_degraded) selected_format = ctx->format_degraded; } @@ -205,9 +203,6 @@ void print_memory(memory_ctx_t *ctx) { OUTPUT_FORMATTED; free(formatted); - if (output_color) - END_COLOR; - OUTPUT_FULL_TEXT(ctx->buf); return; diff --git a/src/print_path_exists.c b/src/print_path_exists.c index 054f12e9..afe1bef0 100644 --- a/src/print_path_exists.c +++ b/src/print_path_exists.c @@ -13,6 +13,7 @@ void print_path_exists(path_exists_ctx_t *ctx) { const char *walk; char *outwalk = ctx->buf; + output_color_t outcolor = COLOR_DEFAULT; struct stat st; const bool exists = (stat(ctx->path, &st) == 0); @@ -24,7 +25,7 @@ void print_path_exists(path_exists_ctx_t *ctx) { INSTANCE(ctx->path); - START_COLOR((exists ? "color_good" : "color_bad")); + outcolor = (exists ? COLOR_GOOD : COLOR_BAD); char string_status[STRING_SIZE]; @@ -39,6 +40,5 @@ void print_path_exists(path_exists_ctx_t *ctx) { OUTPUT_FORMATTED; free(formatted); - END_COLOR; OUTPUT_FULL_TEXT(ctx->buf); } diff --git a/src/print_run_watch.c b/src/print_run_watch.c index 15cec6f7..fb48498a 100644 --- a/src/print_run_watch.c +++ b/src/print_run_watch.c @@ -13,6 +13,7 @@ void print_run_watch(run_watch_ctx_t *ctx) { bool running = process_runs(ctx->pidfile); const char *walk; char *outwalk = ctx->buf; + output_color_t outcolor = COLOR_DEFAULT; if (running || ctx->format_down == NULL) { walk = ctx->format; @@ -22,7 +23,7 @@ void print_run_watch(run_watch_ctx_t *ctx) { INSTANCE(ctx->pidfile); - START_COLOR((running ? "color_good" : "color_bad")); + outcolor = (running ? COLOR_GOOD : COLOR_BAD); char string_status[STRING_SIZE]; snprintf(string_status, STRING_SIZE, "%s", (running ? "yes" : "no")); @@ -34,6 +35,5 @@ void print_run_watch(run_watch_ctx_t *ctx) { const size_t num = sizeof(placeholders) / sizeof(placeholder_t); char *formatted = format_placeholders(walk, &placeholders[0], num); OUTPUT_FORMATTED; - END_COLOR; OUTPUT_FULL_TEXT(ctx->buf); } diff --git a/src/print_time.c b/src/print_time.c index 5b57e2d7..a0ad0b17 100644 --- a/src/print_time.c +++ b/src/print_time.c @@ -39,6 +39,7 @@ void set_timezone(const char *tz) { void print_time(time_ctx_t *ctx) { char *outwalk = ctx->buf; + output_color_t outcolor = COLOR_DEFAULT; struct tm local_tm, tm; if (ctx->title != NULL) diff --git a/src/print_volume.c b/src/print_volume.c index c622457d..34422581 100644 --- a/src/print_volume.c +++ b/src/print_volume.c @@ -46,7 +46,7 @@ if ((err = snd_mixer_selem_get_##channel##_switch(elem, 0, &pbval)) < 0) \ fprintf(stderr, "i3status: ALSA: " #channel "_switch: %s\n", snd_strerror(err)); \ if (!pbval) { \ - START_COLOR("color_degraded"); \ + outcolor = COLOR_DEGRADED; \ ctx->fmt = ctx->fmt_muted; \ } @@ -66,6 +66,7 @@ static char *apply_volume_format(const char *fmt, int ivolume, const char *devic void print_volume(volume_ctx_t *ctx) { char *outwalk = ctx->buf; + output_color_t outcolor = COLOR_DEFAULT; int pbval = 1; /* Printing volume works with ALSA and PulseAudio at the moment */ @@ -102,8 +103,7 @@ void print_volume(volume_ctx_t *ctx) { 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 */ @@ -124,8 +124,7 @@ void print_volume(volume_ctx_t *ctx) { bool muted = DECOMPOSE_MUTED(cvolume); if (ivolume >= 0 && success) { if (muted) { - START_COLOR("color_degraded"); - pbval = 0; + outcolor = COLOR_DEGRADED; } char *formatted = apply_volume_format(muted ? ctx->fmt_muted : ctx->fmt, ivolume, @@ -248,7 +247,6 @@ void print_volume(volume_ctx_t *ctx) { char defaultmixer[] = "/dev/mixer"; int mixfd, vol, devmask = 0; const char *devicename = "UNSUPPORTED"; /* TODO: implement support for this */ - pbval = 1; if (ctx->mixer_idx > 0) asprintf(&mixerpath, "/dev/mixer%d", ctx->mixer_idx); @@ -329,9 +327,8 @@ void print_volume(volume_ctx_t *ctx) { goto out; if (vinfo.un.ord) { - START_COLOR("color_degraded"); + outcolor = COLOR_DEGRADED; ctx->fmt = ctx->fmt_muted; - pbval = 0; } } @@ -346,8 +343,7 @@ void print_volume(volume_ctx_t *ctx) { } if (((vol & 0x7f) == 0) && (((vol >> 8) & 0x7f) == 0)) { - START_COLOR("color_degraded"); - pbval = 0; + outcolor = COLOR_DEGRADED; } #endif @@ -357,13 +353,9 @@ void print_volume(volume_ctx_t *ctx) { #endif out: - if (!pbval) - END_COLOR; OUTPUT_FULL_TEXT(ctx->buf); return; out_with_format: - if (!pbval) - END_COLOR; OUTPUT_FULL_TEXT(ctx->buf); } diff --git a/src/print_wireless_info.c b/src/print_wireless_info.c index 7c577f50..213eded9 100644 --- a/src/print_wireless_info.c +++ b/src/print_wireless_info.c @@ -513,6 +513,7 @@ static int get_wireless_info(const char *interface, wireless_info_t *info) { void print_wireless_info(wireless_info_ctx_t *ctx) { const char *walk; char *outwalk = ctx->buf; + output_color_t outcolor = COLOR_DEFAULT; wireless_info_t info; INSTANCE(ctx->interface); @@ -534,10 +535,9 @@ void print_wireless_info(wireless_info_ctx_t *ctx) { bool prefer_ipv4 = true; if (ipv4_address == NULL) { if (ipv6_address == NULL) { - START_COLOR("color_bad"); + outcolor = COLOR_BAD; outwalk += sprintf(outwalk, "%s", ctx->format_down); - END_COLOR; free(ipv4_address); free(ipv6_address); OUTPUT_FULL_TEXT(ctx->buf); @@ -552,16 +552,16 @@ void print_wireless_info(wireless_info_ctx_t *ctx) { const char *ip_address = (prefer_ipv4) ? ipv4_address : ipv6_address; if (!get_wireless_info(ctx->interface, &info)) { walk = ctx->format_down; - START_COLOR("color_bad"); + outcolor = COLOR_BAD; } else { walk = ctx->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; } } } @@ -634,7 +634,6 @@ void print_wireless_info(wireless_info_ctx_t *ctx) { OUTPUT_FORMATTED; free(formatted); - END_COLOR; free(ipv4_address); free(ipv6_address); OUTPUT_FULL_TEXT(ctx->buf);