summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/print_ipv6_addr.c16
-rw-r--r--src/print_load.c35
2 files changed, 22 insertions, 29 deletions
diff --git a/src/print_ipv6_addr.c b/src/print_ipv6_addr.c
index 5a0c38d..8fd8669 100644
--- a/src/print_ipv6_addr.c
+++ b/src/print_ipv6_addr.c
@@ -118,7 +118,6 @@ static char *get_ipv6_addr(void) {
}
void print_ipv6_info(yajl_gen json_gen, char *buffer, const char *format_up, const char *format_down) {
- const char *walk;
char *addr_string = get_ipv6_addr();
char *outwalk = buffer;
@@ -131,18 +130,13 @@ void print_ipv6_info(yajl_gen json_gen, char *buffer, const char *format_up, con
}
START_COLOR("color_good");
- for (walk = format_up; *walk != '\0'; walk++) {
- if (*walk != '%') {
- *(outwalk++) = *walk;
- } else if (BEGINS_WITH(walk + 1, "ip")) {
- outwalk += sprintf(outwalk, "%s", addr_string);
- walk += strlen("ip");
+ placeholder_t placeholders[] = {
+ {.name = "%ip", .value = addr_string}};
- } else {
- *(outwalk++) = '%';
- }
- }
+ const size_t num = sizeof(placeholders) / sizeof(placeholder_t);
+ buffer = format_placeholders(format_up, &placeholders[0], num);
END_COLOR;
OUTPUT_FULL_TEXT(buffer);
+ free(buffer);
}
diff --git a/src/print_load.c b/src/print_load.c
index 5d97a2c..3ffc576 100644
--- a/src/print_load.c
+++ b/src/print_load.c
@@ -7,6 +7,10 @@
#include <yajl/yajl_gen.h>
#include <yajl/yajl_version.h>
+#include "i3status.h"
+
+#define STRING_SIZE 10
+
void print_load(yajl_gen json_gen, char *buffer, const char *format, const char *format_above_threshold, const float max_threshold) {
char *outwalk = buffer;
/* Get load */
@@ -14,7 +18,6 @@ void print_load(yajl_gen json_gen, char *buffer, const char *format, const char
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(linux) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__) || defined(sun) || defined(__DragonFly__)
double loadavg[3];
const char *selected_format = format;
- const char *walk;
bool colorful_output = false;
if (getloadavg(loadavg, 3) == -1)
@@ -27,32 +30,28 @@ void print_load(yajl_gen json_gen, char *buffer, const char *format, const char
selected_format = format_above_threshold;
}
- for (walk = selected_format; *walk != '\0'; walk++) {
- if (*walk != '%') {
- *(outwalk++) = *walk;
+ char string_loadavg_1[STRING_SIZE];
+ char string_loadavg_5[STRING_SIZE];
+ char string_loadavg_15[STRING_SIZE];
- } else if (BEGINS_WITH(walk + 1, "1min")) {
- outwalk += sprintf(outwalk, "%1.2f", loadavg[0]);
- walk += strlen("1min");
+ snprintf(string_loadavg_1, STRING_SIZE, "%1.2f", loadavg[0]);
+ snprintf(string_loadavg_5, STRING_SIZE, "%1.2f", loadavg[1]);
+ snprintf(string_loadavg_15, STRING_SIZE, "%1.2f", loadavg[2]);
- } else if (BEGINS_WITH(walk + 1, "5min")) {
- outwalk += sprintf(outwalk, "%1.2f", loadavg[1]);
- walk += strlen("5min");
+ placeholder_t placeholders[] = {
+ {.name = "%1min", .value = string_loadavg_1},
+ {.name = "%5min", .value = string_loadavg_5},
+ {.name = "%15min", .value = string_loadavg_15}};
- } else if (BEGINS_WITH(walk + 1, "15min")) {
- outwalk += sprintf(outwalk, "%1.2f", loadavg[2]);
- walk += strlen("15min");
-
- } else {
- *(outwalk++) = '%';
- }
- }
+ const size_t num = sizeof(placeholders) / sizeof(placeholder_t);
+ buffer = format_placeholders(selected_format, &placeholders[0], num);
if (colorful_output)
END_COLOR;
*outwalk = '\0';
OUTPUT_FULL_TEXT(buffer);
+ free(buffer);
return;
error: