Last active
September 22, 2015 22:20
-
-
Save sinapinto/dd4a1abd95ee9056655d to your computer and use it in GitHub Desktop.
wallpaper changer
This file contains hidden or 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
#!/bin/env bash | |
usage() { | |
cat <<"EOF" | |
Usage: wl [-R depth|-h] [dir] | |
-h show this help | |
-R depth Recursively search subdirectories with the specified depth. | |
for example, a depth of 1 searches only files the current directory. | |
-s shuffle images | |
Example: wl -R 2 ~/backgrounds/ | |
Keys: | |
j go to the next image | |
k go to the previous image | |
D delete the current image | |
q restore the original background and quit | |
w quit (can also use ^C) | |
EOF | |
} | |
die() { | |
echo "${0##*/}: $@" >&2 | |
exit 1 | |
} | |
hash feh &>/dev/null || die "feh not found" | |
if [ -f "$HOME/.fehbg" ] | |
then | |
current=$(cat ~/.fehbg | sed -n '/^feh*/p') | |
fi | |
# TODO: better detection for tiling wallpapers - image dimensions? | |
if [ ${PWD##*/} = "tile" ] | |
then | |
arg="--bg-tile" | |
else | |
arg="--bg-fill" | |
fi | |
depth=1 | |
shuffle=0 | |
while getopts ":R::sh" opt | |
do | |
case $opt in | |
R) | |
depth=$OPTARG ;; | |
s) | |
shuffle=1 ;; | |
h|\?) | |
usage | |
exit 1 ;; | |
esac | |
done | |
shift $(( OPTIND - 1 )) | |
[ -n "$1" ] && dir="$@" || dir=$(pwd) | |
[ ! -d "$dir" ] && die "$@" is not an existing directory | |
IFS=$'\n' | |
files=( $(find "$dir" -maxdepth $depth -print -name '*' \ | |
-exec file '{}' -Nn -e apptype -e ascii -e encoding -e tokens -e cdf -e compress -e elf -e tar \; \ | |
| grep -oE '^[^:]*:\s\w*\simage' \ | |
| awk -F ':' '{print $1}') ) 2>/dev/null | |
[ -z $files ] && die "no images found" | |
if [ "$shuffle" -eq 1 ]; then | |
shuffled=() | |
checkArray () { | |
for item in ${shuffled[@]}; do | |
[[ "$item" == "$1" ]] && return 0 | |
done | |
return 1 | |
} | |
while [ "${#shuffled[@]}" -ne "${#files[@]}" ]; do | |
rand=$[ $RANDOM % ${#files[@]} ] | |
checkArray "${files[$rand]}" || shuffled=(${shuffled[@]} "${files[$rand]}") | |
done | |
files=(${shuffled[@]}) | |
fi | |
echo "j = next; k = previous; D = delete; w = save; q = quit" | |
i=0 | |
while true | |
do | |
read -n1 -s key | |
case $key in | |
j) | |
if [ $i -eq ${#files[@]} ] | |
then | |
i=1 | |
else | |
(( i++ )) | |
fi | |
feh $arg ${files[$i-1]} 2>/dev/null | |
echo "[$i/${#files[@]}] $(basename ${files[$i-1]})" | |
;; | |
k) | |
if [ $i -le 1 ] | |
then | |
i=${#files[@]} | |
else | |
(( i-- )) | |
fi | |
feh $arg ${files[$i-1]} 2>/dev/null | |
echo "[$i/${#files[@]}] $(basename ${files[$i-1]})" | |
;; | |
D) | |
if [ $i -eq 0 ] | |
then | |
echo "${0##*/}: no active file to delete" | |
else | |
rm ${files[$i-1]} | |
echo -e "\e[1;31m[$i/${#files[@]}] ${files[$i-1]} deleted\e[0m" | |
del=${files[$i-1]} | |
files=(${files[@]#$del}) | |
feh $arg ${files[$i-1]} | |
echo "[$i/${#files[@]}] $(basename ${files[$i-1]})" | |
fi | |
;; | |
q) | |
[ -n $current ] && eval $current 2>/dev/null | |
exit 0 | |
;; | |
w) | |
exit 0 | |
;; | |
esac | |
done | |
unset IFS |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment