Support setting current
[khome.git] / home / bin / wallpapers_preview
index 3595147..34d98d7 100755 (executable)
@@ -1,5 +1,9 @@
 #! /bin/bash
 
+### XXX Chose not to 'set -e' because arithmetic evaluation can cause a non-zero exit.
+### XXX Chose not to 'set -u' because unset params ($1, $2, ...) cause failures.
+### Solutions to the above are ugly.
+
 declare -a paths
 declare -i n=0
 
@@ -11,73 +15,97 @@ read_command() {
         q | Q) echo 'CMD_QUIT';;
         h | H) echo 'CMD_MOVE_BACK';;
         l | L) echo 'CMD_MOVE_FORWARD';;
-        f | F) echo 'CMD_FAVORITE';;
+        f | F) echo 'CMD_FAVORITE_ADD';;
+        r | R) echo 'CMD_FAVORITE_REMOVE';;
+        s | S) echo 'CMD_SET_CURRENT';;
             *) echo 'CMD_UKNOWN';;
     esac
 }
 
-paths_find() {
-    local -r root_dir="$1"
+paths_set() {
     local path
 
-    IFS=$'\n'
-    for path in $(find "$root_dir" -type f)
+    while read -r path
     do
         if file "$path" | grep 'image data' > /dev/null
         then
             (( n++ ))
             # \r jumps to the beginning of the line:
             printf '\rFound %d images.' "$n" >&2
-            paths["$n"]="$path"
+            paths["$(( n - 1 ))"]="$path"
         fi
     done
-    unset IFS
     printf '\n' >&2
 }
 
+paths_set_from_dir_find() {
+    paths_set < <(find "$1" -type f)
+}
+
+paths_set_from_favs() {
+    paths_set < <(sort -k 2 "$FILE_WALLPAPER_FAVS" | awk '{sub("^" $1 " +", ""); print}')
+}
+
+set_wallpaper() {
+    local -ri i="$1"
+    local -r path="$2"
+
+    printf '%d of %d %s\n' "$(( i + 1 ))" "$n" "$path"
+    feh --bg-scale "$path"
+}
+
 paths_preview() {
-    local -i i
-    local path
+    local -i i=0
+    local path="${paths[$i]}"
 
-    for ((i=1; i<=n; i++))
-    do
-        path="${paths[$i]}"
+    set_wallpaper "$i" "$path"
 
-        printf '%d of %d : %s\n' "$i" "$n" "$path"
-        feh --bg-scale "$path"
+    while :
+    do
+        if [[ "${paths[$i]}" != "$path" ]]
+        then
+            path="${paths[$i]}"
+            set_wallpaper "$i" "$path"
+        fi
 
         case "$(read_command)" in
             CMD_QUIT)
                 exit 0;;
             CMD_MOVE_BACK)
-                # TODO Cycle around.
-                # One step further back than needed,
-                # to correct for the upcoming i++.
-                ((i = i - 2))
-                if [[ i -lt 0 ]]; then
-                    i=-1
-                fi;;
+                (( i = i == 0 ? (n - 1) : i - 1));;
             CMD_MOVE_FORWARD)
+                (( i = (i + 1) % n ));;
+            CMD_FAVORITE_ADD)
+                printf 'Adding to favorites set: "%s"\n' "$path" >&2
+                wallpaper_fav;;
+            CMD_FAVORITE_REMOVE)
+                printf 'Removing from favorites set: "%s"\n' "$path" >&2
+                digest=$(sha256sum "$path" | awk '{print $1}')
+                grep -v "$digest" "$FILE_WALLPAPER_FAVS" \
+                | sort -u \
+                | sponge "$FILE_WALLPAPER_FAVS"
                 ;;
-            CMD_FAVORITE)
-                wallpaper_fav
-                ((i--));;
+            CMD_SET_CURRENT)
+                printf 'Setting as current: "%s"\n' "$path" >&2
+                echo "$path" > "$FILE_WALLPAPER_CURR";;
             CMD_UKNOWN)
-                # Remain
-                ((i--));;
+                continue;;
         esac
     done
 }
 
 main() {
-    local root_dir
-
     case "$1" in
-        '') root_dir="$DIR_WALLPAPERS";;
-        *) root_dir="$1";;
+        f | fav) paths_set_from_favs;;
+        d | dir) paths_set_from_dir_find "$2";;
+        a | all) paths_set_from_dir_find "$DIR_WALLPAPERS";;
+        '')
+            paths_set_from_dir_find "$DIR_WALLPAPERS";;
+        *)
+            printf 'Error: unknown source "%s"\n' "$1" >&2
+            exit 1;;
     esac
 
-    paths_find "$root_dir"
     paths_preview
 }
 
This page took 0.031656 seconds and 4 git commands to generate.