Use a tabulated-list to display package configuration statistics

Fix https://github.com/jwiegley/use-package/issues/641
This commit is contained in:
Damien Cassou 2018-03-08 20:48:07 +01:00
parent 76e6d3e8bc
commit ae22d74a16

View file

@ -41,6 +41,7 @@
(require 'bytecomp) (require 'bytecomp)
(require 'cl-lib) (require 'cl-lib)
(require 'tabulated-list)
(eval-when-compile (eval-when-compile
(require 'cl) (require 'cl)
@ -964,6 +965,43 @@ If RECURSED is non-nil, recurse into sublists."
(interactive) (interactive)
(setq use-package-statistics (make-hash-table))) (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 () (defun use-package-report ()
"Show current statistics gathered about use-package declarations. "Show current statistics gathered about use-package declarations.
In the table that's generated, the status field has the following In the table that's generated, the status field has the following
@ -974,32 +1012,24 @@ meaning:
Declared the use-package declaration was seen" Declared the use-package declaration was seen"
(interactive) (interactive)
(with-current-buffer (get-buffer-create "*use-package statistics*") (with-current-buffer (get-buffer-create "*use-package statistics*")
(delete-region (point-min) (point-max)) (setq tabulated-list-entries
(insert "|Package|Status|Last Event|Time|\n") (mapcar #'use-package-statistics-convert
(insert "|-\n") (hash-table-keys use-package-statistics)))
(maphash (use-package-statistics-mode)
#'(lambda (key hash) (tabulated-list-print)
(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)
(display-buffer (current-buffer)))) (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) (defun use-package-statistics-gather (keyword name after)
(let* ((hash (gethash name use-package-statistics (let* ((hash (gethash name use-package-statistics
(make-hash-table))) (make-hash-table)))