colorize battery output if remaining time below threshold

This commit is contained in:
Simon Elsbrock 2012-05-22 23:14:59 +02:00 committed by Michael Stapelberg
parent 7a77472a81
commit 7c02c10b72
4 changed files with 12 additions and 3 deletions

View 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;
}

View 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();

View 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.

View 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;