i3 - improved tiling WM


Colorized output for disk usage

Patch status: needinfo

Patch by Raphael Michel

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

b/i3status.c

16
@@ -275,6 +275,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", 10737418240, CFGF_NONE), // 10 GB
21
+                CFG_STR("threshold_type", "bytes", CFGF_NONE),
22
+                CFG_CUSTOM_COLOR_OPTS,
23
                 CFG_END()
24
         };
25
 
26
@@ -454,7 +457,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
@@ -141,7 +141,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, int low_threshold, char *threshold_type);
45
 void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char *path, const char *format, 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
@@ -170,12 +170,21 @@ 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
-*Example order*: +disk /mnt/usbstick+
57
+It is possible to define a low_threshold that causes the disk text to be
58
+colored red if the free space is unter a certain amount. The low_threshold 
59
+type can be of threshold_type "bytes" or "percentage". So, if you configure 
60
+low_threshold to 10 and threshold_type to "percentage", and your disk is
61
+91 percent full, it will be colored red.
62
 
63
 *Example format*: +%free (%avail)/ %total+
64
 
65
 *Example format*: +%percentage_used used, %percentage_free free, %percentage_avail avail+
66
 
67
+*Example low_threshold*: +10737418240+
68
+
69
+*Example threshold_type*: +bytes+
70
+
71
+
72
 === Run-watch
73
 
74
 Expands the given path to a pidfile and checks if the process ID found inside

b/src/print_disk_info.c

79
@@ -43,9 +43,10 @@ static int print_bytes_human(char *outwalk, uint64_t bytes) {
80
  * human readable manner.
81
  *
82
  */
83
-void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const char *format) {
84
+void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const char *format, int low_threshold, char *threshold_type) {
85
         const char *walk;
86
         char *outwalk = buffer;
87
+        bool colorful_output;
88
 
89
         INSTANCE(path);
90
 
91
@@ -67,6 +68,20 @@ void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const ch
92
                         continue;
93
                 }
94
 
95
+                int percentage_free = ((uint64_t)buf.f_bsize * (uint64_t)buf.f_bfree / ((uint64_t)buf.f_bsize * (uint64_t)buf.f_blocks))*100;
96
+
97
+                if (strncmp(threshold_type, "percentage", strlen(threshold_type))
98
+                        && percentage_free < low_threshold) {
99
+                        START_COLOR("color_bad");
100
+                        colorful_output = true;
101
+                } else if (strncmp(threshold_type, "bytes", strlen(threshold_type))
102
+                        && ((uint64_t)buf.f_bsize * (uint64_t)buf.f_bfree) < (uint64_t) low_threshold) {
103
+                        START_COLOR("color_bad");
104
+                        colorful_output = true;
105
+                } else {
106
+                        colorful_output = false;
107
+                }
108
+
109
                 if (BEGINS_WITH(walk+1, "free")) {
110
                         outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_bfree);
111
                         walk += strlen("free");
112
@@ -106,6 +121,9 @@ void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const ch
113
                         outwalk += sprintf(outwalk, "%.01f%%", 100.0 * (double)buf.f_bavail / (double)buf.f_blocks);
114
                         walk += strlen("percentage_avail");
115
                 }
116
+                
117
+                if (colorful_output)
118
+                        END_COLOR;
119
         }
120
 
121
         *outwalk = '\0';