Emacs Dired, Display File Size with Number Seperators

By Xah Lee. Date: .

xtodo work in progress

how to get dired to display file size with seperators e.g. 123,456,789.

xtodo
;; 2022-02-22 from Jacob L
;; (defun ls-lisp-format-file-size (FileSize HumanReadable)
;;   (if HumanReadable
;;       (format " %6s" (file-size-human-readable FileSize))
;;     (concat
;;      " "
;;      (let*
;;          ((xInput (format (if (floatp FileSize)
;;                               ls-lisp-filesize-f-fmt
;;                             ls-lisp-filesize-d-fmt)
;;                           FileSize))
;;           (xNumber (substring xInput 1))
;;           (xDigits (length xNumber))
;;           (xSepRequired (/ xDigits 3)))
;;        (dotimes (i xSepRequired xNumber)
;;          (let ((xSepPos (- xDigits (* (+ 1 i) 3))))
;;            (setq
;;             xNumber;;
;;             (concat (substring xNumber 0 xSepPos)
;;                     (if (string-equal " " (substring xNumber (- xSepPos 1) xSepPos))
;;                         " "
;;                       "_")
;;                     (substring xNumber xSepPos)))))))))

(defun jacob-format-filesize-with-seperator (input seperator)
  (letrec ((digits (split-string input "" t))
           (go (lambda (l c nl)
                 (if (null l)
                     nl
                   (funcall
                    go (cdr l) (+ c 1)
                    (cons (car l)
                          (cons (if (and (= (% c 3) 0)
                                         (not (null nl)))
                                    (if (string-equal " " (car l))
                                        " "
                                      seperator))
                                nl)))))))
    (mapconcat 'identity
               (funcall go (reverse digits) 0 nil)
               "")))

(defun ls-lisp-format-file-size (file-size human-readable)
  (if (not human-readable)
      (format " %s"
              (jacob-format-filesize-with-seperator
               (format (if (floatp file-size)
                           ls-lisp-filesize-f-fmt
                         ls-lisp-filesize-d-fmt)
                       file-size)
               ","))
    (format " %6s" (file-size-human-readable file-size))))

;; ssss---------------------------------------------------

(defun xah-format-int (Int)
  "add _ every 3 digits.
version 2022-03-30"
  (interactive)
  (let* ((xDigits (mapcar
                   'identity
                   (number-to-string Int)))
         (xLen (length xDigits))
         (xResultList nil)
         (xTimes (/ xLen 3))
         (xStart (mod xLen 3))
         (xEnd (+ xStart xLen)))
    (while (<= xStart xEnd)
      (if (= (mod xStart 3) 0)
          (push 95 xResultList)
        (progn
          (push (pop xDigits) xResultList)
          (setq xStart (1+ xStart)))))
    (mapconcat 'char-to-string xResultList ""))
  ;;
  )

(mapconcat 'number-to-string '(3 4 5) "_")

(xah-format-int 123)
;; 2022-03-30 original, in lisp/ls-lisp.el

(defun ls-lisp-format-file-size (file-size human-readable)
  (if (not human-readable)
      (format (if (floatp file-size)
		  ls-lisp-filesize-f-fmt
		ls-lisp-filesize-d-fmt)
	      file-size)
    (format " %6s" (file-size-human-readable file-size))))

Emacs Dired, File Management


Emacs Init

Init Basics

Keys

Font

Text Editing

Completion

File

Restore Things

Backup/AutoSave

Line

Appearance

Misc

Advanced Init Tips

packages