eshell: externalize cat to dat and use an alias

This commit is contained in:
MitchMarq42 2022-11-29 15:59:12 -09:00
parent 4b31e4c769
commit 67c5128c33
2 changed files with 5 additions and 70 deletions

View File

@ -1 +1,3 @@
alias catimg dat $*
alias dat cat $*
alias clear clear-scrollback alias clear clear-scrollback

View File

@ -180,76 +180,9 @@ This is taken from a website that I can't remember at the moment."
;; :hook (eshell-mode . eshell-vterm-mode)) ;; :hook (eshell-mode . eshell-vterm-mode))
(use-package eshell-syntax-highlighting (use-package eshell-syntax-highlighting
:after eshell :after eshell
:hook (eshell-mode . eshell-syntax-highlighting-mode) :hook (eshell-mode . eshell-syntax-highlighting-mode))
:config (use-package eshell-dat
;; Make cat with syntax highlight. :straight (:type git :repo "https://git.mitchmarq42.xyz/mitch/eshell-dat"))
(defun aweshell-cat-with-syntax-highlight (filename)
"Like cat(1) but with syntax highlighting.
Taken from https://github.com/manateelazycat/aweshell/blob/d246df619573ca3f46070cc0ac82d024271ed243/aweshell.el#L775"
(let ((existing-buffer (get-file-buffer filename))
(buffer (find-file-noselect filename)))
(eshell-print
(with-current-buffer buffer
(if (fboundp 'font-lock-ensure)
(font-lock-ensure)
(with-no-warnings
(font-lock-fontify-buffer)))
(let ((contents (buffer-string)))
(remove-text-properties 0 (length contents) '(read-only nil) contents)
contents)))
(unless existing-buffer
(kill-buffer buffer)) nil))
;; helpers for catimg below
(defun esh-catimg--imagep (filename)
"Check if FILENAME is an image. Helper for `esh-catimg--image-print'.
Taken from https://emacs.stackexchange.com/questions/3432/display-images-in-eshell-with-iimage-mode "
(let ((extension (file-name-extension filename))
(image-extensions '("png" "jpg" "bmp")))
(member extension image-extensions)))
(defun esh-catimg--image-width (filename)
"Get the pixel (?) width of the image FILENAME, using imagemagick. Helper
for `esh-catimg--image-print'.
Taken from https://emacs.stackexchange.com/questions/3432/display-images-in-eshell-with-iimage-mode "
(string-to-number
(shell-command-to-string
(format "convert '%s' -ping -format \"%%w\" info:" filename))))
(defun esh-catimg--rescale-image (filename)
"Rescale an image to a maximum width, or leave untouched if already small.
Returns the new file path. Helper for `esh-catimg--image-print'.
Taken from https://emacs.stackexchange.com/questions/3432/display-images-in-eshell-with-iimage-mode "
(let ((file (make-temp-file "resized_emacs"))
(max-width 350))
(if (> (esh-catimg--image-width filename) max-width)
(progn
(shell-command-to-string
(format "convert -resize %dx '%s' '%s'" max-width filename file))
file)
filename)))
(defun esh-catimg--image-print (file)
"Print the single image FILE.
Taken from https://emacs.stackexchange.com/questions/3432/display-images-in-eshell-with-iimage-mode "
(eshell/printnl (propertize " " 'display (create-image file))))
(defun eshell/cat (&rest args)
"Wrapper around `aweshell-cat-with-syntax-highlight' for multiple ARGS.
Also, can cat images for some reason.
See:
https://github.com/manateelazycat/aweshell/blob/d246df619573ca3f46070cc0ac82d024271ed243/aweshell.el#L775
https://emacs.stackexchange.com/questions/3432/display-images-in-eshell-with-iimage-mode "
(setq args (eshell-stringify-list (flatten-tree args)))
(dolist (file args)
(if (string= file "-")
(throw 'eshell-external
(eshell-external-command "cat" args))
(if (esh-catimg--imagep file)
(esh-catimg--image-print (esh-catimg--rescale-image file))
(aweshell-cat-with-syntax-highlight file))))))
(use-package eshell-prompt-extras (use-package eshell-prompt-extras
:after eshell :after eshell
:custom :custom