-
Notifications
You must be signed in to change notification settings - Fork 0
/
pbcopy
executable file
·89 lines (71 loc) · 2.68 KB
/
pbcopy
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
#! /usr/bin/env python3
# From: https://github.com/skaji/remote-pbcopy-iterm2/blob/master/README.md
# What a PITA - tmux seems to support native OC52 codes in latest ... bleh!
# See configuration options here; https://github.com/tmux/tmux/wiki/Clipboard#quick-summary
# NOTE: Note may need to change the vim-oc52 plugin to use raw codes not the old tmux codes
# Talk about Janky!
# Usage - link into the bin directory on unix to replace the version on osx
import base64
import os
import subprocess
import sys
from typing import Callable
from icecream import ic
def normal_to_clipboard(b64: str) -> str:
return "\x1B]52;c;" + b64 + "\a"
# Seems like on mosh atleast, normal escape sequence just works ...
def tmux_to_clipboard(b64: str) -> str:
# return "\ePtmux;\e\e]52;c;" . b64 . "\x07\e\\"
return ""
def screen_to_clipboard(b64: str) -> str:
out = []
for i in range(sys.maxsize):
begin, end = i * 76, min((i + 1) * 76, len(b64))
out.append(
("\x1BP\x1B]52;;" if begin == 0 else "\x1B\x5C\x1BP") + b64[begin:end]
)
if end == len(b64):
break
out.append("\x07\x1B\x5C")
return "".join(out)
def is_tmux_cc(pid: str) -> bool:
try:
out = subprocess.check_output(["ps", "-p", pid, "-o", "command="])
out = out.rstrip(b"\n\r")
for arg in out.split(b" "):
if arg == b"-CC":
return True
return False
except subprocess.CalledProcessError:
return False
def run():
if len(sys.argv) == 1:
b = sys.stdin.buffer.read()
else:
if sys.argv[1] == "-h" or sys.argv[1] == "--help":
print("Usage:\n pbcopy some string\n some-command | pbcopy\n", end="")
sys.exit(1)
# Ah, arcane argv
all_params = " ".join(sys.argv[1:])
b=all_params.encode('utf-8')
new_clipboard_content = b.rstrip(b"\n\r")
b64_clipboard_content = base64.b64encode(new_clipboard_content).decode(
encoding="UTF-8"
)
# ic(new_clipboard_content)
# ic(b64_clipboard_content)
# clipboard_targets = [tmux_to_clipboard, screen_to_clipboard, normal_to_clipboard]:
clipboard_targets = [normal_to_clipboard]
for to_clipboard in clipboard_targets:
to_clipboard_string = to_clipboard(b64_clipboard_content)
# ic(to_clipboard_string)
print(to_clipboard_string, end="")
# also copy into tmp file (create new one daily)
import datetime
import os
filename = os.path.expanduser(f"~/tmp/pbcopy_{datetime.date.today()}")
with open(filename, 'a+') as fp:
fp.write('\n-------------\n')
fp.write(new_clipboard_content.decode())
if __name__ == "__main__":
run()