-
-
Save xero/a8b3f7501112f401684d3c1f4f8e6790 to your computer and use it in GitHub Desktop.
heinous edit of a yank script by samoshkin that tries to push to every clipboard it possibly can
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# heinous edit of a yank script by samoshkin | |
# https://github.com/samoshkin/tmux-config/blob/master/tmux/yank.sh | |
# tries to push to every clipboard it possibly can xD | |
set -eu | |
shopt -s extglob | |
installed() { | |
type "$1" &>/dev/null | |
} | |
# get data either form stdin or from file | |
buf=$(cat "$@") | |
# send to a system clipboard if possible | |
copy_backend="" | |
if installed pbcopy; then | |
copy_backend="pbcopy" | |
elif installed reattach-to-user-namespace; then | |
copy_backend="reattach-to-user-namespace pbcopy" | |
elif [ -n "${DISPLAY-}" ] && installed xsel; then | |
copy_backend="xsel -i --clipboard" | |
elif [ -n "${DISPLAY-}" ] && installed xclip; then | |
copy_backend="xclip -i -f -selection primary | xclip -i -selection clipboard" | |
elif [ -n "${copy_backend_remote_tunnel_port-}" ] && [ "$(ss -n -4 state listening "( sport = $copy_backend_remote_tunnel_port )" | tail -n +2 | wc -l)" -eq 1 ]; then | |
copy_backend="nc localhost $copy_backend_remote_tunnel_port" | |
fi | |
# if copy backend is resolved, copy to local clipboard | |
[ -n "$copy_backend" ] && printf "$buf" | eval "$copy_backend" | |
# copy via OSC 52 ANSI escape sequence to controlling terminal(s) | |
# https://sunaku.github.io/tmux-yank-osc52.html | |
# the maximum length of an OSC 52 escape sequence is 100_000 bytes, of which | |
# 7 bytes are occupied by a "\033]52;c;" header, 1 byte by a "\a" footer, and | |
# 99_992 bytes by the base64-encoded result of 74_994 bytes of copyable text | |
maxlen=74994 | |
buflen=$( printf %s "$buf" | wc -c ) | |
# warn if exceeds maxlen | |
if [ "$buflen" -gt "$maxlen" ]; then | |
printf "rip. input is %d bytes too long" "$(( buflen - maxlen ))" >&2 | |
fi | |
# build OSC 52 ANSI escape sequence | |
esc="\033]52;c;$( printf %s "$buf" | head -c $maxlen | base64 | tr -d '\r\n' )\a" | |
# multiplexin? update the escape sequence | |
if $(tmux has-session 2> /dev/null); then | |
esc="\033Ptmux;\033$esc\033\\" | |
# sent directly to the ssh tty (bad idea) | |
#[ -n "$SSH_TTY" ] && printf "$esc" > "$SSH_TTY" | |
# send to all terminals \o/ | |
for pts in /dev/pts/[0-999]; do | |
printf "$esc" > "$pts" | |
done | |
else | |
# print that shiz raw | |
printf "$esc" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment