Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions i3status.c
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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"),
Expand Down
1 change: 1 addition & 0 deletions include/i3status.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 4 additions & 1 deletion man/i3status.man
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,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,
Expand All @@ -395,6 +396,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"+
Expand Down
7 changes: 6 additions & 1 deletion src/print_battery_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions testcases/027-battery-low-threshold-format/BAT0_uevent
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
BELOW_THRESHOLD
13 changes: 13 additions & 0 deletions testcases/027-battery-low-threshold-format/i3status.conf
Original file line number Diff line number Diff line change
@@ -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"
}