Created
November 29, 2022 15:12
-
-
Save igorepst/db9ee3c62664ea06f011d5671715ac33 to your computer and use it in GitHub Desktop.
Multiline Eshell prompt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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