aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJim Porter2024-01-30 10:51:03 -0800
committerJim Porter2024-11-09 17:01:34 -0800
commit37c583a2c2f97c6fb0ed0433db9c74c6a1758e3e (patch)
treead5c1fd655c7420385d41390c52c588d8074205d
parentf26fb25ec8ca12901db6224973c7fef29ce938f9 (diff)
downloademacs-37c583a2c2f97c6fb0ed0433db9c74c6a1758e3e.tar.gz
emacs-37c583a2c2f97c6fb0ed0433db9c74c6a1758e3e.zip
Don't use dynamically-bound variables for Eshell's "du" command
* lisp/eshell/em-unix.el (block-size, by-bytes, dereference-links) (grand-total, human-readable, max-depth, only-one-filesystem, show-all) (eshell-du-size-string): Remove. (eshell-du-sum-directory): Accept key arguments for adjusting behavior. (eshell/du): Use 'block-size' of 1 instead of 'by-bytes', and define a print function.
-rw-r--r--lisp/eshell/em-unix.el87
1 files changed, 37 insertions, 50 deletions
diff --git a/lisp/eshell/em-unix.el b/lisp/eshell/em-unix.el
index 17dd14d0f3e..74d61e82bef 100644
--- a/lisp/eshell/em-unix.el
+++ b/lisp/eshell/em-unix.el
@@ -858,54 +858,35 @@ external command."
858 pcomplete-last-completion-raw t) 858 pcomplete-last-completion-raw t)
859 (throw 'pcomplete-completions (pcomplete-read-host-names))))) 859 (throw 'pcomplete-completions (pcomplete-read-host-names)))))
860 860
861(defvar block-size) 861(cl-defun eshell-du-sum-directory (path depth-remaining &rest args
862(defvar by-bytes) 862 &key print-function show-all
863(defvar dereference-links) 863 dereference-links only-one-filesystem)
864(defvar grand-total)
865(defvar human-readable)
866(defvar max-depth)
867(defvar only-one-filesystem)
868(defvar show-all)
869
870(defsubst eshell-du-size-string (size)
871 (let* ((str (eshell-printable-size size human-readable block-size t))
872 (len (length str)))
873 (concat str (if (< len 8)
874 (make-string (- 8 len) ? )))))
875
876(defun eshell-du-sum-directory (path depth)
877 "Summarize PATH, and its member directories." 864 "Summarize PATH, and its member directories."
878 (let ((entries (eshell-directory-files-and-attributes path)) 865 (let ((size 0.0))
879 (size 0.0)) 866 (dolist (entry (eshell-directory-files-and-attributes path))
880 (while entries 867 (unless (string-match "\\`\\.\\.?\\'" (car entry))
881 (unless (string-match "\\`\\.\\.?\\'" (caar entries)) 868 (let* ((file-name (concat path "/" (car entry)))
882 (let* ((entry (concat path "/" 869 (file-type (file-attribute-type (cdr entry)))
883 (caar entries))) 870 (symlink (and (stringp file-type) file-type)))
884 (symlink (and (stringp (file-attribute-type (cdar entries)))
885 (file-attribute-type (cdar entries)))))
886 (unless (or (and symlink (not dereference-links)) 871 (unless (or (and symlink (not dereference-links))
887 (and only-one-filesystem 872 (and only-one-filesystem
888 (/= only-one-filesystem 873 (/= only-one-filesystem
889 (file-attribute-device-number (cdar entries))))) 874 (file-attribute-device-number (cdr entry)))))
890 (if symlink 875 (when symlink
891 (setq entry symlink)) 876 (setq file-name symlink))
892 (setq size 877 (setq size
893 (+ size 878 (+ size
894 (if (eq t (car (cdar entries))) 879 (if (eq file-type t) ; This is a directory.
895 (eshell-du-sum-directory entry (1+ depth)) 880 (apply #'eshell-du-sum-directory file-name
896 (let ((file-size (file-attribute-size (cdar entries)))) 881 (when depth-remaining (1- depth-remaining))
897 (prog1 882 args)
898 file-size 883 (let ((file-size (file-attribute-size (cdr entry))))
899 (if show-all 884 (when show-all
900 (eshell-print 885 (funcall print-function file-size file-name))
901 (concat (eshell-du-size-string file-size) 886 file-size))))))))
902 entry "\n"))))))))))) 887 (when (or (not depth-remaining)
903 (setq entries (cdr entries))) 888 (natnump depth-remaining))
904 (if (or (not max-depth) 889 (funcall print-function size (directory-file-name path)))
905 (= depth max-depth)
906 (= depth 0))
907 (eshell-print (concat (eshell-du-size-string size)
908 (directory-file-name path) "\n")))
909 size)) 890 size))
910 891
911(defun eshell/du (&rest args) 892(defun eshell/du (&rest args)
@@ -917,7 +898,7 @@ external command."
917 "write counts for all files, not just directories") 898 "write counts for all files, not just directories")
918 (nil "block-size" t block-size 899 (nil "block-size" t block-size
919 "use SIZE-byte blocks (i.e., --block-size SIZE)") 900 "use SIZE-byte blocks (i.e., --block-size SIZE)")
920 (?b "bytes" nil by-bytes 901 (?b "bytes" 1 block-size
921 "print size in bytes") 902 "print size in bytes")
922 (?c "total" nil grand-total 903 (?c "total" nil grand-total
923 "produce a grand total") 904 "produce a grand total")
@@ -948,8 +929,7 @@ Summarize disk usage of each FILE, recursively for directories.")
948 args))) 929 args)))
949 (ext-du (eshell-search-path "du"))) 930 (ext-du (eshell-search-path "du")))
950 (throw 'eshell-external (eshell-external-command ext-du original-args))) 931 (throw 'eshell-external (eshell-external-command ext-du original-args)))
951 (unless by-bytes 932 (setq block-size (or block-size 1024))
952 (setq block-size (or block-size 1024)))
953 (when (stringp block-size) 933 (when (stringp block-size)
954 (setq block-size (string-to-number block-size))) 934 (setq block-size (string-to-number block-size)))
955 (when (stringp max-depth) 935 (when (stringp max-depth)
@@ -957,17 +937,24 @@ Summarize disk usage of each FILE, recursively for directories.")
957 ;; Filesystem support means nothing under MS-Windows. 937 ;; Filesystem support means nothing under MS-Windows.
958 (when (eshell-under-windows-p) 938 (when (eshell-under-windows-p)
959 (setq only-one-filesystem nil)) 939 (setq only-one-filesystem nil))
960 (let ((size 0.0)) 940 (let ((size 0.0)
941 (print-function
942 (lambda (size name)
943 (let ((size-str (eshell-printable-size size human-readable
944 block-size t)))
945 (eshell-print (concat (string-pad size-str 8) name "\n"))))))
961 (dolist (arg (or args '("."))) 946 (dolist (arg (or args '(".")))
962 (when only-one-filesystem 947 (when only-one-filesystem
963 (setq only-one-filesystem 948 (setq only-one-filesystem
964 (file-attribute-device-number 949 (file-attribute-device-number
965 (eshell-file-attributes (file-name-as-directory arg))))) 950 (eshell-file-attributes (file-name-as-directory arg)))))
966 (setq size (+ size (eshell-du-sum-directory 951 (setq size (+ size (eshell-du-sum-directory
967 (directory-file-name arg) 0)))) 952 (directory-file-name arg) max-depth
968 (if grand-total 953 :print-function print-function :show-all show-all
969 (eshell-print (concat (eshell-du-size-string size) 954 :dereference-links dereference-links
970 "total\n"))))))) 955 :only-one-filesystem only-one-filesystem))))
956 (when grand-total
957 (funcall print-function size "total"))))))
971 958
972(put 'eshell/du 'eshell-filename-arguments t) 959(put 'eshell/du 'eshell-filename-arguments t)
973 960