diff options
Diffstat (limited to 'examples/rifle_sxiv.sh')
-rwxr-xr-x | examples/rifle_sxiv.sh | 72 |
1 files changed, 44 insertions, 28 deletions
diff --git a/examples/rifle_sxiv.sh b/examples/rifle_sxiv.sh index 0bdd892d..82c65f6c 100755 --- a/examples/rifle_sxiv.sh +++ b/examples/rifle_sxiv.sh @@ -10,39 +10,55 @@ # # mime ^image, has sxiv, X, flag f = path/to/this/script -- "$@" # -# Implementation notes: this script is quite slow because of POSIX limitations -# and portability concerns. First calling the shell function 'abspath' is -# quicker than calling 'realpath' because it would fork a whole process, which -# is slow. Second, we need to append a file list to sxiv, which can only be done -# properly in two ways: arrays (which are not POSIX) or \0 sperated -# strings. Unfortunately, assigning \0 to a variable is not POSIX either (will -# not work in dash and others), so we cannot store the result of listfiles to a -# variable. +# Implementation note: the script tries to be POSIX compliant both in terms of +# shell syntax and calls being made to external utilities, such as grep or find. +# This makes it portable across many unix like systems, although it may not be +# the cleanest or fastest approach. +# +# First, using case statement to get absolute path is quicker than calling +# 'realpath' because it would fork a whole process, which is slow. +# +# Second, we need to append a file list to sxiv, which can only be done +# properly in three ways: arrays (which are not POSIX). +# \0 separated strings; but assigning \0 to a variable is not POSIX either +# so we cannot store the result of listfiles to a variable. +# +# The third approach is to store the result to a tmpfile and using `-i` to feed +# the list to sxiv. This is the fastest approach since we won't have to call +# listfiles twice. -if [ $# -eq 0 ]; then - echo "Usage: ${0##*/} PICTURES" - exit -fi -[ "$1" = '--' ] && shift +TMPDIR="${TMPDIR:-/tmp}" +tmp="$TMPDIR/sxiv_rifle_$$" -abspath () { - case "$1" in - /*) printf "%s\n" "$1";; - *) printf "%s\n" "$PWD/$1";; - esac +listfiles () { + find -L "///${1%/*}" \( ! -path "///${1%/*}" -prune \) -type f -print | + grep -iE '\.(jpe?g|png|gif|webp|tiff|bmp)$' | + sort | tee "$tmp" } -listfiles () { - find -L "$(dirname "$target")" -maxdepth 1 -type f -iregex \ - '.*\(jpe?g\|bmp\|png\|gif\)$' -print0 | sort -z +is_img () { + case "${1##*.}" in + "jpg"|"jpeg"|"png"|"gif"|"webp"|"tiff"|"bmp") return 0 ;; + *) return 1 ;; + esac } -target="$(abspath "$1")" -count="$(listfiles | grep -m 1 -ZznF "$target" | cut -d: -f1)" +open_img () { + is_img "$1" || exit 1 + trap 'rm -f $tmp' EXIT + count="$(listfiles "$1" | grep -nF "$1")" + if [ -n "$count" ]; then + sxiv -i -n "${count%%:*}" -- < "$tmp" + else + sxiv -- "$@" # fallback + fi +} -if [ -n "$count" ]; then - listfiles | xargs -0 sxiv -n "$count" -- -else - sxiv -- "$@" # fallback -fi +[ "$1" = '--' ] && shift +case "$1" in + "") echo "Usage: ${0##*/} PICTURES" >&2; exit 1 ;; + /*) open_img "$1" ;; + "~"/*) open_img "$HOME/${1#"~"/}" ;; + *) open_img "$PWD/$1" ;; +esac |