From 6f348e612bd4eee05d7bcffa54cab23f49f450e2 Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Tue, 2 Nov 2021 21:33:08 +0100 Subject: [PATCH] cpu_temperature: fix colors (+param struct) --- i3status.c | 12 +++++++++++- include/i3status.h | 13 ++++++++++++- src/print_cpu_temperature.c | 28 +++++++++++++++------------- 3 files changed, 38 insertions(+), 15 deletions(-) diff --git a/i3status.c b/i3status.c index c4a9071..8c55b3c 100644 --- a/i3status.c +++ b/i3status.c @@ -868,7 +868,17 @@ int main(int argc, char *argv[]) { CASE_SEC_TITLE("cpu_temperature") { SEC_OPEN_MAP("cpu_temperature"); - print_cpu_temperature_info(json_gen, buffer, atoi(title), cfg_getstr(sec, "path"), cfg_getstr(sec, "format"), cfg_getstr(sec, "format_above_threshold"), cfg_getint(sec, "max_threshold")); + cpu_temperature_ctx_t ctx = { + .json_gen = json_gen, + .buf = buffer, + .buflen = sizeof(buffer), + .zone = atoi(title), + .path = cfg_getstr(sec, "path"), + .format = cfg_getstr(sec, "format"), + .format_above_threshold = cfg_getstr(sec, "format_above_threshold"), + .max_threshold = cfg_getint(sec, "max_threshold"), + }; + print_cpu_temperature_info(&ctx); SEC_CLOSE_MAP; } diff --git a/include/i3status.h b/include/i3status.h index e2e7060..d6c919c 100644 --- a/include/i3status.h +++ b/include/i3status.h @@ -323,7 +323,18 @@ typedef struct { void print_path_exists(path_exists_ctx_t *ctx); -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); +typedef struct { + yajl_gen json_gen; + char *buf; + const size_t buflen; + int zone; + const char *path; + const char *format; + const char *format_above_threshold; + int max_threshold; +} cpu_temperature_ctx_t; + +void print_cpu_temperature_info(cpu_temperature_ctx_t *ctx); typedef struct { yajl_gen json_gen; diff --git a/src/print_cpu_temperature.c b/src/print_cpu_temperature.c index f94d1a7..6759235 100644 --- a/src/print_cpu_temperature.c +++ b/src/print_cpu_temperature.c @@ -211,25 +211,26 @@ error_netbsd1: * the user provided path) and returns the temperature in degree celsius. * */ -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; +void print_cpu_temperature_info(cpu_temperature_ctx_t *ctx) { + char *outwalk = ctx->buf; #ifdef THERMAL_ZONE - const char *selected_format = format; + const char *selected_format = ctx->format; bool colorful_output = false; char *thermal_zone; temperature_t temperature; temperature.raw_value = 0; sprintf(temperature.formatted_value, "%.2f", 0.0); +#define json_gen ctx->json_gen - if (path == NULL) - asprintf(&thermal_zone, THERMAL_ZONE, zone); + if (ctx->path == NULL) + asprintf(&thermal_zone, THERMAL_ZONE, ctx->zone); else { static glob_t globbuf; - if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) != 0) + if (glob(ctx->path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) != 0) die("glob() failed\n"); if (globbuf.gl_pathc == 0) { /* No glob matches, the specified path does not contain a wildcard. */ - asprintf(&thermal_zone, path, zone); + asprintf(&thermal_zone, ctx->path, ctx->zone); } else { /* glob matched, we take the first match and ignore the others */ asprintf(&thermal_zone, "%s", globbuf.gl_pathv[0]); @@ -242,11 +243,11 @@ void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const if (read_temperature(thermal_zone, &temperature) != 0) goto error; - if (temperature.raw_value >= max_threshold) { + if (temperature.raw_value >= ctx->max_threshold) { START_COLOR("color_bad"); colorful_output = true; - if (format_above_threshold != NULL) - selected_format = format_above_threshold; + if (ctx->format_above_threshold != NULL) + selected_format = ctx->format_above_threshold; } char string_degrees[STRING_SIZE]; @@ -255,7 +256,9 @@ void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const {.name = "%degrees", .value = string_degrees}}; const size_t num = sizeof(placeholders) / sizeof(placeholder_t); - buffer = format_placeholders(selected_format, &placeholders[0], num); + char *formatted = format_placeholders(selected_format, &placeholders[0], num); + OUTPUT_FORMATTED; + free(formatted); if (colorful_output) { END_COLOR; @@ -264,8 +267,7 @@ void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const free(thermal_zone); - OUTPUT_FULL_TEXT(buffer); - free(buffer); + OUTPUT_FULL_TEXT(ctx->buf); return; error: free(thermal_zone);