Skip to main content

add a shortcut for natsize

ID
dce4d56
date
2023-07-13 07:25:55+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
a365db1
message
add a shortcut for natsize
changed files
2 files, 25 additions

Changed files

text/README.md (2032) → text/README.md (2301)

diff --git a/text/README.md b/text/README.md
index 5fa06e8..376ee87 100644
--- a/text/README.md
+++ b/text/README.md
@@ -15,6 +15,15 @@ These are utilities for manipulating streams of text; I consider them in a simil
   </dd>
 
   <dt>
+    <a href="https://github.com/alexwlchan/scripts/blob/main/text/natsize">
+      <code>natsize < [NUMBER]</code>
+    </a>
+  </dt>
+  <dd>
+    prints a numeric file size as a human-readable string, e.g. <code>32036032</code> becomes <code>32.0 MB</code>
+  </dd>
+
+  <dt>
     <a href="https://github.com/alexwlchan/scripts/blob/main/text/noplaylist">
       <code>noplaylist < [URL]</code>
     </a>

text/natsize (0) → text/natsize (240)

diff --git a/text/natsize b/text/natsize
new file mode 100755
index 0000000..9ae2fe5
--- /dev/null
+++ b/text/natsize
@@ -0,0 +1,16 @@
+#!/usr/bin/env python3
+"""
+Converts a number of bytes into a human-readable size.
+
+    $ echo '32036032' | natsize
+    32.0 MB
+
+"""
+
+import sys
+
+import humanize
+
+
+if __name__ == "__main__":
+    print(humanize.naturalsize(sys.stdin.read()))