sistema_progs

Programas para customizar o meu entorno de traballo nos meus equipos persoais
Log | Files | Refs

dragdrop (1884B)


      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 else
     23     dnd="dragon"
     24 fi
     25 
     26 add_file ()
     27 {
     28     printf '%s\0' "$@" >> "$selection"
     29 }
     30 
     31 use_all ()
     32 {
     33     printf "mark --all (a) [default=none]: "
     34     read -r resp
     35     if [ "$resp" = "a" ]; then
     36         all="--all"
     37     else
     38         all=""
     39     fi
     40 }
     41 
     42 if [ -s "$selection" ]; then
     43     printf "Drop file (r). Drag selection (s), Drag current directory (d) or drag current file (f) [default=f]: "
     44     read -r resp
     45 else
     46     printf "Drop file (r). Drag current directory (d) or drag current file (f) [default=f]: "
     47     read -r resp
     48     if [ "$resp" = "s" ]; then
     49         resp=f
     50     fi
     51 fi
     52 
     53 if [ "$resp" = "s" ]; then
     54     use_all
     55     sed -z 's|'"$PWD/"'||g' < "$selection" | xargs -0 "$dnd" "$all" &
     56 elif [ "$resp" = "d" ]; then
     57     use_all
     58     "$dnd" "$all" "$PWD/"* &
     59 elif [ "$resp" = "r" ]; then
     60     true > "$selection"
     61     "$dnd" --print-path --target | while read -r f
     62     do
     63             if printf "%s" "$f" | grep '^\(https\?\|ftps\?\|s\?ftp\):\/\/' ; then
     64                     curl -LJO "$f"
     65                     add_file "$PWD/$(basename "$f")"
     66             elif [ -e "$f" ]; then
     67                     add_file "$f"
     68             fi
     69     done &
     70 else
     71     if [ -n "$1" ] && [ -e "$1" ]; then
     72         "$dnd" "$1" &
     73     fi
     74 fi
     75