all repos — slstatus @ 46c4540dd2f6181e77b0800a4e007d78d0162487

my build of slstatus (tools.suckless.org/slstatus/)

Implement fmt_human_2() and fmt_human_10()

These functions take the raw number and a unit and automatically
print it out "scaled down" to a proper SI-prefix, for powers of 2
and 10 respectively.

Apply them to the 2-power cases and keep the 10-power for a later
commit.
Laslo Hunhold dev@frign.de
Sat, 19 May 2018 22:52:17 +0200
commit

46c4540dd2f6181e77b0800a4e007d78d0162487

parent

74c4f4ebdae8a12fc95840954dde574692234b01

6 files changed, 61 insertions(+), 32 deletions(-)

jump to
M components/disk.ccomponents/disk.c

@@ -16,7 +16,7 @@ warn("statvfs '%s':", mnt);

return NULL; } - return fmt_scaled(fs.f_frsize * fs.f_bavail); + return fmt_human_2(fs.f_frsize * fs.f_bavail, "B"); } const char *

@@ -43,7 +43,7 @@ warn("statvfs '%s':", mnt);

return NULL; } - return fmt_scaled(fs.f_frsize * fs.f_blocks); + return fmt_human_2(fs.f_frsize * fs.f_blocks, "B"); } const char *

@@ -56,5 +56,5 @@ warn("statvfs '%s':", mnt);

return NULL; } - return fmt_scaled(fs.f_frsize * (fs.f_blocks - fs.f_bfree)); + return fmt_human_2(fs.f_frsize * (fs.f_blocks - fs.f_bfree), "B"); }
M components/netspeeds.ccomponents/netspeeds.c

@@ -25,9 +25,12 @@ }

if (pscanf(path, "%llu", &rxbytes) != 1) { return NULL; } + if (oldrxbytes == 0) { + return NULL; + } - return oldrxbytes ? fmt_scaled((rxbytes - oldrxbytes) * - 1000 / interval) : NULL; + return fmt_human_2((rxbytes - oldrxbytes) * + 1000 / interval, "B/s"); } const char *

@@ -48,9 +51,12 @@ }

if (pscanf(path, "%llu", &txbytes) != 1) { return NULL; } + if (oldtxbytes == 0) { + return NULL; + } - return oldtxbytes ? fmt_scaled((txbytes - oldtxbytes) * - 1000 / interval) : NULL; + return fmt_human_2((txbytes - oldtxbytes) * + 1000 / interval, "B/s"); } #elif defined(__OpenBSD__) #include <string.h>

@@ -87,9 +93,12 @@ if (!if_ok) {

warn("reading 'if_data' failed"); return NULL; } + if (oldrxbytes == 0) { + return NULL; + } - return oldrxbytes ? fmt_scaled((rxbytes - oldrxbytes) * - 1000 / interval) : NULL; + return fmt_human_2((rxbytes - oldrxbytes) * + 1000 / interval, "B/s"); } const char *

@@ -120,8 +129,11 @@ if (!if_ok) {

warn("reading 'if_data' failed"); return NULL; } + if (oldtxbytes == 0) { + return NULL; + } - return oldtxbytes ? fmt_scaled((txbytes - oldtxbytes) * - 1000 / interval) : NULL; + return fmt_human_2((txbytes - oldtxbytes) * + 1000 / interval, "B/s"); } #endif
M components/ram.ccomponents/ram.c

@@ -14,7 +14,7 @@ "MemTotal: %ld kB\n"

"MemFree: %ld kB\n" "MemAvailable: %ld kB\n", &free, &free, &free) == 3) ? - fmt_scaled(free * 1024) : NULL; + fmt_human_2(free * 1024, "B") : NULL; } const char *

@@ -39,7 +39,7 @@ {

long total; return (pscanf("/proc/meminfo", "MemTotal: %ld kB\n", &total) == 1) ? - fmt_scaled(total * 1024) : NULL; + fmt_human_2(total * 1024, "B") : NULL; } const char *

@@ -53,7 +53,7 @@ "MemFree: %ld kB\n"

