battery: fix colors (+param struct)

This commit is contained in:
Michael Stapelberg 2021-11-02 20:48:50 +01:00
parent 5760a1d53f
commit 9d9a6e8072
3 changed files with 85 additions and 28 deletions

View File

@ -718,7 +718,25 @@ int main(int argc, char *argv[]) {
CASE_SEC_TITLE("battery") {
SEC_OPEN_MAP("battery");
print_battery_info(json_gen, buffer, (strcasecmp(title, "all") == 0 ? -1 : atoi(title)), cfg_getstr(sec, "path"), cfg_getstr(sec, "format"), cfg_getstr(sec, "format_down"), cfg_getstr(sec, "status_chr"), cfg_getstr(sec, "status_bat"), cfg_getstr(sec, "status_unk"), cfg_getstr(sec, "status_full"), cfg_getint(sec, "low_threshold"), cfg_getstr(sec, "threshold_type"), cfg_getbool(sec, "last_full_capacity"), cfg_getstr(sec, "format_percentage"), cfg_getbool(sec, "hide_seconds"));
battery_info_ctx_t ctx = {
.json_gen = json_gen,
.buf = buffer,
.buflen = sizeof(buffer),
.number = (strcasecmp(title, "all") == 0 ? -1 : atoi(title)),
.path = cfg_getstr(sec, "path"),
.format = cfg_getstr(sec, "format"),
.format_down = cfg_getstr(sec, "format_down"),
.status_chr = cfg_getstr(sec, "status_chr"),
.status_bat = cfg_getstr(sec, "status_bat"),
.status_unk = cfg_getstr(sec, "status_unk"),
.status_full = cfg_getstr(sec, "status_full"),
.low_threshold = cfg_getint(sec, "low_threshold"),
.threshold_type = cfg_getstr(sec, "threshold_type"),
.last_full_capacity = cfg_getbool(sec, "last_full_capacity"),
.format_percentage = cfg_getstr(sec, "format_percentage"),
.hide_seconds = cfg_getbool(sec, "hide_seconds"),
};
print_battery_info(&ctx);
SEC_CLOSE_MAP;
}

View File

@ -245,11 +245,47 @@ typedef struct {
void print_ipv6_info(ipv6_info_ctx_t *ctx);
void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const char *format, const char *format_below_threshold, const char *format_not_mounted, const char *prefix_type, const char *threshold_type, const double low_threshold);
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, const char *format_percentage, bool hide_seconds);
typedef struct {
yajl_gen json_gen;
char *buf;
const size_t buflen;
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;
const char *format_percentage;
bool hide_seconds;
} battery_info_ctx_t;
void print_battery_info(battery_info_ctx_t *ctx);
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);
void print_ddate(yajl_gen json_gen, char *buffer, const char *format, time_t t);
const char *get_ip_addr(const char *interface, int family);
void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down, const char *format_bitrate, const char *format_noise, const char *format_quality, const char *format_signal);
typedef struct {
yajl_gen json_gen;
char *buf;
const size_t buflen;
const char *interface;
const char *format_up;
const char *format_down;
const char *format_bitrate;
const char *format_noise;
const char *format_quality;
const char *format_signal;
} wireless_info_ctx_t;
void print_wireless_info(wireless_info_ctx_t *ctx);
void print_run_watch(yajl_gen json_gen, char *buffer, const char *title, const char *pidfile, const char *format, const char *format_down);
void print_path_exists(yajl_gen json_gen, char *buffer, const char *title, const char *path, const char *format, const char *format_down);
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);

View File

