-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshare-files
executable file
·125 lines (101 loc) · 2.37 KB
/
share-files
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env bash
# ┏━╸╻╻ ┏━╸┏━┓
# ┣╸ ┃┃ ┣╸ ┗━┓
# ╹ ╹┗━╸┗━╸┗━┛
# simple file-selector and share with <dragon-drop> | <localsend>
# deps: <nnn> <dragon-drop> <localsend>
set -euo pipefail
PROG="${0##*/}"
TMPFILE="$(mktemp -t "$PROG.XXXXXX")"
# shellcheck source=/dev/null
[[ -f "$HOME/.config/shell/nnn.sh" ]] && . "$HOME/.config/shell/nnn.sh"
function _usage {
cat <<-EOF
Usage: $PROG [COMMAND]
Drop files with <dragon-drop>
Send files with <localsend>
Commands:
-d, --drag Drag and drop files
-s, --send Send files locally
EOF
}
function _detach_cmd {
nohup "$@" >"/dev/null" 2>&1 &
}
function _logerr {
local msg="$1"
_notifyme "$msg"
printf "%s: %s\n" "$PROG" "$msg" >&2
exit 1
}
function _has {
local verbose=false
local c
if [[ $1 == '-v' ]]; then
verbose=true
shift
fi
for c in "$@"; do
c="${c%% *}"
if ! command -v "$c" &>/dev/null; then
[[ "$verbose" == true ]] && _logerr "'$c' not found"
return 1
fi
done
}
function _notifyme {
_has -v notify-send || return
local prog
local mesg="<b>$1</b>"
prog=$(echo "$PROG" | tr '[:lower:]' '[:upper:]')
notify-send -i "gnome-user-share" "$prog" "$mesg"
}
function _preexit {
if [[ -f "$TMPFILE" ]]; then
rm "$TMPFILE" 2>/dev/null
fi
}
function _select_file {
_has -v nnn
nnn -p "$TMPFILE"
}
function _dragon_drop {
_has -v dragon-drop
local file="${1:-}"
if [[ -f "$file" ]]; then
setsid -f dragon-drop -x "$file"
exit 0
fi
_select_file
if [[ -f "$TMPFILE" ]]; then
setsid -f dragon-drop --and-exit --stdin --on-top <"$TMPFILE"
fi
}
function _localsend {
_has -v localsend
local files=("$@")
if [[ ${#files[@]} -eq 0 ]]; then
# select files
_select_file
while IFS= read -r f; do
files+=("$f")
done <"$TMPFILE"
fi
for file in "${files[@]}"; do
echo "f::$file"
done
_notifyme "Sending ${#files[@]} files with localsend..."
_detach_cmd localsend "${files[@]}"
}
trap _preexit EXIT SIGTERM
function main {
case "${1:-}" in
-s | --send)
shift
_localsend "$@"
;;
-h | --help) _usage ;;
*) _dragon_drop "$@" ;;
esac
}
main "$@"