imgview (3929B)
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://github.com/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" -eq 3 ]; then 35 [ -d "$target" ] && exit_prompt "$2 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" -ne 0 ] && ffmpeg -i "$file" "$NNN_PREVIEWDIR$file.jpg" -y >/dev/null 2>&1 ;; 49 video/*) [ "$1" -ne 1 ] && ffmpegthumbnailer -i "$file" -o "$NNN_PREVIEWDIR$file.jpg" 2> /dev/null ;; 50 esac 51 fi 52 done 53 for file in "$NNN_PREVIEWDIR$dir"/*; do 54 filename="$(basename "$file" .jpg)" 55 [ ! -e "$dir/$filename" ] && rm "$file" 2>/dev/null 56 done 57 } 58 59 listimages() { 60 find -L "$dir" "$NNN_PREVIEWDIR$dir" -maxdepth 1 -type f -print0 2>/dev/null | sort -z 61 } 62 63 view_files() { 64 [ -f "$target" ] && count="-n $(listimages | grep -a -m 1 -ZznF "$target" | cut -d: -f1)" 65 case "$1" in 66 nsxiv) listimages | xargs -0 nsxiv -a "${count:--t}" -- ;; 67 sxiv) listimages | xargs -0 sxiv -a "${count:--t}" -- ;; 68 imv*) listimages | xargs -0 "$1" "${count:-}" -- ;; 69 esac 70 } 71 72 target="$(readlink -f "$1")" 73 [ -d "$target" ] && dir="$target" || dir="${target%/*}" 74 if uname | grep -q "Darwin"; then 75 [ -f "$1" ] && open "$1" >/dev/null 2>&1 & 76 elif type lsix >/dev/null 2>&1; then 77 if [ -d "$target" ]; then 78 cd "$target" || exit_prompt 79 fi 80 make_thumbs "" 81 clear 82 lsix 83 cd "$NNN_PREVIEWDIR$dir" && lsix 84 exit_prompt 85 elif type ucollage >/dev/null 2>&1; then 86 type ffmpeg >/dev/null 2>&1 && make_thumbs 1 87 UCOLLAGE_EXPAND_DIRS=1 ucollage "$dir" "$NNN_PREVIEWDIR$dir" || exit_prompt 88 elif type sxiv >/dev/null 2>&1; then 89 type ffmpegthumbnailer >/dev/null 2>&1 && make_thumbs 0 90 view_files sxiv >/dev/null 2>&1 & 91 elif type nsxiv >/dev/null 2>&1; then 92 type ffmpegthumbnailer >/dev/null 2>&1 && make_thumbs 0 93 view_files nsxiv >/dev/null 2>&1 & 94 elif type imv >/dev/null 2>&1; then 95 make_thumbs "" 96 view_files imv >/dev/null 2>&1 & 97 elif type imvr >/dev/null 2>&1; then 98 make_thumbs "" 99 view_files imvr >/dev/null 2>&1 & 100 elif type viu >/dev/null 2>&1; then 101 clear 102 make_thumbs 3 viu 103 viu -n "$ret" 104 exit_prompt 105 elif type catimg >/dev/null 2>&1; then 106 make_thumbs 3 catimg 107 catimg "$ret" 108 exit_prompt 109 else 110 exit_prompt "Please install sxiv/nsxiv/imv/viu/catimg/lsix." 111 fi