[RFC PATCH] emacs: show: show attachment size

Mark Walters markwalters1009 at gmail.com
Sat Jun 13 00:44:06 PDT 2015


Display the attachment size in kB or MB in the part button. This uses
the content-length header and guesses the actual size from that, so
the value will be close but not exact.
---

This patch adds some text to the part button saying how big the part
is. This is particularly useful when running notmuch over ssh but may
be informative anyway.

One particular question is am I putting sizes on the correct parts?
This patch puts a size on all parts where we know the size and it is
not hidden ( part buttons of the form [ text/html (hidden) (30kB) ]
looked messy).

There are a couple of caveats with the patch. First the size is
approximate. This is because gmime/notmuch only outputs the encoded
size. We guess the true size is roughly 3/4 the encoded size if it is
base64 encoded and the encoded size otherwise. (See
id:20120807232414.GA22132 at hili.localdomain and Austin's reply.)

Secondly, lots of tests fail. This is expected as the output for any
message with attachment has changed. I thought I would see whether
people liked the idea, and which parts people think need a size before
fixing them.

Best wishes

Mark



emacs/notmuch-show.el | 26 +++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)

diff --git a/emacs/notmuch-show.el b/emacs/notmuch-show.el
index 2a53461..f26e2f8 100644
--- a/emacs/notmuch-show.el
+++ b/emacs/notmuch-show.el
@@ -488,6 +488,25 @@ (define-button-type 'notmuch-show-part-button-type
   'face 'message-mml
   :supertype 'notmuch-button-type)
 
+(defun notmuch-show-part-pretty-size (part)
+  "Return a nicely formatted string of the approximate part size in kB or MB if known"
+  ;; We guess the size from the encoded length (which is all
+  ;; gmime tells us). If it is base64 encoded the size will be
+  ;; approximately 3/4 of the encoded size, otherwise (e.g. if it
+  ;; is quoted printable) the size is probably pretty close to
+  ;; the encoded size.
+  (let ((approx-size (if (string= (plist-get part :content-transfer-encoding) "base64")
+			 (/ (* (plist-get part :content-length) 3) 4)
+		       (plist-get part :content-length))))
+    (cond ((not approx-size)
+	   nil)
+	  ((< approx-size 1000000)
+	   (format " (%s kB)" (1+ (/ approx-size 1000))))
+	  ((< approx-size 10000000)
+	   (format " (%s.%s MB)" (/ approx-size 1000000) (% (/ approx-size 100000) 10)))
+	  (t
+	   (format " (%s MB)" (/ approx-size 1000000))))))
+
 (defun notmuch-show-insert-part-header (nth content-type declared-type &optional name comment)
   (let ((button)
 	(base-label (concat (when name (concat name ": "))
@@ -948,11 +967,16 @@ (defun notmuch-show-insert-bodypart (msg part depth &optional hide)
 		    (> notmuch-show-max-text-part-size 0)
 		    (> (length (plist-get part :content)) notmuch-show-max-text-part-size)))
 	 (beg (point))
+	 (pretty-size (unless hide (notmuch-show-part-pretty-size part)))
 	 ;; We omit the part button for the first (or only) part if
 	 ;; this is text/plain, or HIDE is 'no-buttons.
 	 (button (unless (or (equal hide 'no-buttons)
 			     (and (string= mime-type "text/plain") (<= nth 1)))
-		   (notmuch-show-insert-part-header nth mime-type content-type (plist-get part :filename))))
+		   (notmuch-show-insert-part-header nth
+						    mime-type
+						    content-type
+						    (plist-get part :filename)
+						    pretty-size)))
 	 ;; Hide the part initially if HIDE is t, or if it is too long
 	 ;; and we have a button to allow toggling (thus reply which
 	 ;; uses 'no-buttons automatically includes long parts)
-- 
2.1.4



More information about the notmuch mailing list