Commit | Line | Data |
---|---|---|
2953aaf3 | 1 | #! /bin/bash |
31df3021 | 2 | |
55b6848c SK |
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 | ||
2953aaf3 SK |
7 | declare -a paths |
8 | declare -i n=0 | |
31df3021 | 9 | |
2953aaf3 SK |
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';; | |
19 | *) echo 'CMD_UKNOWN';; | |
20 | esac | |
21 | } | |
22 | ||
cdc33eec | 23 | paths_set() { |
2953aaf3 SK |
24 | local path |
25 | ||
cdc33eec | 26 | while read -r path |
2953aaf3 SK |
27 | do |
28 | if file "$path" | grep 'image data' > /dev/null | |
29 | then | |
30 | (( n++ )) | |
31 | # \r jumps to the beginning of the line: | |
32 | printf '\rFound %d images.' "$n" >&2 | |
55b6848c | 33 | paths["$(( n - 1 ))"]="$path" |
2953aaf3 SK |
34 | fi |
35 | done | |
2953aaf3 SK |
36 | printf '\n' >&2 |
37 | } | |
38 | ||
cdc33eec SK |
39 | paths_set_from_dir_find() { |
40 | paths_set < <(find "$1" -type f) | |
41 | } | |
42 | ||
43 | paths_set_from_favs() { | |
44 | paths_set < <(sort -k 2 "$FILE_WALLPAPER_FAVS" | awk '{sub("^" $1 " +", ""); print}') | |
45 | } | |
46 | ||
55b6848c SK |
47 | set_wallpaper() { |
48 | local -ri i="$1" | |
49 | local -r path="$2" | |
50 | ||
51 | printf '%d of %d %s\n' "$(( i + 1 ))" "$n" "$path" | |
52 | feh --bg-scale "$path" | |
53 | } | |
54 | ||
2953aaf3 | 55 | paths_preview() { |
55b6848c SK |
56 | local -i i=0 |
57 | local path="${paths[$i]}" | |
2953aaf3 | 58 | |
55b6848c | 59 | set_wallpaper "$i" "$path" |
2953aaf3 | 60 | |
55b6848c SK |
61 | while : |
62 | do | |
63 | if [[ "${paths[$i]}" != "$path" ]] | |
64 | then | |
65 | path="${paths[$i]}" | |
66 | set_wallpaper "$i" "$path" | |
67 | fi | |
2953aaf3 SK |
68 | |
69 | case "$(read_command)" in | |
70 | CMD_QUIT) | |
71 | exit 0;; | |
72 | CMD_MOVE_BACK) | |
55b6848c | 73 | (( i = i == 0 ? (n - 1) : i - 1));; |
2953aaf3 | 74 | CMD_MOVE_FORWARD) |
55b6848c | 75 | (( i = (i + 1) % n ));; |
2953aaf3 | 76 | CMD_FAVORITE) |
55b6848c | 77 | wallpaper_fav;; |
2953aaf3 | 78 | CMD_UKNOWN) |
55b6848c | 79 | continue;; |
2953aaf3 SK |
80 | esac |
81 | done | |
82 | } | |
83 | ||
84 | main() { | |
2953aaf3 | 85 | case "$1" in |
cdc33eec SK |
86 | f | fav) paths_set_from_favs;; |
87 | d | dir) paths_set_from_dir_find "$2";; | |
88 | a | all) paths_set_from_dir_find "$DIR_WALLPAPERS";; | |
89 | '') | |
90 | paths_set_from_dir_find "$DIR_WALLPAPERS";; | |
91 | *) | |
92 | printf 'Error: unknown source "%s"\n' "$1" >&2 | |
93 | exit 1;; | |
2953aaf3 SK |
94 | esac |
95 | ||
2953aaf3 SK |
96 | paths_preview |
97 | } | |
98 | ||
99 | main "$@" |