]> Sergey Matveev's repositories - nnn.git/commitdiff
opener improvements
authorArun Prakash Jana <engineerarun@gmail.com>
Fri, 1 Apr 2022 01:46:16 +0000 (07:16 +0530)
committerArun Prakash Jana <engineerarun@gmail.com>
Fri, 1 Apr 2022 03:30:51 +0000 (09:00 +0530)
- show file name
- open hovered if none selected
- open and exit if only one file selected
- code improvements

plugins/openall

index fe382a4e7f6e7cf0ab3da51b24530266746e12ae..5a7941fb5b74099f6b085afdfcd7cb95bb45f6c6 100755 (executable)
@@ -1,42 +1,49 @@
 #!/usr/bin/env bash
 
 # Description: Open selected files in nuke one by one or in oneshot
-#              Tip: keep pressing "Enter" to open files one by one
 #
-# Note: Uses nuke by default, easy to replace with preferred opener
-#       nuke is invoked once for each file in a loop
+# Notes: 1. Opens the hovered file if the selection is empty
+#        2. nuke is the default, set OPENER below for custom
+#        3. Opener is invoked once for each file in a loop
+#        4. Keep pressing "Enter" to open files one by one
 #
 # Shell: bash
 # Author: Arun Prakash Jana
 
 sel=${NNN_SEL:-${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.selection}
-nuke="${XDG_CONFIG_HOME:-$HOME/.config}/nnn/plugins/nuke"
-
-printf "open [A]ll? "
-read -r all
+OPENER="${XDG_CONFIG_HOME:-$HOME/.config}/nnn/plugins/nuke"
 
 if [ -s "$sel" ]; then
     targets=()
     while IFS= read -r -d '' entry || [ -n "$entry" ]; do
         targets+=( "$entry" )
     done < "$sel"
-fi
 
-last=${targets[${#targets[@]}-1]}
+    elements=${#targets[@]}
 
-for entry in "${targets[@]}"; do
-    # replace nuke with your preferred opened below
-    "$nuke" "$entry"
-    if [ "$all" != "A" ] && [ "$entry" != "$last" ]; then
-        printf "press Enter to open next\n"
-        read -r -s -n 1 key
-        if [[ $key != "" ]]; then
-            break
-        fi
+    if (( elements == 1 )); then
+        # If there's only one file selected, open without prompts
+        "$OPENER" "${targets[0]}"
+    else
+        printf "open [A]ll? "
+        read -r all
+
+        for ((index=0; index <= ${#targets[@]}; index++)); do
+            "$OPENER" "${targets[index]}"
+            if [ "$all" != "A" ] && (( index+1 < elements )); then
+                printf "press Enter to open '%s'\n" "${targets[index+1]}"
+                read -r -s -n 1 key
+                if [[ $key != "" ]]; then
+                    break
+                fi
+            fi
+        done
     fi
-done
 
-# Clear selection
-if [ -s "$sel" ] && [ -p "$NNN_PIPE" ]; then
-    printf "-" > "$NNN_PIPE"
+    # Clear selection
+    if [ -s "$sel" ] && [ -p "$NNN_PIPE" ]; then
+        printf "-" > "$NNN_PIPE"
+    fi
+elif [ -n "$1" ]; then
+    "$OPENER" "$1"
 fi