-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclipboard-copy
executable file
·45 lines (34 loc) · 1.12 KB
/
clipboard-copy
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
#! /usr/bin/env zsh
# Original found here: https://blog.d46.us/zsh-tmux-emacs-copy-paste/
# Copies data to clipboard from stdin.
function clipboard-copy() {
emulate -L zsh
# If there's no display, use the OCS-52 ANSI code to allow the host
# terminal emulator to copy into the host clipboard.
if ! has-display; then
local buf=$( cat "$@" )
# Copy to tmux if possible
is-tmux && tmux set-buffer "$buf"
# Otherwise copy to terminal
local len=$( printf %s "$buf" | wc -c )
local max=74994
test $len -gt $max &&
echo "$0: input is $(( len - max )) bytes too long" >&2
# Base64 encode the data.
esc="\033]52;c;$( printf %s "$buf" | head -c $max | base64 | tr -d '\r\n' )\a"
is-tmux && esc="\033Ptmux;\033$esc\033\\"
printf "$esc"
elif is-darwin; then
pbcopy
elif command-exists xclip; then
xclip -in -selection clipboard
elif command-exists xsel; then
xsel --clipboard --input
else
local message="clipboard-copy: Platform $(uname -s) not supported or "
message+="xclip/xsel not installed"
print message >&2
return 1
fi
}
clipboard-copy $@