Simplify conversion to human-readable units
[khome.git] / home / bin / wallpaper_review
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 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
34 read_command() {
35 local char
36
37 read -rsn 1 char
38 case "$char" in
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';;
45 z | Z) _command='CMD_FZF';;
46 :) read_command_goto;;
47 *)
48 printf 'Error: unknown command: %s\n' "$char" >&2
49 _command='CMD_UKNOWN';;
50 esac
51 }
52
53 paths_set() {
54 local path
55
56 while read -r path
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
63 paths["$(( n - 1 ))"]="$path"
64 fi
65 done
66 printf '\n' >&2
67 }
68
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
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
85 paths_preview() {
86 local -i i=0
87 local path="${paths[$i]}"
88
89 set_wallpaper "$i" "$path"
90
91 while :
92 do
93 if [[ "${paths[$i]}" != "$path" ]]
94 then
95 path="${paths[$i]}"
96 set_wallpaper "$i" "$path"
97 fi
98
99 read_command
100 case "$_command" in
101 CMD_QUIT)
102 feh --bg-scale "$(< "$FILE_WALLPAPER_CURR")"
103 exit 0;;
104 CMD_GOTO)
105 i="$_goto";;
106 CMD_MOVE_BACK)
107 (( i = i == 0 ? (n - 1) : i - 1));;
108 CMD_MOVE_FORWARD)
109 (( i = (i + 1) % n ));;
110 CMD_FAVORITE_ADD)
111 printf 'Adding to favorites set: "%s"\n' "$path" >&2
112 wallpaper_fav "$path";;
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 ;;
120 CMD_SET_CURRENT)
121 printf 'Setting as current: "%s"\n' "$path" >&2
122 echo "$path" > "$FILE_WALLPAPER_CURR";;
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 );;
131 CMD_UKNOWN)
132 continue;;
133 esac
134 done
135 }
136
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
157 usage() {
158 cat <<EOF >&2
159
160 $(basename "$0") [cmd]
161
162 cmd | args | meaning
163 ----+-------+------------------------------------------------
164 f | | favorites
165 d | <dir> | from given dir
166 a | | all from $DIR_WALLPAPERS <- DEFAULT if no cmd.
167 h | | usage help. i.e. this message.
168 ----+-------+------------------------------------------------
169
170 EOF
171 }
172
173 main() {
174 case "$1" in
175 f | fav) paths_set_from_favs;;
176 d | dir) paths_set_from_dir_find "$2";;
177 a | all) paths_set_from_dir_find "$DIR_WALLPAPERS";;
178 h | -h | help | -help | --help)
179 usage;
180 exit 1;;
181 '')
182 paths_set_from_dir_find "$DIR_WALLPAPERS";;
183 *)
184 printf 'Error: unknown source "%s"\n' "$1" >&2
185 exit 1;;
186 esac
187 help_print
188 paths_preview
189 }
190
191 main "$@"
This page took 0.128445 seconds and 4 git commands to generate.