Support goto index
[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 :) read_command_goto;;
46 *)
47 printf 'Error: unknown command: %s\n' "$char" >&2
48 _command='CMD_UKNOWN';;
49 esac
50 }
51
52 paths_set() {
53 local path
54
55 while read -r path
56 do
57 if file "$path" | grep 'image data' > /dev/null
58 then
59 (( n++ ))
60 # \r jumps to the beginning of the line:
61 printf '\rFound %d images.' "$n" >&2
62 paths["$(( n - 1 ))"]="$path"
63 fi
64 done
65 printf '\n' >&2
66 }
67
68 paths_set_from_dir_find() {
69 paths_set < <(find "$1" -type f)
70 }
71
72 paths_set_from_favs() {
73 paths_set < <(sort -k 2 "$FILE_WALLPAPER_FAVS" | awk '{sub("^" $1 " +", ""); print}')
74 }
75
76 set_wallpaper() {
77 local -ri i="$1"
78 local -r path="$2"
79
80 printf '%d of %d %s\n' "$(( i + 1 ))" "$n" "$path"
81 feh --bg-scale "$path"
82 }
83
84 paths_preview() {
85 local -i i=0
86 local path="${paths[$i]}"
87
88 set_wallpaper "$i" "$path"
89
90 while :
91 do
92 if [[ "${paths[$i]}" != "$path" ]]
93 then
94 path="${paths[$i]}"
95 set_wallpaper "$i" "$path"
96 fi
97
98 read_command
99 case "$_command" in
100 CMD_QUIT)
101 feh --bg-scale "$(< "$FILE_WALLPAPER_CURR")"
102 exit 0;;
103 CMD_GOTO)
104 i="$_goto";;
105 CMD_MOVE_BACK)
106 (( i = i == 0 ? (n - 1) : i - 1));;
107 CMD_MOVE_FORWARD)
108 (( i = (i + 1) % n ));;
109 CMD_FAVORITE_ADD)
110 printf 'Adding to favorites set: "%s"\n' "$path" >&2
111 wallpaper_fav;;
112 CMD_FAVORITE_REMOVE)
113 printf 'Removing from favorites set: "%s"\n' "$path" >&2
114 digest=$(sha256sum "$path" | awk '{print $1}')
115 grep -v "$digest" "$FILE_WALLPAPER_FAVS" \
116 | sort -u \
117 | sponge "$FILE_WALLPAPER_FAVS"
118 ;;
119 CMD_SET_CURRENT)
120 printf 'Setting as current: "%s"\n' "$path" >&2
121 echo "$path" > "$FILE_WALLPAPER_CURR";;
122 CMD_UKNOWN)
123 continue;;
124 esac
125 done
126 }
127
128 main() {
129 case "$1" in
130 f | fav) paths_set_from_favs;;
131 d | dir) paths_set_from_dir_find "$2";;
132 a | all) paths_set_from_dir_find "$DIR_WALLPAPERS";;
133 '')
134 paths_set_from_dir_find "$DIR_WALLPAPERS";;
135 *)
136 printf 'Error: unknown source "%s"\n' "$1" >&2
137 exit 1;;
138 esac
139
140 paths_preview
141 }
142
143 main "$@"
This page took 0.126118 seconds and 4 git commands to generate.