Skip to content

Instantly share code, notes, and snippets.

@igorepst
Created November 29, 2022 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igorepst/db9ee3c62664ea06f011d5671715ac33 to your computer and use it in GitHub Desktop.
Save igorepst/db9ee3c62664ea06f011d5671715ac33 to your computer and use it in GitHub Desktop.
Multiline Eshell prompt
(add-hook 'eshell-mode-hook (lambda ()
(add-hook 'eshell-pre-command-hook #'ig-eshell-pre-command nil t)
(add-hook 'eshell-post-command-hook #'ig-eshell-post-command nil t)))
(with-eval-after-load 'eshell
(defvar-local ig-eshell-last-command-status 0)
(defvar-local ig-eshell-last-command-start-time nil)
(defvar-local ig-eshell-last-command-time nil)
(defconst ig-color-blue "#3465A4" "Blue color.")
(defconst ig-color-bright-blue "#729FCF" "Bright blue color.")
(defconst ig-color-red "#CC0000" "Red color.")
(defconst ig-color-green "#4E9A06" "Green color.")
(setq eshell-highlight-prompt nil
eshell-prompt-regexp "^[#❯] "
eshell-prompt-function #'ig-eshell-prompt)
(defun ig-with-face (str &rest fpl)
"Propertize STR with FPL."
(propertize str 'face fpl))
(defun ig-eshell-pre-command()
"Run on Eshell pre command."
(setq ig-eshell-last-command-start-time (current-time)))
(defun ig-eshell-post-command()
"Run on Eshell post command."
(if ig-eshell-last-command-start-time
(setq ig-eshell-last-command-time
(let ((last-cmd-time (float-time (time-subtract (current-time) ig-eshell-last-command-start-time))))
(if (> 1 last-cmd-time) nil (format "%.0fs" last-cmd-time)))
ig-eshell-last-command-start-time nil)
(setq ig-eshell-last-command-time nil))
(setq ig-eshell-last-command-status eshell-last-command-status)
(eshell-interactive-print "\n"))
(defun ig-eshell-prompt()
"Define custom Eshell prompt."
(let* ((cur-dir (abbreviate-file-name (eshell/pwd)))
(prompt (concat
(ig-with-face (concat (if (string-equal cur-dir "~") " " " ") cur-dir) :weight 'bold :foreground ig-color-blue)
(ig-with-face (concat "  " (format-time-string "%H:%M:%S" (current-time))
(when ig-eshell-last-command-time (concat "  " ig-eshell-last-command-time)))
:foreground ig-color-bright-blue)
"\n"
(ig-with-face (if (zerop (user-uid)) "#" "❯")
:foreground (if (zerop ig-eshell-last-command-status) ig-color-green ig-color-red))
" ")))
(add-text-properties 0 (length prompt) '(read-only t
front-sticky (read-only)
rear-nonsticky (read-only))
prompt)
prompt)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment