Support removing from favorites set
[khome.git] / home / bin / wallpapers_preview
index 7d92de3..c859451 100755 (executable)
@@ -15,17 +15,16 @@ 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';;
             *) 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
@@ -35,10 +34,17 @@ paths_find() {
             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"
@@ -68,8 +74,16 @@ paths_preview() {
                 (( i = i == 0 ? (n - 1) : i - 1));;
             CMD_MOVE_FORWARD)
                 (( i = (i + 1) % n ));;
-            CMD_FAVORITE)
+            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_UKNOWN)
                 continue;;
         esac
@@ -77,14 +91,17 @@ paths_preview() {
 }
 
 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.028867 seconds and 4 git commands to generate.