From af2974fe03fd0566094fd5c07e4a012574f66ff0 Mon Sep 17 00:00:00 2001 From: MitchMarq42 Date: Sat, 19 Nov 2022 17:13:58 -0900 Subject: [PATCH] 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 --- lisp/mitch-packages.el | 45 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/lisp/mitch-packages.el b/lisp/mitch-packages.el index c345da0..8c78d72 100644 --- a/lisp/mitch-packages.el +++ b/lisp/mitch-packages.el @@ -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