Support goto index
[khome.git] / home / bin / wallpaper_review
CommitLineData
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
7declare -a paths
8declare -i n=0
31df3021 9
5b897004
SK
10declare _command
11declare -i _goto=0
12
13read_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
34read_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';;
45 :) read_command_goto;;
46 *)
47 printf 'Error: unknown command: %s\n' "$char" >&2
48 _command='CMD_UKNOWN';;
2953aaf3
SK
49 esac
50}
51
cdc33eec 52paths_set() {
2953aaf3
SK
53 local path
54
cdc33eec 55 while read -r path
2953aaf3
SK
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
55b6848c 62 paths["$(( n - 1 ))"]="$path"
2953aaf3
SK
63 fi
64 done
2953aaf3
SK
65 printf '\n' >&2
66}
67
cdc33eec
SK
68paths_set_from_dir_find() {
69 paths_set < <(find "$1" -type f)
70}
71
72paths_set_from_favs() {
73 paths_set < <(sort -k 2 "$FILE_WALLPAPER_FAVS" | awk '{sub("^" $1 " +", ""); print}')
74}
75
55b6848c
SK
76set_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
2953aaf3 84paths_preview() {
55b6848c
SK
85 local -i i=0
86 local path="${paths[$i]}"
2953aaf3 87
55b6848c 88 set_wallpaper "$i" "$path"
2953aaf3 89
55b6848c
SK
90 while :
91 do
92 if [[ "${paths[$i]}" != "$path" ]]
93 then
94 path="${paths[$i]}"
95 set_wallpaper "$i" "$path"
96 fi
2953aaf3 97
5b897004
SK
98 read_command
99 case "$_command" in
2953aaf3 100 CMD_QUIT)
b3a26007 101 feh --bg-scale "$(< "$FILE_WALLPAPER_CURR")"
2953aaf3 102 exit 0;;
5b897004
SK
103 CMD_GOTO)
104 i="$_goto";;
2953aaf3 105 CMD_MOVE_BACK)
55b6848c 106 (( i = i == 0 ? (n - 1) : i - 1));;
2953aaf3 107 CMD_MOVE_FORWARD)
55b6848c 108 (( i = (i + 1) % n ));;
0e76831b
SK
109 CMD_FAVORITE_ADD)
110 printf 'Adding to favorites set: "%s"\n' "$path" >&2
55b6848c 111 wallpaper_fav;;
0e76831b
SK
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 ;;
ddd3f251
SK
119 CMD_SET_CURRENT)
120 printf 'Setting as current: "%s"\n' "$path" >&2
121 echo "$path" > "$FILE_WALLPAPER_CURR";;
2953aaf3 122 CMD_UKNOWN)
55b6848c 123 continue;;
2953aaf3
SK
124 esac
125 done
126}
127
128main() {
2953aaf3 129 case "$1" in
cdc33eec
SK
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;;
2953aaf3
SK
138 esac
139
2953aaf3
SK
140 paths_preview
141}
142
143main "$@"
This page took 0.05264 seconds and 4 git commands to generate.