From: Stefan Kangas Date: Sat, 5 Aug 2023 10:54:26 +0000 (+0200) Subject: Fix eshell "ls" command for files larger than 1TiB X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=3b2b0b5f921fd49ef0ac21e1522b0e037b80dffa;p=emacs.git Fix eshell "ls" command for files larger than 1TiB * lisp/eshell/esh-util.el (eshell-printable-size): Fix displaying file sizes larger than 1 TiB or 1 TB. * test/lisp/eshell/esh-util-tests.el (esh-util-test/eshell-printable-size) (esh-util-test/eshell-printable-size/zero) (esh-util-test/eshell-printable-size/terabyte) (esh-util-test/eshell-printable-size/use-colors) (esh-util-test/eshell-printable-size/block-size) (esh-util-test/eshell-printable-size/human-readable-arg): New tests. --- diff --git a/lisp/eshell/esh-util.el b/lisp/eshell/esh-util.el index 1d0f41d7b82..82b0d9fc623 100644 --- a/lisp/eshell/esh-util.el +++ b/lisp/eshell/esh-util.el @@ -433,6 +433,10 @@ Prepend remote identification of `default-directory', if any." (defun eshell-printable-size (filesize &optional human-readable block-size use-colors) "Return a printable FILESIZE." + (when (and human-readable + (not (= human-readable 1000)) + (not (= human-readable 1024))) + (error "human-readable must be 1000 or 1024")) (let ((size (float (or filesize 0)))) (if human-readable (if (< size human-readable) @@ -463,7 +467,9 @@ Prepend remote identification of `default-directory', if any." (if use-colors (put-text-property 0 (length str) 'face 'bold-italic str)) - str))))) + str) + (let ((flavor (and (= human-readable 1000) 'si))) + (file-size-human-readable filesize flavor)))))) (if block-size (setq size (/ size block-size))) (format "%.0f" size)))) diff --git a/test/lisp/eshell/esh-util-tests.el b/test/lisp/eshell/esh-util-tests.el index 8585677e14e..fe4eb9f31dd 100644 --- a/test/lisp/eshell/esh-util-tests.el +++ b/test/lisp/eshell/esh-util-tests.el @@ -125,4 +125,35 @@ (should (equal (eshell-convert-to-number "123") "123")) (should (equal (eshell-convert-to-number "1.23") "1.23")))) +(ert-deftest esh-util-test/eshell-printable-size () + (should (equal (eshell-printable-size (expt 2 16)) "65536")) + (should (equal (eshell-printable-size (expt 2 32)) "4294967296"))) + +(ert-deftest esh-util-test/eshell-printable-size/zero () + (should (equal (eshell-printable-size 0 1000 nil t) "0"))) + +(ert-deftest esh-util-test/eshell-printable-size/terabyte () + (should (equal (eshell-printable-size (1- (expt 2 40)) 1024 nil t) "1024G")) + (should (equal (eshell-printable-size (expt 2 40) 1024 nil t) "1T")) + (should (equal (eshell-printable-size (1- (expt 10 12)) 1000 nil t) "1000G")) + (should (equal (eshell-printable-size (expt 10 12) 1000 nil t) "1T"))) + +(ert-deftest esh-util-test/eshell-printable-size/use-colors () + (should (equal-including-properties + (eshell-printable-size (1- (expt 2 20)) 1024 nil t) + "1024k")) + (should (equal-including-properties + (eshell-printable-size (1- (expt 2 30)) 1024 nil t) + (propertize "1024M" 'face 'bold))) + (should (equal-including-properties + (eshell-printable-size (1- (expt 2 40)) 1024 nil t) + (propertize "1024G" 'face 'bold-italic)))) + +(ert-deftest esh-util-test/eshell-printable-size/block-size () + (should (equal (eshell-printable-size (1- (expt 2 20)) nil 4096) "256")) + (should (equal (eshell-printable-size (1- (expt 2 30)) nil 4096) "262144"))) + +(ert-deftest esh-util-test/eshell-printable-size/human-readable-arg () + (should-error (eshell-printable-size 0 999 nil t))) + ;;; esh-util-tests.el ends here