File: openall

package info (click to toggle)
nnn 5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,184 kB
  • sloc: ansic: 11,902; sh: 3,585; makefile: 512; cpp: 80; python: 31; csh: 2
file content (49 lines) | stat: -rwxr-xr-x 1,429 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env bash

# Description: Open selected files in nuke one by one or in oneshot
#
# 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}
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"

    elements=${#targets[@]}

    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

    # Clear selection
    if [ -s "$sel" ] && [ -p "$NNN_PIPE" ]; then
        printf "-" > "$NNN_PIPE"
    fi
elif [ -n "$1" ]; then
    "$OPENER" "$1"
fi