[hackers] [slstatus] Add fmt_scaled util function || Aaron Marcher

From: <git_AT_suckless.org>
Date: Fri, 18 May 2018 23:14:37 +0200 (CEST)

commit 35219d39caa5ab5e0b1c9e6d4a1cbb29a388842e
Author: Aaron Marcher <me_AT_drkhsh.at>
AuthorDate: Fri May 18 23:14:10 2018 +0200
Commit: Aaron Marcher <me_AT_drkhsh.at>
CommitDate: Fri May 18 23:14:10 2018 +0200

    Add fmt_scaled util function

diff --git a/util.c b/util.c
index 08f14ff..a7576b4 100644
--- a/util.c
+++ b/util.c
_AT_@ -65,6 +65,22 @@ bprintf(const char *fmt, ...)
         return buf;
 }
 
+const char *
+fmt_scaled(size_t bytes)
+{
+ unsigned int i;
+ float scaled;
+ const char *units[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB",
+ "ZiB", "YiB" };
+
+ scaled = bytes;
+ for (i = 0; i < LEN(units) && scaled >= 1024; i++) {
+ scaled /= 1024.0;
+ }
+
+ return bprintf("%.1f%s", scaled, units[i]);
+}
+
 int
 pscanf(const char *path, const char *fmt, ...)
 {
diff --git a/util.h b/util.h
index a73b103..bd05574 100644
--- a/util.h
+++ b/util.h
_AT_@ -9,4 +9,5 @@ void warn(const char *, ...);
 void die(const char *, ...);
 
 const char *bprintf(const char *fmt, ...);
+const char *fmt_scaled(size_t);
 int pscanf(const char *path, const char *fmt, ...);
Received on Fri May 18 2018 - 23:14:37 CEST

This archive was generated by hypermail 2.3.0 : Fri May 18 2018 - 23:24:24 CEST