From 714e9184fdcf4b947655ffa73f2d8639045b6edf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorben=20Kr=C3=BCger?= Date: Tue, 1 Feb 2022 13:43:07 +0100 Subject: [PATCH] battery: add format_below_threshold feature --- i3status.c | 2 ++ include/i3status.h | 1 + man/i3status.man | 5 ++++- src/print_battery_info.c | 7 ++++++- .../027-battery-low-threshold-format/BAT0_uevent | 4 ++++ .../expected_output.txt | 1 + .../027-battery-low-threshold-format/i3status.conf | 13 +++++++++++++ 7 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 testcases/027-battery-low-threshold-format/BAT0_uevent create mode 100644 testcases/027-battery-low-threshold-format/expected_output.txt create mode 100644 testcases/027-battery-low-threshold-format/i3status.conf diff --git a/i3status.c b/i3status.c index 437144f6..eeed02f5 100644 --- a/i3status.c +++ b/i3status.c @@ -316,6 +316,7 @@ int main(int argc, char *argv[]) { cfg_opt_t battery_opts[] = { CFG_STR("format", "%status %percentage %remaining", CFGF_NONE), + CFG_STR("format_below_threshold", NULL, CFGF_NONE), CFG_STR("format_down", "No battery", CFGF_NONE), CFG_STR("format_percentage", "%.02f%s", CFGF_NONE), CFG_STR("status_chr", "CHR", CFGF_NONE), @@ -737,6 +738,7 @@ int main(int argc, char *argv[]) { .number = (strcasecmp(title, "all") == 0 ? -1 : atoi(title)), .path = cfg_getstr(sec, "path"), .format = cfg_getstr(sec, "format"), + .format_below_threshold = cfg_getstr(sec, "format_below_threshold"), .format_down = cfg_getstr(sec, "format_down"), .status_chr = cfg_getstr(sec, "status_chr"), .status_bat = cfg_getstr(sec, "status_bat"), diff --git a/include/i3status.h b/include/i3status.h index fe44780b..0b458340 100644 --- a/include/i3status.h +++ b/include/i3status.h @@ -266,6 +266,7 @@ typedef struct { int number; const char *path; const char *format; + const char *format_below_threshold; const char *format_down; const char *status_chr; const char *status_bat; diff --git a/man/i3status.man b/man/i3status.man index a9d7a021..ae8efb7a 100644 --- a/man/i3status.man +++ b/man/i3status.man @@ -366,7 +366,8 @@ It is possible to define a low_threshold that causes the battery text to be colored +color_bad+. The +low_threshold+ type can be of +threshold_type+ "time" or "percentage". So, if you configure +low_threshold+ to 10 and +threshold_type+ to "time", and your battery lasts another 9 minutes, it will be colored -+color_bad+. ++color_bad+. You can customize the output format when below ++low_threshold+ with +format_below_threshold.+ To show an aggregate of all batteries in the system, use "all" as the number. In this case (for Linux), the /sys path must contain the "%d" sequence. Otherwise, @@ -385,6 +386,8 @@ FULL) is used. *Example format*: +%status %remaining (%emptytime %consumption)+ +*Example format_below_threshold*: +Warning: %remaining (%emptytime)+ + *Example format_down*: +No battery+ *Example format_percentage*: +"%.02f%s"+ diff --git a/src/print_battery_info.c b/src/print_battery_info.c index 7465d171..b9bc8e6f 100644 --- a/src/print_battery_info.c +++ b/src/print_battery_info.c @@ -575,6 +575,7 @@ static bool slurp_all_batteries(battery_info_ctx_t *ctx, struct battery_info *ba } void print_battery_info(battery_info_ctx_t *ctx) { + const char *selected_format = ctx->format; char *outwalk = ctx->buf; struct battery_info batt_info = { .full_design = -1, @@ -650,9 +651,13 @@ void print_battery_info(battery_info_ctx_t *ctx) { 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; + if (ctx->format_below_threshold != NULL) + selected_format = ctx->format_below_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; + if (ctx->format_below_threshold != NULL) + selected_format = ctx->format_below_threshold; } } @@ -713,7 +718,7 @@ void print_battery_info(battery_info_ctx_t *ctx) { {.name = "%consumption", .value = string_consumption}}; const size_t num = sizeof(placeholders) / sizeof(placeholder_t); - char *untrimmed = format_placeholders(ctx->format, &placeholders[0], num); + char *untrimmed = format_placeholders(selected_format, &placeholders[0], num); char *formatted = trim(untrimmed); OUTPUT_FORMATTED; free(formatted); diff --git a/testcases/027-battery-low-threshold-format/BAT0_uevent b/testcases/027-battery-low-threshold-format/BAT0_uevent new file mode 100644 index 00000000..0b383a26 --- /dev/null +++ b/testcases/027-battery-low-threshold-format/BAT0_uevent @@ -0,0 +1,4 @@ +POWER_SUPPLY_STATUS=Discharging +POWER_SUPPLY_TIME_TO_EMPTY_NOW=42 +POWER_SUPPLY_CHARGE_FULL_DESIGN=7800000 +POWER_SUPPLY_CHARGE_NOW=1090000 diff --git a/testcases/027-battery-low-threshold-format/expected_output.txt b/testcases/027-battery-low-threshold-format/expected_output.txt new file mode 100644 index 00000000..6c5f96cd --- /dev/null +++ b/testcases/027-battery-low-threshold-format/expected_output.txt @@ -0,0 +1 @@ +BELOW_THRESHOLD diff --git a/testcases/027-battery-low-threshold-format/i3status.conf b/testcases/027-battery-low-threshold-format/i3status.conf new file mode 100644 index 00000000..e52117a5 --- /dev/null +++ b/testcases/027-battery-low-threshold-format/i3status.conf @@ -0,0 +1,13 @@ +general { + output_format = "none" +} + +order += "battery 0" + +battery 0 { + format = "%status %percentage %remaining" + format_below_threshold = "BELOW_THRESHOLD" + low_threshold = 15 + threshold_type = percentage + path = "testcases/027-battery-low-threshold-format/BAT0_uevent" +}