about summary refs log tree commit diff stats
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rwxr-xr-xexamples/rifle_sxiv.sh26
1 files changed, 9 insertions, 17 deletions
diff --git a/examples/rifle_sxiv.sh b/examples/rifle_sxiv.sh
index 0bdd892d..866dd730 100755
--- a/examples/rifle_sxiv.sh
+++ b/examples/rifle_sxiv.sh
@@ -11,7 +11,7 @@
 #   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
+# and portability concerns. 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 two ways: arrays (which are not POSIX) or \0 sperated
@@ -19,26 +19,18 @@
 # not work in dash and others), so we cannot store the result of listfiles to a
 # variable.
 
-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 \
+    find -L "${target%/*}" -maxdepth 1 -type f -iregex \
       '.*\(jpe?g\|bmp\|png\|gif\)$' -print0 | sort -z
 }
 
-target="$(abspath "$1")"
+[ "$1" = '--' ] && shift
+case "$1" in
+    "") echo "Usage: ${0##*/} PICTURES" >/dev/stderr && exit ;;
+    /*) target="$1" ;;
+    *)  target="$PWD/$1" ;;
+esac
+
 count="$(listfiles | grep -m 1 -ZznF "$target" | cut -d: -f1)"
 
 if [ -n "$count" ]; then