When receiving contiguous non-empty OSC 52 clipboard writes, combine them.

Max combined text is set at 1MB. Allows sending texts larger than the
escape code size limit.
This commit is contained in:
Kovid Goyal 2018-05-28 21:50:42 +05:30
parent 88d0499818
commit dab57f3819
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 23 additions and 4 deletions

View File

@ -235,7 +235,15 @@ def write_to_clipboard(data, use_primary=False) -> str:
if isinstance(data, str): if isinstance(data, str):
data = data.encode('utf-8') data = data.encode('utf-8')
from base64 import standard_b64encode from base64 import standard_b64encode
return '\x1b]52;{};{}\x07'.format('p' if use_primary else 'c', standard_b64encode(data).decode('ascii')) fmt = 'p' if use_primary else 'c'
def esc(chunk):
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)):
chunk = standard_b64encode(chunk).decode('ascii')
ans += esc(chunk)
return ans
def request_from_clipboard(use_primary=False) -> str: def request_from_clipboard(use_primary=False) -> str:

View File

@ -99,6 +99,7 @@ class Window:
self.overlay_for = None self.overlay_for = None
self.child_title = appname self.child_title = appname
self.id = add_window(tab.os_window_id, tab.id, self.title) self.id = add_window(tab.os_window_id, tab.id, self.title)
self.clipboard_control_buffers = {'p': '', 'c': ''}
if not self.id: if not self.id:
raise Exception('No tab with id: {} in OS Window: {} was found, or the window counter wrapped'.format(tab.id, tab.os_window_id)) raise Exception('No tab with id: {} in OS Window: {} was found, or the window counter wrapped'.format(tab.id, tab.os_window_id))
self.tab_id = tab.id self.tab_id = tab.id
@ -362,15 +363,25 @@ class Window:
text = standard_b64decode(text).decode('utf-8') text = standard_b64decode(text).decode('utf-8')
except Exception: except Exception:
text = '' text = ''
def write(key, func):
if text:
if len(self.clipboard_control_buffers[key]) > 1024*1024:
self.clipboard_control_buffers[key] = ''
self.clipboard_control_buffers[key] += text
else:
self.clipboard_control_buffers[key] = ''
func(self.clipboard_control_buffers[key])
if 's' in where or 'c' in where: if 's' in where or 'c' in where:
if 'write-clipboard' in self.opts.clipboard_control: if 'write-clipboard' in self.opts.clipboard_control:
set_clipboard_string(text) write('c', set_clipboard_string)
if 'p' in where: if 'p' in where:
if self.opts.copy_on_select: if self.opts.copy_on_select:
if 'write-clipboard' in self.opts.clipboard_control: if 'write-clipboard' in self.opts.clipboard_control:
set_clipboard_string(text) write('c', set_clipboard_string)
if 'write-primary' in self.opts.clipboard_control: if 'write-primary' in self.opts.clipboard_control:
set_primary_selection(text) write('p', set_primary_selection)
# }}} # }}}
def text_for_selection(self): def text_for_selection(self):