summary refs log tree commit diff
path: root/emacs/custom
diff options
context:
space:
mode:
authorFranck Cuny <franck@fcuny.net>2021-08-05 08:44:47 -0700
committerFranck Cuny <franck@fcuny.net>2021-08-05 08:44:47 -0700
commitb4800e89c31ccae04af46de811890699778e5858 (patch)
tree5617089a8e33555d48fe1737bd434adf0c6d443b /emacs/custom
parentemacs: don't use modus theme (diff)
downloademacs.d-b4800e89c31ccae04af46de811890699778e5858.tar.gz
emacs: track command's execution time in eshell
With zsh I report how long a command takes to execute when it takes more
than a few seconds, and it's pretty useful.

This article [1] shows how to do the same with eshell.

[1] https://www.birkey.co/2021-06-20-why-eshell-part-1.html
Diffstat (limited to 'emacs/custom')
-rw-r--r--emacs/custom/fcuny-eshell.el79
1 files changed, 50 insertions, 29 deletions
diff --git a/emacs/custom/fcuny-eshell.el b/emacs/custom/fcuny-eshell.el
index 8c04eab..6c268f0 100644
--- a/emacs/custom/fcuny-eshell.el
+++ b/emacs/custom/fcuny-eshell.el
@@ -1,5 +1,53 @@
+(defun fcuny/eshell-mode-setup ()
+  (eshell/alias "e" "find-file $1")
+  (eshell/alias "emacs" "find-file $1")
+  (eshell/alias "ee" "find-file-other-window $1")
+
+  (eshell/alias "gs" "magit-status")
+  (eshell/alias "gd" "magit-diff-unstaged")
+  (eshell/alias "gds" "magit-diff-staged")
+  (eshell/alias "d" "dired $1"))
+
+(defun eshell/gst (&rest args)
+  (magit-status (pop args) nil)
+  (eshell/echo))   ;; The echo command suppresses output
+
+(defun fcuny/eshell-here ()
+  "Opens up a new shell in the directory associated with the current
+buffer's file. The eshell is renamed to match that directory to make
+multiple eshell windows easier."
+  (interactive)
+  (let* ((height (/ (window-total-height) 3)))
+    (split-window-vertically (- height))
+    (other-window 1)
+    (eshell "new")
+    (insert (concat "ls"))
+    (eshell-send-input)))
+
+;; eshell time and notification
+(defvar-local eshell-current-command-start-time nil)
+
+;; https://www.birkey.co/2021-06-20-why-eshell-part-1.html
+(defun fcuny/eshell-current-command-start ()
+  (setq eshell-current-command-start-time (current-time)))
+
+(defun fcuny/eshell-current-command-stop ()
+  (when eshell-current-command-start-time
+    (let ((elapsed-time (float-time
+			             (time-subtract (current-time)
+					                    eshell-current-command-start-time))))
+      (if (> elapsed-time 5)
+          (eshell-interactive-print
+	       (format "Finished in: %.0fs\n" elapsed-time)))))
+    (setq eshell-current-command-start-time nil))
+
+(defun fcuny/eshell-current-command-time-track ()
+  (add-hook 'eshell-pre-command-hook #'fcuny/eshell-current-command-start nil t)
+  (add-hook 'eshell-post-command-hook #'fcuny/eshell-current-command-stop nil t))
+
 (use-package eshell
-  :hook (eshell-mode . fcuny/eshell-mode-setup)
+  :hook ((eshell-mode . fcuny/eshell-mode-setup)
+         (eshell-mode . fcuny/eshell-current-command-time-track))
   :bind (("C-c e h" . fcuny/eshell-here))
   :custom
   (eshell-scroll-to-bottom-on-input 'all)
@@ -7,33 +55,6 @@
   (eshell-hist-ignoredups t)
   (eshell-save-history-on-exit t)
   (eshell-prefer-lisp-functions nil)
-  (eshell-destroy-buffer-when-process-dies t)
-
-  :init
-  (defun fcuny/eshell-mode-setup ()
-    (eshell/alias "e" "find-file $1")
-    (eshell/alias "emacs" "find-file $1")
-    (eshell/alias "ee" "find-file-other-window $1")
-
-    (eshell/alias "gs" "magit-status")
-    (eshell/alias "gd" "magit-diff-unstaged")
-    (eshell/alias "gds" "magit-diff-staged")
-    (eshell/alias "d" "dired $1"))
-
-  (defun eshell/gst (&rest args)
-    (magit-status (pop args) nil)
-    (eshell/echo))   ;; The echo command suppresses output
-
-  (defun fcuny/eshell-here ()
-    "Opens up a new shell in the directory associated with the current
-buffer's file. The eshell is renamed to match that directory to make
-multiple eshell windows easier."
-    (interactive)
-    (let* ((height (/ (window-total-height) 3)))
-      (split-window-vertically (- height))
-      (other-window 1)
-      (eshell "new")
-      (insert (concat "ls"))
-      (eshell-send-input))))
+  (eshell-destroy-buffer-when-process-dies t))
 
 (provide 'fcuny-eshell)