feat(wpcarro/emacs): Package bytes.el
Another meh package, but let's finish the job and package it up. Change-Id: I7852a776c93c8c6717878a5ee0742287d2d23052 Reviewed-on: https://cl.tvl.fyi/c/depot/+/7394 Reviewed-by: wpcarro <wpcarro@gmail.com> Tested-by: BuildkiteCI
This commit is contained in:
		
							parent
							
								
									fb7f461cff
								
							
						
					
					
						commit
						bbad770cf2
					
				
					 3 changed files with 62 additions and 37 deletions
				
			
		| 
						 | 
				
			
			@ -34,31 +34,32 @@
 | 
			
		|||
;;  overflow.  I imagine a larger integer type may exist, but for now, I'll
 | 
			
		||||
;;  treat this as a YAGNI.
 | 
			
		||||
 | 
			
		||||
(require 'prelude)
 | 
			
		||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 | 
			
		||||
;; Dependencies
 | 
			
		||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 | 
			
		||||
 | 
			
		||||
(require 'tuple)
 | 
			
		||||
(require 'math)
 | 
			
		||||
(require 'number)
 | 
			
		||||
 | 
			
		||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 | 
			
		||||
;; Constants
 | 
			
		||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 | 
			
		||||
 | 
			
		||||
(defconst bytes-kb (math-exp 2 10)
 | 
			
		||||
(defconst bytes-kb (expt 2 10)
 | 
			
		||||
  "Number of bytes in a kilobyte.")
 | 
			
		||||
 | 
			
		||||
(defconst bytes-mb (math-exp 2 20)
 | 
			
		||||
(defconst bytes-mb (expt 2 20)
 | 
			
		||||
  "Number of bytes in a megabytes.")
 | 
			
		||||
 | 
			
		||||
(defconst bytes-gb (math-exp 2 30)
 | 
			
		||||
(defconst bytes-gb (expt 2 30)
 | 
			
		||||
  "Number of bytes in a gigabyte.")
 | 
			
		||||
 | 
			
		||||
(defconst bytes-tb (math-exp 2 40)
 | 
			
		||||
(defconst bytes-tb (expt 2 40)
 | 
			
		||||
  "Number of bytes in a terabyte.")
 | 
			
		||||
 | 
			
		||||
(defconst bytes-pb (math-exp 2 50)
 | 
			
		||||
(defconst bytes-pb (expt 2 50)
 | 
			
		||||
  "Number of bytes in a petabyte.")
 | 
			
		||||
 | 
			
		||||
(defconst bytes-eb (math-exp 2 60)
 | 
			
		||||
(defconst bytes-eb (expt 2 60)
 | 
			
		||||
  "Number of bytes in an exabyte.")
 | 
			
		||||
 | 
			
		||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 | 
			
		||||
| 
						 | 
				
			
			@ -67,7 +68,6 @@
 | 
			
		|||
 | 
			
		||||
(defun bytes-classify (x)
 | 
			
		||||
  "Return unit that closest fits byte count, X."
 | 
			
		||||
  (prelude-assert (number-whole? x))
 | 
			
		||||
  (cond
 | 
			
		||||
   ((and (>= x 0)        (< x bytes-kb))     'byte)
 | 
			
		||||
   ((and (>= x bytes-kb) (< x bytes-mb)) 'kilobyte)
 | 
			
		||||
| 
						 | 
				
			
			@ -80,33 +80,15 @@
 | 
			
		|||
  "Convert integer X into a human-readable string."
 | 
			
		||||
  (let ((base-and-unit
 | 
			
		||||
         (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")))))
 | 
			
		||||
    (string-format "%d%s"
 | 
			
		||||
                   (round x (tuple/first base-and-unit))
 | 
			
		||||
                   (tuple/second base-and-unit))))
 | 
			
		||||
 | 
			
		||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 | 
			
		||||
;; Tests
 | 
			
		||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 | 
			
		||||
 | 
			
		||||
(progn
 | 
			
		||||
  (prelude-assert
 | 
			
		||||
   (equal "1000B" (bytes-to-string 1000)))
 | 
			
		||||
  (prelude-assert
 | 
			
		||||
   (equal "2KB" (bytes-to-string (* 2 bytes-kb))))
 | 
			
		||||
  (prelude-assert
 | 
			
		||||
   (equal "17MB" (bytes-to-string (* 17 bytes-mb))))
 | 
			
		||||
  (prelude-assert
 | 
			
		||||
   (equal "419GB" (bytes-to-string (* 419 bytes-gb))))
 | 
			
		||||
  (prelude-assert
 | 
			
		||||
   (equal "999TB" (bytes-to-string (* 999 bytes-tb))))
 | 
			
		||||
  (prelude-assert
 | 
			
		||||
   (equal "2PB" (bytes-to-string (* 2 bytes-pb)))))
 | 
			
		||||
           ('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")))))
 | 
			
		||||
    (format "%d%s"
 | 
			
		||||
            (round x (tuple-first base-and-unit))
 | 
			
		||||
            (tuple-second base-and-unit))))
 | 
			
		||||
 | 
			
		||||
(provide 'bytes)
 | 
			
		||||
;;; bytes.el ends here
 | 
			
		||||
							
								
								
									
										25
									
								
								users/wpcarro/emacs/pkgs/bytes/default.nix
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								users/wpcarro/emacs/pkgs/bytes/default.nix
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,25 @@
 | 
			
		|||
{ pkgs, depot, ... }:
 | 
			
		||||
 | 
			
		||||
let
 | 
			
		||||
  bytes = pkgs.callPackage
 | 
			
		||||
    ({ emacsPackages }:
 | 
			
		||||
      emacsPackages.trivialBuild {
 | 
			
		||||
        pname = "bytes";
 | 
			
		||||
        version = "1.0.0";
 | 
			
		||||
        src = ./bytes.el;
 | 
			
		||||
        packageRequires =
 | 
			
		||||
          (with depot.users.wpcarro.emacs.pkgs; [
 | 
			
		||||
            tuple
 | 
			
		||||
          ]);
 | 
			
		||||
      })
 | 
			
		||||
    { };
 | 
			
		||||
 | 
			
		||||
  emacs = (pkgs.emacsPackagesFor pkgs.emacs28).emacsWithPackages (epkgs: [ bytes ]);
 | 
			
		||||
in
 | 
			
		||||
bytes.overrideAttrs (_old: {
 | 
			
		||||
  doCheck = true;
 | 
			
		||||
  checkPhase = ''
 | 
			
		||||
    ${emacs}/bin/emacs -batch \
 | 
			
		||||
      -l ert -l ${./tests.el} -f ert-run-tests-batch-and-exit
 | 
			
		||||
  '';
 | 
			
		||||
})
 | 
			
		||||
							
								
								
									
										18
									
								
								users/wpcarro/emacs/pkgs/bytes/tests.el
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								users/wpcarro/emacs/pkgs/bytes/tests.el
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,18 @@
 | 
			
		|||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 | 
			
		||||
;; Dependencies
 | 
			
		||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 | 
			
		||||
 | 
			
		||||
(require 'ert)
 | 
			
		||||
(require 'bytes)
 | 
			
		||||
 | 
			
		||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 | 
			
		||||
;; Tests
 | 
			
		||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 | 
			
		||||
 | 
			
		||||
(ert-deftest bytes-to-string ()
 | 
			
		||||
  (should (equal "1000B" (bytes-to-string 1000)))
 | 
			
		||||
  (should (equal "2KB" (bytes-to-string (* 2 bytes-kb))))
 | 
			
		||||
  (should (equal "17MB" (bytes-to-string (* 17 bytes-mb))))
 | 
			
		||||
  (should (equal "419GB" (bytes-to-string (* 419 bytes-gb))))
 | 
			
		||||
  (should (equal "999TB" (bytes-to-string (* 999 bytes-tb))))
 | 
			
		||||
  (should (equal "2PB" (bytes-to-string (* 2 bytes-pb)))))
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue