Skip to content

Instantly share code, notes, and snippets.

@jeffmhubbard
Last active January 19, 2020 18:17
Show Gist options
  • Save jeffmhubbard/ced72361038db554c94d10a99cd47890 to your computer and use it in GitHub Desktop.
Save jeffmhubbard/ced72361038db554c94d10a99cd47890 to your computer and use it in GitHub Desktop.
Replace get_user_wall with get_image and get_wall_list
# populate $WALL_LIST depending on number of displays and images passed
get_wall_list() {
local paths=("$@")
declare -ga WALL_LIST
# multiple images and spanning conflict, bail out
if [ "${#paths[@]}" -gt 1 ] && [ "$span_image" = true ]; then
echo "ERROR: can't use --span with multiple images"
exit 1
fi
# if # paths is 1
if [ "${#paths[@]}" -eq 1 ]; then
for ((i=0; i<${#DISPLAY_LIST[@]}; i++)); do
# add same image to $WALL_LIST for each display
get_image "${paths[0]}"
done
# if # of paths equals # of displays
elif [ ${#paths[@]} -eq "${#DISPLAY_LIST[@]}" ]; then
for ((i=0; i<${#DISPLAY_LIST[@]}; i++)); do
# add each image to $WALL_LIST
get_image "${paths[$i]}"
done
# if # of paths differ from # of display, bail out
else
echo "ERROR: ${#paths[@]} images provided for ${#DISPLAY_LIST[@]} displays!"
exit 1
fi
}
# get image path, append to $WALL_LIST
get_image() {
local path="$1"
# we have a file
if [ -f "$path" ]; then
WALL_LIST+=("$path")
return
# we have a directory
elif [ -d "$path" ]; then
dir=("$path"/*)
rdir="${dir[RANDOM % ${#dir[@]}]}"
get_image "$rdir" # <-- calls itself
# not file or directory, bail out
else
echo "ERROR: invalid path" "$path"
exit 1
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment