aboutsummaryrefslogtreecommitdiff
path: root/.local/bin/script/sxiv-rifle
blob: 4adea54a37d08e03d0f485e4117f4ca17e9d49c4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/bin/sh
# Modified version of nsxiv-rifle from https://codeberg.org/nsxiv/nsxiv-extra

TMPDIR="${TMPDIR:-/tmp}"
tmp="$TMPDIR/sxiv_rifle_$$"


is_img_extension() {
	grep -iE '\.(avif|bmp|gif|heic|heif|ico|jpe?g|jxl|png|qoi|svg|svgz|tiff|webp)$'
}

listfiles() {
	find -L "${1%/*}" -maxdepth 1 -type f -print |
		is_img_extension | sort | tee "$tmp"
}

open_img() {
	# only go through listfiles() if the file has a valid img extension
	if echo "$1" | is_img_extension 1>/dev/null 2>&1; then
		trap 'rm -f $tmp' EXIT
		count="$(listfiles "$1" | grep -nF "$1")"
	fi

	if [ -n "$count" ]; then
		$sxiv -i -n "${count%%:*}" -- < "$tmp"
	else
		# fallback incase file didn't have a valid extension, or we couldn't
		# find it inside the list
		$sxiv -- "$@"
	fi
}


sxiv="$(command -v nsxiv sxiv | head -1)"
[ "$1" = "--" ] && shift
case "$1" in
	"") echo "Usage: ${0##*/} PICTURES"; exit 1;;
	/*) open_img "$1" ;;
	"~"/*) open_img "$HOME/${1#"~"/}";;
	*) open_img "$PWD/$1";;
esac