nnn/plugins/imgview

118 lines
3.3 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-05-30 19:33:48 +00:00
# Supported image viewers open in thumbnail mode when
# the hovered entry is a directory.
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
2020-01-02 18:57:28 +00:00
2021-05-30 18:11:54 +00:00
target="$(readlink -f "$1")"
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
nthumb_cleanup() {
[ -n "$!" ] && while [ -e /proc/"$!" ]; do sleep 1; done
if [ -f "$target" ]; then
rm -r "$(dirname "$target")"/.nthumbs
elif [ -d "$target" ]; then
rm -r "$target"/.nthumbs
fi &
}
2020-03-03 00:46:52 +00:00
2021-05-30 18:11:54 +00:00
make_thumbs() {
2021-06-03 13:09:20 +00:00
if [ -d "$target" ]; then
[ "$1" -eq 4 ] && exit_prompt "$3 can only display a single image"
cd "$target" || exit_prompt
fi
2021-05-30 18:11:54 +00:00
if mkdir .nthumbs >/dev/null; then
thumbs=1
2020-03-03 00:46:52 +00:00
else
2021-05-30 18:11:54 +00:00
exit_prompt
2020-03-03 00:46:52 +00:00
fi
2021-06-03 13:09:20 +00:00
if [ "$1" -eq 4 ]; then
mime="$(file -bL --mime-type -- "$2")"
case "$mime" in
audio/*) ffmpeg -i "$2" ".nthumbs/${2%%.*}.jpg" -y >/dev/null 2>&1
ret=".nthumbs/${2%%.*}.jpg" ;;
video/*) ffmpegthumbnailer -i "$2" -o .nthumbs/"${2%%.*}".jpg 2> /dev/null
ret=".nthumbs/${2%%.*}.jpg" ;;
*) ret="$2" ;;
esac
fi
2021-05-30 18:11:54 +00:00
for file in *; do
case "$(file -bL --mime-type -- "$file")" in
2021-06-03 13:09:20 +00:00
audio/*) [ "$1" -ne 0 ] && ffmpeg -i "$file" ".nthumbs/${file%%.*}.jpg" -y >/dev/null 2>&1 ;;
video/*) [ "$1" -ne 1 ] && ffmpegthumbnailer -i "$file" -o .nthumbs/"${file%%.*}".jpg 2> /dev/null ;;
2021-05-30 18:11:54 +00:00
esac
done
2020-03-03 00:46:52 +00:00
}
2021-06-03 13:09:20 +00:00
view_files() {
find . .nthumbs -maxdepth 1 -type f -print0 | xargs -0 "$@" --
2021-05-30 18:11:54 +00:00
}
2020-01-02 18:57:28 +00:00
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-06-03 13:09:20 +00:00
cd .nthumbs && lsix
2021-05-30 18:11:54 +00:00
nthumb_cleanup
exit_prompt
2021-05-30 19:33:48 +00:00
elif type ucollage >/dev/null 2>&1; then
2021-06-03 13:09:20 +00:00
make_thumbs 1
if ! UCOLLAGE_EXPAND_DIRS=1 ucollage . .nthumbs; then
nthumb_cleanup && exit_prompt
fi
2021-05-30 19:33:48 +00:00
nthumb_cleanup
exit
elif type sxiv >/dev/null 2>&1; then
2021-06-03 13:09:20 +00:00
make_thumbs 0
2021-05-30 18:11:54 +00:00
if [ -f "$target" ]; then
2021-06-27 15:11:39 +00:00
view_files sxiv -a >/dev/null 2>&1 &
2021-05-30 18:11:54 +00:00
elif [ -d "$target" ]; then
2021-06-03 13:09:20 +00:00
view_files sxiv -aqt >/dev/null 2>&1 &
2021-05-30 18:11:54 +00:00
fi
elif type imv >/dev/null 2>&1; then
2021-06-03 13:09:20 +00:00
make_thumbs 3
view_files imv >/dev/null 2>&1 &
2021-05-30 18:11:54 +00:00
elif type imvr >/dev/null 2>&1; then
2021-06-03 13:09:20 +00:00
make_thumbs 3
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-06-03 13:09:20 +00:00
make_thumbs 4 "$1" viu
viu -n "$ret"
nthumb_cleanup
2021-05-30 18:11:54 +00:00
exit_prompt
elif type catimg >/dev/null 2>&1; then
2021-06-03 13:09:20 +00:00
make_thumbs 4 "$1" catimg
catimg "$ret"
nthumb_cleanup
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
2021-06-03 13:09:20 +00:00
[ "$thumbs" -ne 0 ] && nthumb_cleanup &