[hackers] [slstatus] Implement fmt_human_2() and fmt_human_10() || Laslo Hunhold

From: <git_AT_suckless.org>
Date: Sat, 19 May 2018 22:58:54 +0200 (CEST)

commit 46c4540dd2f6181e77b0800a4e007d78d0162487
Author: Laslo Hunhold <dev_AT_frign.de>
AuthorDate: Sat May 19 22:52:17 2018 +0200
Commit: Aaron Marcher <me_AT_drkhsh.at>
CommitDate: Sat May 19 22:58:21 2018 +0200

    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.

diff --git a/components/disk.c b/components/disk.c
index db18d12..998ad47 100644
--- a/components/disk.c
+++ b/components/disk.c
_AT_@ -16,7 +16,7 @@ disk_free(const char *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 *
_AT_@ -43,7 +43,7 @@ disk_total(const char *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 *
_AT_@ -56,5 +56,5 @@ disk_used(const char *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");
 }
diff --git a/components/netspeeds.c b/components/netspeeds.c
index 32e78d6..f0f7455 100644
--- a/components/netspeeds.c
+++ b/components/netspeeds.c
_AT_@ -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 *
_AT_@ -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>
_AT_@ -87,9 +93,12 @@
                         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 *
_AT_@ -120,8 +129,11 @@
                         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
diff --git a/components/ram.c b/components/ram.c
index 0eb2ea9..1c12aab 100644
--- a/components/ram.c
+++ b/components/ram.c
_AT_@ -14,7 +14,7 @@
                                "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 *
_AT_@ -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 *
_AT_@ -53,7 +53,7 @@
                                "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>
_AT_@ -83,7 +83,7 @@
 
                 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;
_AT_@ -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;
_AT_@ -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;
diff --git a/components/swap.c b/components/swap.c
index 64feceb..c005691 100644
--- a/components/swap.c
+++ b/components/swap.c
_AT_@ -48,7 +48,7 @@
                 }
                 sscanf(match, "SwapFree: %ld kB\n", &free);
 
- return fmt_scaled(free * 1024);
+ return fmt_human_2(free * 1024, "B");
         }
 
         const char *
_AT_@ -94,7 +94,7 @@
                 }
                 sscanf(match, "SwapTotal: %ld kB\n", &total);
 
- return fmt_scaled(total * 1024);
+ return fmt_human_2(total * 1024, "B");
         }
 
         const char *
_AT_@ -122,7 +122,7 @@
                 }
                 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>
_AT_@ -174,7 +174,7 @@
 
                 getstats(&total, &used);
 
- return fmt_scaled((total - used) * 1024);
+ return fmt_human_2((total - used) * 1024, "B");
         }
 
         const char *
_AT_@ -194,7 +194,7 @@
 
                 getstats(&total, &used);
 
- return fmt_scaled(total * 1024);
+ return fmt_human_2(total * 1024, "B");
         }
 
         const char *
_AT_@ -204,6 +204,6 @@
 
                 getstats(&total, &used);
 
- return fmt_scaled(used * 1024);
+ return fmt_human_2(used * 1024, "B");
         }
 #endif
diff --git a/util.c b/util.c
index 923d4df..5936a6d 100644
--- a/util.c
+++ b/util.c
_AT_@ -87,19 +87,35 @@ bprintf(const char *fmt, ...)
 }
 
 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
diff --git a/util.h b/util.h
index 5111b37..64e30d0 100644
--- a/util.h
+++ b/util.h
_AT_@ -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, ...);
Received on Sat May 19 2018 - 22:58:54 CEST

This archive was generated by hypermail 2.3.0 : Sat May 19 2018 - 23:00:26 CEST