Subject: bug#75495: [PATCH] ibuffer: New defcustom `ibuffer-human-readable-size'

From: Daniel Mendler <[email-masked]>
Date: Sat, 11 Jan 2025 14:28:07 +0100

Tags: patch

This patch adds the customization option `ibuffer-human-readable-size'.
If customized to t, buffer sizes in Ibuffer are shown in human readable
format.

>From e516d867edcfeda4840ba8e067534bc8582ee0e9 Mon Sep 17 00:00:00 2001
From: Daniel Mendler <[email-masked]>
Date: Sat, 11 Jan 2025 14:22:02 +0100
Subject: [PATCH] ibuffer: New defcustom `ibuffer-human-readable-size'

* lisp/ibuffer.el (ibuffer-human-readable-size): New defcustom.
(define-ibuffer-column size): Use it.
* etc/NEWS: Mention new defcustom.
---
 etc/NEWS        |  3 +++
 lisp/ibuffer.el | 26 +++++++++++++++++---------
 2 files changed, 20 insertions(+), 9 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index d017c872fa3..d20c71897bc 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -338,6 +338,9 @@ modal editing packages.
 The variable 'ibuffer-formats' configures the Ibuffer formats.  Add
 'recency' to the format to display the column.
 
+*** New user option 'ibuffer-human-readable-size'.
+When non-nil, buffer sizes are shown in human readable format.
+
 ** Smerge
 *** New command 'smerge-extend' extends a conflict over surrounding lines.
 
diff --git a/lisp/ibuffer.el b/lisp/ibuffer.el
index 32bc8140703..65f8ca53693 100644
--- a/lisp/ibuffer.el
+++ b/lisp/ibuffer.el
@@ -186,6 +186,10 @@ ibuffer-fontification-alist
 		(sexp :tag "Test Form")
                 face)))
 
+(defcustom ibuffer-human-readable-size nil
+  "Show buffer sizes in human-readable format."
+  :type 'boolean)
+
 (defcustom ibuffer-use-other-window nil
   "If non-nil, display Ibuffer in another window by default."
   :type 'boolean)
@@ -1714,15 +1718,19 @@ size
   (:inline t
    :header-mouse-map ibuffer-size-header-map
    :summarizer
-   (lambda (column-strings)
-     (let ((total 0))
-       (dolist (string column-strings)
-	 (setq total
-	       ;; like, ewww ...
-	       (+ (float (string-to-number string))
-		  total)))
-       (format "%.0f" total))))
-  (format "%s" (buffer-size)))
+   (lambda (strings)
+     (let ((total
+            (cl-loop
+             for s in strings sum
+             (or (get-text-property 0 'ibuffer--buffer-size s) 0))))
+       (if ibuffer-human-readable-size
+           (file-size-human-readable total)
+         (number-to-string total)))))
+  (let ((size (buffer-size)))
+    (propertize (if ibuffer-human-readable-size
+                    (file-size-human-readable size)
+                  (number-to-string size))
+                'ibuffer--buffer-size size)))
 
 (define-ibuffer-column recency
   (:inline t :summarizer ignore :header-mouse-map ibuffer-recency-header-map)
-- 
2.45.2