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
14 changes: 14 additions & 0 deletions i3status.c
Original file line number Diff line number Diff line change
Expand Up @@ -320,11 +320,18 @@ int main(int argc, char *argv[]) {
CFG_STR("format_percentage", "%.02f%s", CFGF_NONE),
CFG_STR("status_chr", "CHR", CFGF_NONE),
CFG_STR("status_bat", "BAT", CFGF_NONE),
CFG_STR("status_bat_low", "BAT", CFGF_NONE),
CFG_STR("status_bat_quarter", "BAT", CFGF_NONE),
CFG_STR("status_bat_half", "BAT", CFGF_NONE),
CFG_STR("status_bat_three_quarters", "BAT", CFGF_NONE),
CFG_STR("status_unk", "UNK", CFGF_NONE),
CFG_STR("status_full", "FULL", CFGF_NONE),
CFG_STR("status_idle", "IDLE", CFGF_NONE),
CFG_STR("path", "/sys/class/power_supply/BAT%d/uevent", CFGF_NONE),
CFG_INT("low_threshold", 30, CFGF_NONE),
CFG_INT("quarter_threshold", 45, CFGF_NONE),
CFG_INT("half_threshold", 60, CFGF_NONE),
CFG_INT("three_quarters_threshold", 75, CFGF_NONE),
CFG_STR("threshold_type", "time", CFGF_NONE),
CFG_BOOL("last_full_capacity", false, CFGF_NONE),
CFG_BOOL("integer_battery_capacity", false, CFGF_NONE),
Expand Down Expand Up @@ -741,10 +748,17 @@ int main(int argc, char *argv[]) {
.format_down = cfg_getstr(sec, "format_down"),
.status_chr = cfg_getstr(sec, "status_chr"),
.status_bat = cfg_getstr(sec, "status_bat"),
.status_bat_low = cfg_getstr(sec, "status_bat_low"),
.status_bat_quarter = cfg_getstr(sec, "status_bat_quarter"),
.status_bat_half = cfg_getstr(sec, "status_bat_half"),
.status_bat_three_quarters = cfg_getstr(sec, "status_bat_three_quarters"),
.status_unk = cfg_getstr(sec, "status_unk"),
.status_full = cfg_getstr(sec, "status_full"),
.status_idle = cfg_getstr(sec, "status_idle"),
.low_threshold = cfg_getint(sec, "low_threshold"),
.quarter_threshold = cfg_getint(sec, "quarter_threshold"),
.half_threshold = cfg_getint(sec, "half_threshold"),
.three_quarters_threshold = cfg_getint(sec, "three_quarters_threshold"),
.threshold_type = cfg_getstr(sec, "threshold_type"),
.last_full_capacity = cfg_getbool(sec, "last_full_capacity"),
.format_percentage = cfg_getstr(sec, "format_percentage"),
Expand Down
7 changes: 7 additions & 0 deletions include/i3status.h
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,17 @@ typedef struct {
const char *format_down;
const char *status_chr;
const char *status_bat;
const char *status_bat_low;
const char *status_bat_quarter;
const char *status_bat_half;
const char *status_bat_three_quarters;
const char *status_unk;
const char *status_full;
const char *status_idle;
int low_threshold;
int quarter_threshold;
int half_threshold;
int three_quarters_threshold;
char *threshold_type;
bool last_full_capacity;
const char *format_percentage;
Expand Down
60 changes: 49 additions & 11 deletions src/print_battery_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -285,10 +285,10 @@ static bool slurp_battery_info(battery_info_ctx_t *ctx, struct battery_info *bat
batt_info->status = CS_DISCHARGING;
#elif defined(__OpenBSD__)
/*
* We're using apm(4) here, which is the interface to acpi(4) on amd64/i386 and
* the generic interface on macppc/sparc64/zaurus. Machines that have ACPI
* battery sensors gain some extra information.
*/
* We're using apm(4) here, which is the interface to acpi(4) on amd64/i386 and
* the generic interface on macppc/sparc64/zaurus. Machines that have ACPI
* battery sensors gain some extra information.
*/
struct apm_power_info apm_info;
struct sensordev sensordev;
struct sensor sensor;
Expand Down Expand Up @@ -596,6 +596,10 @@ void print_battery_info(battery_info_ctx_t *ctx) {
.status = CS_UNKNOWN,
};
bool colorful_output = false;
bool discharging_low = false;
bool discharging_quarter = false;
bool discharging_half = false;
bool discharging_three_quarters = false;

#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__OpenBSD__)
/* These OSes report battery stats in whole percent. */
Expand Down Expand Up @@ -656,16 +660,38 @@ void print_battery_info(battery_info_ctx_t *ctx) {
batt_info.seconds_remaining = 0;
}

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(ctx->threshold_type, "time") == 0 && batt_info.seconds_remaining < 60 * ctx->low_threshold) {
START_COLOR("color_bad");
colorful_output = true;
if (batt_info.status == CS_DISCHARGING) {
if (!strcasecmp(ctx->threshold_type, "percentage")) {
if (batt_info.percentage_remaining <= ctx->low_threshold)
discharging_low = true;
else if (batt_info.percentage_remaining <= ctx->quarter_threshold)
discharging_quarter = true;
else if (batt_info.percentage_remaining <= ctx->half_threshold)
discharging_half = true;
else if (batt_info.percentage_remaining <= ctx->three_quarters_threshold)
discharging_three_quarters = true;
} else if (!strcasecmp(ctx->threshold_type, "time")) {
if (batt_info.seconds_remaining <= 60 * ctx->low_threshold)
discharging_low = true;
else if (batt_info.seconds_remaining <= 60 * ctx->quarter_threshold)
discharging_quarter = true;
else if (batt_info.seconds_remaining <= 60 * ctx->half_threshold)
discharging_half = true;
else if (batt_info.seconds_remaining <= 60 * ctx->three_quarters_threshold)
discharging_three_quarters = true;
}
}

if (discharging_low) {
START_COLOR("color_bad");
colorful_output = true;
}

if (discharging_quarter) {
START_COLOR("color_degraded");
colorful_output = true;
}

char string_status[STRING_SIZE];
char string_percentage[STRING_SIZE];
// following variables are not alwasy set. If they are not set they should be empty.
Expand All @@ -680,6 +706,18 @@ void print_battery_info(battery_info_ctx_t *ctx) {
break;
case CS_DISCHARGING:
statusstr = ctx->status_bat;

if (discharging_low && strcmp(ctx->status_bat_low, "BAT"))
statusstr = ctx->status_bat_low;

if (discharging_quarter && strcmp(ctx->status_bat_quarter, "BAT"))
statusstr = ctx->status_bat_quarter;

if (discharging_half && strcmp(ctx->status_bat_half, "BAT"))
statusstr = ctx->status_bat_half;

if (discharging_three_quarters && strcmp(ctx->status_bat_three_quarters, "BAT"))
statusstr = ctx->status_bat_three_quarters;
break;
case CS_FULL:
statusstr = ctx->status_full;
Expand Down
8 changes: 4 additions & 4 deletions src/print_eth_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ static int print_eth_speed(char *outwalk, const char *interface) {

for (desc = ifm_subtype_descriptions; desc->ifmt_string != NULL; desc++) {
/*
* Skip these non-informative values and go right ahead to the
* actual speeds.
*/
* Skip these non-informative values and go right ahead to the
* actual speeds.
*/
if (BEGINS_WITH(desc->ifmt_string, "autoselect") ||
BEGINS_WITH(desc->ifmt_string, "auto"))
continue;
Expand Down Expand Up @@ -157,7 +157,7 @@ void print_eth_info(eth_info_ctx_t *ctx) {
/*
* Removing '%' and following characters from IPv6 since the interface identifier is redundant,
* as the output already includes the interface name.
*/
*/
if (ipv6_address != NULL) {
char *prct_ptr = strstr(ipv6_address, "%");
if (prct_ptr != NULL) {
Expand Down
4 changes: 4 additions & 0 deletions testcases/027-battery-quartiles/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=655
POWER_SUPPLY_CHARGE_FULL_DESIGN=7800000
POWER_SUPPLY_CHARGE_NOW=7600000
4 changes: 4 additions & 0 deletions testcases/027-battery-quartiles/BAT0_uevent_half_pct
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
POWER_SUPPLY_STATUS=Discharging
POWER_SUPPLY_TIME_TO_EMPTY_NOW=655
POWER_SUPPLY_CHARGE_FULL_DESIGN=7800000
POWER_SUPPLY_CHARGE_NOW=3900000
4 changes: 4 additions & 0 deletions testcases/027-battery-quartiles/BAT0_uevent_half_time
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
POWER_SUPPLY_STATUS=Discharging
POWER_SUPPLY_TIME_TO_EMPTY_NOW=60
POWER_SUPPLY_CHARGE_FULL_DESIGN=7800000
POWER_SUPPLY_CHARGE_NOW=7600000
4 changes: 4 additions & 0 deletions testcases/027-battery-quartiles/BAT0_uevent_low_pct
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
POWER_SUPPLY_STATUS=Discharging
POWER_SUPPLY_TIME_TO_EMPTY_NOW=655
POWER_SUPPLY_CHARGE_FULL_DESIGN=7800000
POWER_SUPPLY_CHARGE_NOW=1014000
4 changes: 4 additions & 0 deletions testcases/027-battery-quartiles/BAT0_uevent_low_time
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
POWER_SUPPLY_STATUS=Discharging
POWER_SUPPLY_TIME_TO_EMPTY_NOW=30
POWER_SUPPLY_CHARGE_FULL_DESIGN=7800000
POWER_SUPPLY_CHARGE_NOW=7600000
4 changes: 4 additions & 0 deletions testcases/027-battery-quartiles/BAT0_uevent_quarter_pct
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
POWER_SUPPLY_STATUS=Discharging
POWER_SUPPLY_TIME_TO_EMPTY_NOW=655
POWER_SUPPLY_CHARGE_FULL_DESIGN=7800000
POWER_SUPPLY_CHARGE_NOW=1950000
4 changes: 4 additions & 0 deletions testcases/027-battery-quartiles/BAT0_uevent_quarter_time
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
POWER_SUPPLY_STATUS=Discharging
POWER_SUPPLY_TIME_TO_EMPTY_NOW=45
POWER_SUPPLY_CHARGE_FULL_DESIGN=7800000
POWER_SUPPLY_CHARGE_NOW=7600000
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
POWER_SUPPLY_STATUS=Discharging
POWER_SUPPLY_TIME_TO_EMPTY_NOW=655
POWER_SUPPLY_CHARGE_FULL_DESIGN=7800000
POWER_SUPPLY_CHARGE_NOW=5850000
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
POWER_SUPPLY_STATUS=Discharging
POWER_SUPPLY_TIME_TO_EMPTY_NOW=75
POWER_SUPPLY_CHARGE_FULL_DESIGN=7800000
POWER_SUPPLY_CHARGE_NOW=7600000
1 change: 1 addition & 0 deletions testcases/027-battery-quartiles/expected_output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
L 13.00% 10:55 | Q 25.00% 10:55 | H 50.00% 10:55 | T 75.00% 10:55 | L 97.44% 00:30 | Q 97.44% 00:45 | H 97.44% 01:00 | T 97.44% 01:15 | BAT 97.44% 10:55
94 changes: 94 additions & 0 deletions testcases/027-battery-quartiles/i3status.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
general {
output_format = "none"
}

order += "battery 0"
order += "battery 1"
order += "battery 2"
order += "battery 3"
order += "battery 4"
order += "battery 5"
order += "battery 6"
order += "battery 7"
order += "battery 8"

battery 0 {
format = "%status %percentage %remaining"
status_bat_low = "L"
low_threshold = 15
threshold_type = "percentage"
path = "testcases/027-battery-quartiles/BAT0_uevent_low_pct"
}

battery 1 {
format = "%status %percentage %remaining"
status_bat_quarter = "Q"
low_threshold = 0
quarter_threshold = 35
threshold_type = "percentage"
path = "testcases/027-battery-quartiles/BAT0_uevent_quarter_pct"
}

battery 2 {
format = "%status %percentage %remaining"
status_bat_half = "H"
low_threshold = 0
quarter_threshold = 0
half_threshold = 65
threshold_type = "percentage"
path = "testcases/027-battery-quartiles/BAT0_uevent_half_pct"
}

battery 3 {
format = "%status %percentage %remaining"
status_bat_three_quarters = "T"
low_threshold = 0
quarter_threshold = 0
half_threshold = 0
three_quarters_threshold = 85
threshold_type = "percentage"
path = "testcases/027-battery-quartiles/BAT0_uevent_three_quarters_pct"
}

battery 4 {
format = "%status %percentage %remaining"
status_bat_low = "L"
low_threshold = 30
threshold_type = "time"
path = "testcases/027-battery-quartiles/BAT0_uevent_low_time"
}

battery 5 {
format = "%status %percentage %remaining"
status_bat_quarter = "Q"
low_threshold = 0
quarter_threshold = 45
threshold_type = "time"
path = "testcases/027-battery-quartiles/BAT0_uevent_quarter_time"
}

battery 6 {
format = "%status %percentage %remaining"
status_bat_half = "H"
low_threshold = 0
quarter_threshold = 0
half_threshold = 60
threshold_type = "time"
path = "testcases/027-battery-quartiles/BAT0_uevent_half_time"
}

battery 7 {
format = "%status %percentage %remaining"
status_bat_three_quarters = "T"
low_threshold = 0
quarter_threshold = 0
half_threshold = 0
three_quarters_threshold = 75
threshold_type = "time"
path = "testcases/027-battery-quartiles/BAT0_uevent_three_quarters_time"
}

battery 8 {
format = "%status %percentage %remaining"
path = "testcases/027-battery-quartiles/BAT0_uevent"
}