From dab57f3819cadaf44ffda465e0677bcccc634f2e Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 28 May 2018 21:50:42 +0530 Subject: [PATCH] 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. --- kittens/tui/operations.py | 10 +++++++++- kitty/window.py | 17 ++++++++++++++--- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/kittens/tui/operations.py b/kittens/tui/operations.py index a5784e7a9..920bcd364 100644 --- a/kittens/tui/operations.py +++ b/kittens/tui/operations.py @@ -235,7 +235,15 @@ def write_to_clipboard(data, use_primary=False) -> str: if isinstance(data, str): data = data.encode('utf-8') 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: diff --git a/kitty/window.py b/kitty/window.py index 3e0dc88ac..65ddf01eb 100644 --- a/kitty/window.py +++ b/kitty/window.py @@ -99,6 +99,7 @@ class Window: self.overlay_for = None self.child_title = appname self.id = add_window(tab.os_window_id, tab.id, self.title) + self.clipboard_control_buffers = {'p': '', 'c': ''} 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)) self.tab_id = tab.id @@ -362,15 +363,25 @@ class Window: text = standard_b64decode(text).decode('utf-8') except Exception: 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 'write-clipboard' in self.opts.clipboard_control: - set_clipboard_string(text) + write('c', set_clipboard_string) if 'p' in where: if self.opts.copy_on_select: 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: - set_primary_selection(text) + write('p', set_primary_selection) # }}} def text_for_selection(self):