| 1 | #! /bin/bash |
| 2 | |
| 3 | ### XXX Chose not to 'set -e' because arithmetic evaluation can cause a non-zero exit. |
| 4 | ### XXX Chose not to 'set -u' because unset params ($1, $2, ...) cause failures. |
| 5 | ### Solutions to the above are ugly. |
| 6 | |
| 7 | declare -a paths |
| 8 | declare -i n=0 |
| 9 | |
| 10 | read_command() { |
| 11 | local char |
| 12 | |
| 13 | read -rsn 1 char |
| 14 | case "$char" in |
| 15 | q | Q) echo 'CMD_QUIT';; |
| 16 | h | H) echo 'CMD_MOVE_BACK';; |
| 17 | l | L) echo 'CMD_MOVE_FORWARD';; |
| 18 | f | F) echo 'CMD_FAVORITE_ADD';; |
| 19 | r | R) echo 'CMD_FAVORITE_REMOVE';; |
| 20 | s | S) echo 'CMD_SET_CURRENT';; |
| 21 | *) echo 'CMD_UKNOWN';; |
| 22 | esac |
| 23 | } |
| 24 | |
| 25 | paths_set() { |
| 26 | local path |
| 27 | |
| 28 | while read -r path |
| 29 | do |
| 30 | if file "$path" | grep 'image data' > /dev/null |
| 31 | then |
| 32 | (( n++ )) |
| 33 | # \r jumps to the beginning of the line: |
| 34 | printf '\rFound %d images.' "$n" >&2 |
| 35 | paths["$(( n - 1 ))"]="$path" |
| 36 | fi |
| 37 | done |
| 38 | printf '\n' >&2 |
| 39 | } |
| 40 | |
| 41 | paths_set_from_dir_find() { |
| 42 | paths_set < <(find "$1" -type f) |
| 43 | } |
| 44 | |
| 45 | paths_set_from_favs() { |
| 46 | paths_set < <(sort -k 2 "$FILE_WALLPAPER_FAVS" | awk '{sub("^" $1 " +", ""); print}') |
| 47 | } |
| 48 | |
| 49 | set_wallpaper() { |
| 50 | local -ri i="$1" |
| 51 | local -r path="$2" |
| 52 | |
| 53 | printf '%d of %d %s\n' "$(( i + 1 ))" "$n" "$path" |
| 54 | feh --bg-scale "$path" |
| 55 | } |
| 56 | |
| 57 | paths_preview() { |
| 58 | local -i i=0 |
| 59 | local path="${paths[$i]}" |
| 60 | |
| 61 | set_wallpaper "$i" "$path" |
| 62 | |
| 63 | while : |
| 64 | do |
| 65 | if [[ "${paths[$i]}" != "$path" ]] |
| 66 | then |
| 67 | path="${paths[$i]}" |
| 68 | set_wallpaper "$i" "$path" |
| 69 | fi |
| 70 | |
| 71 | case "$(read_command)" in |
| 72 | CMD_QUIT) |
| 73 | feh --bg-scale "$(< "$FILE_WALLPAPER_CURR")" |
| 74 | exit 0;; |
| 75 | CMD_MOVE_BACK) |
| 76 | (( i = i == 0 ? (n - 1) : i - 1));; |
| 77 | CMD_MOVE_FORWARD) |
| 78 | (( i = (i + 1) % n ));; |
| 79 | CMD_FAVORITE_ADD) |
| 80 | printf 'Adding to favorites set: "%s"\n' "$path" >&2 |
| 81 | wallpaper_fav;; |
| 82 | CMD_FAVORITE_REMOVE) |
| 83 | printf 'Removing from favorites set: "%s"\n' "$path" >&2 |
| 84 | digest=$(sha256sum "$path" | awk '{print $1}') |
| 85 | grep -v "$digest" "$FILE_WALLPAPER_FAVS" \ |
| 86 | | sort -u \ |
| 87 | | sponge "$FILE_WALLPAPER_FAVS" |
| 88 | ;; |
| 89 | CMD_SET_CURRENT) |
| 90 | printf 'Setting as current: "%s"\n' "$path" >&2 |
| 91 | echo "$path" > "$FILE_WALLPAPER_CURR";; |
| 92 | CMD_UKNOWN) |
| 93 | continue;; |
| 94 | esac |
| 95 | done |
| 96 | } |
| 97 | |
| 98 | main() { |
| 99 | case "$1" in |
| 100 | f | fav) paths_set_from_favs;; |
| 101 | d | dir) paths_set_from_dir_find "$2";; |
| 102 | a | all) paths_set_from_dir_find "$DIR_WALLPAPERS";; |
| 103 | '') |
| 104 | paths_set_from_dir_find "$DIR_WALLPAPERS";; |
| 105 | *) |
| 106 | printf 'Error: unknown source "%s"\n' "$1" >&2 |
| 107 | exit 1;; |
| 108 | esac |
| 109 | |
| 110 | paths_preview |
| 111 | } |
| 112 | |
| 113 | main "$@" |