]> git.sur5r.net Git - i3/i3status/blob - src/print_path_exists.c
able to print percentage
[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
29         } else if (BEGINS_WITH(walk + 1, "title")) {
30             outwalk += sprintf(outwalk, "%s", title);
31             walk += strlen("title");
32
33         } else if (BEGINS_WITH(walk + 1, "status")) {
34             outwalk += sprintf(outwalk, "%s", (exists ? "yes" : "no"));
35             walk += strlen("status");
36
37         } else {
38             *(outwalk++) = '%';
39         }
40     }
41
42     END_COLOR;
43     OUTPUT_FULL_TEXT(buffer);
44 }