2019-11-02 01:24:14 +00:00
|
|
|
#!/usr/bin/env sh
|
|
|
|
|
|
|
|
# Description: Open a Drag and drop window, to drop files onto other programs.
|
2020-05-06 05:12:29 +00:00
|
|
|
# Also provides drag and drop window for files.
|
2019-11-02 01:24:14 +00:00
|
|
|
#
|
2020-05-06 05:29:57 +00:00
|
|
|
# Dependencies: https://github.com/mwh/dragon
|
2019-11-02 01:24:14 +00:00
|
|
|
#
|
2020-05-06 05:12:29 +00:00
|
|
|
# Notes:
|
|
|
|
# - 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
|
2019-11-02 01:24:14 +00:00
|
|
|
#
|
|
|
|
# Shell: POSIX compliant
|
|
|
|
# Author: 0xACE
|
|
|
|
|
2020-04-24 04:42:24 +00:00
|
|
|
selection=${NNN_SEL:-${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.selection}
|
2019-11-02 01:24:14 +00:00
|
|
|
resp=f
|
|
|
|
all=
|
2021-05-14 12:03:28 +00:00
|
|
|
if type dragon-drag-and-drop >/dev/null 2>&1; then
|
2019-11-15 16:27:37 +00:00
|
|
|
dnd="dragon-drag-and-drop"
|
|
|
|
else
|
|
|
|
dnd="dragon"
|
|
|
|
fi
|
2019-11-02 01:24:14 +00:00
|
|
|
|
2019-11-21 20:44:25 +00:00
|
|
|
add_file ()
|
|
|
|
{
|
2019-11-26 12:36:31 +00:00
|
|
|
printf '%s\0' "$@" >> "$selection"
|
2019-11-02 01:24:14 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 20:44:25 +00:00
|
|
|
use_all ()
|
2019-11-02 01:24:14 +00:00
|
|
|
{
|
2019-11-21 20:44:25 +00:00
|
|
|
printf "mark --all (a) [default=none]: "
|
|
|
|
read -r resp
|
2019-11-02 01:24:14 +00:00
|
|
|
if [ "$resp" = "a" ]; then
|
|
|
|
all="--all"
|
|
|
|
else
|
|
|
|
all=""
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
if [ -s "$selection" ]; then
|
2019-11-21 20:44:25 +00:00
|
|
|
printf "Drop file (r). Drag selection (s), Drag current directory (d) or drag current file (f) [default=f]: "
|
|
|
|
read -r resp
|
2019-11-02 01:24:14 +00:00
|
|
|
else
|
2019-11-21 20:44:25 +00:00
|
|
|
printf "Drop file (r). Drag current directory (d) or drag current file (f) [default=f]: "
|
|
|
|
read -r resp
|
2019-11-02 01:24:14 +00:00
|
|
|
if [ "$resp" = "s" ]; then
|
|
|
|
resp=f
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "$resp" = "s" ]; then
|
|
|
|
use_all
|
2019-11-15 16:27:37 +00:00
|
|
|
sed -z 's|'"$PWD/"'||g' < "$selection" | xargs -0 "$dnd" "$all" &
|
2019-11-02 01:24:14 +00:00
|
|
|
elif [ "$resp" = "d" ]; then
|
|
|
|
use_all
|
2019-11-15 16:27:37 +00:00
|
|
|
"$dnd" "$all" "$PWD/"* &
|
2019-11-02 01:24:14 +00:00
|
|
|
elif [ "$resp" = "r" ]; then
|
2019-11-26 12:36:31 +00:00
|
|
|
true > "$selection"
|
2019-11-21 20:44:25 +00:00
|
|
|
"$dnd" --print-path --target | while read -r f
|
2019-11-02 01:24:14 +00:00
|
|
|
do
|
2019-11-21 20:44:25 +00:00
|
|
|
if printf "%s" "$f" | grep '^\(https\?\|ftps\?\|s\?ftp\):\/\/' ; then
|
2019-11-02 01:24:14 +00:00
|
|
|
curl -LJO "$f"
|
|
|
|
add_file "$PWD/$(basename "$f")"
|
|
|
|
elif [ -e "$f" ]; then
|
|
|
|
add_file "$f"
|
|
|
|
fi
|
|
|
|
done &
|
|
|
|
else
|
|
|
|
if [ -n "$1" ] && [ -e "$1" ]; then
|
2019-11-15 16:27:37 +00:00
|
|
|
"$dnd" "$1" &
|
2019-11-02 01:24:14 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|