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|webp|tiff|heif|avif|ico|bmp)$' 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"; shift; 17 # only go through listfiles() if the file has a valid img extension 18 if echo "$file" | is_img_extension >/dev/null 2>&1; then 19 trap 'rm -f $tmp' EXIT 20 count="$(listfiles "///${file%/*}" | grep -nF "$file")" 21 fi 22 if [ -n "$count" ]; then 23 nsxiv -i -n "${count%%:*}" "$@" -- < "$tmp" 24 else 25 # fallback incase file didn't have a valid extension, or we couldn't 26 # find it inside the list 27 nsxiv "$@" -- "$file" 28 fi 29 } 30 31 uri2path() { 32 python3 - "$@" <<'___HEREDOC' 33 from urllib.parse import unquote, urlparse 34 from sys import argv 35 for arg in argv[1:]: 36 print(unquote(urlparse(arg).path)) 37 ___HEREDOC 38 } 39 40 [ "$1" = '--' ] && shift 41 case "$1" in 42 "") echo "Usage: ${0##*/} PICTURES" >&2; exit 1 ;; 43 /*) open_img "$1" ;; 44 "~"/*) open_img "$HOME/${1#"~"/}" ;; 45 file:///*) open_img "$(uri2path "$1")" ;; 46 trash:///*) 47 trash_dir="${XDG_DATA_HOME:-$HOME/.local/share}/Trash/files" 48 open_img "${trash_dir}$(uri2path "$1")" -N "nsxiv_trash" 49 ;; 50 *) open_img "$PWD/$1" ;; 51 esac