diff options
Diffstat (limited to 'emacs/.emacs.d/wpc/bytes.el')
-rw-r--r-- | emacs/.emacs.d/wpc/bytes.el | 60 |
1 files changed, 32 insertions, 28 deletions
diff --git a/emacs/.emacs.d/wpc/bytes.el b/emacs/.emacs.d/wpc/bytes.el index 660fa3219469..48d3932f1cf8 100644 --- a/emacs/.emacs.d/wpc/bytes.el +++ b/emacs/.emacs.d/wpc/bytes.el @@ -1,5 +1,9 @@ ;;; bytes.el --- Working with byte values -*- lexical-binding: t -*- + ;; Author: William Carroll <wpcarro@gmail.com> +;; Version: 0.0.1 +;; URL: https://git.wpcarro.dev/wpcarro/briefcase +;; Package-Requires: ((emacs "24.3")) ;;; Commentary: ;; Functions to help with human-readable representations of byte values. @@ -40,49 +44,49 @@ ;; Constants ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -(defconst bytes/kb (math/exp 2 10) +(defconst bytes-kb (math-exp 2 10) "Number of bytes in a kilobyte.") -(defconst bytes/mb (math/exp 2 20) +(defconst bytes-mb (math-exp 2 20) "Number of bytes in a megabytes.") -(defconst bytes/gb (math/exp 2 30) +(defconst bytes-gb (math-exp 2 30) "Number of bytes in a gigabyte.") -(defconst bytes/tb (math/exp 2 40) +(defconst bytes-tb (math-exp 2 40) "Number of bytes in a terabyte.") -(defconst bytes/pb (math/exp 2 50) +(defconst bytes-pb (math-exp 2 50) "Number of bytes in a petabyte.") -(defconst bytes/eb (math/exp 2 60) +(defconst bytes-eb (math-exp 2 60) "Number of bytes in an exabyte.") ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -(defun bytes/classify (x) +(defun bytes-classify (x) "Return unit that closest fits byte count, X." - (prelude-assert (number/whole? x)) + (prelude-assert (number-whole? x)) (cond - ((and (>= x 0) (< x bytes/kb)) 'byte) - ((and (>= x bytes/kb) (< x bytes/mb)) 'kilobyte) - ((and (>= x bytes/mb) (< x bytes/gb)) 'megabyte) - ((and (>= x bytes/gb) (< x bytes/tb)) 'gigabyte) - ((and (>= x bytes/tb) (< x bytes/pb)) 'terabyte) - ((and (>= x bytes/pb) (< x bytes/eb)) 'petabyte))) - -(defun bytes/to-string (x) + ((and (>= x 0) (< x bytes-kb)) 'byte) + ((and (>= x bytes-kb) (< x bytes-mb)) 'kilobyte) + ((and (>= x bytes-mb) (< x bytes-gb)) 'megabyte) + ((and (>= x bytes-gb) (< x bytes-tb)) 'gigabyte) + ((and (>= x bytes-tb) (< x bytes-pb)) 'terabyte) + ((and (>= x bytes-pb) (< x bytes-eb)) 'petabyte))) + +(defun bytes-to-string (x) "Convert integer X into a human-readable string." (let ((base-and-unit - (pcase (bytes/classify x) + (pcase (bytes-classify x) ('byte (tuple/from 1 "B")) - ('kilobyte (tuple/from bytes/kb "KB")) - ('megabyte (tuple/from bytes/mb "MB")) - ('gigabyte (tuple/from bytes/gb "GB")) - ('terabyte (tuple/from bytes/tb "TB")) - ('petabyte (tuple/from bytes/pb "PB"))))) + ('kilobyte (tuple/from bytes-kb "KB")) + ('megabyte (tuple/from bytes-mb "MB")) + ('gigabyte (tuple/from bytes-gb "GB")) + ('terabyte (tuple/from bytes-tb "TB")) + ('petabyte (tuple/from bytes-pb "PB"))))) (string-format "%d%s" (round x (tuple/first base-and-unit)) (tuple/second base-and-unit)))) @@ -93,17 +97,17 @@ (progn (prelude-assert - (equal "1000B" (bytes/to-string 1000))) + (equal "1000B" (bytes-to-string 1000))) (prelude-assert - (equal "2KB" (bytes/to-string (* 2 bytes/kb)))) + (equal "2KB" (bytes-to-string (* 2 bytes-kb)))) (prelude-assert - (equal "17MB" (bytes/to-string (* 17 bytes/mb)))) + (equal "17MB" (bytes-to-string (* 17 bytes-mb)))) (prelude-assert - (equal "419GB" (bytes/to-string (* 419 bytes/gb)))) + (equal "419GB" (bytes-to-string (* 419 bytes-gb)))) (prelude-assert - (equal "999TB" (bytes/to-string (* 999 bytes/tb)))) + (equal "999TB" (bytes-to-string (* 999 bytes-tb)))) (prelude-assert - (equal "2PB" (bytes/to-string (* 2 bytes/pb))))) + (equal "2PB" (bytes-to-string (* 2 bytes-pb))))) (provide 'bytes) ;;; bytes.el ends here |