i3 - improved tiling WM


Colorized output for disk usage

Patch status: needinfo

Patch by Kevin Pulo

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

b/i3status.c

16
@@ -281,6 +281,9 @@ int main(int argc, char *argv[]) {
17
 
18
         cfg_opt_t disk_opts[] = {
19
                 CFG_STR("format", "%free", CFGF_NONE),
20
+                CFG_INT("low_threshold", 10 * 1024, CFGF_NONE), // 10 GiB
21
+                CFG_STR("threshold_type", "mibibytes", CFGF_NONE),
22
+                CFG_CUSTOM_COLOR_OPTS,
23
                 CFG_END()
24
         };
25
 
26
@@ -482,7 +485,7 @@ int main(int argc, char *argv[]) {
27
 
28
                         CASE_SEC_TITLE("disk") {
29
                                 SEC_OPEN_MAP("disk_info");
30
-                                print_disk_info(json_gen, buffer, title, cfg_getstr(sec, "format"));
31
+                                print_disk_info(json_gen, buffer, title, cfg_getstr(sec, "format"), cfg_getint(sec, "low_threshold"), cfg_getstr(sec, "threshold_type"));
32
                                 SEC_CLOSE_MAP;
33
                         }
34
 

b/include/i3status.h

39
@@ -145,7 +145,7 @@ char *auto_detect_format();
40
 void set_timezone(const char *tz);
41
 
42
 void print_ipv6_info(yajl_gen json_gen, char *buffer, const char *format_up, const char *format_down);
43
-void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const char *format);
44
+void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const char *format, const int low_threshold, const char *threshold_type);
45
 void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char *path, const char *format, const char *format_down, int low_threshold, char *threshold_type, bool last_full_capacity, bool integer_battery_capacity);
46
 void print_time(yajl_gen json_gen, char *buffer, const char *format, const char *tz, time_t t);
47
 void print_ddate(yajl_gen json_gen, char *buffer, const char *format, time_t t);

b/man/i3status.man

52
@@ -177,12 +177,23 @@ Gets used, free, available and total amount of bytes on the given mounted filesy
53
 These values can also be expressed in percentages with the percentage_used,
54
 percentage_free, percentage_avail and percentage_used_of_avail formats.
55
 
56
+It is possible to define a low_threshold that causes the disk text to be
57
+colored red if the free space is unter a certain amount. The low_threshold
58
+type can be of threshold_type "mibibytes" or "percentage". So, if you configure
59
+low_threshold to 10 and threshold_type to "percentage", and your disk is
60
+91 percent full, it will be colored red.
61
+
62
 *Example order*: +disk /mnt/usbstick+
63
 
64
 *Example format*: +%free (%avail)/ %total+
65
 
66
 *Example format*: +%percentage_used used, %percentage_free free, %percentage_avail avail+
67
 
68
+*Example low_threshold*: +10240+
69
+
70
+*Example threshold_type*: +mibibytes+
71
+
72
+
73
 === Run-watch
74
 
75
 Expands the given path to a pidfile and checks if the process ID found inside

b/src/print_disk_info.c

80
@@ -43,9 +43,10 @@ static int print_bytes_human(char *outwalk, uint64_t bytes) {
81
  * human readable manner.
82
  *
83
  */
84
-void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const char *format) {
85
+void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const char *format, const int low_threshold, const char *threshold_type) {
86
         const char *walk;
87
         char *outwalk = buffer;
88
+        bool colorful_output = false;
89
 
90
         INSTANCE(path);
91
 
92
@@ -61,6 +62,19 @@ void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const ch
93
                 return;
94
 #endif
95
 
96
+        int percentage_free = ((float)buf.f_bfree / (float)buf.f_blocks) * 100;
97
+        uint64_t megabytes_free = ((uint64_t)buf.f_bsize * (uint64_t)buf.f_bavail)/(1024*1024);
98
+        if ((strcmp(threshold_type, "percentage") == 0 &&
99
+                percentage_free < low_threshold) ||
100
+                ((strcmp(threshold_type, "mibibytes") == 0 ||
101
+                strcmp(threshold_type, "megabytes")) &&
102
+                megabytes_free < (uint64_t) low_threshold)) {
103
+                START_COLOR("color_bad");
104
+                colorful_output = true;
105
+        } else {
106
+                colorful_output = false;
107
+        }
108
+
109
         for (walk = format; *walk != '\0'; walk++) {
110
                 if (*walk != '%') {
111
                         *(outwalk++) = *walk;
112
@@ -107,6 +121,8 @@ void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const ch
113
                         walk += strlen("percentage_avail");
114
                 }
115
         }
116
+        if (colorful_output)
117
+                END_COLOR;
118
 
119
         *outwalk = '\0';
120
         OUTPUT_FULL_TEXT(buffer);