]> Sergey Matveev's repositories - nnn.git/blob - plugins/dragdrop
Clear less'es screen
[nnn.git] / plugins / dragdrop
1 #!/usr/bin/env sh
2
3 # Description: Open a Drag and drop window, to drop files onto other programs.
4 #              Also provides drag and drop window for files.
5 #
6 # Dependencies: dragon - https://github.com/mwh/dragon
7 #
8 # Notes:
9 #   1. Files that are dropped will be added to nnn's selection
10 #      Some web-based files will be downloaded to current dir
11 #      with curl and it may overwrite some existing files
12 #   2. The user has to mm to clear nnn's selection first
13 #
14 # Shell: POSIX compliant
15 # Author: 0xACE
16
17 selection=${NNN_SEL:-${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.selection}
18 resp=f
19 all=
20 if type dragon-drag-and-drop >/dev/null 2>&1; then
21     dnd="dragon-drag-and-drop"
22 elif type dragon-drop >/dev/null 2>&1; then
23     dnd="dragon-drop"
24 else
25     dnd="dragon"
26 fi
27
28 add_file ()
29 {
30     printf '%s\0' "$@" >> "$selection"
31 }
32
33 use_all ()
34 {
35     printf "mark --all (a) [default=none]: "
36     read -r resp
37     if [ "$resp" = "a" ]; then
38         all="--all"
39     else
40         all=""
41     fi
42 }
43
44 if [ -s "$selection" ]; then
45     printf "Drop file (r). Drag selection (s), Drag current directory (d) or drag current file (f) [default=f]: "
46     read -r resp
47 else
48     printf "Drop file (r). Drag current directory (d) or drag current file (f) [default=f]: "
49     read -r resp
50     if [ "$resp" = "s" ]; then
51         resp=f
52     fi
53 fi
54
55 if [ "$resp" = "s" ]; then
56     use_all
57     sed -z 's|'"$PWD/"'||g' < "$selection" | xargs -0 "$dnd" "$all" &
58 elif [ "$resp" = "d" ]; then
59     use_all
60     "$dnd" "$all" "$PWD/"* &
61 elif [ "$resp" = "r" ]; then
62     true > "$selection"
63     "$dnd" --print-path --target | while read -r f
64     do
65             if printf "%s" "$f" | grep '^\(https\?\|ftps\?\|s\?ftp\):\/\/' ; then
66                     curl -LJO "$f"
67                     add_file "$PWD/$(basename "$f")"
68             elif [ -e "$f" ]; then
69                     add_file "$f"
70             fi
71     done &
72 else
73     if [ -n "$1" ] && [ -e "$1" ]; then
74         "$dnd" "$1" &
75     fi
76 fi
77