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.
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
(charging ? "(CHR)" : apm_info.minutes_left));
#endif
(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;
for (walk = format; *walk != '\0'; walk++) {
if (*walk != '%') {
*(outwalk++) = *walk;
} else if (strncmp(walk+1, "remaining", strlen("remaining")) == 0) {
outwalk += sprintf(outwalk, "%s", remainingbuf);
walk += strlen("remaining");
} 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");
} 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");
} else if (strncmp(walk+1, "consumption", strlen("consumption")) == 0) {
outwalk += sprintf(outwalk, "%s", consumptionbuf);
walk += strlen("consumption");
+ EAT_SPACE_FROM_OUTPUT_IF_EMPTY(consumptionbuf);