@ -574,8 +574,8 @@ static bool slurp_all_batteries(struct battery_info *batt_info, yajl_gen json_ge
return true;
}
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, const char *format_percentage, bool hide_seconds) {
char *outwalk = buffer;
void print_battery_info(battery_info_ctx_t *ctx) {
char *outwalk = ctx->buf;
struct battery_info batt_info = {
.full_design = -1,
.full_last = -1,
@ -586,11 +586,12 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char
.status = CS_UNKNOWN,
};
bool colorful_output = false;
#define json_gen ctx->json_gen
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__OpenBSD__)
/* These OSes report battery stats in whole percent. */
if (strcmp("%.02f%s", format_percentage) == 0) {
format_percentage = "%.00f%s";
if (strcmp("%.02f%s", ctx->format_percentage) == 0) {
ctx->format_percentage = "%.00f%s";
}
#endif
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__OpenBSD__)
@ -598,11 +599,11 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char
hide_seconds = true;
#endif
if (number < 0) {
if (!slurp_all_batteries(&batt_info, json_gen, buffer, path, format_down))
if (ctx->number < 0) {
if (!slurp_all_batteries(&batt_info, json_gen, ctx->buf, ctx->path, ctx->format_down))
return;
} else {
if (!slurp_battery_info(&batt_info, json_gen, buffer, number, path, format_down))
if (!slurp_battery_info(&batt_info, json_gen, ctx->buf, ctx->number, ctx->path, ctx->format_down))
return;
}
@ -615,13 +616,13 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char
// If we don't have either then both full_design and full_last <= 0,
// which implies full <= 0, which bails out on the following line.
int full = batt_info.full_design;
if (full <= 0 || (last_full_capacity && batt_info.full_last > 0)) {
if (full <= 0 || (ctx->last_full_capacity && batt_info.full_last > 0)) {
full = batt_info.full_last;
}
if (full <= 0 && batt_info.remaining < 0 && batt_info.percentage_remaining < 0) {
/* We have no physical measurements and no estimates. Nothing
* much we can report, then. */
OUTPUT_FULL_TEXT(format_down);
OUTPUT_FULL_TEXT(ctx->format_down);
return;
}
@ -632,7 +633,7 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char
* the percentage calculated based on the last full capacity, we clamp the
* value to 100%, as that makes more sense.
* See http://bugs.debian.org/785398 */
if (last_full_capacity && batt_info.percentage_remaining > 100) {
if (ctx->last_full_capacity && batt_info.percentage_remaining > 100) {
batt_info.percentage_remaining = 100;
}
}
@ -646,11 +647,11 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char
batt_info.seconds_remaining = 0;
}
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) {
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;
} else if (batt_info.seconds_remaining >= 0 && strcasecmp(threshold_type, "time") == 0 && batt_info.seconds_remaining < 60 * low_threshold) {
} 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;
}
@ -666,19 +667,19 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char
const char *statusstr;
switch (batt_info.status) {
case CS_CHARGING:
statusstr = status_chr;
statusstr = ctx->status_chr;
break;
case CS_DISCHARGING:
statusstr = status_bat;
statusstr = ctx->status_bat;
break;
case CS_FULL:
statusstr = status_full;
statusstr = ctx->status_full;
break;
default:
statusstr = status_unk;
statusstr = ctx->status_unk;
}
snprintf(string_status, STRING_SIZE, "%s", statusstr);
snprintf(string_percentage, STRING_SIZE, format_percentage, batt_info.percentage_remaining, pct_mark);
snprintf(string_percentage, STRING_SIZE, ctx->format_percentage, batt_info.percentage_remaining, pct_mark);
if (batt_info.seconds_remaining >= 0) {
int seconds, hours, minutes;
@ -686,7 +687,7 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char
seconds = batt_info.seconds_remaining - (hours * 3600);
minutes = seconds / 60;
seconds -= (minutes * 60);
if (hide_seconds)
if (ctx->hide_seconds)
snprintf(string_remaining, STRING_SIZE, "%02d:%02d", max(hours, 0), max(minutes, 0));
else
snprintf(string_remaining, STRING_SIZE, "%02d:%02d:%02d", max(hours, 0), max(minutes, 0), max(seconds, 0));
@ -696,7 +697,7 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char
time_t empty_time = time(NULL) + batt_info.seconds_remaining;
set_timezone(NULL); /* Use local time. */
struct tm *empty_tm = localtime(&empty_time);
if (hide_seconds)
if (ctx->hide_seconds)
snprintf(string_emptytime, STRING_SIZE, "%02d:%02d", max(empty_tm->tm_hour, 0), max(empty_tm->tm_min, 0));
else
snprintf(string_emptytime, STRING_SIZE, "%02d:%02d:%02d", max(empty_tm->tm_hour, 0), max(empty_tm->tm_min, 0), max(empty_tm->tm_sec, 0));
@ -713,13 +714,15 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char
{.name = "%consumption", .value = string_consumption}};
const size_t num = sizeof(placeholders) / sizeof(placeholder_t);
char *untrimmed = format_placeholders(format, &placeholders[0], num);
buffer = trim(untrimmed);
char *untrimmed = format_placeholders(ctx->format, &placeholders[0], num);
char *formatted = trim(untrimmed);
OUTPUT_FORMATTED;
free(formatted);
free(untrimmed);
if (colorful_output)
if (colorful_output) {
END_COLOR;
}
OUTPUT_FULL_TEXT(buffer);
free(buffer);
OUTPUT_FULL_TEXT(ctx->buf);
}