]> git.sur5r.net Git - i3/i3status/blob - src/print_path_exists.c
Adding the option "format_down" to path_exists and run_watch.
[i3/i3status] / src / print_path_exists.c
1 // vim:ts=4:sw=4:expandtab
2 #include <stdio.h>
3 #include <string.h>
4 #include <yajl/yajl_gen.h>
5 #include <yajl/yajl_version.h>
6 #include <sys/stat.h>
7 #include "i3status.h"
8
9 void print_path_exists(yajl_gen json_gen, char *buffer, const char *title, const char *path, const char *format, const char *format_down) {
10     const char *walk;
11     char *outwalk = buffer;
12     struct stat st;
13     const bool exists = (stat(path, &st) == 0);
14
15     if (exists || format_down == NULL) {
16         walk = format;
17     } else {
18         walk = format_down;
19     }
20
21     INSTANCE(path);
22
23     START_COLOR((exists ? "color_good" : "color_bad"));
24
25     for (; *walk != '\0'; walk++) {
26         if (*walk != '%') {
27             *(outwalk++) = *walk;
28             continue;
29         }
30
31         if (BEGINS_WITH(walk + 1, "title")) {
32             outwalk += sprintf(outwalk, "%s", title);
33             walk += strlen("title");
34         } else if (BEGINS_WITH(walk + 1, "status")) {
35             outwalk += sprintf(outwalk, "%s", (exists ? "yes" : "no"));
36             walk += strlen("status");
37         }
38     }
39
40     END_COLOR;
41     OUTPUT_FULL_TEXT(buffer);
42 }