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

From: Daniel Mendler <[email-masked]>
Date: Sat, 18 Jan 2025 11:33:30 +0100

Eli Zaretskii <[email-masked]> writes:

>> Date: Sun, 12 Jan 2025 13:34:30 +0100
>> From:  Daniel Mendler via "Bug reports for GNU Emacs,
>>  the Swiss army knife of text editors" <[email-masked]>
>> 
>> Daniel Mendler <[email-masked]> writes:
>> 
>> > This patch adds the customization option `ibuffer-human-readable-size'.
>> > If customized to t, buffer sizes in Ibuffer are shown in human readable
>> > format.
>> 
>> I have attached an improved version of the patch to this mail, where it
>> is ensured that the summarizer works for a left or right aligned size
>> column.
>
> Thanks.
>
>> +(defcustom ibuffer-human-readable-size nil
>> +  "Show buffer sizes in human-readable format."
>> +  :type 'boolean)
>
> This needs a :version tag.
>
> Also, I think the doc string should explain in more detail what we
> mean by "human-readable format".

Thank you for the review. I have attached an updated patch to this mail.

Daniel

>From 57358674a7c5b5231c6be97546f86838c3413e33 Mon Sep 17 00:00:00 2001
From: Daniel Mendler <[email-masked]>
Date: Sat, 11 Jan 2025 14:22:02 +0100
Subject: [PATCH 1/2] 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 | 29 ++++++++++++++++++++---------
 2 files changed, 23 insertions(+), 9 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index 0b849dec450..ad099f34541 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -353,6 +353,9 @@ You can now set `asm-comment-char' from 'asm-mode-hook' instead.
 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..c0033af8435 100644
--- a/lisp/ibuffer.el
+++ b/lisp/ibuffer.el
@@ -186,6 +186,12 @@ ibuffer-fontification-alist
 		(sexp :tag "Test Form")
                 face)))
 
+(defcustom ibuffer-human-readable-size nil
+  "Show buffer sizes in human-readable format.
+Use the function `file-size-human-readable' for formatting."
+  :type 'boolean
+  :version "31.1")
+
 (defcustom ibuffer-use-other-window nil
   "If non-nil, display Ibuffer in another window by default."
   :type 'boolean)
@@ -1714,15 +1720,20 @@ 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 (1- (length s)) 'ibuffer-size s)
+                 (get-text-property 0 'ibuffer-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-size size)))
 
 (define-ibuffer-column recency
   (:inline t :summarizer ignore :header-mouse-map ibuffer-recency-header-map)
-- 
2.45.2