i3 - improved tiling WM


disk: Colorize output when below given threshold

Patch status: needinfo

Patch by Mats

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

b/i3status.c

16
@@ -290,6 +290,9 @@ int main(int argc, char *argv[]) {
17
         cfg_opt_t disk_opts[] = {
18
                 CFG_STR("format", "%free", CFGF_NONE),
19
                 CFG_STR("prefix_type", "binary", CFGF_NONE),
20
+                CFG_STR("threshold_type", "mbytes_avail", CFGF_NONE),
21
+                CFG_FLOAT("low_threshold", 0, CFGF_NONE),
22
+                CFG_CUSTOM_COLOR_OPTS,
23
                 CFG_END()
24
         };
25
 
26
@@ -499,7 +502,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"), cfg_getstr(sec, "prefix_type"));
31
+                                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"));
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, const char *prefix_type);
44
+void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const char *format, const char *prefix_type, const char *threshold_type, const double low_threshold);
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, bool hide_seconds);
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
@@ -196,6 +196,16 @@ SI prefixes (k, M, G, T) represent multiples of powers of 1000.
53
 custom::
54
 The custom prefixes (K, M, G, T) represent multiples of powers of 1024.
55
 
56
+It is possible to define a low_threshold that causes the disk text to be
57
+displayed using color_bad. The low_threshold type can be of threshold_type
58
+"mbytes_free", "mbytes_avail", "percentage_free", or "percentage_avail", where
59
+mbytes stands either for mega- or mebibytes depending on the prefix_type. So,
60
+if you configure low_threshold to 1024, threshold_type to "mbytes_avail", and
61
+prefix_type to "binary", and the remaining available disk space is below 1 GiB,
62
+it will be colored bad. If not specified, threshold_type is assumed to be
63
+"mbytes_avail" and low_threshold to be set to 0, which implies no highlighting
64
+at all.
65
+
66
 *Example order*: +disk /mnt/usbstick+
67
 
68
 *Example format*: +%free (%avail)/ %total+
69
@@ -204,6 +214,10 @@ The custom prefixes (K, M, G, T) represent multiples of powers of 1024.
70
 
71
 *Example prefix_type*: +custom+
72
 
73
+*Example low_threshold*: +1000+
74
+
75
+*Example threshold_type*: +mbytes_avail+
76
+
77
 === Run-watch
78
 
79
 Expands the given path to a pidfile and checks if the process ID found inside

b/src/print_disk_info.c

84
@@ -53,13 +53,39 @@ static int print_bytes_human(char *outwalk, uint64_t bytes, const char *prefix_t
85
 }
86
 
87
 /*
88
+ * Determines whether remaining bytes are below given threshold.
89
+ *
90
+ */
91
+#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__)
92
+static bool below_threshold(struct statfs buf, const char *prefix_type, const char *threshold_type, double low_threshold) {
93
+#else
94
+static bool below_threshold(struct statvfs buf, const char *prefix_type, const char *threshold_type, double low_threshold) {
95
+#endif
96
+        if (BEGINS_WITH(threshold_type, "mbytes")) {
97
+                low_threshold *= strcmp(prefix_type, "decimal") == 0 ? DECIMAL_BASE * DECIMAL_BASE : BINARY_BASE * BINARY_BASE;
98
+                if (strcmp(threshold_type, "mbytes_free") == 0) {
99
+                        return (double)buf.f_bsize * (double)buf.f_bfree < low_threshold;
100
+                } else if (strcmp(threshold_type, "mbytes_avail") == 0) {
101
+                        return (double)buf.f_bsize * (double)buf.f_bavail < low_threshold;
102
+                }
103
+        } else if (strcmp(threshold_type, "percentage_free") == 0) {
104
+                return 100.0 * (double)buf.f_bfree / (double)buf.f_blocks < low_threshold;
105
+        } else if (strcmp(threshold_type, "percentage_avail") == 0) {
106
+                return 100.0 * (double)buf.f_bavail / (double)buf.f_blocks < low_threshold;
107
+        }
108
+
109
+        return false;
110
+}
111
+
112
+/*
113
  * Does a statvfs and prints either free, used or total amounts of bytes in a
114
  * human readable manner.
115
  *
116
  */
117
-void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const char *format, const char *prefix_type) {
118
+void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const char *format, const char *prefix_type, const char *threshold_type, const double low_threshold) {
119
         const char *walk;
120
         char *outwalk = buffer;
121
+        bool colorful_output = false;
122
 
123
         INSTANCE(path);
124
 
125
@@ -75,6 +101,11 @@ void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const ch
126
                 return;
127
 #endif
128
 
129
+        if (low_threshold > 0 && below_threshold(buf, prefix_type, threshold_type, low_threshold)) {
130
+                START_COLOR("color_bad");
131
+                colorful_output = true;
132
+        }
133
+
134
         for (walk = format; *walk != '\0'; walk++) {
135
                 if (*walk != '%') {
136
                         *(outwalk++) = *walk;
137
@@ -122,6 +153,9 @@ void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const ch
138
                 }
139
         }
140
 
141
+        if (colorful_output)
142
+                END_COLOR;
143
+
144
         *outwalk = '\0';
145
         OUTPUT_FULL_TEXT(buffer);
146
 }