about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorNRK <nrk@disroot.org>2021-08-08 16:37:53 +0600
committerNRK <nrk@disroot.org>2021-08-09 04:21:15 +0600
commite3a98d51be122294d5792347358f9d6f3e17a8c1 (patch)
treef555538aac38f695cce5d1a5729d194b57cd619f
parent3688ddd92ba13b88f57f8a8760b4d8155d8c3e0b (diff)
downloadranger-e3a98d51be122294d5792347358f9d6f3e17a8c1.tar.gz
rifle_sxiv: performance improvement
-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