1
0
Fork 0
dotfiles/.config/lf/preview

62 lines
3.4 KiB
Plaintext
Raw Normal View History

2022-07-06 13:21:33 +02:00
#!/usr/bin/env bash
2022-07-13 11:24:42 +02:00
image() {
2022-08-08 17:42:12 +02:00
f=$1
2022-07-13 11:24:42 +02:00
w=$2
h=$3
x=$4
y=$5
2023-02-06 19:45:35 +01:00
kitty +kitten icat --transfer-mode file --place "${w}x${h}@${x}x${y}" -- "$f" && exit 1
chafa "$f" -f symbols -s "$((w-2))x$h" && exit 1
2022-08-08 17:42:12 +02:00
echo -e "\e[31mImage previewer not installed\e[0m"
return 1
2022-07-13 11:24:42 +02:00
}
2022-07-04 21:36:33 +02:00
2022-07-13 11:24:42 +02:00
video() {
2022-08-08 17:42:12 +02:00
f=$1
w=$2
h=$3
x=$4
y=$5
thumb="$(vidthumb "$f")" || ( echo -e "\e[31mvidthumb script not in path\e[0m" && return 1 )
2022-08-08 20:00:54 +02:00
image "$thumb" "$w" "$h" "$x" "$y"
2022-08-08 17:42:12 +02:00
return 1
2022-07-13 11:24:42 +02:00
}
batorcat() {
2022-08-08 17:42:12 +02:00
f=$1
w=$2
command -v bat > /dev/null 2>&1 && bat --color=always --style=plain --pager=never --terminal-width "$((w-2))" "$f" && exit 0
command -v batcat > /dev/null 2>&1 && batcat --color=always --style=plain --pager=never --terminal-width "$((w-2))" "$f" && exit 0
2022-08-08 20:00:54 +02:00
cat "$f" && exit 0
2022-07-13 11:24:42 +02:00
}
CACHE="$HOME/.cache/lf/thumbnail.$(stat --printf '%n\0%i\0%F\0%s\0%W\0%Y' -- "$(readlink -f "$1")" | sha256sum | awk '{print $1}'))"
2022-08-08 17:42:12 +02:00
case "$(xdg-mime query filetype "$1")" in
2022-08-08 20:00:54 +02:00
*/x-bzip-compressed-tar|*/x-compressed-tar|*/x-xz-compressed-tar) als -- "$1" && exit 0 || echo -e "\e[31matools not installed\e[0m" && exit 1;;
2022-08-08 17:42:12 +02:00
*/x-7z-compressed|*/vnd.rar|*/x-tar|*/zip|*/x-java-archive|*/x-xz) 7z l "$1" | sed 1,11d && exit 0 || echo -e "\e[31m7zip not installed\e[0m" && exit 1;;
2022-08-08 20:00:54 +02:00
*/x-cd-image) iso-info --no-header -l "$1" && exit 0 || echo -e "\e[31mlibcdio not installed\e[0m" && exit 1;;
2022-08-08 17:42:12 +02:00
*/x-bittorrent) transmission-show "$1" && exit 0 || echo -e "\e[31mtransmission-cli not installed\e[0m" && exit 1;;
*/vnd.sun.xml.writer) odt2txt "$1" && exit 0 || echo -e "\e[31modt2txt not installed\e[0m" && exit 1;;
*/msword) catdoc "$1" && exit 0 || echo -e "\e[31mcatdoc not installed\e[0m" && exit 1;;
*/vnd.openxmlformats-officedocument.wordprocessingml.document) docx2txt < "$1" && exit 0 || echo -e "\e[31mdocx2txt not installed\e[0m" && exit 1;;
*/vnd.ms-excel) ssconvert --export-type=Gnumeric_stf:stf_csv "$1" "fd://1" | batorcat --language=csv && exit 0 || echo -e "\e[31mgnumeric not installed\e[0m" && exit 1;;
*/vnd.openxmlformats-officedocument.spreadsheetml.sheet) ssconvert --export-type=Gnumeric_stf:stf_csv "$1" "fd://1" | batorcat --language=csv && exit 0 || echo -e "\e[31mgnumeric not installed\e[0m" && exit 1;;
*/epub+zip) [ ! -f "$CACHE" ] && epub-thumbnailer "$1" "$CACHE" 1024; image "$CACHE" "$2" "$3" "$4" "$5" && exit 0 || echo -e "\e[31mepubthumbnailer not installed (https://github.com/marianosimone/epub-thumbnailer)\e[0m" && exit 1;;
*/pgp-encrypted) gpg -d -- "$1" && exit 0 || echo -e "\e[31mgpg not installed\e[0m" && exit 1;;
*/pkix-cert) openssl x509 -text -noout -in "$1" && exit 0 || echo -e "\e[31mopenssl not installed\e[0m" && exit 1;;
2022-08-08 20:00:54 +02:00
image/*) image "$1" "$2" "$3" "$4" "$5" ;;
video/*) video "$1" "$2" "$3" "$4" "$5" ;;
2022-08-08 17:42:12 +02:00
*opendocument*) odt2txt "$1" && exit 0 || echo -e "\e[31modt2txt not installed\e[0m" && exit 1;;
2022-08-17 17:16:52 +02:00
*/markdown) lowdown -Tterm "$1" && exit 0 || echo -e "\e[31mlowdown not installed \e[0m" && exit 1;;
2022-08-23 21:00:49 +02:00
*/html) lynx -dump "$1" && exit 0 || w3m -dump "$1" && exit 0 || echo -e "\e[31neither lynx nor w3m installed\e[0m" && exit 1;;
2022-08-08 20:00:54 +02:00
text/*|*/json|*/xml) batorcat "$1" "$2" ;;
2022-08-23 21:00:49 +02:00
*/pdf) [ ! -f "${CACHE}.jpg" ] && pdftoppm -jpeg -f 1 -singlefile "$1" "$CACHE"; image "${CACHE}.jpg" "$2" "$3" "$4" "$5" && exit 0 || echo -e "\e[31mpoppler-utils/poppler not installed\e[0m" && exit 1;;
2022-07-13 11:24:42 +02:00
esac
2022-08-08 17:42:12 +02:00
case "$(file -bL --mime-type "$1")" in
2022-08-08 20:00:54 +02:00
text/*) batorcat "$1" "$2" ;;
2022-08-02 22:35:20 +02:00
esac
xdg-mime query filetype "$1"
2022-08-08 20:00:54 +02:00
mediainfo "$1"
2022-08-08 17:42:12 +02:00
exit 1