cpu_temperature: fix colors (+param struct)
This commit is contained in:
parent
e57f14ffa1
commit
6f348e612b
12
i3status.c
12
i3status.c
@ -868,7 +868,17 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
CASE_SEC_TITLE("cpu_temperature") {
|
CASE_SEC_TITLE("cpu_temperature") {
|
||||||
SEC_OPEN_MAP("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;
|
SEC_CLOSE_MAP;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -323,7 +323,18 @@ typedef struct {
|
|||||||
|
|
||||||
void print_path_exists(path_exists_ctx_t *ctx);
|
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 {
|
typedef struct {
|
||||||
yajl_gen json_gen;
|
yajl_gen json_gen;
|
||||||
|
@ -211,25 +211,26 @@ error_netbsd1:
|
|||||||
* the user provided path) and returns the temperature in degree celsius.
|
* 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) {
|
void print_cpu_temperature_info(cpu_temperature_ctx_t *ctx) {
|
||||||
char *outwalk = buffer;
|
char *outwalk = ctx->buf;
|
||||||
#ifdef THERMAL_ZONE
|
#ifdef THERMAL_ZONE
|
||||||
const char *selected_format = format;
|
const char *selected_format = ctx->format;
|
||||||
bool colorful_output = false;
|
bool colorful_output = false;
|
||||||
char *thermal_zone;
|
char *thermal_zone;
|
||||||
temperature_t temperature;
|
temperature_t temperature;
|
||||||
temperature.raw_value = 0;
|
temperature.raw_value = 0;
|
||||||
sprintf(temperature.formatted_value, "%.2f", 0.0);
|
sprintf(temperature.formatted_value, "%.2f", 0.0);
|
||||||
|
#define json_gen ctx->json_gen
|
||||||
|
|
||||||
if (path == NULL)
|
if (ctx->path == NULL)
|
||||||
asprintf(&thermal_zone, THERMAL_ZONE, zone);
|
asprintf(&thermal_zone, THERMAL_ZONE, ctx->zone);
|
||||||
else {
|
else {
|
||||||
static glob_t globbuf;
|
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");
|
die("glob() failed\n");
|
||||||
if (globbuf.gl_pathc == 0) {
|
if (globbuf.gl_pathc == 0) {
|
||||||
/* No glob matches, the specified path does not contain a wildcard. */
|
/* No glob matches, the specified path does not contain a wildcard. */
|
||||||
asprintf(&thermal_zone, path, zone);
|
asprintf(&thermal_zone, ctx->path, ctx->zone);
|
||||||
} else {
|
} else {
|
||||||
/* glob matched, we take the first match and ignore the others */
|
/* glob matched, we take the first match and ignore the others */
|
||||||
asprintf(&thermal_zone, "%s", globbuf.gl_pathv[0]);
|
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)
|
if (read_temperature(thermal_zone, &temperature) != 0)
|
||||||
goto error;
|
goto error;
|
||||||
|
|
||||||
if (temperature.raw_value >= max_threshold) {
|
if (temperature.raw_value >= ctx->max_threshold) {
|
||||||
START_COLOR("color_bad");
|
START_COLOR("color_bad");
|
||||||
colorful_output = true;
|
colorful_output = true;
|
||||||
if (format_above_threshold != NULL)
|
if (ctx->format_above_threshold != NULL)
|
||||||
selected_format = format_above_threshold;
|
selected_format = ctx->format_above_threshold;
|
||||||
}
|
}
|
||||||
|
|
||||||
char string_degrees[STRING_SIZE];
|
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}};
|
{.name = "%degrees", .value = string_degrees}};
|
||||||
|
|
||||||
const size_t num = sizeof(placeholders) / sizeof(placeholder_t);
|
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) {
|
if (colorful_output) {
|
||||||
END_COLOR;
|
END_COLOR;
|
||||||
@ -264,8 +267,7 @@ void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const
|
|||||||
|
|
||||||
free(thermal_zone);
|
free(thermal_zone);
|
||||||
|
|
||||||
OUTPUT_FULL_TEXT(buffer);
|
OUTPUT_FULL_TEXT(ctx->buf);
|
||||||
free(buffer);
|
|
||||||
return;
|
return;
|
||||||
error:
|
error:
|
||||||
free(thermal_zone);
|
free(thermal_zone);
|
||||||
|
Loading…
Reference in New Issue
Block a user