i3 - improved tiling WM


Add colorized output for load avg

Patch status: merged

Patch by Raphael Michel

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

b/i3status.c

16
@@ -257,6 +257,8 @@ int main(int argc, char *argv[]) {
17
 
18
         cfg_opt_t load_opts[] = {
19
                 CFG_STR("format", "%1min %5min %15min", CFGF_NONE),
20
+                CFG_INT("max_threshold", 5, CFGF_NONE),
21
+                CFG_CUSTOM_COLOR_OPTS,
22
                 CFG_END()
23
         };
24
 
25
@@ -460,7 +462,7 @@ int main(int argc, char *argv[]) {
26
 
27
                         CASE_SEC("load") {
28
                                 SEC_OPEN_MAP("load");
29
-                                print_load(json_gen, buffer, cfg_getstr(sec, "format"));
30
+                                print_load(json_gen, buffer, cfg_getstr(sec, "format"), cfg_getint(sec, "max_threshold"));
31
                                 SEC_CLOSE_MAP;
32
                         }
33
 

b/include/i3status.h

38
@@ -151,7 +151,7 @@ void print_run_watch(yajl_gen json_gen, char *buffer, const char *title, const c
39
 void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const char *path, const char *format, int);
40
 void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format);
41
 void print_eth_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down);
42
-void print_load(yajl_gen json_gen, char *buffer, const char *format);
43
+void print_load(yajl_gen json_gen, char *buffer, const char *format, const int max_threshold);
44
 void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char *device, const char *mixer, int mixer_idx);
45
 bool process_runs(const char *path);
46
 

b/man/i3status.man

51
@@ -259,12 +259,16 @@ Gets the percentual CPU usage from +/proc/stat+ (Linux) or +sysctl(3)+ (FreeBSD/
52
 === Load
53
 
54
 Gets the system load (number of processes waiting for CPU time in the last
55
-1, 5 and 15 minutes).
56
+1, 5 and 15 minutes). It is possible to define a max_threshold that will
57
+color the load value red in case the load average of the last minute is
58
+getting higher than the configured threshold. Defaults to 5.
59
 
60
 *Example order*: +load+
61
 
62
 *Example format*: +%1min %5min %15min+
63
 
64
+*Example max_threshold*: 5
65
+
66
 === Time
67
 
68
 Outputs the current time in the local timezone.

b/src/print_load.c

73
@@ -6,13 +6,14 @@
74
 #include <yajl/yajl_gen.h>
75
 #include <yajl/yajl_version.h>
76
 
77
-void print_load(yajl_gen json_gen, char *buffer, const char *format) {
78
+void print_load(yajl_gen json_gen, char *buffer, const char *format, const int max_threshold) {
79
         char *outwalk = buffer;
80
         /* Get load */
81
 
82
 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(linux) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__) || defined(sun) || defined(__DragonFly__)
83
         double loadavg[3];
84
         const char *walk;
85
+        bool colorful_output = false;
86
 
87
         if (getloadavg(loadavg, 3) == -1)
88
                 goto error;
89
@@ -22,6 +23,10 @@ void print_load(yajl_gen json_gen, char *buffer, const char *format) {
90
                         *(outwalk++) = *walk;
91
                         continue;
92
                 }
93
+                if (loadavg[0] >= max_threshold) {
94
+                        START_COLOR("color_bad");
95
+                        colorful_output = true;
96
+                }
97
 
98
                 if (BEGINS_WITH(walk+1, "1min")) {
99
                         outwalk += sprintf(outwalk, "%1.2f", loadavg[0]);
100
@@ -37,6 +42,8 @@ void print_load(yajl_gen json_gen, char *buffer, const char *format) {
101
                         outwalk += sprintf(outwalk, "%1.2f", loadavg[2]);
102
                         walk += strlen("15min");
103
                 }
104
+                if (colorful_output)
105
+                        END_COLOR;
106
         }
107
 
108
         *outwalk = '\0';