i3 - improved tiling WM


disk: Colorize output when below given threshold

Patch status: merged

Patch by Mats

Long description:

New disk module options:
* threshold_type: ^(percentage|[kmgt]?bytes)_(free|avail)$
* low_threshold: <double>

fixes #912

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

b/i3status.c

21
@@ -382,7 +382,10 @@ int main(int argc, char *argv[]) {
22
         cfg_opt_t disk_opts[] = {
23
                 CFG_STR("format", "%free", CFGF_NONE),
24
                 CFG_STR("prefix_type", "binary", CFGF_NONE),
25
+                CFG_STR("threshold_type", "percentage_avail", CFGF_NONE),
26
+                CFG_FLOAT("low_threshold", 0, CFGF_NONE),
27
                 CFG_CUSTOM_ALIGN_OPT,
28
+                CFG_CUSTOM_COLOR_OPTS,
29
                 CFG_CUSTOM_MIN_WIDTH_OPT,
30
                 CFG_END()
31
         };
32
@@ -601,7 +604,7 @@ int main(int argc, char *argv[]) {
33
 
34
                         CASE_SEC_TITLE("disk") {
35
                                 SEC_OPEN_MAP("disk_info");
36
-                                print_disk_info(json_gen, buffer, title, cfg_getstr(sec, "format"), cfg_getstr(sec, "prefix_type"));
37
+                                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"));
38
                                 SEC_CLOSE_MAP;
39
                         }
40
 

b/include/i3status.h

45
@@ -174,7 +174,7 @@ char *auto_detect_format();
46
 void set_timezone(const char *tz);
47
 
48
 void print_ipv6_info(yajl_gen json_gen, char *buffer, const char *format_up, const char *format_down);
49
-void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const char *format, const char *prefix_type);
50
+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);
51
 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);
52
 void print_time(yajl_gen json_gen, char *buffer, const char *format, const char *tz, time_t t);
53
 void print_ddate(yajl_gen json_gen, char *buffer, const char *format, time_t t);

b/man/i3status.man

58
@@ -223,6 +223,16 @@ SI prefixes (k, M, G, T) represent multiples of powers of 1000.
59
 custom::
60
 The custom prefixes (K, M, G, T) represent multiples of powers of 1024.
61
 
62
+It is possible to define a low_threshold that causes the disk text to be
63
+displayed using color_bad. The low_threshold type can be of threshold_type
64
+"bytes_free", "bytes_avail", "percentage_free", or "percentage_avail", where
65
+the former two can be prepended by a generic prefix (k, m, g, t) having
66
+prefix_type. So, if you configure low_threshold to 2, threshold_type to
67
+"gbytes_avail", and prefix_type to "binary", and the remaining available disk
68
+space is below 2 GiB, it will be colored bad. If not specified, threshold_type
69
+is assumed to be "percentage_avail" and low_threshold to be set to 0, which
70
+implies no coloring at all.
71
+
72
 *Example order*: +disk /mnt/usbstick+
73
 
74
 *Example format*: +%free (%avail)/ %total+
75
@@ -231,6 +241,10 @@ The custom prefixes (K, M, G, T) represent multiples of powers of 1024.
76
 
77
 *Example prefix_type*: +custom+
78
 
79
+*Example low_threshold*: +5+
80
+
81
+*Example threshold_type*: +percentage_free+
82
+
83
 === Run-watch
84
 
85
 Expands the given path to a pidfile and checks if the process ID found inside

b/src/print_disk_info.c

90
@@ -53,13 +53,63 @@ static int print_bytes_human(char *outwalk, uint64_t bytes, const char *prefix_t
91
 }
92
 
93
 /*
94
+ * Determines whether remaining bytes are below given threshold.
95
+ *
96
+ */
97
+#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__)
98
+static bool below_threshold(struct statfs buf, const char *prefix_type, const char *threshold_type, const double low_threshold) {
99
+#else
100
+static bool below_threshold(struct statvfs buf, const char *prefix_type, const char *threshold_type, const double low_threshold) {
101
+#endif
102
+        if (strcasecmp(threshold_type, "percentage_free") == 0) {
103
+                return 100.0 * (double)buf.f_bfree / (double)buf.f_blocks < low_threshold;
104
+        } else if (strcasecmp(threshold_type, "percentage_avail") == 0) {
105
+                return 100.0 * (double)buf.f_bavail / (double)buf.f_blocks < low_threshold;
106
+        } else if (strcasecmp(threshold_type, "bytes_free") == 0) {
107
+                return (double)buf.f_bsize * (double)buf.f_bfree < low_threshold;
108
+        } else if (strcasecmp(threshold_type, "bytes_avail") == 0) {
109
+                return (double)buf.f_bsize * (double)buf.f_bavail < low_threshold;
110
+        } else if (threshold_type[0] != '\0' && strncasecmp(threshold_type+1, "bytes_", strlen("bytes_")) == 0) {
111
+                uint64_t base = strcasecmp(prefix_type, "decimal") == 0 ? DECIMAL_BASE : BINARY_BASE;
112
+                double factor = 1;
113
+
114
+                switch (threshold_type[0]) {
115
+                case 'T':
116
+                case 't':
117
+                        factor *= base;
118
+                case 'G':
119
+                case 'g':
120
+                        factor *= base;
121
+                case 'M':
122
+                case 'm':
123
+                        factor *= base;
124
+                case 'K':
125
+                case 'k':
126
+                        factor *= base;
127
+                        break;
128
+                default:
129
+                        return false;
130
+                }
131
+
132
+                if (strcasecmp(threshold_type+1, "bytes_free") == 0) {
133
+                        return (double)buf.f_bsize * (double)buf.f_bfree < low_threshold * factor;
134
+                } else if (strcasecmp(threshold_type+1, "bytes_avail") == 0) {
135
+                        return (double)buf.f_bsize * (double)buf.f_bavail < low_threshold * factor;
136
+                }
137
+        }
138
+
139
+        return false;
140
+}
141
+
142
+/*
143
  * Does a statvfs and prints either free, used or total amounts of bytes in a
144
  * human readable manner.
145
  *
146
  */
147
-void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const char *format, const char *prefix_type) {
148
+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) {
149
         const char *walk;
150
         char *outwalk = buffer;
151
+        bool colorful_output = false;
152
 
153
         INSTANCE(path);
154
 
155
@@ -75,6 +125,11 @@ void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const ch
156
                 return;
157
 #endif
158
 
159
+        if (low_threshold > 0 && below_threshold(buf, prefix_type, threshold_type, low_threshold)) {
160
+                START_COLOR("color_bad");
161
+                colorful_output = true;
162
+        }
163
+
164
         for (walk = format; *walk != '\0'; walk++) {
165
                 if (*walk != '%') {
166
                         *(outwalk++) = *walk;
167
@@ -122,6 +177,9 @@ void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const ch
168
                 }
169
         }
170
 
171
+        if (colorful_output)
172
+                END_COLOR;
173
+
174
         *outwalk = '\0';
175
         OUTPUT_FULL_TEXT(buffer);
176
 }