Skip to content

Instantly share code, notes, and snippets.

@willemw12
Created November 8, 2022 16:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willemw12/f8a1005b94e9e185ce2539f9bc6df2e3 to your computer and use it in GitHub Desktop.
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
#!/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