Created
November 8, 2022 16:33
-
-
Save willemw12/f8a1005b94e9e185ce2539f9bc6df2e3 to your computer and use it in GitHub Desktop.
Swayimg: add the ability to navigate to the other images in the folder, starting at the selected image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Swayimg: add the ability to navigate to the other images in the folder, | |
# starting at the selected image or the first image by default. | |
# | |
# Links: | |
# https://gist.github.com/willemw12 | |
# https://github.com/artemsen/swayimg | |
SWAYIMG_FIND_OPTS="${SWAYIMG_FIND_OPTS:--maxdepth 1}" | |
#SWAYIMG_SORT_OPTS="${SWAYIMG_SORT_OPTS:-}" | |
#### | |
if (( $# == 0 )); then | |
printf "usage: [SWAYIMG_FIND_OPTS=<OPTION>...] [SWAYIMG_SORT_OPTS=<OPTION>...] %s [<SWAYIMG_OPTION>...] <FILE|FOLDER>\n" "${0##*/}" >&2 | |
exit 1 | |
fi | |
FILE="${*:$#:1}" | |
REST_ARGS=("${@:1:$#-1}") | |
SWAYIMG_OPTS=(--order=none) | |
# shellcheck disable=SC2048,SC2086 | |
if [ -d "$FILE" ]; then | |
readarray -d '' FILES < <(find -L "$FILE" ${SWAYIMG_FIND_OPTS[*]} -mindepth 1 -type f -print0 | sort ${SWAYIMG_SORT_OPTS[*]} -z) | |
elif [ -f "$FILE" ]; then | |
readarray -d '' FILES < <(find -L "$(dirname "$FILE")" ${SWAYIMG_FIND_OPTS[*]} -mindepth 1 -type f -print0 | sort ${SWAYIMG_SORT_OPTS[*]} -z) | |
# Rotate the filenames in the FILES array to make FILE the selected file for swaymg, | |
# i.e. make it the first file in the array. | |
I=0 | |
for F in "${FILES[@]}"; do | |
[ "$(realpath "$F")" = "$(realpath "$FILE")" ] && break | |
HEAD+=("$F") | |
(( I++ )) | |
done | |
# File not found | |
(( I == ${#FILES[@]} )) && exit 0 | |
for F in "${FILES[@]:I:${#FILES[@]}}"; do | |
TAIL+=("$F") | |
done | |
FILES=("${TAIL[@]}" "${HEAD[@]}") | |
else | |
exit 1 | |
fi | |
(( "${#FILES[@]}" > 0 )) || exit | |
exec swayimg "${SWAYIMG_OPTS[@]}" "${REST_ARGS[@]}" "${FILES[@]}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment