add all files back. i broke this repo whoops

This commit is contained in:
MitchMarq42 2024-01-20 08:30:34 -09:00
commit 60cbc75cd4

98
eshell-dat.el Normal file
View File

@ -0,0 +1,98 @@
;;; eshell-dat.el --- A better `cat' for Eshell -*- lexical-binding: t -*-
;;; Commentary:
;; An alternative to `eshell/cat'. If you `dat' an image file it will be
;; displayed as a thumbnail in the terminal similar to Kitty's "icat" or the
;; "catimg" command. If you Dat a file it will be shown with syntax
;; highlighting, sort of like how the "bat" command can be configured.
;; This is mostly lifted directly from
;; https://emacs.stackexchange.com/questions/3432/display-images-in-eshell-with-iimage-mode
;; and also a bit from https://github.com/manateelazycat/aweshell/blob/d246df619573ca3f46070cc0ac82d024271ed243/aweshell.el#L775
;; . However, I have gone ahead and cleaned up function names, et cetera.
;;; Code:
(defvar em-dat--old-eshell/cat (symbol-function 'eshell/cat))
(defvar em-dat--right-pad 50
"Padding to use on the right of images displayed by `dat'.")
(defun em-dat--max-width (window)
(- (window-width window t) em-dat--right-pad))
(defun em-dat--txtfile-print (filename)
"Propertize the contents of FILENAME and output to terminal."
(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))
(defun em-dat--imagep (filename)
"Check if FILENAME is an image."
(let ((extension (file-name-extension filename))
(image-extensions '("png" "jpg" "bmp")))
(member extension image-extensions)))
(defun em-dat--image-width (filename)
"Get the width of the image FILENAME, using imagemagick."
(string-to-number
(shell-command-to-string
(format "convert '%s' -ping -format \"%%w\" info:" filename))))
(defun em-dat--rescale-image (filename)
"Rescale image FILENAME to a maximum width, or leave untouched if already
small. Returns the new file path."
(let ((file (make-temp-file "resized_emacs"))
(max-width (em-dat--max-width (get-buffer-window (current-buffer)))))
(if (> (em-dat--image-width filename) max-width)
(progn
(shell-command-to-string
(format "convert -resize %dx '%s' '%s'" max-width filename file))
file)
filename)))
(defun em-dat--image-print (file)
"Print the single image FILE."
(eshell/printnl (propertize " " 'display (create-image file))))
(defun em-dat--videop (filename)
"Check if FILENAME is a video."
(let ((extension (file-name-extension filename))
(image-extensions '("mp4" "mov" "mkv")))
(member extension image-extensions)))
(defun em-dat--vid2img (filename)
"Given video FILENAME, return the path to a newly created thumbnail image."
(let* ((width (em-dat--max-width (get-buffer-window (current-buffer))))
(new-fpath (make-temp-file "thumbnail_emacs")))
(shell-command-to-string
(format "ffmpegthumbnailer -i '%s' -f -s '%s' -o '%s'"
filename
width
new-fpath))
new-fpath))
(defun eshell/dat (&rest args)
"Wrapper around `em-dat--txtfile-print' for multiple ARGS.
Also, can cat images for some reason."
(setq args (eshell-stringify-list (flatten-tree args)))
(dolist (file args)
(if (string= file "-")
(apply 'em-dat--old-eshell/cat file)
(cond ((em-dat--imagep file) (em-dat--image-print
(em-dat--rescale-image file)))
((em-dat--videop file) (em-dat--image-print
(em-dat--vid2img file)))
(t (em-dat--txtfile-print file))))))
(put 'eshell/dat 'eshell-no-numeric-conversions t)
(put 'eshell/dat 'eshell-filename-arguments t)
(provide 'eshell-dat)
;;; eshell-dat.el ends here