Print help in wallpaper_review start
[khome.git] / home / bin / wallpaper_review
index c62a54c..66d7e4f 100755 (executable)
@@ -7,18 +7,46 @@
 declare -a paths
 declare -i n=0
 
+declare _command
+declare -i _goto=0
+
+read_command_goto() {
+    local buf
+
+    printf ':' >&2
+    read -r buf
+    printf '\n' >&2
+    if [[ "$buf" =~ ^[0-9]+$ ]]; then
+        if [[ "$buf" -gt 0 && "$buf" -le "$n" ]]; then
+            (( buf-- ))
+            _goto="$buf"
+            _command='CMD_GOTO'
+        else
+            printf 'Error: out of range: %s\n' "$buf" >&2
+            _command='CMD_UKNOWN'
+        fi
+    else
+        printf 'Error: not an integer: %s\n' "$buf" >&2
+        _command='CMD_UKNOWN'
+    fi
+}
+
 read_command() {
     local char
 
     read -rsn 1 char
     case "$char" in
-        q | Q) echo 'CMD_QUIT';;
-        h | H) echo 'CMD_MOVE_BACK';;
-        l | L) echo 'CMD_MOVE_FORWARD';;
-        f | F) echo 'CMD_FAVORITE_ADD';;
-        r | R) echo 'CMD_FAVORITE_REMOVE';;
-        s | S) echo 'CMD_SET_CURRENT';;
-            *) echo 'CMD_UKNOWN';;
+        q | Q) _command='CMD_QUIT';;
+        h | H) _command='CMD_MOVE_BACK';;
+        l | L) _command='CMD_MOVE_FORWARD';;
+        f | F) _command='CMD_FAVORITE_ADD';;
+        r | R) _command='CMD_FAVORITE_REMOVE';;
+        s | S) _command='CMD_SET_CURRENT';;
+        z | Z) _command='CMD_FZF';;
+            :) read_command_goto;;
+            *)
+                printf 'Error: unknown command: %s\n' "$char" >&2
+                _command='CMD_UKNOWN';;
     esac
 }
 
@@ -68,17 +96,20 @@ paths_preview() {
             set_wallpaper "$i" "$path"
         fi
 
-        case "$(read_command)" in
+        read_command
+        case "$_command" in
             CMD_QUIT)
                 feh --bg-scale "$(< "$FILE_WALLPAPER_CURR")"
                 exit 0;;
+            CMD_GOTO)
+                i="$_goto";;
             CMD_MOVE_BACK)
                 (( 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;;
+                wallpaper_fav "$path";;
             CMD_FAVORITE_REMOVE)
                 printf 'Removing from favorites set: "%s"\n' "$path" >&2
                 digest=$(sha256sum "$path" | awk '{print $1}')
@@ -89,12 +120,40 @@ paths_preview() {
             CMD_SET_CURRENT)
                 printf 'Setting as current: "%s"\n' "$path" >&2
                 echo "$path" > "$FILE_WALLPAPER_CURR";;
+            CMD_FZF)
+                i=$(
+                    for j in "${!paths[@]}"; do
+                        printf '%d %s\n' "$((j + 1))" "${paths[j]}"
+                    done \
+                    | fzf -e \
+                    | awk '{print $1 - 1}'
+                );;
             CMD_UKNOWN)
                 continue;;
         esac
     done
 }
 
+help_print() {
+    cat <<EOF >&2
+
+Navigation help:
+
+key | action
+----+----------------------------------------------
+ q  |  quit
+ h  |  move back
+ l  |  move forward
+ f  |  add to favorites
+ r  |  remove from favorites
+ s  |  set current
+ z  |  fuzzy search for next file path
+ :N |  goto Nth image
+----+----------------------------------------------
+
+EOF
+}
+
 main() {
     case "$1" in
         f | fav) paths_set_from_favs;;
@@ -106,7 +165,7 @@ main() {
             printf 'Error: unknown source "%s"\n' "$1" >&2
             exit 1;;
     esac
-
+    help_print
     paths_preview
 }
 
This page took 0.022976 seconds and 4 git commands to generate.