From 24c11301b8e06a696ca371eff4087d5e539504f5 Mon Sep 17 00:00:00 2001 From: JSDurand Date: Mon, 19 Jul 2021 12:19:37 +0800 Subject: new: track time in eshell Now eshell will display the elapsed time of the last command at the beginning of the prompt. * eshell-conf.el (eshell-current-command-start-time): the variable to track the start time of the commands. (eshell-prompt-time-string): this will be displayed at the prompt, if non-nil. (eshell-current-command-start): set an appropriate variable as the start time of the command. (eshell-current-command-stop): calculat the elapsed time since the last command started and convert that into a pretty string to display. (eshell-current-command-time-track): add hooks. (eshell-start-track-command-time): (eshell-stop-track-command-time): switches of the time-tracking mechanism. (#'eshell-emit-prompt): (durand-eshell-emit-prompt): advice the original function to take into account the time information. refactor: (eshell/marks): add some colors --- eshell-conf.el | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 87 insertions(+), 1 deletion(-) (limited to 'eshell-conf.el') diff --git a/eshell-conf.el b/eshell-conf.el index d0fc491..425d5cc 100644 --- a/eshell-conf.el +++ b/eshell-conf.el @@ -32,6 +32,92 @@ (require 'esh-util) (require 'ring) +;; Measure time of commands and display automatically. + +;;;###autoload +(defvar-local eshell-current-command-start-time nil + "The time of the start of the current command.") + +;;;###autoload +(defvar-local eshell-prompt-time-string "" + "The string displaying the time of the last command, if any.") + +;;;###autoload +(defun eshell-current-command-start () + (setq-local eshell-current-command-start-time (current-time))) + +;;;###autoload +(defun eshell-current-command-stop () + (cond + ((timep eshell-current-command-start-time) + (let* ((elapsed-time (time-since eshell-current-command-start-time)) + (elapsed-time-list (time-convert elapsed-time 'list)) + (elapsed-time-int (time-convert elapsed-time 'integer)) + (format-seconds-string + (format-seconds "%yy %dd %hh %mm %ss%z" elapsed-time-int)) + (microseconds (caddr elapsed-time-list)) + (micro-str + (cond ((> microseconds 10000) + (format "%dμs" microseconds))))) + (setq eshell-prompt-time-string + (mapconcat #'identity + (delq nil (list format-seconds-string + micro-str)) + " ")) + (message eshell-prompt-time-string)) + (setq eshell-current-command-start-time nil)))) + +;;;###autoload +(defun eshell-current-command-time-track () + (add-hook 'eshell-pre-command-hook #'eshell-current-command-start nil t) + (add-hook 'eshell-post-command-hook #'eshell-current-command-stop nil t)) + +;;;###autoload +(defun eshell-start-track-command-time () + "Start tracking the time of commands." + (cond + ((derived-mode-p 'eshell-mode) + (eshell-current-command-time-track))) + (add-hook 'eshell-mode-hook #'eshell-current-command-time-track)) + +;;;###autoload +(defun eshell-stop-track-command-time () + (remove-hook 'eshell-pre-command-hook #'eshell-current-command-start t) + (remove-hook 'eshell-post-command-hook #'eshell-current-command-stop t) + (remove-hook 'eshell-mode-hook #'eshell-current-command-time-track)) + +;;;###autoload +(defun durand-eshell-emit-prompt () + "Emit a prompt if eshell is being used interactively. +Add a time information at the beginning. -- Modified by Durand." + (when (boundp 'ansi-color-context-region) + (setq ansi-color-context-region nil)) + (run-hooks 'eshell-before-prompt-hook) + (if (not eshell-prompt-function) + (set-marker eshell-last-output-end (point)) + (let ((prompt (funcall eshell-prompt-function))) + (and eshell-highlight-prompt + (add-text-properties 0 (length prompt) + '(read-only t + font-lock-face eshell-prompt + front-sticky (font-lock-face read-only) + rear-nonsticky (font-lock-face read-only)) + prompt)) + (eshell-interactive-print + (mapconcat #'identity + (delq + nil + (list + (cond ((> (length eshell-prompt-time-string) 0) + (propertize eshell-prompt-time-string + 'font-lock-face 'modus-themes-heading-1)) ) + prompt)) + " ")) + (setq eshell-prompt-time-string ""))) + (run-hooks 'eshell-after-prompt-hook)) + +(advice-add #'eshell-emit-prompt :override #'durand-eshell-emit-prompt) + ;; Eshell sets the keymap in the major mode function... ;;;###autoload @@ -90,7 +176,7 @@ Just for the completeness." (mapconcat (function (lambda (mark) - (concat mark + (concat (propertize mark 'font-lock-face 'modus-themes-mark-symbol) (make-string (- max-length (length mark)) #x20) " -> " (file-truename (expand-file-name mark eshell-mark-directory))))) -- cgit v1.2.3-18-g5258