diff options
author | Piotr Domagalski <piotr@domagalski.com> | 2012-08-28 22:07:33 +0200 |
---|---|---|
committer | Michael Stapelberg <michael@stapelberg.de> | 2012-08-31 12:08:24 +0200 |
commit | fa4e9cdfb3147fbcfb9e42067a49652d0a0d7456 (patch) | |
tree | beacee945f35d28e166f4a6b8f858930230ca218 /src/print_battery_info.c | |
parent | 68f438ec9eefbca5d735a1a7897d52e0e8e6f1c7 (diff) |
Eat unnecessary space from the battery format str.
If the battery is not discharging it may be not possible to give information on
remaining time or consumption. The resulting strings (%remaining, %consumption,
%emptytime) are empty then. But because they are in the format string, the
output string contains unnecessary spaces in this case.
This commit makes i3status strip these spaces.
Diffstat (limited to 'src/print_battery_info.c')
-rw-r--r-- | src/print_battery_info.c | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/src/print_battery_info.c b/src/print_battery_info.c index 202d9bb..4fbf411 100644 --- a/src/print_battery_info.c +++ b/src/print_battery_info.c @@ -1,4 +1,5 @@ // vim:ts=8:expandtab +#include <ctype.h> #include <time.h> #include <string.h> #include <stdlib.h> @@ -266,6 +267,16 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char (charging ? "(CHR)" : apm_info.minutes_left)); #endif +#define EAT_SPACE_FROM_OUTPUT_IF_EMPTY(_buf) \ + do { \ + if (strlen(_buf) == 0) { \ + if (outwalk > buffer && isspace(outwalk[-1])) \ + outwalk--; \ + else if (isspace(*(walk+1))) \ + walk++; \ + } \ + } while (0) + for (walk = format; *walk != '\0'; walk++) { if (*walk != '%') { *(outwalk++) = *walk; @@ -281,12 +292,15 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char } else if (strncmp(walk+1, "remaining", strlen("remaining")) == 0) { outwalk += sprintf(outwalk, "%s", remainingbuf); walk += strlen("remaining"); + EAT_SPACE_FROM_OUTPUT_IF_EMPTY(remainingbuf); } else if (strncmp(walk+1, "emptytime", strlen("emptytime")) == 0) { outwalk += sprintf(outwalk, "%s", emptytimebuf); walk += strlen("emptytime"); + EAT_SPACE_FROM_OUTPUT_IF_EMPTY(emptytimebuf); } else if (strncmp(walk+1, "consumption", strlen("consumption")) == 0) { outwalk += sprintf(outwalk, "%s", consumptionbuf); walk += strlen("consumption"); + EAT_SPACE_FROM_OUTPUT_IF_EMPTY(consumptionbuf); } } |