forked from 3Xpl0it3r/gonvim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clipboard-provider
executable file
·77 lines (65 loc) · 1.72 KB
/
clipboard-provider
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
#!/bin/bash
# https://github.com/doodleEsc/nvim/blob/master/clipboard-provider
#
# clipboard provider for neovim
#
# :help provider-clipboard
: ${COPY_PROVIDERS:=osc52 pb xclip tmux local}
: ${PASTE_PROVIDERS:=pb xclip tmux local}
: ${TTY:=`(tty || tmux display-message -p '#{pane_tty}') 2>/dev/null | grep /dev/`}
LOCAL_STORAGE=$HOME/.clipboard-provider.out
main() {
declare status=1
case $1 in
copy)
slurp
for p in $COPY_PROVIDERS; do
$p-provider copy && status=0
done ;;
paste)
for p in $PASTE_PROVIDERS; do
$p-provider paste && status=0 && break
done ;;
esac
exit $status
}
# N.B. buffer is global for simplicity
slurp() { buffer=$(base64 | tr -d '\n'); }
spit() { base64 --decode <<<"$buffer"; }
is-copy() {
if [[ "$1" == "copy" ]]; then return 0; else return 1; fi
}
tmux-provider() {
[[ -n $TMUX ]] || return $(is-copy $1)
case $1 in
copy) spit | tmux load-buffer - ;;
paste) tmux save-buffer - ;;
esac
}
pb-provider() {
if ! command -v pbcopy &>/dev/null;then return $(is-copy $1); fi
case $1 in
copy) spit | pbcopy ;;
paste) pbpaste ;;
esac
}
osc52-provider() {
case $1 in
copy) [[ -n "$TTY" ]] && printf $'\e]52;c;%s\a' "$buffer" > "$TTY" ;;
paste) return 1 ;;
esac
}
local-provider() {
case $1 in
copy) spit > $LOCAL_STORAGE ;;
paste) cat $LOCAL_STORAGE && return 0;;
esac
}
xclip-provider() {
[[ -n $DISPLAY ]] && command -v xclip &>/dev/null || return $(is-copy $1)
case $1 in
copy) spit | xclip -i -selection clipboard;;
paste) xclip -o -selection clipboard;;
esac
}
main "$@"