summaryrefslogtreecommitdiff
path: root/src/print_battery_info.c
diff options
context:
space:
mode:
authorMichael Stapelberg <michael@stapelberg.de>2012-03-25 20:55:55 +0200
committerMichael Stapelberg <michael@stapelberg.de>2012-03-25 22:05:00 +0200
commit12b1bfa9b8485de88b0bda82821c021aee197673 (patch)
treeb6d87f7ed5876109863a6599992e351509a30d58 /src/print_battery_info.c
parentafb0525235c57fc4896633003a45bfb013af38d2 (diff)
Properly output JSON with libyajl
Diffstat (limited to 'src/print_battery_info.c')
-rw-r--r--src/print_battery_info.c22
1 files changed, 11 insertions, 11 deletions
diff --git a/src/print_battery_info.c b/src/print_battery_info.c
index 10137c2..3892d35 100644
--- a/src/print_battery_info.c
+++ b/src/print_battery_info.c
@@ -3,6 +3,7 @@
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
+#include <yajl/yajl_gen.h>
#include "i3status.h"
@@ -17,7 +18,7 @@
* worn off your battery is.
*
*/
-void print_battery_info(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, bool last_full_capacity) {
time_t empty_time;
struct tm *empty_tm;
char buf[1024];
@@ -26,6 +27,7 @@ void print_battery_info(int number, const char *path, const char *format, bool l
char remainingbuf[256];
char emptytimebuf[256];
const char *walk, *last;
+ char *outwalk = buffer;
int full_design = -1,
remaining = -1,
present_rate = -1;
@@ -36,14 +38,13 @@ void print_battery_info(int number, const char *path, const char *format, bool l
memset(remainingbuf, '\0', sizeof(remainingbuf));
memset(emptytimebuf, '\0', sizeof(emptytimebuf));
- if (output_format == O_I3BAR)
- printf("{\"name\":\"battery\", \"instance\": \"%s\", \"full_text\":\"", path);
+ INSTANCE(path);
#if defined(LINUX)
static char batpath[512];
sprintf(batpath, path, number);
if (!slurp(batpath, buf, sizeof(buf))) {
- printf("No battery");
+ OUTPUT_FULL_TEXT("No battery");
return;
}
@@ -170,25 +171,24 @@ void print_battery_info(int number, const char *path, const char *format, bool l
for (walk = format; *walk != '\0'; walk++) {
if (*walk != '%') {
- putchar(*walk);
+ *(outwalk++) = *walk;
continue;
}
if (strncmp(walk+1, "status", strlen("status")) == 0) {
- printf("%s", statusbuf);
+ outwalk += sprintf(outwalk, "%s", statusbuf);
walk += strlen("status");
} else if (strncmp(walk+1, "percentage", strlen("percentage")) == 0) {
- printf("%s", percentagebuf);
+ outwalk += sprintf(outwalk, "%s", percentagebuf);
walk += strlen("percentage");
} else if (strncmp(walk+1, "remaining", strlen("remaining")) == 0) {
- printf("%s", remainingbuf);
+ outwalk += sprintf(outwalk, "%s", remainingbuf);
walk += strlen("remaining");
} else if (strncmp(walk+1, "emptytime", strlen("emptytime")) == 0) {
- printf("%s", emptytimebuf);
+ outwalk += sprintf(outwalk, "%s", emptytimebuf);
walk += strlen("emptytime");
}
}
- if (output_format == O_I3BAR)
- printf("\"}");
+ OUTPUT_FULL_TEXT(buffer);
}