diff options
author | Simon Elsbrock <simon@iodev.org> | 2012-10-16 22:45:19 +0200 |
---|---|---|
committer | Michael Stapelberg <michael@stapelberg.de> | 2012-10-16 22:49:22 +0200 |
commit | 35f3bcb348cdbc43060a23c2116259bb74c981a1 (patch) | |
tree | f9c71f55ff02d6e6c95a62035166f2ab9e1ca8b3 /src/print_battery_info.c | |
parent | 450424f97a9d02e5392ce331902c95d0fa0e82ec (diff) |
bugfix: colorize output even without discharge rate (thanks f8l)
On some systems, the discharge rate may be missing, although the battery
is still working. This leads to an edge case on Linux systems in which
the output may not be colorized although a threshold was defined.
This commit fixes the behavior by colorizing output if threshold_type is
set to "percentage". Since we cannot calculate remaining time without
discharge rate, output is still uncolorized in case of threshold_type
set to "time".
Diffstat (limited to 'src/print_battery_info.c')
-rw-r--r-- | src/print_battery_info.c | 19 |
1 files changed, 16 insertions, 3 deletions
diff --git a/src/print_battery_info.c b/src/print_battery_info.c index 6878540..ae11348 100644 --- a/src/print_battery_info.c +++ b/src/print_battery_info.c @@ -157,6 +157,8 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char && seconds_remaining < 60 * low_threshold) { START_COLOR("color_bad"); colorful_output = true; + } else { + colorful_output = false; } } @@ -172,9 +174,17 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char (void)snprintf(consumptionbuf, sizeof(consumptionbuf), "%1.2fW", ((float)present_rate / 1000.0 / 1000.0)); - - if (colorful_output) - END_COLOR; + } else { + /* On some systems, present_rate may not exist. Still, make sure + * we colorize the output if threshold_type is set to percentage + * (since we don't have any information on remaining time). */ + if (status == CS_DISCHARGING && low_threshold > 0) { + if (strncmp(threshold_type, "percentage", strlen(threshold_type)) == 0 + && percentage_remaining < low_threshold) { + START_COLOR("color_bad"); + colorful_output = true; + } + } } #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) int state; @@ -323,5 +333,8 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char } } + if (colorful_output) + END_COLOR; + OUTPUT_FULL_TEXT(buffer); } |