nnn/plugins/imgview

109 lines
2.9 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: ffmpegthumbnailer for video thumbnails
# - optional: ffmpeg for audio thumbnails (album art)
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() {
if [ -d "$target" ]; then cd "$target" || exit_prompt; fi
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-05-30 18:11:54 +00:00
for file in *; do
case "$(file -bL --mime-type -- "$file")" in
image/*) ln -s "$PWD/$file" "$PWD/.nthumbs/$file" ;;
audio/*) ffmpeg -i "$file" ".nthumbs/${file%%.*}.jpg" -y >/dev/null 2>&1;;
video/*) ffmpegthumbnailer -i "$file" -o .nthumbs/"${file%%.*}".jpg 2> /dev/null ;;
esac
done
cd .nthumbs || return
2020-03-03 00:46:52 +00:00
}
2021-05-30 18:11:54 +00:00
view_dir() {
find . -maxdepth 0 -print0 | xargs -0 "$1" --
}
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-05-30 18:11:54 +00:00
make_thumbs
clear
lsix
nthumb_cleanup
exit_prompt
2021-05-30 19:33:48 +00:00
elif type ucollage >/dev/null 2>&1; then
make_thumbs
ucollage
nthumb_cleanup
exit
elif type sxiv >/dev/null 2>&1; then
2021-05-30 18:11:54 +00:00
make_thumbs
if [ -f "$target" ]; then
view_dir sxiv >/dev/null 2>&1 &
elif [ -d "$target" ]; then
sxiv -aqt . >/dev/null 2>&1 &
fi
elif type imv >/dev/null 2>&1; then
make_thumbs
if [ -f "$target" ]; then
view_dir imv >/dev/null 2>&1 &
elif [ -d "$target" ]; then
imv . >/dev/null 2>&1 &
fi
elif type imvr >/dev/null 2>&1; then
make_thumbs
if [ -f "$target" ]; then
view_dir imvr >/dev/null 2>&1 &
elif [ -d "$target" ]; then
imvr . >/dev/null 2>&1 &
2020-01-02 18:57:28 +00:00
fi
elif type viu >/dev/null 2>&1; then
2021-05-30 18:11:54 +00:00
clear
viu -n "$1"
exit_prompt
elif type catimg >/dev/null 2>&1; then
clear
catimg "$1"
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-05-30 18:11:54 +00:00
[ -n "$thumbs" ] && nthumb_cleanup &