aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/dragdrop
blob: 47b325c57c4b1d16bd8c660b1a25857f49a972fd (plain) (blame)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env sh

# Description: Open a Drag and drop window, to drop files onto other programs.
# Also provides drag and drop window for files.
#
# Files that are dropped will be added to nnn's selection
# Some webbased files will be downloaded to current directory with curl
# and it may overwrite some existing files
#
# The user has to mm to clear nnn's selection first
#
# Dependency: https://github.com/mwh/dragon
# Shell: POSIX compliant
# Author: 0xACE

selection=${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.selection
resp=f
all=
if which dragon-drag-and-drop 2>&1 >/dev/null; then
    dnd="dragon-drag-and-drop"
else
    dnd="dragon"
fi

function add_file() {
	echo -n "$@" >> "$selection"
	echo -ne "\0" >> "$selection"
}

function use_all()
{
    echo -n "mark --all (a) [default=none]: "
    read resp
    if [ "$resp" = "a" ]; then
        all="--all"
    else
        all=""
    fi
}

if [ -s "$selection" ]; then
    echo -n "Drop file (r). Drag selection (s), Drag current directory (d) or drag current file (f) [default=f]: "
    read resp
else
    echo -n "Drop file (r). Drag current directory (d) or drag current file (f) [default=f]: "
    read resp
    if [ "$resp" = "s" ]; then
        resp=f
    fi
fi

if [ "$resp" = "s" ]; then
    use_all
    sed -z 's|'"$PWD/"'||g' < "$selection" | xargs -0 "$dnd" "$all" &
elif [ "$resp" = "d" ]; then
    use_all
    "$dnd" "$all" "$PWD/"* &
elif [ "$resp" = "r" ]; then
    echo -n > "$selection"
    "$dnd" --print-path --target | while read f
    do
            if echo -n "$f" | grep '^\(https\?\|ftps\?\|s\?ftp\):\/\/' ; then
                    curl -LJO "$f"
                    add_file "$PWD/$(basename "$f")"
            elif [ -e "$f" ]; then
                    add_file "$f"
            fi
    done &
else
    if [ -n "$1" ] && [ -e "$1" ]; then
        "$dnd" "$1" &
    fi
fi