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 | |
5b897004 SK |
10 | declare _command |
11 | declare -i _goto=0 | |
12 | ||
13 | read_command_goto() { | |
14 | local buf | |
15 | ||
16 | printf ':' >&2 | |
17 | read -r buf | |
18 | printf '\n' >&2 | |
19 | if [[ "$buf" =~ ^[0-9]+$ ]]; then | |
20 | if [[ "$buf" -gt 0 && "$buf" -le "$n" ]]; then | |
21 | (( buf-- )) | |
22 | _goto="$buf" | |
23 | _command='CMD_GOTO' | |
24 | else | |
25 | printf 'Error: out of range: %s\n' "$buf" >&2 | |
26 | _command='CMD_UKNOWN' | |
27 | fi | |
28 | else | |
29 | printf 'Error: not an integer: %s\n' "$buf" >&2 | |
30 | _command='CMD_UKNOWN' | |
31 | fi | |
32 | } | |
33 | ||
2953aaf3 SK |
34 | read_command() { |
35 | local char | |
36 | ||
37 | read -rsn 1 char | |
38 | case "$char" in | |
5b897004 SK |
39 | q | Q) _command='CMD_QUIT';; |
40 | h | H) _command='CMD_MOVE_BACK';; | |
41 | l | L) _command='CMD_MOVE_FORWARD';; | |
42 | f | F) _command='CMD_FAVORITE_ADD';; | |
43 | r | R) _command='CMD_FAVORITE_REMOVE';; | |
44 | s | S) _command='CMD_SET_CURRENT';; | |
8c5e077f | 45 | z | Z) _command='CMD_FZF';; |
5b897004 SK |
46 | :) read_command_goto;; |
47 | *) | |
48 | printf 'Error: unknown command: %s\n' "$char" >&2 | |
49 | _command='CMD_UKNOWN';; | |
2953aaf3 SK |
50 | esac |
51 | } | |
52 | ||
cdc33eec | 53 | paths_set() { |
2953aaf3 SK |
54 | local path |
55 | ||
cdc33eec | 56 | while read -r path |
2953aaf3 SK |
57 | do |
58 | if file "$path" | grep 'image data' > /dev/null | |
59 | then | |
60 | (( n++ )) | |
61 | # \r jumps to the beginning of the line: | |
62 | printf '\rFound %d images.' "$n" >&2 | |
55b6848c | 63 | paths["$(( n - 1 ))"]="$path" |
2953aaf3 SK |
64 | fi |
65 | done | |
2953aaf3 SK |
66 | printf '\n' >&2 |
67 | } | |
68 | ||
cdc33eec SK |
69 | paths_set_from_dir_find() { |
70 | paths_set < <(find "$1" -type f) | |
71 | } | |
72 | ||
73 | paths_set_from_favs() { | |
74 | paths_set < <(sort -k 2 "$FILE_WALLPAPER_FAVS" | awk '{sub("^" $1 " +", ""); print}') | |
75 | } | |
76 | ||
55b6848c SK |
77 | set_wallpaper() { |
78 | local -ri i="$1" | |
79 | local -r path="$2" | |
80 | ||
81 | printf '%d of %d %s\n' "$(( i + 1 ))" "$n" "$path" | |
82 | feh --bg-scale "$path" | |
83 | } | |
84 | ||
2953aaf3 | 85 | paths_preview() { |
55b6848c SK |
86 | local -i i=0 |
87 | local path="${paths[$i]}" | |
2953aaf3 | 88 | |
55b6848c | 89 | set_wallpaper "$i" "$path" |
2953aaf3 | 90 | |
55b6848c SK |
91 | while : |
92 | do | |
93 | if [[ "${paths[$i]}" != "$path" ]] | |
94 | then | |
95 | path="${paths[$i]}" | |
96 | set_wallpaper "$i" "$path" | |
97 | fi | |
2953aaf3 | 98 | |
5b897004 SK |
99 | read_command |
100 | case "$_command" in | |
2953aaf3 | 101 | CMD_QUIT) |
b3a26007 | 102 | feh --bg-scale "$(< "$FILE_WALLPAPER_CURR")" |
2953aaf3 | 103 | exit 0;; |
5b897004 SK |
104 | CMD_GOTO) |
105 | i="$_goto";; | |
2953aaf3 | 106 | CMD_MOVE_BACK) |
55b6848c | 107 | (( i = i == 0 ? (n - 1) : i - 1));; |
2953aaf3 | 108 | CMD_MOVE_FORWARD) |
55b6848c | 109 | (( i = (i + 1) % n ));; |
0e76831b SK |
110 | CMD_FAVORITE_ADD) |
111 | printf 'Adding to favorites set: "%s"\n' "$path" >&2 | |
fdc87852 | 112 | wallpaper_fav "$path";; |
0e76831b SK |
113 | CMD_FAVORITE_REMOVE) |
114 | printf 'Removing from favorites set: "%s"\n' "$path" >&2 | |
115 | digest=$(sha256sum "$path" | awk '{print $1}') | |
116 | grep -v "$digest" "$FILE_WALLPAPER_FAVS" \ | |
117 | | sort -u \ | |
118 | | sponge "$FILE_WALLPAPER_FAVS" | |
119 | ;; | |
ddd3f251 SK |
120 | CMD_SET_CURRENT) |
121 | printf 'Setting as current: "%s"\n' "$path" >&2 | |
122 | echo "$path" > "$FILE_WALLPAPER_CURR";; | |
8c5e077f SK |
123 | CMD_FZF) |
124 | i=$( | |
125 | for j in "${!paths[@]}"; do | |
126 | printf '%d %s\n' "$((j + 1))" "${paths[j]}" | |
127 | done \ | |
128 | | fzf -e \ | |
129 | | awk '{print $1 - 1}' | |
130 | );; | |
2953aaf3 | 131 | CMD_UKNOWN) |
55b6848c | 132 | continue;; |
2953aaf3 SK |
133 | esac |
134 | done | |
135 | } | |
136 | ||
e2dd436b SK |
137 | help_print() { |
138 | cat <<EOF >&2 | |
139 | ||
140 | Navigation help: | |
141 | ||
142 | key | action | |
143 | ----+---------------------------------------------- | |
144 | q | quit | |
145 | h | move back | |
146 | l | move forward | |
147 | f | add to favorites | |
148 | r | remove from favorites | |
149 | s | set current | |
150 | z | fuzzy search for next file path | |
151 | :N | goto Nth image | |
152 | ----+---------------------------------------------- | |
153 | ||
154 | EOF | |
155 | } | |
156 | ||
2953aaf3 | 157 | main() { |
2953aaf3 | 158 | case "$1" in |
cdc33eec SK |
159 | f | fav) paths_set_from_favs;; |
160 | d | dir) paths_set_from_dir_find "$2";; | |
161 | a | all) paths_set_from_dir_find "$DIR_WALLPAPERS";; | |
162 | '') | |
163 | paths_set_from_dir_find "$DIR_WALLPAPERS";; | |
164 | *) | |
165 | printf 'Error: unknown source "%s"\n' "$1" >&2 | |
166 | exit 1;; | |
2953aaf3 | 167 | esac |
e2dd436b | 168 | help_print |
2953aaf3 SK |
169 | paths_preview |
170 | } | |
171 | ||
172 | main "$@" |