imgview (4021B)
1 #!/usr/bin/env sh 2 3 # Description: Open hovered or current directory in image viewer. 4 # Generates media thumbnails with optional dependencies. 5 # 6 # Dependencies: 7 # - imv (https://github.com/eXeC64/imv) or, 8 # - sxiv (https://github.com/muennich/sxiv) or, 9 # - nsxiv (https://codeberg.org/nsxiv/nsxiv) or, 10 # - ucollage (https://github.com/ckardaris/ucollage) or, 11 # - lsix (https://github.com/hackerb9/lsix), or 12 # - viu (https://github.com/atanunq/viu), or 13 # - catimg (https://github.com/posva/catimg), or 14 # - optional: ffmpeg for audio thumbnails (album art) 15 # - optional: ffmpegthumbnailer for video thumbnails 16 # 17 # Shell: POSIX compliant 18 # Author: Arun Prakash Jana, Luuk van Baal 19 # 20 # Consider setting NNN_PREVIEWDIR to $XDG_CACHE_HOME/nnn/previews 21 # if you want to keep media thumbnails on disk between reboots. 22 NNN_PREVIEWDIR="${NNN_PREVIEWDIR:-${TMPDIR:-/tmp}/nnn/previews}" 23 24 exit_prompt() { 25 [ -n "$1" ] && printf "%s\n" "$1" 26 printf "%s" "Press any key to exit..." 27 cfg=$(stty -g); stty raw -echo; head -c 1; stty "$cfg" 28 clear 29 exit 30 } 31 32 make_thumbs() { 33 mkdir -p "$NNN_PREVIEWDIR$dir" || return 34 if [ "$1" = "viu" ] || [ "$1" = "catimg" ]; then 35 [ -d "$target" ] && exit_prompt "$1 can only display a single image" 36 mime="$(file -bL --mime-type -- "$target")" 37 case "$mime" in 38 audio/*) ffmpeg -i "$target" "$NNN_PREVIEWDIR$target.jpg" -y >/dev/null 2>&1 39 ret="$NNN_PREVIEWDIR/$target.jpg" ;; 40 video/*) ffmpegthumbnailer -i "$target" -o "$NNN_PREVIEWDIR$target.jpg" 2> /dev/null 41 ret="$NNN_PREVIEWDIR/$target.jpg" ;; 42 *) ret="$target" ;; 43 esac 44 fi 45 for file in "$dir"/*; do 46 if [ ! -f "$NNN_PREVIEWDIR$file.jpg" ]; then 47 case "$(file -bL --mime-type -- "$file")" in 48 audio/*) [ "$1" != "sxiv" ] && 49 ffmpeg -i "$file" "$NNN_PREVIEWDIR$file.jpg" -y >/dev/null 2>&1 ;; 50 video/*) [ "$1" != "ucollage" ] && 51 ffmpegthumbnailer -i "$file" -o "$NNN_PREVIEWDIR$file.jpg" 2> /dev/null ;; 52 esac 53 fi 54 done 55 for file in "$NNN_PREVIEWDIR$dir"/*; do 56 filename="$(basename "$file" .jpg)" 57 [ ! -e "$dir/$filename" ] && rm "$file" 2>/dev/null 58 done 59 } 60 61 listimages() { 62 find -L "$dir" "$NNN_PREVIEWDIR$dir" -maxdepth 1 -type f -print0 2>/dev/null | sort -z 63 } 64 65 view_files() { 66 [ -f "$target" ] && count="-n $(listimages | grep -a -m 1 -ZznF "$target" | cut -d: -f1)" 67 case "$1" in 68 nsxiv) listimages | xargs -0 nsxiv -a "${count:--t}" -- ;; 69 sxiv) listimages | xargs -0 sxiv -a "${count:--t}" -- ;; 70 imv*) listimages | xargs -0 "$1" "${count:-}" -- ;; 71 esac 72 } 73 74 target="$(readlink -f "$1")" 75 [ -d "$target" ] && dir="$target" || dir="${target%/*}" 76 if uname | grep -q "Darwin"; then 77 [ -f "$1" ] && open "$1" >/dev/null 2>&1 & 78 elif type lsix >/dev/null 2>&1; then 79 if [ -d "$target" ]; then 80 cd "$target" || exit_prompt 81 fi 82 make_thumbs lsix 83 clear 84 lsix 85 cd "$NNN_PREVIEWDIR$dir" && lsix 86 exit_prompt 87 elif type ucollage >/dev/null 2>&1; then 88 type ffmpeg >/dev/null 2>&1 && make_thumbs ucollage 89 UCOLLAGE_EXPAND_DIRS=1 ucollage "$dir" "$NNN_PREVIEWDIR$dir" || exit_prompt 90 elif type sxiv >/dev/null 2>&1; then 91 type ffmpegthumbnailer >/dev/null 2>&1 && make_thumbs sxiv 92 view_files sxiv >/dev/null 2>&1 & 93 elif type nsxiv >/dev/null 2>&1; then 94 type ffmpegthumbnailer >/dev/null 2>&1 && make_thumbs sxiv 95 view_files nsxiv >/dev/null 2>&1 & 96 elif type imv >/dev/null 2>&1; then 97 make_thumbs imv 98 view_files imv >/dev/null 2>&1 & 99 elif type imvr >/dev/null 2>&1; then 100 make_thumbs imv 101 view_files imvr >/dev/null 2>&1 & 102 elif type viu >/dev/null 2>&1; then 103 clear 104 make_thumbs viu 105 viu -n "$ret" 106 exit_prompt 107 elif type catimg >/dev/null 2>&1; then 108 make_thumbs catimg 109 catimg "$ret" 110 exit_prompt 111 else 112 exit_prompt "Please install sxiv/nsxiv/imv/viu/catimg/lsix." 113 fi