nsxiv-rifle (1310B)
1 #!/bin/sh 2 3 TMPDIR="${TMPDIR:-/tmp}" 4 tmp="$TMPDIR/nsxiv_rifle_$$" 5 6 is_img_extension() { 7 grep -iE '\.(jpe?g|png|gif|svg|jxl|webp|tiff|heif|avif|ico|bmp|pam|pbm|ppm|tga|qoi|ff)$' 8 } 9 10 listfiles() { 11 find -L "$1" -maxdepth 1 -type f -print | 12 is_img_extension | sort | tee "$tmp" 13 } 14 15 open_img() { 16 file="$1" 17 shift 18 # only go through listfiles() if the file has a valid img extension 19 if echo "$file" | is_img_extension >/dev/null 2>&1; then 20 trap 'rm -f $tmp' EXIT 21 count="$(listfiles "///${file%/*}" | grep -nF "$file")" 22 fi 23 if [ -n "$count" ]; then 24 nsxiv -i -n "${count%%:*}" "$@" <"$tmp" 25 else 26 # fallback incase file didn't have a valid extension, or we couldn't 27 # find it inside the list 28 nsxiv "$file" "$@" 29 fi 30 } 31 32 uri2path() { 33 python3 - "$@" <<'___HEREDOC' 34 from urllib.parse import unquote, urlparse 35 from sys import argv 36 for arg in argv[1:]: 37 print(unquote(urlparse(arg).path)) 38 ___HEREDOC 39 } 40 41 file="$1" 42 shift 43 case "$file" in 44 "") 45 echo "Usage: ${0##*/} PICTURE [OPTIONS...]" >&2 46 exit 1 47 ;; 48 /*) open_img "$file" "$@" ;; 49 "~"/*) open_img "$HOME/${file#"~"/}" "$@" ;; 50 file:///*) open_img "$(uri2path "$file")" "$@" ;; 51 trash:///*) 52 trash_dir="${XDG_DATA_HOME:-$HOME/.local/share}/Trash/files" 53 open_img "${trash_dir}$(uri2path "$file")" -N "nsxiv_trash" "$@" 54 ;; 55 *) open_img "$PWD/$file" "$@" ;; 56 esac