nnn/plugins/imgview

107 lines
3.6 KiB
Plaintext
Raw Normal View History

2020-01-02 18:57:28 +00:00
#!/usr/bin/env sh
2021-05-30 18:11:54 +00:00
# Description: Open hovered or current directory in image viewer.
2021-07-01 00:26:47 +00:00
# Generates media thumbnails with optional dependencies.
2021-05-15 17:32:01 +00:00
#
# Dependencies:
# - imv (https://github.com/eXeC64/imv) or,
# - sxiv (https://github.com/muennich/sxiv) or,
2021-05-30 19:33:48 +00:00
# - ucollage (https://github.com/ckardaris/ucollage) or,
# - lsix (https://github.com/hackerb9/lsix), or
2021-05-30 18:11:54 +00:00
# - viu (https://github.com/atanunq/viu), or
# - catimg (https://github.com/posva/catimg), or
2021-05-30 19:33:48 +00:00
# - optional: ffmpeg for audio thumbnails (album art)
2021-06-03 13:09:20 +00:00
# - optional: ffmpegthumbnailer for video thumbnails
2020-01-02 18:57:28 +00:00
#
# Shell: POSIX compliant
2021-05-30 18:11:54 +00:00
# Author: Arun Prakash Jana, Luuk van Baal
2021-07-01 00:26:47 +00:00
#
# Consider setting NNN_PREVIEWDIR to $XDG_CACHE_HOME/nnn/previews
# if you want to keep media thumbnails on disk between reboots.
NNN_PREVIEWDIR="${NNN_PREVIEWDIR:-${TMPDIR:-/tmp}/nnn/previews}"
2020-03-03 00:46:52 +00:00
2021-05-30 18:11:54 +00:00
exit_prompt() {
[ -n "$1" ] && printf "%s\n" "$1"
printf "%s" "Press any key to exit..."
cfg=$(stty -g); stty raw -echo; head -c 1; stty "$cfg"
clear
exit
2020-03-03 00:46:52 +00:00
}
2021-05-30 18:11:54 +00:00
make_thumbs() {
2021-07-01 00:26:47 +00:00
mkdir -p "$NNN_PREVIEWDIR$dir" || return
if [ "$1" -eq 3 ]; then
[ -d "$target" ] && exit_prompt "$2 can only display a single image"
mime="$(file -bL --mime-type -- "$target")"
2021-06-03 13:09:20 +00:00
case "$mime" in
2021-07-01 00:26:47 +00:00
audio/*) ffmpeg -i "$target" "$NNN_PREVIEWDIR$target.jpg" -y >/dev/null 2>&1
ret="$NNN_PREVIEWDIR/$target.jpg" ;;
video/*) ffmpegthumbnailer -i "$target" -o "$NNN_PREVIEWDIR$target.jpg" 2> /dev/null
ret="$NNN_PREVIEWDIR/$target.jpg" ;;
*) ret="$target" ;;
2021-06-03 13:09:20 +00:00
esac
fi
2021-07-01 00:26:47 +00:00
for file in "$dir"/*; do
if [ ! -f "$NNN_PREVIEWDIR$file.jpg" ]; then
case "$(file -bL --mime-type -- "$file")" in
audio/*) [ "$1" -ne 0 ] && ffmpeg -i "$file" "$NNN_PREVIEWDIR$file.jpg" -y >/dev/null 2>&1 ;;
video/*) [ "$1" -ne 1 ] && ffmpegthumbnailer -i "$file" -o "$NNN_PREVIEWDIR$file.jpg" 2> /dev/null ;;
esac
fi
2021-05-30 18:11:54 +00:00
done
2021-07-01 00:26:47 +00:00
for file in "$NNN_PREVIEWDIR$dir"/*; do
filename="$(basename "$file" .jpg)"
[ ! -e "$dir/$filename" ] && rm "$file" 2>/dev/null
done
}
listimages() {
find -L "$dir" "$NNN_PREVIEWDIR$dir" -maxdepth 1 -type f -print0 2>/dev/null | sort -z
2020-03-03 00:46:52 +00:00
}
2021-06-03 13:09:20 +00:00
view_files() {
2021-07-01 00:26:47 +00:00
[ -f "$target" ] && count="-n $(listimages | grep -a -m 1 -ZznF "$target" | cut -d: -f1)"
case "$1" in
sxiv) listimages | xargs -0 sxiv -a "${count:--t}" -- ;;
imv*) listimages | xargs -0 "$1" "${count:-}" -- ;;
esac
2021-05-30 18:11:54 +00:00
}
2020-01-02 18:57:28 +00:00
2021-07-01 00:26:47 +00:00
target="$(readlink -f "$1")"
[ -d "$target" ] && dir="$target" || dir="${target%/*}"
if uname | grep -q "Darwin"; then
2021-05-30 18:11:54 +00:00
[ -f "$1" ] && open "$1" >/dev/null 2>&1 &
elif type lsix >/dev/null 2>&1; then
if [ -d "$target" ]; then
cd "$target" || exit_prompt
2020-06-10 20:19:40 +00:00
fi
2021-06-03 13:09:20 +00:00
make_thumbs ""
2021-05-30 18:11:54 +00:00
clear
lsix
2021-07-01 00:26:47 +00:00
cd "$NNN_PREVIEWDIR$dir" && lsix
2021-05-30 18:11:54 +00:00
exit_prompt
2021-05-30 19:33:48 +00:00
elif type ucollage >/dev/null 2>&1; then
2021-07-01 00:26:47 +00:00
type ffmpeg >/dev/null 2>&1 && make_thumbs 1
2021-07-01 09:23:57 +00:00
UCOLLAGE_EXPAND_DIRS=1 ucollage "$dir" "$NNN_PREVIEWDIR$dir" || exit_prompt
elif type sxiv >/dev/null 2>&1; then
2021-07-01 00:26:47 +00:00
type ffmpegthumbnailer >/dev/null 2>&1 && make_thumbs 0
view_files sxiv >/dev/null 2>&1 &
2021-05-30 18:11:54 +00:00
elif type imv >/dev/null 2>&1; then
2021-07-01 00:26:47 +00:00
make_thumbs ""
2021-06-03 13:09:20 +00:00
view_files imv >/dev/null 2>&1 &
2021-05-30 18:11:54 +00:00
elif type imvr >/dev/null 2>&1; then
2021-07-01 00:26:47 +00:00
make_thumbs ""
2021-06-03 13:09:20 +00:00
view_files imvr >/dev/null 2>&1 &
elif type viu >/dev/null 2>&1; then
2021-05-30 18:11:54 +00:00
clear
2021-07-01 00:26:47 +00:00
make_thumbs 3 viu
2021-06-03 13:09:20 +00:00
viu -n "$ret"
2021-05-30 18:11:54 +00:00
exit_prompt
elif type catimg >/dev/null 2>&1; then
2021-07-01 00:26:47 +00:00
make_thumbs 3 catimg
2021-06-03 13:09:20 +00:00
catimg "$ret"
2021-05-30 18:11:54 +00:00
exit_prompt
2020-01-02 18:57:28 +00:00
else
2021-05-30 18:11:54 +00:00
exit_prompt "Please install sxiv/imv/viu/catimg/lsix."
2020-01-02 18:57:28 +00:00
fi