i3 - improved tiling WM


i3status: external command support

Patch status: rejected

Patch by Christoph Hoopmann

To apply this patch, use:
curl http://cr.i3wm.org/patch/664/raw.patch | git am

b/i3status.c

18
@@ -289,6 +289,13 @@ int main(int argc, char *argv[]) {
19
                 CFG_END()
20
         };
21
 
22
+        cfg_opt_t external_opts[] = {
23
+                CFG_STR("command", NULL, CFGF_NONE),
24
+                CFG_STR("format", "%title: %status", CFGF_NONE),
25
+                CFG_CUSTOM_COLOR_OPTS,
26
+                CFG_END()
27
+        };
28
+
29
         cfg_opt_t wireless_opts[] = {
30
                 CFG_STR("format_up", "W: (%quality at %essid, %bitrate) %ip", CFGF_NONE),
31
                 CFG_STR("format_down", "W: down", CFGF_NONE),
32
@@ -410,6 +417,7 @@ int main(int argc, char *argv[]) {
33
                 CFG_SEC("general", general_opts, CFGF_NONE),
34
                 CFG_SEC("run_watch", run_watch_opts, CFGF_TITLE | CFGF_MULTI),
35
                 CFG_SEC("path_exists", path_exists_opts, CFGF_TITLE | CFGF_MULTI),
36
+                CFG_SEC("external", external_opts, CFGF_TITLE | CFGF_MULTI),
37
                 CFG_SEC("wireless", wireless_opts, CFGF_TITLE | CFGF_MULTI),
38
                 CFG_SEC("ethernet", ethernet_opts, CFGF_TITLE | CFGF_MULTI),
39
                 CFG_SEC("battery", battery_opts, CFGF_TITLE | CFGF_MULTI),
40
@@ -604,6 +612,12 @@ int main(int argc, char *argv[]) {
41
                                 SEC_CLOSE_MAP;
42
                         }
43
 
44
+                        CASE_SEC_TITLE("external") {
45
+                                SEC_OPEN_MAP("external");
46
+                                print_external(json_gen, buffer, title, cfg_getstr(sec, "command"), cfg_getstr(sec, "format"));
47
+                                SEC_CLOSE_MAP;
48
+                        }
49
+
50
                         CASE_SEC_TITLE("disk") {
51
                                 SEC_OPEN_MAP("disk_info");
52
                                 print_disk_info(json_gen, buffer, title, cfg_getstr(sec, "format"), cfg_getstr(sec, "prefix_type"), cfg_getstr(sec, "threshold_type"), cfg_getfloat(sec, "low_threshold"));

b/i3status.conf

57
@@ -15,6 +15,7 @@ order += "ipv6"
58
 order += "disk /"
59
 order += "run_watch DHCP"
60
 order += "run_watch VPN"
61
+order += "external krel"
62
 order += "wireless wlan0"
63
 order += "ethernet eth0"
64
 order += "battery 0"
65
@@ -44,6 +45,11 @@ run_watch VPN {
66
         pidfile = "/var/run/vpnc/pid"
67
 }
68
 
69
+external krel {
70
+        command = "uname -r"
71
+        format = "%title: %status"
72
+}
73
+
74
 tztime local {
75
         format = "%Y-%m-%d %H:%M:%S"
76
 }

b/include/i3status.h

81
@@ -179,6 +179,7 @@ const char *get_ip_addr();
82
 void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down);
83
 void print_run_watch(yajl_gen json_gen, char *buffer, const char *title, const char *pidfile, const char *format);
84
 void print_path_exists(yajl_gen json_gen, char *buffer, const char *title, const char *path, const char *format);
85
+void print_external(yajl_gen json_gen, char *buffer, const char *title, const char *command, const char *format);
86
 void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const char *path, const char *format, int);
87
 void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format);
88
 void print_eth_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down);

b/man/i3status.man

93
@@ -52,6 +52,7 @@ order += "disk /"
94
 order += "run_watch DHCP"
95
 order += "run_watch VPNC"
96
 order += "path_exists VPN"
97
+order += "external krel"
98
 order += "wireless wlan0"
99
 order += "ethernet eth0"
100
 order += "battery 0"
101
@@ -95,6 +96,11 @@ path_exists VPN {
102
         path = "/proc/sys/net/ipv4/conf/tun0"
103
 }
104
 
105
+external krel {
106
+        command = "uname -r"
107
+        format = "%title: %status"
108
+}
109
+
110
 tztime local {
111
         format = "%Y-%m-%d %H:%M:%S"
112
 }
113
@@ -268,6 +274,16 @@ something is active, like for example a VPN tunnel managed by NetworkManager.
114
 
115
 *Example format*: +%title: %status+
116
 
117
+=== External
118
+
119
+Executes the given command and returns the first line of output.
120
+
121
+*Example order*: +external krel+
122
+
123
+*Example command*: +uname -r+
124
+
125
+*Example format*: +%title: %status+
126
+
127
 === Wireless
128
 
129
 Gets the link quality and ESSID of the given wireless network interface. You

b/src/print_external.c

135
@@ -0,0 +1,42 @@
136
+#include <stdio.h>
137
+#include <string.h>
138
+#include <yajl/yajl_gen.h>
139
+#include <yajl/yajl_version.h>
140
+#include <sys/stat.h>
141
+#include "i3status.h"
142
+
143
+void print_external(yajl_gen json_gen, char *buffer, const char *title, const char *command, const char *format) {
144
+        const char *walk;
145
+        char *outwalk = buffer;
146
+        char pbuffer[1024] = "\0";
147
+        FILE *fp;
148
+
149
+        INSTANCE(title);
150
+
151
+        fp = popen(command, "r");
152
+        if (!fp) {
153
+            strcpy(pbuffer, "Failed");
154
+        } else {
155
+            if (fgets(pbuffer, sizeof(pbuffer)-1, fp))
156
+                pbuffer[strlen(pbuffer) - 1] = '\0';
157
+            pclose(fp);
158
+        }
159
+            
160
+        for (walk = format; *walk != '\0'; walk++) {
161
+                if (*walk != '%') {
162
+                        *(outwalk++) = *walk;
163
+                        continue;
164
+                }
165
+
166
+                if (strncmp(walk+1, "title", strlen("title")) == 0) {
167
+                        outwalk += sprintf(outwalk, "%s", title);
168
+                        walk += strlen("title");
169
+                } else if (strncmp(walk+1, "status", strlen("status")) == 0) {
170
+                        outwalk += sprintf(outwalk, "%s", pbuffer);
171
+                        walk += strlen("status");
172
+                }
173
+        }
174
+
175
+        END_COLOR;
176
+        OUTPUT_FULL_TEXT(buffer);
177
+}