Skip to content

Instantly share code, notes, and snippets.

@marcosnils
Last active October 24, 2024 02:51
Show Gist options
  • Select an option

  • Save marcosnils/428dae19265e09eca8d00a07db8cd962 to your computer and use it in GitHub Desktop.

Select an option

Save marcosnils/428dae19265e09eca8d00a07db8cd962 to your computer and use it in GitHub Desktop.
i3status patch to show green or bad status depending on battery consumption
diff --git a/i3status.c b/i3status.c
index 3922bf2..1767375 100644
--- a/i3status.c
+++ b/i3status.c
@@ -329,6 +329,7 @@ int main(int argc, char *argv[]) {
CFG_BOOL("last_full_capacity", false, CFGF_NONE),
CFG_BOOL("integer_battery_capacity", false, CFGF_NONE),
CFG_BOOL("hide_seconds", true, CFGF_NONE),
+ CFG_FLOAT("consumption_low_threshold", 100, CFGF_NONE),
CFG_CUSTOM_ALIGN_OPT,
CFG_CUSTOM_COLOR_OPTS,
CFG_CUSTOM_MIN_WIDTH_OPT,
@@ -749,6 +750,7 @@ int main(int argc, char *argv[]) {
.last_full_capacity = cfg_getbool(sec, "last_full_capacity"),
.format_percentage = cfg_getstr(sec, "format_percentage"),
.hide_seconds = cfg_getbool(sec, "hide_seconds"),
+ .consumption_low_threshold = cfg_getfloat(sec, "consumption_low_threshold"),
};
print_battery_info(&ctx);
SEC_CLOSE_MAP;
diff --git a/include/i3status.h b/include/i3status.h
index ad05055..65527c4 100644
--- a/include/i3status.h
+++ b/include/i3status.h
@@ -277,6 +277,7 @@ typedef struct {
bool last_full_capacity;
const char *format_percentage;
bool hide_seconds;
+ float consumption_low_threshold;
} battery_info_ctx_t;
void print_battery_info(battery_info_ctx_t *ctx);
diff --git a/src/print_battery_info.c b/src/print_battery_info.c
index 1f56723..13e372f 100644
--- a/src/print_battery_info.c
+++ b/src/print_battery_info.c
@@ -661,6 +661,26 @@ void print_battery_info(battery_info_ctx_t *ctx) {
batt_info.seconds_remaining = 0;
}
+ 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.
+ char string_remaining[STRING_SIZE] = "";
+ char string_emptytime[STRING_SIZE] = "";
+ char string_consumption[STRING_SIZE] = "";
+
+ if (batt_info.present_rate >= 0) {
+ if (batt_info.status == CS_DISCHARGING) {
+ if (batt_info.present_rate / 1e6 > ctx->consumption_low_threshold) {
+ START_COLOR("color_degraded");
+ colorful_output = true;
+ } else if (batt_info.present_rate / 1e6 <= ctx->consumption_low_threshold) {
+ START_COLOR("color_good");
+ colorful_output = true;
+ }
+ }
+ snprintf(string_consumption, STRING_SIZE, "%1.2fW", batt_info.present_rate / 1e6);
+ }
+
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");
@@ -671,12 +691,6 @@ void print_battery_info(battery_info_ctx_t *ctx) {
}
}
- 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.
- char string_remaining[STRING_SIZE] = "";
- char string_emptytime[STRING_SIZE] = "";
- char string_consumption[STRING_SIZE] = "";
const char *statusstr;
switch (batt_info.status) {
@@ -720,9 +734,6 @@ void print_battery_info(battery_info_ctx_t *ctx) {
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));
}
- if (batt_info.present_rate >= 0)
- snprintf(string_consumption, STRING_SIZE, "%1.2fW", batt_info.present_rate / 1e6);
-
placeholder_t placeholders[] = {
{.name = "%status", .value = string_status},
{.name = "%percentage", .value = string_percentage},
@sicelo

sicelo commented Oct 12, 2024

Copy link
Copy Markdown

Nice!
Minor nit: https://gist.github.com/marcosnils/428dae19265e09eca8d00a07db8cd962#file-i3_bat_consumption-patch-L53 will never be reached when batt_info.present_rate / 1e6 == ctx->consumption_low_threshold since that will have already been matched in https://gist.github.com/marcosnils/428dae19265e09eca8d00a07db8cd962#file-i3_bat_consumption-patch-L50

@marcosnils

marcosnils commented Oct 12, 2024 via email

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment