+;; 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 (if (> (mod file-size 1.0) 0.05)
+ "%.1f%s%s"
+ "%.0f%s%s")
+ file-size
+ (if (and (eq flavor 'iec) (string= (car post-fixes) "k"))
+ "K"
+ (car post-fixes))
+ (if (eq flavor 'iec) "iB" ""))))
+