Merge pull request #404 from Stunkymonkey/format_placeholder-file_content

use format_placeholder for file_content
This commit is contained in:
Ingo Bürk 2020-04-07 08:31:57 +02:00 committed by GitHub
commit e697a1f19f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -12,6 +12,8 @@
#include <errno.h>
#include "i3status.h"
#define STRING_SIZE 10
static void *scalloc(size_t size) {
void *result = calloc(size, 1);
if (result == NULL) {
@ -42,32 +44,32 @@ void print_file_contents(yajl_gen json_gen, char *buffer, const char *title, con
START_COLOR("color_bad");
}
for (; *walk != '\0'; walk++) {
if (*walk != '%') {
*(outwalk++) = *walk;
} else if (BEGINS_WITH(walk + 1, "title")) {
outwalk += sprintf(outwalk, "%s", title);
walk += strlen("title");
} else if (BEGINS_WITH(walk + 1, "content")) {
for (char *s = buf; *s != '\0' && n > 0; s++, n--) {
if (*s != '\n') {
*(outwalk++) = *s;
}
}
walk += strlen("content");
} else if (BEGINS_WITH(walk + 1, "errno")) {
outwalk += sprintf(outwalk, "%d", errno);
walk += strlen("errno");
} else if (BEGINS_WITH(walk + 1, "error")) {
outwalk += sprintf(outwalk, "%s", strerror(errno));
walk += strlen("error");
} else {
*(outwalk++) = '%';
// remove newline chars
char *src, *dst;
for (src = dst = buf; *src != '\0'; src++) {
*dst = *src;
if (*dst != '\n') {
dst++;
}
}
*dst = '\0';
char string_errno[STRING_SIZE];
sprintf(string_errno, "%d", errno);
placeholder_t placeholders[] = {
{.name = "%title", .value = title},
{.name = "%content", .value = buf},
{.name = "%errno", .value = string_errno},
{.name = "%error", .value = strerror(errno)}};
const size_t num = sizeof(placeholders) / sizeof(placeholder_t);
buffer = format_placeholders(walk, &placeholders[0], num);
free(buf);
END_COLOR;
OUTPUT_FULL_TEXT(buffer);
free(buffer);
}