2

The compilation buffer prints out start and end date for my compilation. How can I make it also tell me how many minutes & seconds it spent?

I'd like something like: Compilation started at Thu Mar 16 09:10:44 [..] Compilation finished at Thu Mar 16 09:13:04 Compilation took n hours n minutes n seconds

skybert
  • 383
  • 4
  • 11
  • 1
    Perhaps an "around" advice to the compile function? Store the current time at the start, again at the end and print out the difference to the compilation buffer. – NickD Mar 16 '17 at 22:02

2 Answers2

4

Amazing nobody's answered this yet, but I can't find an answer anywhere. Here's my solution:

(make-variable-buffer-local 'my-compilation-start-time)

(add-hook 'compilation-start-hook #'my-compilation-start-hook)
(defun my-compilation-start-hook (proc) 
  (setq my-compilation-start-time (current-time)))

(add-hook 'compilation-finish-functions #'my-compilation-finish-function)
(defun my-compilation-finish-function (buf why)
  (let* ((elapsed  (time-subtract nil my-compilation-start-time))
         (msg (format "Elapsed: %s" (format-time-string "%T.%N" elapsed t))))
    (save-excursion (goto-char (point-max)) (insert msg))
    (message "Compilation %s: %s" (string-trim-right why) msg)))
Stefan
  • 26,404
  • 3
  • 48
  • 85
Alan Conway
  • 156
  • 2
0

I've tweaked above answers with better logic and tested it, working perfectly:

  1. Add Hooks to functions:
  (add-hook 'compilation-start-hook 'compilation-started)
  (add-hook 'compilation-finish-functions 'hide-compile-buffer-if-successful)
  1. Auto Hide Compile Buffer Delay Customizable Variable (via M-x customize-variable RET auto-hide-compile-buffer-delay):
  (defcustom auto-hide-compile-buffer-delay 0
    "Time in seconds before auto hiding compile buffer."
    :group 'compilation
    :type 'number
  )
  1. Get time taken in compilation and use compilation-num-* for warnings and errors count in compilation buffer:
  (defun hide-compile-buffer-if-successful (buffer string)
    (setq compilation-total-time (time-subtract nil compilation-start-time))
    (setq time-str (concat " (Time: " (format-time-string "%s.%3N" compilation-total-time) "s)"))
(if
  (with-current-buffer buffer
    (setq warnings (eval compilation-num-warnings-found))
    (setq warnings-str (concat " (Warnings: " (number-to-string warnings) ")"))
    (setq errors (eval compilation-num-errors-found))

    (if (eq errors 0) nil t)
  )

  ;;If Errors then
  (message (concat "Compiled with Errors" warnings-str time-str))

  ;;If Compiled Successfully or with Warnings then
  (progn
    (bury-buffer buffer)
    (run-with-timer auto-hide-compile-buffer-delay nil 'delete-window (get-buffer-window buffer 'visible))
    (message (concat "Compiled Successfully" warnings-str time-str))
  )
)

)

(make-variable-buffer-local 'compilation-start-time)

(defun compilation-started (proc) (setq compilation-start-time (current-time)) )

Demo

nitin
  • 31
  • 3