]> git.sur5r.net Git - i3/i3status/commitdiff
colorize battery output if remaining time below threshold
authorSimon Elsbrock <simon@iodev.org>
Tue, 22 May 2012 21:14:59 +0000 (23:14 +0200)
committerMichael Stapelberg <michael@stapelberg.de>
Fri, 25 May 2012 07:53:49 +0000 (09:53 +0200)
i3status.c
include/i3status.h
man/i3status.man
src/print_battery_info.c

index 15c58da668eab3e2cead1d81ee5a4cc97fec60af..e812893cf0e3c01862c24f1b75720ac1d89272b9 100644 (file)
@@ -214,6 +214,7 @@ int main(int argc, char *argv[]) {
         cfg_opt_t battery_opts[] = {
                 CFG_STR("format", "%status %percentage %remaining", CFGF_NONE),
                 CFG_STR("path", "/sys/class/power_supply/BAT%d/uevent", CFGF_NONE),
+                CFG_INT("threshold", 10, CFGF_NONE),
                 CFG_BOOL("last_full_capacity", false, CFGF_NONE),
                 CFG_END()
         };
@@ -413,7 +414,7 @@ int main(int argc, char *argv[]) {
 
                         CASE_SEC_TITLE("battery") {
                                 SEC_OPEN_MAP("battery");
-                                print_battery_info(json_gen, buffer, atoi(title), cfg_getstr(sec, "path"), cfg_getstr(sec, "format"), cfg_getbool(sec, "last_full_capacity"));
+                                print_battery_info(json_gen, buffer, atoi(title), cfg_getstr(sec, "path"), cfg_getstr(sec, "format"), cfg_getint(sec, "threshold"), cfg_getbool(sec, "last_full_capacity"));
                                 SEC_CLOSE_MAP;
                         }
 
index a6d5fc6ce4eecec9f68dab948ae191da0166ee2e..eb19d52e16626c8ce584f86deb205521ee465de9 100644 (file)
@@ -137,7 +137,7 @@ char *auto_detect_format();
 
 void print_ipv6_info(yajl_gen json_gen, char *buffer, const char *format_up, const char *format_down);
 void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const char *format);
-void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char *path, const char *format, bool last_full_capacity);
+void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char *path, const char *format, int threshold, bool last_full_capacity);
 void print_time(yajl_gen json_gen, char *buffer, const char *format, struct tm *current_tm);
 void print_ddate(yajl_gen json_gen, char *buffer, const char *format, struct tm *current_tm);
 const char *get_ip_addr();
index 195b648d625fd49c03523d81b5576798d253505a..7f8875096f4545f9298057b75c3d2a32ef7d70d5 100644 (file)
@@ -72,6 +72,7 @@ ethernet eth0 {
 battery 0 {
         format = "%status %percentage %remaining %emptytime"
         path = "/sys/class/power_supply/BAT%d/uevent"
+        threshold = 10
 }
 
 run_watch DHCP {
@@ -207,6 +208,8 @@ with the battery number, but you can just hard-code a path as well.
 
 *Example format*: +%status %remaining (%emptytime)+
 
+*Example threshold*: +threshold 10+
+
 === CPU-Temperature
 
 Gets the temperature of the given thermal zone.
index 1c000e1ea0a9a49c139f0be67e612cbbb00311d8..cc7c5fef580a4c36e557757c52d015493fddc350 100644 (file)
@@ -26,7 +26,7 @@
  * worn off your battery is.
  *
  */
-void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char *path, const char *format, bool last_full_capacity) {
+void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char *path, const char *format, int threshold, bool last_full_capacity) {
         time_t empty_time;
         struct tm *empty_tm;
         char buf[1024];
@@ -120,6 +120,9 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char
                 minutes = seconds / 60;
                 seconds -= (minutes * 60);
 
+                if (threshold > 0 && seconds_remaining < 60 * threshold)
+                        START_COLOR("color_bad");
+
                 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02d:%02d:%02d",
                         max(hours, 0), max(minutes, 0), max(seconds, 0));
 
@@ -129,6 +132,8 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char
 
                 (void)snprintf(emptytimebuf, sizeof(emptytimebuf), "%02d:%02d:%02d",
                         max(empty_tm->tm_hour, 0), max(empty_tm->tm_min, 0), max(empty_tm->tm_sec, 0));
+
+                END_COLOR;
         }
 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
         int state;