disk: Distinguish between IEC, SI and custom prefixes
Patch status: merged
Patch by Mats
Long description:
* IEC: Ki, Mi, Gi, Ti (powers of 1024) * SI: k, M, G, T (powers of 1000) * custom: K, M, G, T (powers of 1024)
To apply this patch, use:
curl http://cr.i3wm.org/patch/342/raw.patch | git am
b/i3status.c
| 19 |
@@ -288,6 +288,7 @@ int main(int argc, char *argv[]) {
|
| 20 |
|
| 21 |
cfg_opt_t disk_opts[] = {
|
| 22 |
CFG_STR("format", "%free", CFGF_NONE),
|
| 23 |
+ CFG_STR("prefix_type", "binary", CFGF_NONE),
|
| 24 |
CFG_END() |
| 25 |
}; |
| 26 |
|
| 27 |
@@ -497,7 +498,7 @@ int main(int argc, char *argv[]) {
|
| 28 |
|
| 29 |
CASE_SEC_TITLE("disk") {
|
| 30 |
SEC_OPEN_MAP("disk_info");
|
| 31 |
- print_disk_info(json_gen, buffer, title, cfg_getstr(sec, "format")); |
| 32 |
+ print_disk_info(json_gen, buffer, title, cfg_getstr(sec, "format"), cfg_getstr(sec, "prefix_type")); |
| 33 |
SEC_CLOSE_MAP; |
| 34 |
} |
| 35 |
|
b/include/i3status.h
| 40 |
@@ -145,7 +145,7 @@ char *auto_detect_format(); |
| 41 |
void set_timezone(const char *tz); |
| 42 |
|
| 43 |
void print_ipv6_info(yajl_gen json_gen, char *buffer, const char *format_up, const char *format_down); |
| 44 |
-void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const char *format); |
| 45 |
+void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const char *format, const char *prefix_type); |
| 46 |
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); |
| 47 |
void print_time(yajl_gen json_gen, char *buffer, const char *format, const char *tz, time_t t); |
| 48 |
void print_ddate(yajl_gen json_gen, char *buffer, const char *format, time_t t); |
b/man/i3status.man
| 53 |
@@ -184,12 +184,26 @@ Gets used, free, available and total amount of bytes on the given mounted filesy |
| 54 |
These values can also be expressed in percentages with the percentage_used, |
| 55 |
percentage_free, percentage_avail and percentage_used_of_avail formats. |
| 56 |
|
| 57 |
+Byte sizes are presented in a human readable format using a set of prefixes |
| 58 |
+whose type can be specified via the "prefix_type" option. Three sets of |
| 59 |
+prefixes are available: |
| 60 |
+ |
| 61 |
+binary:: |
| 62 |
+IEC prefixes (Ki, Mi, Gi, Ti) represent multiples of powers of 1024. |
| 63 |
+This is the default. |
| 64 |
+decimal:: |
| 65 |
+SI prefixes (k, M, G, T) represent multiples of powers of 1000. |
| 66 |
+custom:: |
| 67 |
+The custom prefixes (K, M, G, T) represent multiples of powers of 1024. |
| 68 |
+ |
| 69 |
*Example order*: +disk /mnt/usbstick+ |
| 70 |
|
| 71 |
*Example format*: +%free (%avail)/ %total+ |
| 72 |
|
| 73 |
*Example format*: +%percentage_used used, %percentage_free free, %percentage_avail avail+ |
| 74 |
|
| 75 |
+*Example prefix_type*: +custom+ |
| 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 |
@@ -15,26 +15,40 @@ |
| 85 |
|
| 86 |
#include "i3status.h" |
| 87 |
|
| 88 |
-#define TERABYTE (1024ULL * 1024 * 1024 * 1024) |
| 89 |
-#define GIGABYTE (1024ULL * 1024 * 1024) |
| 90 |
-#define MEGABYTE (1024ULL * 1024) |
| 91 |
-#define KILOBYTE (1024ULL) |
| 92 |
+#define BINARY_BASE UINT64_C(1024) |
| 93 |
+#define DECIMAL_BASE UINT64_C(1000) |
| 94 |
+ |
| 95 |
+#define MAX_EXPONENT 4 |
| 96 |
+ |
| 97 |
+static const char * const iec_symbols[MAX_EXPONENT+1] = {"", "Ki", "Mi", "Gi", "Ti"};
|
| 98 |
+static const char * const si_symbols[MAX_EXPONENT+1] = {"", "k", "M", "G", "T"};
|
| 99 |
+static const char * const custom_symbols[MAX_EXPONENT+1] = {"", "K", "M", "G", "T"};
|
| 100 |
+ |
| 101 |
+/* |
| 102 |
+ * Formats bytes according to the given base and set of symbols. |
| 103 |
+ * |
| 104 |
+ */ |
| 105 |
+static int format_bytes(char *outwalk, uint64_t bytes, uint64_t base, const char * const symbols[]) {
|
| 106 |
+ double size = bytes; |
| 107 |
+ int exponent = 0; |
| 108 |
+ while (size >= base && exponent < MAX_EXPONENT) {
|
| 109 |
+ size /= base; |
| 110 |
+ exponent += 1; |
| 111 |
+ } |
| 112 |
+ return sprintf(outwalk, "%.1f %sB", size, symbols[exponent]); |
| 113 |
+} |
| 114 |
|
| 115 |
/* |
| 116 |
* Prints the given amount of bytes in a human readable manner. |
| 117 |
* |
| 118 |
*/ |
| 119 |
-static int print_bytes_human(char *outwalk, uint64_t bytes) {
|
| 120 |
- if (bytes > TERABYTE) |
| 121 |
- return sprintf(outwalk, "%.02f TB", (double)bytes / TERABYTE); |
| 122 |
- else if (bytes > GIGABYTE) |
| 123 |
- return sprintf(outwalk, "%.01f GB", (double)bytes / GIGABYTE); |
| 124 |
- else if (bytes > MEGABYTE) |
| 125 |
- return sprintf(outwalk, "%.01f MB", (double)bytes / MEGABYTE); |
| 126 |
- else if (bytes > KILOBYTE) |
| 127 |
- return sprintf(outwalk, "%.01f KB", (double)bytes / KILOBYTE); |
| 128 |
- else {
|
| 129 |
- return sprintf(outwalk, "%.01f B", (double)bytes); |
| 130 |
+static int print_bytes_human(char *outwalk, uint64_t bytes, const char *prefix_type) {
|
| 131 |
+ if (strncmp(prefix_type, "decimal", strlen(prefix_type)) == 0) {
|
| 132 |
+ return format_bytes(outwalk, bytes, DECIMAL_BASE, si_symbols); |
| 133 |
+ } else if (strncmp(prefix_type, "custom", strlen(prefix_type)) == 0) {
|
| 134 |
+ return format_bytes(outwalk, bytes, BINARY_BASE, custom_symbols); |
| 135 |
+ } else {
|
| 136 |
+ return format_bytes(outwalk, bytes, BINARY_BASE, iec_symbols); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
@@ -43,7 +57,7 @@ static int print_bytes_human(char *outwalk, uint64_t bytes) {
|
| 141 |
* human readable manner. |
| 142 |
* |
| 143 |
*/ |
| 144 |
-void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const char *format) {
|
| 145 |
+void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const char *format, const char *prefix_type) {
|
| 146 |
const char *walk; |
| 147 |
char *outwalk = buffer; |
| 148 |
|
| 149 |
@@ -68,22 +82,22 @@ void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const ch |
| 150 |
} |
| 151 |
|
| 152 |
if (BEGINS_WITH(walk+1, "free")) {
|
| 153 |
- outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_bfree); |
| 154 |
+ outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_bfree, prefix_type); |
| 155 |
walk += strlen("free");
|
| 156 |
} |
| 157 |
|
| 158 |
if (BEGINS_WITH(walk+1, "used")) {
|
| 159 |
- outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * ((uint64_t)buf.f_blocks - (uint64_t)buf.f_bfree)); |
| 160 |
+ outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * ((uint64_t)buf.f_blocks - (uint64_t)buf.f_bfree), prefix_type); |
| 161 |
walk += strlen("used");
|
| 162 |
} |
| 163 |
|
| 164 |
if (BEGINS_WITH(walk+1, "total")) {
|
| 165 |
- outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_blocks); |
| 166 |
+ outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_blocks, prefix_type); |
| 167 |
walk += strlen("total");
|
| 168 |
} |
| 169 |
|
| 170 |
if (BEGINS_WITH(walk+1, "avail")) {
|
| 171 |
- outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_bavail); |
| 172 |
+ outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_bavail, prefix_type); |
| 173 |
walk += strlen("avail");
|
| 174 |
} |
| 175 |
|