emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Emacs-diffs] /srv/bzr/emacs/trunk r103867: New function file-size-human


From: Eli Zaretskii
Subject: [Emacs-diffs] /srv/bzr/emacs/trunk r103867: New function file-size-human-readable.
Date: Fri, 08 Apr 2011 18:31:33 +0300
User-agent: Bazaar (2.0.3)

------------------------------------------------------------
revno: 103867
committer: Eli Zaretskii <address@hidden>
branch nick: trunk
timestamp: Fri 2011-04-08 18:31:33 +0300
message:
  New function file-size-human-readable.
  
   lisp/files.el (file-size-human-readable): New function.
   lisp/ls-lisp.el (ls-lisp-format-file-size): Use it, instead of
   computing the representation inline.  Don't require `cl'.
modified:
  lisp/ChangeLog
  lisp/files.el
  lisp/ls-lisp.el
=== modified file 'lisp/ChangeLog'
--- a/lisp/ChangeLog    2011-04-08 03:30:26 +0000
+++ b/lisp/ChangeLog    2011-04-08 15:31:33 +0000
@@ -1,3 +1,10 @@
+2011-04-08  Eli Zaretskii  <address@hidden>
+
+       * files.el (file-size-human-readable): New function.
+
+       * ls-lisp.el (ls-lisp-format-file-size): Use it, instead of
+       computing the representation inline.  Don't require `cl'.
+
 2011-04-08  Glenn Morris  <address@hidden>
 
        * man.el (Man-page-header-regexp): Solaris < 2.6 no longer supported.

=== modified file 'lisp/files.el'
--- a/lisp/files.el     2011-04-06 21:13:17 +0000
+++ b/lisp/files.el     2011-04-08 15:31:33 +0000
@@ -1140,6 +1140,34 @@
        (setq count (1+ count))))
     newname))
 
+;; A handy function to display file sizes in human-readable form.
+;; See http://en.wikipedia.org/wiki/Kibibyte for the reference.
+(defun file-size-human-readable (file-size &optional flavor)
+  "Produce a string showing FILE-SIZE in human-readable form.
+
+Optional second argument FLAVOR controls the units and the display format:
+
+ If FLAVOR is nil or omitted, each kilobyte is 1024 bytes and the produced
+    suffixes are \"k\", \"M\", \"G\", \"T\", etc.
+ If FLAVOR is `si', each kilobyte is 1000 bytes and the produced suffixes
+    are \"k\", \"M\", \"G\", \"T\", etc.
+ If FLAVOR is `iec', each kilobyte is 1024 bytes and the produced suffixes
+    are \"KiB\", \"MiB\", \"GiB\", \"TiB\", etc."
+  (let ((power (if (or (null flavor) (eq flavor 'iec))
+                  1024.0
+                1000.0))
+       (post-fixes
+        ;; none, kilo, mega, giga, tera, peta, exa, zetta, yotta
+        (list "" "k" "M" "G" "T" "P" "E" "Z" "Y")))
+    (while (and (>= file-size power) (cdr post-fixes))
+      (setq file-size (/ file-size power)
+           post-fixes (cdr post-fixes)))
+    (format "%.0f%s%s" file-size
+           (if (and (eq flavor 'iec) (string= (car post-fixes) "k"))
+               "K"
+             (car post-fixes))
+           (if (eq flavor 'iec) "iB" ""))))
+
 (defun make-temp-file (prefix &optional dir-flag suffix)
   "Create a temporary file.
 The returned file name (created by appending some random characters at the end

=== modified file 'lisp/ls-lisp.el'
--- a/lisp/ls-lisp.el   2011-01-25 04:08:28 +0000
+++ b/lisp/ls-lisp.el   2011-04-08 15:31:33 +0000
@@ -62,8 +62,6 @@
 
 ;;; Code:
 
-(eval-when-compile (require 'cl))
-
 (defgroup ls-lisp nil
   "Emulate the ls program completely in Emacs Lisp."
   :version "21.1"
@@ -726,13 +724,7 @@
                  ls-lisp-filesize-f-fmt
                ls-lisp-filesize-d-fmt)
              file-size)
-    (if (< file-size 1024)
-       (format " %4d" file-size)
-      (do ((file-size (/ file-size 1024.0) (/ file-size 1024.0))
-          ;; kilo, mega, giga, tera, peta, exa
-          (post-fixes (list "k" "M" "G" "T" "P" "E") (cdr post-fixes)))
-         ((< file-size 1024)
-          (format " %3.0f%s"  file-size (car post-fixes)))))))
+    (format " %4s" (file-size-human-readable file-size))))
 
 (provide 'ls-lisp)
 


reply via email to

[Prev in Thread] Current Thread [Next in Thread]