--- a/i3status.c 2019-10-25 23:31:41.210231542 +0200 +++ b/i3status.c 2019-10-25 23:25:25.088507686 +0200 @@ -431,6 +431,7 @@ CFG_STR("threshold_degraded", NULL, CFGF_NONE), CFG_STR("threshold_critical", NULL, CFGF_NONE), CFG_STR("memory_used_method", "classical", CFGF_NONE), + CFG_STR("unit", "auto", CFGF_NONE), CFG_CUSTOM_ALIGN_OPT, CFG_CUSTOM_COLOR_OPTS, CFG_CUSTOM_MIN_WIDTH_OPT, @@ -757,7 +758,7 @@ CASE_SEC("memory") { SEC_OPEN_MAP("memory"); - print_memory(json_gen, buffer, cfg_getstr(sec, "format"), cfg_getstr(sec, "format_degraded"), cfg_getstr(sec, "threshold_degraded"), cfg_getstr(sec, "threshold_critical"), cfg_getstr(sec, "memory_used_method")); + print_memory(json_gen, buffer, cfg_getstr(sec, "format"), cfg_getstr(sec, "format_degraded"), cfg_getstr(sec, "threshold_degraded"), cfg_getstr(sec, "threshold_critical"), cfg_getstr(sec, "memory_used_method"), cfg_getstr(sec, "unit")); SEC_CLOSE_MAP; } --- a/include/i3status.h 2019-10-25 23:31:49.170197818 +0200 +++ b/include/i3status.h 2019-10-25 23:25:25.088507686 +0200 @@ -225,7 +225,7 @@ void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format, const char *format_above_threshold, const char *format_above_degraded_threshold, const char *path, const float max_threshold, const float degraded_threshold); void print_eth_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down); void print_load(yajl_gen json_gen, char *buffer, const char *format, const char *format_above_threshold, const float max_threshold); -void print_memory(yajl_gen json_gen, char *buffer, const char *format, const char *format_degraded, const char *threshold_degraded, const char *threshold_critical, const char *memory_used_method); +void print_memory(yajl_gen json_gen, char *buffer, const char *format, const char *format_degraded, const char *threshold_degraded, const char *threshold_critical, const char *memory_used_method, const char *unit); void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char *fmt_muted, const char *device, const char *mixer, int mixer_idx); bool process_runs(const char *path); int volume_pulseaudio(uint32_t sink_idx, const char *sink_name); --- a/src/print_mem.c 2019-10-25 23:32:07.430120494 +0200 +++ b/src/print_mem.c 2019-10-25 23:25:25.088507686 +0200 @@ -12,21 +12,30 @@ #define MAX_EXPONENT 4 static const char *const iec_symbols[MAX_EXPONENT + 1] = {"", "Ki", "Mi", "Gi", "Ti"}; +static const char *const units[MAX_EXPONENT + 1] = {"", "KiB", "MiB", "GiB", "TiB"}; + static const char memoryfile_linux[] = "/proc/meminfo"; /* * Prints the given amount of bytes in a human readable manner. * */ -static int print_bytes_human(char *outwalk, uint64_t bytes) { +static int print_bytes_human(char *outwalk, uint64_t bytes, const char *unit) { double size = bytes; int exponent = 0; int bin_base = BINARY_BASE; + while (size >= bin_base && exponent < MAX_EXPONENT) { + if (strcasecmp(unit, units[exponent]) == 0) { + break; + } + size /= bin_base; + exponent += 1; } - return sprintf(outwalk, "%.1f %sB", size, iec_symbols[exponent]); + + return sprintf(outwalk, "%.0f %sB", size, iec_symbols[exponent]); } /* @@ -73,7 +82,7 @@ return mem_absolute; } -void print_memory(yajl_gen json_gen, char *buffer, const char *format, const char *format_degraded, const char *threshold_degraded, const char *threshold_critical, const char *memory_used_method) { +void print_memory(yajl_gen json_gen, char *buffer, const char *format, const char *format_degraded, const char *threshold_degraded, const char *threshold_critical, const char *memory_used_method, const char *unit) { char *outwalk = buffer; #if defined(linux) @@ -162,23 +171,23 @@ *(outwalk++) = *walk; } else if (BEGINS_WITH(walk + 1, "total")) { - outwalk += print_bytes_human(outwalk, ram_total); + outwalk += print_bytes_human(outwalk, ram_total, unit); walk += strlen("total"); } else if (BEGINS_WITH(walk + 1, "used")) { - outwalk += print_bytes_human(outwalk, ram_used); + outwalk += print_bytes_human(outwalk, ram_used, unit); walk += strlen("used"); } else if (BEGINS_WITH(walk + 1, "free")) { - outwalk += print_bytes_human(outwalk, ram_free); + outwalk += print_bytes_human(outwalk, ram_free, unit); walk += strlen("free"); } else if (BEGINS_WITH(walk + 1, "available")) { - outwalk += print_bytes_human(outwalk, ram_available); + outwalk += print_bytes_human(outwalk, ram_available, unit); walk += strlen("available"); } else if (BEGINS_WITH(walk + 1, "shared")) { - outwalk += print_bytes_human(outwalk, ram_shared); + outwalk += print_bytes_human(outwalk, ram_shared, unit); walk += strlen("shared"); } else if (BEGINS_WITH(walk + 1, "percentage_free")) {