diff options
author | Mats <d912e3@gmail.com> | 2014-03-08 00:24:42 +0100 |
---|---|---|
committer | Michael Stapelberg <michael@stapelberg.de> | 2014-03-09 22:57:19 +0100 |
commit | bc0bd8c9e03d92ab133f4dfae52dc202c3e0cbf6 (patch) | |
tree | 61eebe406b1a1978d1fb3d1e84706ab2daaba48e | |
parent | 1de12e7b20e7ce38e2777218f1d922b3255599e1 (diff) |
disk: Colorize output when below given threshold
New disk module options:
* threshold_type: ^(percentage|[kmgt]?bytes)_(free|avail)$
* low_threshold: <double>
fixes #912
-rw-r--r-- | i3status.c | 5 | ||||
-rw-r--r-- | include/i3status.h | 2 | ||||
-rw-r--r-- | man/i3status.man | 14 | ||||
-rw-r--r-- | src/print_disk_info.c | 60 |
4 files changed, 78 insertions, 3 deletions
@@ -382,7 +382,10 @@ int main(int argc, char *argv[]) { cfg_opt_t disk_opts[] = { CFG_STR("format", "%free", CFGF_NONE), CFG_STR("prefix_type", "binary", CFGF_NONE), + CFG_STR("threshold_type", "percentage_avail", CFGF_NONE), + CFG_FLOAT("low_threshold", 0, CFGF_NONE), CFG_CUSTOM_ALIGN_OPT, + CFG_CUSTOM_COLOR_OPTS, CFG_CUSTOM_MIN_WIDTH_OPT, CFG_END() }; @@ -601,7 +604,7 @@ int main(int argc, char *argv[]) { CASE_SEC_TITLE("disk") { SEC_OPEN_MAP("disk_info"); - print_disk_info(json_gen, buffer, title, cfg_getstr(sec, "format"), cfg_getstr(sec, "prefix_type")); + 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")); SEC_CLOSE_MAP; } diff --git a/include/i3status.h b/include/i3status.h index 8c64586..829fc4e 100644 --- a/include/i3status.h +++ b/include/i3status.h @@ -174,7 +174,7 @@ char *auto_detect_format(); void set_timezone(const char *tz); void print_ipv6_info(yajl_gen json_gen, char *buffer, const char *format_up, const char *format_down); -void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const char *format, const char *prefix_type); +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); 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); void print_time(yajl_gen json_gen, char *buffer, const char *format, const char *tz, time_t t); void print_ddate(yajl_gen json_gen, char *buffer, const char *format, time_t t); diff --git a/man/i3status.man b/man/i3status.man index 656a8bf..1182e54 100644 --- a/man/i3status.man +++ b/man/i3status.man @@ -223,6 +223,16 @@ SI prefixes (k, M, G, T) represent multiples of powers of 1000. custom:: The custom prefixes (K, M, G, T) represent multiples of powers of 1024. +It is possible to define a low_threshold that causes the disk text to be +displayed using color_bad. The low_threshold type can be of threshold_type +"bytes_free", "bytes_avail", "percentage_free", or "percentage_avail", where +the former two can be prepended by a generic prefix (k, m, g, t) having +prefix_type. So, if you configure low_threshold to 2, threshold_type to +"gbytes_avail", and prefix_type to "binary", and the remaining available disk +space is below 2 GiB, it will be colored bad. If not specified, threshold_type +is assumed to be "percentage_avail" and low_threshold to be set to 0, which +implies no coloring at all. + *Example order*: +disk /mnt/usbstick+ *Example format*: +%free (%avail)/ %total+ @@ -231,6 +241,10 @@ The custom prefixes (K, M, G, T) represent multiples of powers of 1024. *Example prefix_type*: +custom+ +*Example low_threshold*: +5+ + +*Example threshold_type*: +percentage_free+ + === Run-watch Expands the given path to a pidfile and checks if the process ID found inside diff --git a/src/print_disk_info.c b/src/print_disk_info.c index 2308305..d586839 100644 --- a/src/print_disk_info.c +++ b/src/print_disk_info.c @@ -53,13 +53,63 @@ static int print_bytes_human(char *outwalk, uint64_t bytes, const char *prefix_t } /* + * Determines whether remaining bytes are below given threshold. + * + */ +#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__) +static bool below_threshold(struct statfs buf, const char *prefix_type, const char *threshold_type, const double low_threshold) { +#else +static bool below_threshold(struct statvfs buf, const char *prefix_type, const char *threshold_type, const double low_threshold) { +#endif + if (strcasecmp(threshold_type, "percentage_free") == 0) { + return 100.0 * (double)buf.f_bfree / (double)buf.f_blocks < low_threshold; + } else if (strcasecmp(threshold_type, "percentage_avail") == 0) { + return 100.0 * (double)buf.f_bavail / (double)buf.f_blocks < low_threshold; + } else if (strcasecmp(threshold_type, "bytes_free") == 0) { + return (double)buf.f_bsize * (double)buf.f_bfree < low_threshold; + } else if (strcasecmp(threshold_type, "bytes_avail") == 0) { + return (double)buf.f_bsize * (double)buf.f_bavail < low_threshold; + } else if (threshold_type[0] != '\0' && strncasecmp(threshold_type+1, "bytes_", strlen("bytes_")) == 0) { + uint64_t base = strcasecmp(prefix_type, "decimal") == 0 ? DECIMAL_BASE : BINARY_BASE; + double factor = 1; + + switch (threshold_type[0]) { + case 'T': + case 't': + factor *= base; + case 'G': + case 'g': + factor *= base; + case 'M': + case 'm': + factor *= base; + case 'K': + case 'k': + factor *= base; + break; + default: + return false; + } + + if (strcasecmp(threshold_type+1, "bytes_free") == 0) { + return (double)buf.f_bsize * (double)buf.f_bfree < low_threshold * factor; + } else if (strcasecmp(threshold_type+1, "bytes_avail") == 0) { + return (double)buf.f_bsize * (double)buf.f_bavail < low_threshold * factor; + } + } + + return false; +} + +/* * Does a statvfs and prints either free, used or total amounts of bytes in a * human readable manner. * */ -void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const char *format, const char *prefix_type) { +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) { const char *walk; char *outwalk = buffer; + bool colorful_output = false; INSTANCE(path); @@ -75,6 +125,11 @@ void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const ch return; #endif + if (low_threshold > 0 && below_threshold(buf, prefix_type, threshold_type, low_threshold)) { + START_COLOR("color_bad"); + colorful_output = true; + } + for (walk = format; *walk != '\0'; walk++) { if (*walk != '%') { *(outwalk++) = *walk; @@ -122,6 +177,9 @@ void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const ch } } + if (colorful_output) + END_COLOR; + *outwalk = '\0'; OUTPUT_FULL_TEXT(buffer); } |