Add doom-{file,directory}-size file functions

This commit is contained in:
Henrik Lissner 2019-10-19 14:38:56 -04:00
parent 06da7fc20b
commit c8efb45746
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395

View file

@ -167,6 +167,33 @@ single file or nested compound statement of `and' and `or' statements."
`(let ((p ,(doom--resolve-path-forms files directory))) `(let ((p ,(doom--resolve-path-forms files directory)))
(and p (expand-file-name p ,directory)))) (and p (expand-file-name p ,directory))))
;;;###autoload
(defun doom-file-size (file &optional dir)
"Returns the size of FILE (in DIR) in kilobytes."
(when-let (file (file-exists-p! file dir))
(unless (file-readable-p file)
(error "File %S is unreadable; can't acquire its filesize"
file))
(nth 7 (file-attributes file))))
;;;###autoload
(defun doom-directory-size (dir)
"Returns the size of FILE (in DIR) in kilobytes."
(if (executable-find "du")
(/ (string-to-number (cdr (doom-call-process "du" "-sb" dir)))
1024.0)
;; REVIEW This is slow and terribly inaccurate, but it's something
(let ((w32-get-true-file-attributes t)
(file-name-handler-alist dir)
(max-lisp-eval-depth 5000)
(sum 0.0))
(dolist (attrs (directory-files-and-attributes dir nil nil t) sum)
(unless (member (car attrs) '("." ".."))
(cl-incf
sum (if (eq (nth 1 attrs) t) ; is directory
(doom-directory-size (expand-file-name (car attrs) dir))
(/ (nth 8 attrs) 1024.0))))))))
;; ;;
;;; Helpers ;;; Helpers