Skip to content

Commit

Permalink
Make random selection more robust and random
Browse files Browse the repository at this point in the history
  • Loading branch information
onli committed Feb 16, 2015
1 parent e4a8f7d commit 1696be5
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions izulu
Original file line number Diff line number Diff line change
Expand Up @@ -435,12 +435,14 @@ cycleWallpaper() {
return 1
fi

local pictures=()
for picture in "$randomdir"/*;do
pictures+=("$picture")
done
local chosen=$RANDOM
let "chosen %= ${#pictures[*]}"

# The IFS set to \n enables files with spaces
OLD_IFS=$IFS
IFS=$'\n'
# this gets all image files in randomdir and subdirs. The ${} around $randomdir is necessary to evaluate variables like ~
pictures=($(echo -e "$(find ${randomdir} -name '*' -exec file {} \; | grep -o -P '^.+: \w+ image' | sed s"#.*\($randomdir.*\):.*#\1#"g | sort)"))
IFS=$OLD_IFS
chosen=$(shuf -i1-${#pictures[@]} -n1)
echo "${pictures[$chosen]}"
}

Expand Down

0 comments on commit 1696be5

Please sign in to comment.