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 | ||
23 | paths_find() { | |
24 | local -r root_dir="$1" | |
25 | local path | |
26 | ||
27 | IFS=$'\n' | |
28 | for path in $(find "$root_dir" -type f) | |
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 | |
55b6848c | 35 | paths["$(( n - 1 ))"]="$path" |
2953aaf3 SK |
36 | fi |
37 | done | |
38 | unset IFS | |
39 | printf '\n' >&2 | |
40 | } | |
41 | ||
55b6848c SK |
42 | set_wallpaper() { |
43 | local -ri i="$1" | |
44 | local -r path="$2" | |
45 | ||
46 | printf '%d of %d %s\n' "$(( i + 1 ))" "$n" "$path" | |
47 | feh --bg-scale "$path" | |
48 | } | |
49 | ||
2953aaf3 | 50 | paths_preview() { |
55b6848c SK |
51 | local -i i=0 |
52 | local path="${paths[$i]}" | |
2953aaf3 | 53 | |
55b6848c | 54 | set_wallpaper "$i" "$path" |
2953aaf3 | 55 | |
55b6848c SK |
56 | while : |
57 | do | |
58 | if [[ "${paths[$i]}" != "$path" ]] | |
59 | then | |
60 | path="${paths[$i]}" | |
61 | set_wallpaper "$i" "$path" | |
62 | fi | |
2953aaf3 SK |
63 | |
64 | case "$(read_command)" in | |
65 | CMD_QUIT) | |
66 | exit 0;; | |
67 | CMD_MOVE_BACK) | |
55b6848c | 68 | (( i = i == 0 ? (n - 1) : i - 1));; |
2953aaf3 | 69 | CMD_MOVE_FORWARD) |
55b6848c | 70 | (( i = (i + 1) % n ));; |
2953aaf3 | 71 | CMD_FAVORITE) |
55b6848c | 72 | wallpaper_fav;; |
2953aaf3 | 73 | CMD_UKNOWN) |
55b6848c | 74 | continue;; |
2953aaf3 SK |
75 | esac |
76 | done | |
77 | } | |
78 | ||
79 | main() { | |
80 | local root_dir | |
81 | ||
82 | case "$1" in | |
83 | '') root_dir="$DIR_WALLPAPERS";; | |
84 | *) root_dir="$1";; | |
85 | esac | |
86 | ||
87 | paths_find "$root_dir" | |
88 | paths_preview | |
89 | } | |
90 | ||
91 | main "$@" |