]> git.eshelyaron.com Git - emacs.git/commitdiff
Use a tabulated-list to display package configuration statistics
authorDamien Cassou <damien.cassou@gmail.com>
Thu, 8 Mar 2018 19:48:07 +0000 (20:48 +0100)
committerDamien Cassou <damien.cassou@gmail.com>
Thu, 8 Mar 2018 20:00:31 +0000 (21:00 +0100)
Fix https://github.com/jwiegley/use-package/issues/641

lisp/use-package/use-package-core.el

index a649d087fae0706cba434057251c4029c62449e2..83630b8078598bfe50de74c7b1e507d612f8776e 100644 (file)
@@ -41,6 +41,7 @@
 
 (require 'bytecomp)
 (require 'cl-lib)
+(require 'tabulated-list)
 
 (eval-when-compile
   (require 'cl)
@@ -964,6 +965,43 @@ If RECURSED is non-nil, recurse into sublists."
   (interactive)
   (setq use-package-statistics (make-hash-table)))
 
+(defun use-package-statistics-status (package)
+  "Return loading configuration status of PACKAGE."
+  (cond ((gethash :config statistics)      "Configured")
+        ((gethash :init statistics)        "Initialized")
+        ((gethash :preface statistics)     "Prefaced")
+        ((gethash :use-package statistics) "Declared")))
+
+(defun use-package-statistics-last-event (package)
+  "Return the date when package's status last changed.
+The date is returned as a string."
+  (format-time-string "%Y-%m-%d %a %H:%M"
+                      (or (gethash :config statistics)
+                          (gethash :init statistics)
+                          (gethash :preface statistics)
+                          (gethash :use-package statistics))))
+
+(defun use-package-statistics-time (package)
+  "Return the time is took for package to load."
+  (+ (float-time (gethash :config-secs statistics 0))
+     (float-time (gethash :init-secs statistics 0))
+     (float-time (gethash :preface-secs statistics 0))
+     (float-time (gethash :use-package-secs statistics 0))))
+
+(defun use-package-statistics-convert (package)
+  "Return information about PACKAGE.
+
+The information is formatted in a way suitable for
+`use-package-statistics-mode'."
+  (let ((statistics (gethash package use-package-statistics)))
+    (list
+     package
+     (vector
+      (symbol-name package)
+      (use-package-statistics-status package)
+      (use-package-statistics-last-event package)
+      (format "%.2f" (use-package-statistics-time package))))))
+
 (defun use-package-report ()
   "Show current statistics gathered about use-package declarations.
 In the table that's generated, the status field has the following
@@ -974,32 +1012,24 @@ meaning:
   Declared          the use-package declaration was seen"
   (interactive)
   (with-current-buffer (get-buffer-create "*use-package statistics*")
-    (delete-region (point-min) (point-max))
-    (insert "|Package|Status|Last Event|Time|\n")
-    (insert "|-\n")
-    (maphash
-     #'(lambda (key hash)
-         (insert
-          (format "|%s |%s|%s |%.2f|\n" key
-                  (cond ((gethash :config hash)      "Configured")
-                        ((gethash :init hash)        "Initialized")
-                        ((gethash :preface hash)     "Prefaced")
-                        ((gethash :use-package hash) "Declared"))
-                  (format-time-string "[%Y-%m-%d %a %H:%M]"
-                                      (or (gethash :config hash)
-                                          (gethash :init hash)
-                                          (gethash :preface hash)
-                                          (gethash :use-package hash)))
-                  (+ (float-time (gethash :config-secs hash 0))
-                     (float-time (gethash :init-secs hash 0))
-                     (float-time (gethash :preface-secs hash 0))
-                     (float-time (gethash :use-package-secs hash 0))))))
-     use-package-statistics)
-    (goto-char (point-min))
-    (orgtbl-mode)
-    (org-table-align)
+    (setq tabulated-list-entries
+          (mapcar #'use-package-statistics-convert
+                  (hash-table-keys use-package-statistics)))
+    (use-package-statistics-mode)
+    (tabulated-list-print)
     (display-buffer (current-buffer))))
 
+(define-derived-mode use-package-statistics-mode tabulated-list-mode
+  "use-package statistics"
+  "Show current statistics gathered about use-package declarations."
+  (setq tabulated-list-format
+        ;; The sum of column width is 80 caracters:
+        #[("Package" 25 t)
+          ("Status" 13 t)
+          ("Last Event" 23 t)
+          ("Time" 10 t)])
+  (tabulated-list-init-header))
+
 (defun use-package-statistics-gather (keyword name after)
   (let* ((hash (gethash name use-package-statistics
                         (make-hash-table)))