aboutsummaryrefslogtreecommitdiff
path: root/dotfiles/.local/bin/nsxiv-rifle
blob: bec93b7e2894b2c1ee97da937abb47477916d85e (plain)
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
#!/bin/sh

if [ $# -eq 0 ]; then
    echo "Usage: ${0##*/} PICTURES"
    exit
fi

[ "$1" = '--' ] && shift

abspath () {
    case "$1" in
        /*) printf "%s\n" "$1";;
        *)  printf "%s\n" "$PWD/$1";;
    esac
}

listfiles () {
    find -L "$(dirname "$target")" -maxdepth 1 -type f -iregex \
      	'.*\(jpe?g\|bmp\|webp\|png\|gif\|hei\(c\|f\)\)$' -print0 | sort -z
}

target="$(abspath "$1")"
count="$(listfiles | grep -m 1 -ZznF "$target" | cut -d: -f1)"

if [ -n "$count" ]; then
    listfiles | xargs -0 nsxiv -a -n "$count" --
else
    nsxiv -a -- "$@" # fallback
fi