Merge pull request #1084 from luukvbaal/imgview

Imgview open hovered image and reuse NNN_PREVIEWDIR
This commit is contained in:
Piña Colada 2021-07-01 14:36:07 +05:30 committed by GitHub
commit a7603ba1f2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,8 +1,7 @@
#!/usr/bin/env sh #!/usr/bin/env sh
# Description: Open hovered or current directory in image viewer. # Description: Open hovered or current directory in image viewer.
# Supported image viewers open in thumbnail mode when # Generates media thumbnails with optional dependencies.
# the hovered entry is a directory.
# #
# Dependencies: # Dependencies:
# - imv (https://github.com/eXeC64/imv) or, # - imv (https://github.com/eXeC64/imv) or,
@ -16,8 +15,10 @@
# #
# Shell: POSIX compliant # Shell: POSIX compliant
# Author: Arun Prakash Jana, Luuk van Baal # Author: Arun Prakash Jana, Luuk van Baal
#
target="$(readlink -f "$1")" # 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}"
exit_prompt() { exit_prompt() {
[ -n "$1" ] && printf "%s\n" "$1" [ -n "$1" ] && printf "%s\n" "$1"
@ -27,47 +28,47 @@ exit_prompt() {
exit exit
} }
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 &
}
make_thumbs() { make_thumbs() {
if [ -d "$target" ]; then mkdir -p "$NNN_PREVIEWDIR$dir" || return
[ "$1" -eq 4 ] && exit_prompt "$3 can only display a single image" if [ "$1" -eq 3 ]; then
cd "$target" || exit_prompt [ -d "$target" ] && exit_prompt "$2 can only display a single image"
fi mime="$(file -bL --mime-type -- "$target")"
if mkdir .nthumbs >/dev/null; then
thumbs=1
else
exit_prompt
fi
if [ "$1" -eq 4 ]; then
mime="$(file -bL --mime-type -- "$2")"
case "$mime" in case "$mime" in
audio/*) ffmpeg -i "$2" ".nthumbs/${2%%.*}.jpg" -y >/dev/null 2>&1 audio/*) ffmpeg -i "$target" "$NNN_PREVIEWDIR$target.jpg" -y >/dev/null 2>&1
ret=".nthumbs/${2%%.*}.jpg" ;; ret="$NNN_PREVIEWDIR/$target.jpg" ;;
video/*) ffmpegthumbnailer -i "$2" -o .nthumbs/"${2%%.*}".jpg 2> /dev/null video/*) ffmpegthumbnailer -i "$target" -o "$NNN_PREVIEWDIR$target.jpg" 2> /dev/null
ret=".nthumbs/${2%%.*}.jpg" ;; ret="$NNN_PREVIEWDIR/$target.jpg" ;;
*) ret="$2" ;; *) ret="$target" ;;
esac esac
fi fi
for file in *; do for file in "$dir"/*; do
case "$(file -bL --mime-type -- "$file")" in if [ ! -f "$NNN_PREVIEWDIR$file.jpg" ]; then
audio/*) [ "$1" -ne 0 ] && ffmpeg -i "$file" ".nthumbs/${file%%.*}.jpg" -y >/dev/null 2>&1 ;; case "$(file -bL --mime-type -- "$file")" in
video/*) [ "$1" -ne 1 ] && ffmpegthumbnailer -i "$file" -o .nthumbs/"${file%%.*}".jpg 2> /dev/null ;; audio/*) [ "$1" -ne 0 ] && ffmpeg -i "$file" "$NNN_PREVIEWDIR$file.jpg" -y >/dev/null 2>&1 ;;
esac video/*) [ "$1" -ne 1 ] && ffmpegthumbnailer -i "$file" -o "$NNN_PREVIEWDIR$file.jpg" 2> /dev/null ;;
esac
fi
done
for file in "$NNN_PREVIEWDIR$dir"/*; do
filename="$(basename "$file" .jpg)"
[ ! -e "$dir/$filename" ] && rm "$file" 2>/dev/null
done done
} }
view_files() { listimages() {
find . .nthumbs -maxdepth 1 -type f -print0 | xargs -0 "$@" -- find -L "$dir" "$NNN_PREVIEWDIR$dir" -maxdepth 1 -type f -print0 2>/dev/null | sort -z
} }
view_files() {
[ -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
}
target="$(readlink -f "$1")"
[ -d "$target" ] && dir="$target" || dir="${target%/*}"
if uname | grep -q "Darwin"; then if uname | grep -q "Darwin"; then
[ -f "$1" ] && open "$1" >/dev/null 2>&1 & [ -f "$1" ] && open "$1" >/dev/null 2>&1 &
elif type lsix >/dev/null 2>&1; then elif type lsix >/dev/null 2>&1; then
@ -77,41 +78,29 @@ elif type lsix >/dev/null 2>&1; then
make_thumbs "" make_thumbs ""
clear clear
lsix lsix
cd .nthumbs && lsix cd "$NNN_PREVIEWDIR$dir" && lsix
nthumb_cleanup
exit_prompt exit_prompt
elif type ucollage >/dev/null 2>&1; then elif type ucollage >/dev/null 2>&1; then
make_thumbs 1 type ffmpeg >/dev/null 2>&1 && make_thumbs 1
if ! UCOLLAGE_EXPAND_DIRS=1 ucollage . .nthumbs; then UCOLLAGE_EXPAND_DIRS=1 ucollage . "$NNN_PREVIEWDIR$dir" || exit_prompt
nthumb_cleanup && exit_prompt
fi
nthumb_cleanup
exit
elif type sxiv >/dev/null 2>&1; then elif type sxiv >/dev/null 2>&1; then
make_thumbs 0 type ffmpegthumbnailer >/dev/null 2>&1 && make_thumbs 0
if [ -f "$target" ]; then view_files sxiv >/dev/null 2>&1 &
view_files sxiv -a >/dev/null 2>&1 &
elif [ -d "$target" ]; then
view_files sxiv -aqt >/dev/null 2>&1 &
fi
elif type imv >/dev/null 2>&1; then elif type imv >/dev/null 2>&1; then
make_thumbs 3 make_thumbs ""
view_files imv >/dev/null 2>&1 & view_files imv >/dev/null 2>&1 &
elif type imvr >/dev/null 2>&1; then elif type imvr >/dev/null 2>&1; then
make_thumbs 3 make_thumbs ""
view_files imvr >/dev/null 2>&1 & view_files imvr >/dev/null 2>&1 &
elif type viu >/dev/null 2>&1; then elif type viu >/dev/null 2>&1; then
clear clear
make_thumbs 4 "$1" viu make_thumbs 3 viu
viu -n "$ret" viu -n "$ret"
nthumb_cleanup
exit_prompt exit_prompt
elif type catimg >/dev/null 2>&1; then elif type catimg >/dev/null 2>&1; then
make_thumbs 4 "$1" catimg make_thumbs 3 catimg
catimg "$ret" catimg "$ret"
nthumb_cleanup
exit_prompt exit_prompt
else else
exit_prompt "Please install sxiv/imv/viu/catimg/lsix." exit_prompt "Please install sxiv/imv/viu/catimg/lsix."
fi fi
[ "$thumbs" -ne 0 ] && nthumb_cleanup &