i3status/src/print_path_exists.c

45 lines
1.1 KiB
C
Raw Normal View History

// vim:ts=4:sw=4:expandtab
2019-01-23 07:56:40 +00:00
#include <config.h>
2013-11-12 19:51:23 +00:00
#include <stdio.h>
#include <stdlib.h>
2013-11-12 19:51:23 +00:00
#include <string.h>
#include <yajl/yajl_gen.h>
#include <yajl/yajl_version.h>
#include <sys/stat.h>
#include "i3status.h"
2020-03-31 13:47:39 +00:00
#define STRING_SIZE 5
void print_path_exists(path_exists_ctx_t *ctx) {
const char *walk;
char *outwalk = ctx->buf;
struct stat st;
const bool exists = (stat(ctx->path, &st) == 0);
2013-11-12 19:51:23 +00:00
if (exists || ctx->format_down == NULL) {
walk = ctx->format;
} else {
walk = ctx->format_down;
}
INSTANCE(ctx->path);
2013-11-12 19:51:23 +00:00
START_COLOR((exists ? "color_good" : "color_bad"));
2013-11-12 19:51:23 +00:00
2020-03-31 13:47:39 +00:00
char string_status[STRING_SIZE];
2013-11-12 19:51:23 +00:00
2020-03-31 13:47:39 +00:00
snprintf(string_status, STRING_SIZE, "%s", (exists ? "yes" : "no"));
2020-03-31 13:47:39 +00:00
placeholder_t placeholders[] = {
{.name = "%title", .value = ctx->title},
2020-03-31 13:47:39 +00:00
{.name = "%status", .value = string_status}};
2020-03-31 13:47:39 +00:00
const size_t num = sizeof(placeholders) / sizeof(placeholder_t);
char *formatted = format_placeholders(walk, &placeholders[0], num);
OUTPUT_FORMATTED;
free(formatted);
2013-11-12 19:51:23 +00:00
END_COLOR;
OUTPUT_FULL_TEXT(ctx->buf);
2013-11-12 19:51:23 +00:00
}