aboutsummaryrefslogtreecommitdiff
path: root/dotfiles/.local/bin/sxiv-rifle
diff options
context:
space:
mode:
authorYaroslav <contact@yaroslavps.com>2020-01-17 19:14:34 +0300
committerYaroslav <contact@yaroslavps.com>2020-01-17 19:14:34 +0300
commit5eee5b44924bf9079a67d5bc7612bfad56ddeed3 (patch)
tree0408697aba64aa3f012f134bdc35316841a4b404 /dotfiles/.local/bin/sxiv-rifle
parent8f672e8a119e9efd23272f7cb0ff4550872f9d47 (diff)
downloadswayrice-5eee5b44924bf9079a67d5bc7612bfad56ddeed3.tar.gz
swayrice-5eee5b44924bf9079a67d5bc7612bfad56ddeed3.zip
config restructuring and cleanup
Diffstat (limited to 'dotfiles/.local/bin/sxiv-rifle')
-rwxr-xr-xdotfiles/.local/bin/sxiv-rifle48
1 files changed, 48 insertions, 0 deletions
diff --git a/dotfiles/.local/bin/sxiv-rifle b/dotfiles/.local/bin/sxiv-rifle
new file mode 100755
index 0000000..0bdd892
--- /dev/null
+++ b/dotfiles/.local/bin/sxiv-rifle
@@ -0,0 +1,48 @@
+#!/bin/sh
+# Compatible with ranger 1.6.0 through 1.7.*
+#
+# This script searches image files in a directory, opens them all with sxiv and
+# sets the first argument to the first image displayed by sxiv.
+#
+# This is supposed to be used in rifle.conf as a workaround for the fact that
+# sxiv takes no file name arguments for the first image, just the number. Copy
+# this file somewhere into your $PATH and add this at the top of rifle.conf:
+#
+# 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.
+
+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\|png\|gif\)$' -print0 | sort -z
+}
+
+target="$(abspath "$1")"
+count="$(listfiles | grep -m 1 -ZznF "$target" | cut -d: -f1)"
+
+if [ -n "$count" ]; then
+ listfiles | xargs -0 sxiv -n "$count" --
+else
+ sxiv -- "$@" # fallback
+fi