Debian Codegraph @

plugins/openall

raw
1 #!/usr/bin/env bash
2
3 # Description: Open selected files in nuke one by one or in oneshot
4 #
5 # Notes: 1. Opens the hovered file if the selection is empty
6 # 2. nuke is the default, set OPENER below for custom
7 # 3. Opener is invoked once for each file in a loop
8 # 4. Keep pressing "Enter" to open files one by one
9 #
10 # Shell: bash
11 # Author: Arun Prakash Jana
12
13 sel=${NNN_SEL:-${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.selection}
14 OPENER="${XDG_CONFIG_HOME:-$HOME/.config}/nnn/plugins/nuke"
15
16 if [ -s "$sel" ]; then
17 targets=()
18 while IFS= read -r -d '' entry || [ -n "$entry" ]; do
19 targets+=( "$entry" )
20 done < "$sel"
21
22 elements=${#targets[@]}
23
24 if (( elements == 1 )); then
25 # If there's only one file selected, open without prompts
26 "$OPENER" "${targets[0]}"
27 else
28 printf "open [A]ll? "
29 read -r all
30
31 for ((index=0; index <= ${#targets[@]}; index++)); do
32 "$OPENER" "${targets[index]}"
33 if [ "$all" != "A" ] && (( index+1 < elements )); then
34 printf "press Enter to open '%s'\n" "${targets[index+1]}"
35 read -r -s -n 1 key
36 if [[ $key != "" ]]; then
37 break
38 fi
39 fi
40 done
41 fi
42
43 # Clear selection
44 if [ -s "$sel" ] && [ -p "$NNN_PIPE" ]; then
45 printf "-" > "$NNN_PIPE"
46 fi
47 elif [ -n "$1" ]; then
48 "$OPENER" "$1"
49 fi