Merged drag and drop scripts to 1 file (#372)

This commit is contained in:
0xACE 2019-11-02 02:24:14 +01:00 committed by Mischievous Meerkat
parent 013dad1186
commit 3c1645ca90
3 changed files with 78 additions and 98 deletions

78
plugins/drag-and-drop Executable file
View File

@ -0,0 +1,78 @@
#!/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=
dnd()
{
if which dragon-drag-and-drop; then
dragon-drag-and-drop "$@"
else
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

View File

@ -1,54 +0,0 @@
#!/usr/bin/env sh
# Description: Open a Drag and drop window, to drop files onto other programs
#
# Dependency: https://github.com/mwh/dragon
# Shell: POSIX compliant
# Author: 0xACE
selection=${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.selection
resp=f
all=
dnd()
{
if which dragon-drag-and-drop; then
dragon-drag-and-drop "$@"
else
dragon "$@"
fi
}
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 "work with selection (s), current working directory (d) or current file (f) [default=f]: "
read resp
else
echo -n "work with current working directory (d) or 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/"* &
else
if [ -n "$1" ] && [ -e "$1" ]; then
dnd "$1" &
fi
fi

View File

@ -1,44 +0,0 @@
#!/usr/bin/env sh
# Description: Provides drag and drop window for files.
#
# Files that are dropped will be added to nnn's selection
# Some web based files will be downloaded to current directory with curl
# and it may overwrite some existing files
#
# The user has to press 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
dnd()
{
if which dragon-drag-and-drop; then
dragon-drag-and-drop "$@"
else
dragon "$@"
fi
}
function add_file() {
echo -n "$@" >> "$selection"
echo -ne "\0" >> "$selection"
}
echo -n > "$selection"
# which dnd
# upstream calls it dragon
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 &