disk: Colorize output when below given threshold
Patch status: needinfo
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/449/raw.patch | git am
b/i3status.c
21 |
@@ -382,6 +382,9 @@ 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_COLOR_OPTS, |
28 |
CFG_CUSTOM_ALIGN_OPT, |
29 |
CFG_CUSTOM_MIN_WIDTH_OPT, |
30 |
CFG_END() |
31 |
@@ -601,7 +604,7 @@ int main(int argc, char *argv[]) { |
32 |
|
33 |
CASE_SEC_TITLE("disk") { |
34 |
SEC_OPEN_MAP("disk_info"); |
35 |
- print_disk_info(json_gen, buffer, title, cfg_getstr(sec, "format"), cfg_getstr(sec, "prefix_type")); |
36 |
+ 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")); |
37 |
SEC_CLOSE_MAP; |
38 |
} |
39 |
|
b/include/i3status.h
44 |
@@ -174,7 +174,7 @@ char *auto_detect_format(); |
45 |
void set_timezone(const char *tz); |
46 |
|
47 |
void print_ipv6_info(yajl_gen json_gen, char *buffer, const char *format_up, const char *format_down); |
48 |
-void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const char *format, const char *prefix_type); |
49 |
+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); |
50 |
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); |
51 |
void print_time(yajl_gen json_gen, char *buffer, const char *format, const char *tz, time_t t); |
52 |
void print_ddate(yajl_gen json_gen, char *buffer, const char *format, time_t t); |
b/man/i3status.man
57 |
@@ -223,6 +223,16 @@ SI prefixes (k, M, G, T) represent multiples of powers of 1000. |
58 |
custom:: |
59 |
The custom prefixes (K, M, G, T) represent multiples of powers of 1024. |
60 |
|
61 |
+It is possible to define a low_threshold that causes the disk text to be |
62 |
+displayed using color_bad. The low_threshold type can be of threshold_type |
63 |
+"bytes_free", "bytes_avail", "percentage_free", or "percentage_avail", where |
64 |
+the former two can be prepended by a generic prefix (k, m, g, t) having |
65 |
+prefix_type. So, if you configure low_threshold to 2, threshold_type to |
66 |
+"gbytes_avail", and prefix_type to "binary", and the remaining available disk |
67 |
+space is below 2 GiB, it will be colored bad. If not specified, threshold_type |
68 |
+is assumed to be "percentage_avail" and low_threshold to be set to 0, which |
69 |
+implies no coloring at all. |
70 |
+ |
71 |
*Example order*: +disk /mnt/usbstick+ |
72 |
|
73 |
*Example format*: +%free (%avail)/ %total+ |
74 |
@@ -231,6 +241,10 @@ The custom prefixes (K, M, G, T) represent multiples of powers of 1024. |
75 |
|
76 |
*Example prefix_type*: +custom+ |
77 |
|
78 |
+*Example low_threshold*: +5+ |
79 |
+ |
80 |
+*Example threshold_type*: +percentage_free+ |
81 |
+ |
82 |
=== Run-watch |
83 |
|
84 |
Expands the given path to a pidfile and checks if the process ID found inside |
b/src/print_disk_info.c
89 |
@@ -53,13 +53,59 @@ static int print_bytes_human(char *outwalk, uint64_t bytes, const char *prefix_t |
90 |
} |
91 |
|
92 |
/* |
93 |
+ * Determines whether remaining bytes are below given threshold. |
94 |
+ * |
95 |
+ */ |
96 |
+#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__) |
97 |
+static bool below_threshold(struct statfs buf, const char *prefix_type, const char *threshold_type, const double low_threshold) { |
98 |
+#else |
99 |
+static bool below_threshold(struct statvfs buf, const char *prefix_type, const char *threshold_type, const double low_threshold) { |
100 |
+#endif |
101 |
+ if (strcmp(threshold_type, "percentage_free") == 0) { |
102 |
+ return 100.0 * (double)buf.f_bfree / (double)buf.f_blocks < low_threshold; |
103 |
+ } else if (strcmp(threshold_type, "percentage_avail") == 0) { |
104 |
+ return 100.0 * (double)buf.f_bavail / (double)buf.f_blocks < low_threshold; |
105 |
+ } else if (strcmp(threshold_type, "bytes_free") == 0) { |
106 |
+ return (double)buf.f_bsize * (double)buf.f_bfree < low_threshold; |
107 |
+ } else if (strcmp(threshold_type, "bytes_avail") == 0) { |
108 |
+ return (double)buf.f_bsize * (double)buf.f_bavail < low_threshold; |
109 |
+ } else if (BEGINS_WITH(threshold_type+1, "bytes_")) { |
110 |
+ uint64_t base = strcmp(prefix_type, "decimal") == 0 ? DECIMAL_BASE : BINARY_BASE; |
111 |
+ double factor = 1; |
112 |
+ |
113 |
+ switch (threshold_type[0]) { |
114 |
+ case 't': |
115 |
+ factor *= base; |
116 |
+ case 'g': |
117 |
+ factor *= base; |
118 |
+ case 'm': |
119 |
+ factor *= base; |
120 |
+ case 'k': |
121 |
+ factor *= base; |
122 |
+ break; |
123 |
+ default: |
124 |
+ return false; |
125 |
+ } |
126 |
+ |
127 |
+ if (strcmp(threshold_type+1, "bytes_free") == 0) { |
128 |
+ return (double)buf.f_bsize * (double)buf.f_bfree < low_threshold * factor; |
129 |
+ } else if (strcmp(threshold_type+1, "bytes_avail") == 0) { |
130 |
+ return (double)buf.f_bsize * (double)buf.f_bavail < low_threshold * factor; |
131 |
+ } |
132 |
+ } |
133 |
+ |
134 |
+ return false; |
135 |
+} |
136 |
+ |
137 |
+/* |
138 |
* Does a statvfs and prints either free, used or total amounts of bytes in a |
139 |
* human readable manner. |
140 |
* |
141 |
*/ |
142 |
-void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const char *format, const char *prefix_type) { |
143 |
+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) { |
144 |
const char *walk; |
145 |
char *outwalk = buffer; |
146 |
+ bool colorful_output = false; |
147 |
|
148 |
INSTANCE(path); |
149 |
|
150 |
@@ -75,6 +121,11 @@ void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const ch |
151 |
return; |
152 |
#endif |
153 |
|
154 |
+ if (low_threshold > 0 && below_threshold(buf, prefix_type, threshold_type, low_threshold)) { |
155 |
+ START_COLOR("color_bad"); |
156 |
+ colorful_output = true; |
157 |
+ } |
158 |
+ |
159 |
for (walk = format; *walk != '\0'; walk++) { |
160 |
if (*walk != '%') { |
161 |
*(outwalk++) = *walk; |
162 |
@@ -122,6 +173,9 @@ void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const ch |
163 |
} |
164 |
} |
165 |
|
166 |
+ if (colorful_output) |
167 |
+ END_COLOR; |
168 |
+ |
169 |
*outwalk = '\0'; |
170 |
OUTPUT_FULL_TEXT(buffer); |
171 |
} |