Get rid of kitty's special OSC 52 protocol

A better solution from an ecosystem perspective is to just work with the
original protocol. I have modified kitty's escape parser to special case
OSC 52 handling without changing its max escape code size.

Basically, it works by splitting up OSC 52 escape codes longer than the
max size into a series of partial OSC 52 escape codes. These get
dispatched to the UI layer where it accumulates them upto the 8MB limit
and then sends to clipboard when the partial sequence ends.

See https://github.com/ranger/ranger/issues/1861
This commit is contained in:
Kovid Goyal
2021-07-23 22:12:04 +05:30
parent 096c4c78c7
commit 8f214c51c0
12 changed files with 147 additions and 71 deletions

View File

@@ -352,24 +352,17 @@ def set_default_colors(
@cmd
def write_to_clipboard(data: Union[str, bytes], use_primary: bool = False) -> str:
if isinstance(data, str):
data = data.encode('utf-8')
from base64 import standard_b64encode
fmt = 'p' if use_primary else 'c'
def esc(chunk: str) -> str:
return '\x1b]52;{};{}\x07'.format(fmt, chunk)
ans = esc('!') # clear clipboard buffer
for chunk in (data[i:i+512] for i in range(0, len(data), 512)):
s = standard_b64encode(chunk).decode('ascii')
ans += esc(s)
return ans
if isinstance(data, str):
data = data.encode('utf-8')
payload = standard_b64encode(data).decode('ascii')
return f'\x1b]52;{fmt};{payload}\a'
@cmd
def request_from_clipboard(use_primary: bool = False) -> str:
return '\x1b]52;{};?\x07'.format('p' if use_primary else 'c')
return '\x1b]52;{};?\a'.format('p' if use_primary else 'c')
# Boilerplate to make operations available via Handler.cmd {{{