"MemAvailable: %ld kB\nBuffers: %ld kB\n" "Cached: %ld kB\n", &total, &free, &buffers, &buffers, &cached) == 5) ? - fmt_scaled((total - free - buffers - cached) * 1024) : NULL; + fmt_human_2((total - free - buffers - cached) * 1024, "B") : NULL; } #elif defined(__OpenBSD__) #include <stdlib.h>

@@ -83,7 +83,7 @@ int free_pages;

if (load_uvmexp(&uvmexp)) { free_pages = uvmexp.npages - uvmexp.active; - return fmt_scaled(pagetok(free_pages, uvmexp.pageshift) * 1024); + return fmt_human_2(pagetok(free_pages, uvmexp.pageshift) * 1024, "B"); } return NULL;

@@ -109,7 +109,7 @@ {

struct uvmexp uvmexp; if (load_uvmexp(&uvmexp)) { - return fmt_scaled(pagetok(uvmexp.npages, uvmexp.pageshift) * 1024); + return fmt_human_2(pagetok(uvmexp.npages, uvmexp.pageshift) * 1024, "B"); } return NULL;

@@ -121,7 +121,7 @@ {

struct uvmexp uvmexp; if (load_uvmexp(&uvmexp)) { - return fmt_scaled(pagetok(uvmexp.active, uvmexp.pageshift) * 1024); + return fmt_human_2(pagetok(uvmexp.active, uvmexp.pageshift) * 1024, "B"); } return NULL;
M components/swap.ccomponents/swap.c

@@ -48,7 +48,7 @@ return NULL;

} sscanf(match, "SwapFree: %ld kB\n", &free); - return fmt_scaled(free * 1024); + return fmt_human_2(free * 1024, "B"); } const char *

@@ -94,7 +94,7 @@ return NULL;

} sscanf(match, "SwapTotal: %ld kB\n", &total); - return fmt_scaled(total * 1024); + return fmt_human_2(total * 1024, "B"); } const char *

@@ -122,7 +122,7 @@ return NULL;

} sscanf(match, "SwapFree: %ld kB\n", &free); - return fmt_scaled((total - free - cached) * 1024); + return fmt_human_2((total - free - cached) * 1024, "B"); } #elif defined(__OpenBSD__) #include <stdlib.h>

@@ -174,7 +174,7 @@ int total, used;

getstats(&total, &used); - return fmt_scaled((total - used) * 1024); + return fmt_human_2((total - used) * 1024, "B"); } const char *

@@ -194,7 +194,7 @@ int total, used;

getstats(&total, &used); - return fmt_scaled(total * 1024); + return fmt_human_2(total * 1024, "B"); } const char *

@@ -204,6 +204,6 @@ int total, used;

getstats(&total, &used); - return fmt_scaled(used * 1024); + return fmt_human_2(used * 1024, "B"); } #endif
M util.cutil.c

@@ -87,19 +87,35 @@ return buf;

} const char * -fmt_scaled(size_t bytes) +fmt_human_2(size_t num, char *unit) { - unsigned int i; - float scaled; - const char *units[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", - "ZiB", "YiB" }; + size_t i; + double scaled; + const char *prefix[] = { "", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", + "Zi", "Yi" }; - scaled = bytes; - for (i = 0; i < LEN(units) && scaled >= 1024; i++) { + scaled = num; + for (i = 0; i < LEN(prefix) && scaled >= 1024; i++) { scaled /= 1024.0; } - return bprintf("%.1f%s", scaled, units[i]); + return bprintf("%.1f%s%s", scaled, prefix[i], unit); +} + +const char * +fmt_human_10(size_t num, char *unit) +{ + size_t i; + double scaled; + const char *prefix[] = { "", "K", "M", "G", "T", "P", "E", + "Z", "Y" }; + + scaled = num; + for (i = 0; i < LEN(prefix) && scaled >= 1000; i++) { + scaled /= 1000.0; + } + + return bprintf("%.1f%s%s", scaled, prefix[i], unit); } int
M util.hutil.h

@@ -10,5 +10,6 @@ void die(const char *, ...);

int esnprintf(char *str, size_t size, const char *fmt, ...); const char *bprintf(const char *fmt, ...); -const char *fmt_scaled(size_t); +const char *fmt_human_2(size_t num, char *unit); +const char *fmt_human_10(size_t num, char *unit); int pscanf(const char *path, const char *fmt, ...);