Last active
October 24, 2024 02:51
-
-
Save marcosnils/428dae19265e09eca8d00a07db8cd962 to your computer and use it in GitHub Desktop.
i3status patch to show green or bad status depending on battery consumption
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}, |
True, thx for pointing it out.
sent from mobile
Em sáb., 12 de out. de 2024, 17:01, Sicelo ***@***.***>
escreveu:
… ***@***.**** commented on this gist.
------------------------------
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
—
Reply to this email directly, view it on GitHub
<https://gist.github.com/marcosnils/428dae19265e09eca8d00a07db8cd962#gistcomment-5232524>
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAMBLWVLNKQZNPWWPDGIH5DZ3F52FBFKMF2HI4TJMJ2XIZLTSKBKK5TBNR2WLJDUOJ2WLJDOMFWWLO3UNBZGKYLEL5YGC4TUNFRWS4DBNZ2F6YLDORUXM2LUPGBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DFVRZXKYTKMVRXIX3UPFYGLK2HNFZXIQ3PNVWWK3TUUZ2G64DJMNZZDAVEOR4XAZNEM5UXG5FFOZQWY5LFVEYTGMZTGAZDOOJZU52HE2LHM5SXFJTDOJSWC5DF>
.
You are receiving this email because you authored the thread.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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