dragdrop (3211B)
1 #!/usr/bin/env sh 2 # Description: Open a Drag and drop window, to drop files onto other programs. 3 # Also provides drag and drop window for files. 4 # 5 # Dependencies: 6 # - dragon (https://github.com/mwh/dragon), or 7 # - kitty (https://github.com/kovidgoyal/kitty) 8 # 9 # Notes if using dragon: 10 # 1. Files that are dropped will be added to nnn's selection 11 # Some web-based files will be downloaded to current dir 12 # with curl and it may overwrite some existing files 13 # 2. The user has to mm to clear nnn's selection first 14 # 15 # Shell: POSIX compliant 16 # Author: 0xACE 17 18 # If you want system notification, put this equal to 1 19 notify=0 20 21 selection=${NNN_SEL:-${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.selection} 22 resp=f 23 all= 24 if type dragon-drag-and-drop >/dev/null 2>&1; then 25 dnd="dragon-drag-and-drop" 26 elif type dragon-drop >/dev/null 2>&1; then 27 dnd="dragon-drop" 28 elif [ "$TERM" = "xterm-kitty" ]; then 29 dnd="kitten dnd" 30 else 31 dnd="dragon" 32 fi 33 34 add_file() 35 { 36 printf '%s\0' "$@" >>"$selection" 37 } 38 39 use_all() 40 { 41 printf "mark --all (a) [default=none]: " 42 read -r resp 43 if [ "$resp" = "a" ]; then 44 all="--all" 45 else 46 all="" 47 fi 48 } 49 50 if [ -s "$selection" ]; then 51 printf "Drop file (r). Drag selection (s), Drag current directory (d) or drag current file (f) [default=f]: " 52 read -r resp 53 else 54 printf "Drop file (r). Drag current directory (d) or drag current file (f) [default=f]: " 55 read -r resp 56 if [ "$resp" = "s" ]; then 57 resp=f 58 fi 59 fi 60 61 clear_tui() 62 { 63 pid=$! 64 reset # nnn to dnd 65 wait $pid 66 reset # dnd to nnn 67 } 68 69 if [ "$dnd" = "kitten dnd" ]; then 70 if [ "$resp" = "s" ]; then 71 # shellcheck disable=SC2086 72 xargs -0 $dnd 2>/dev/null <"$selection" & 73 elif [ "$resp" = "d" ]; then 74 $dnd "$PWD/"* 2>/dev/null & 75 else # send one or receive 76 $dnd "$1" 2>/dev/null & 77 fi 78 79 clear_tui 80 81 if [ "$resp" = "s" ]; then 82 [ -p "$NNN_PIPE" ] && printf "-" >"$NNN_PIPE" # clear selection 83 fi 84 else 85 if [ "$resp" = "s" ]; then 86 use_all 87 sed -z 's|'"$PWD/"'||g' <"$selection" | xargs -0 "$dnd" "$all" 2>/dev/null & 88 elif [ "$resp" = "d" ]; then 89 use_all 90 "$dnd" "$all" "$PWD/"* 2>/dev/null & 91 elif [ "$resp" = "r" ]; then 92 true >"$selection" 93 "$dnd" --print-path --target 2>/dev/null | while read -r f; do 94 if printf "%s" "$f" | grep -q '^\(https\?\|ftps\?\|s\?ftp\)://'; then 95 if output=$(curl -fsLJOS "$f" 2>&1); then 96 filename="$(basename "${f%%\?*}")" 97 add_file "$PWD/$filename" 98 if [ $notify = 1 ]; then 99 notify-send "Download Complete" "Finished downloading $filename" 100 fi 101 else 102 if [ $notify = 1 ]; then 103 notify-send "Download Failed" "$output" 104 else 105 echo "Download Failed: $output" 106 fi 107 fi 108 elif [ -e "$f" ]; then 109 add_file "$f" 110 fi 111 done & 112 elif [ -n "$1" ] && [ -e "$1" ]; then 113 "$dnd" "$1" 2>/dev/null & 114 fi 115 fi