Eshell: cat can cat images like catimg, in addition to syntax highlight.

Big thanks to the chads at https://emacs.stackexchange.com/questions/3432/display-images-in-eshell-with-iimage-mode
This commit is contained in:
MitchMarq42 2022-11-19 17:13:58 -09:00
parent 0e13705081
commit af2974fe03

View File

@ -203,15 +203,54 @@ Taken from https://github.com/manateelazycat/aweshell/blob/d246df619573ca3f46070
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 arguments.
"
"Wrapper around `aweshell-cat-with-syntax-highlight' for multiple ARGS.
Also, can cat images for some reason.
Taken from 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))
(aweshell-cat-with-syntax-highlight file)))))
(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
:after eshell
:custom