Created
July 21, 2016 19:19
-
-
Save cesarkawakami/81a567af45a16522cb7ee20642feed41 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
import contextlib | |
import io | |
import os | |
import sys | |
inside_tmux = "TMUX" in os.environ | |
def escape_tmux(msg): | |
if inside_tmux: | |
rv = io.BytesIO() | |
while msg: | |
chunk, msg = msg[:100], msg[100:] | |
rv.write("{}{}{}".format( | |
"\033Ptmux;\033", | |
chunk.replace("\033", "\033\033"), | |
"\033\\", | |
)) | |
return rv.getvalue() | |
else: | |
return msg | |
def iterm_copy(msg): | |
return "{}{}{}".format( | |
"\033]1337;CopyToClipboard=\007", | |
msg, | |
"\033]1337;EndCopy\007", | |
) | |
output = sys.stdin.read() | |
output = iterm_copy(output) | |
output = escape_tmux(output) | |
sys.stdout.write(output) | |
sys.stdout.flush() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment