Add support for OSC 1337 SetUserVar

See https://github.com/kovidgoyal/kitty/discussions/6229
This commit is contained in:
Kovid Goyal 2023-05-11 17:57:45 +05:30
parent 6a2edfa847
commit 5b8b91b6a3
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 22 additions and 1 deletions

View File

@ -422,6 +422,7 @@ dispatch_osc(Screen *screen, PyObject DUMP_UNUSED *dump_callback) {
case 9: case 9:
case 99: case 99:
case 777: case 777:
case 1337:
START_DISPATCH START_DISPATCH
DISPATCH_OSC_WITH_CODE(desktop_notify) DISPATCH_OSC_WITH_CODE(desktop_notify)
END_DISPATCH END_DISPATCH

View File

@ -597,6 +597,7 @@ class Window:
self.default_title = os.path.basename(child.argv[0] or appname) self.default_title = os.path.basename(child.argv[0] or appname)
self.child_title = self.default_title self.child_title = self.default_title
self.title_stack: Deque[str] = deque(maxlen=10) self.title_stack: Deque[str] = deque(maxlen=10)
self.user_vars: Dict[str, bytes] = {}
self.id: int = add_window(tab.os_window_id, tab.id, self.title) self.id: int = add_window(tab.os_window_id, tab.id, self.title)
self.clipboard_request_manager = ClipboardRequestManager(self.id) self.clipboard_request_manager = ClipboardRequestManager(self.id)
self.margin = EdgeWidths() self.margin = EdgeWidths()
@ -922,7 +923,27 @@ class Window:
self.override_title = title or None self.override_title = title or None
self.title_updated() self.title_updated()
def set_user_var(self, key: str, val: Optional[bytes]) -> None:
self.user_vars.pop(key, None) # ensure key will be newest in user_vars even if already present
if len(self.user_vars) > 64: # dont store too many user vars
oldest_key = next(iter(self.user_vars))
self.user_vars.pop(oldest_key)
if val is not None:
self.user_vars[key] = val
# screen callbacks {{{
def osc_1337(self, raw_data: str) -> None:
for record in raw_data.split(';'):
key, _, val = record.partition('=')
if key == 'SetUserVar':
from base64 import standard_b64decode
ukey, has_equal, uval = val.partition('=')
self.set_user_var(ukey, (standard_b64decode(uval) if uval else b'') if has_equal == '=' else None)
def desktop_notify(self, osc_code: int, raw_data: str) -> None: def desktop_notify(self, osc_code: int, raw_data: str) -> None:
if osc_code == 1337:
self.osc_1337(raw_data)
if osc_code == 777: if osc_code == 777:
if not raw_data.startswith('notify;'): if not raw_data.startswith('notify;'):
log_error(f'Ignoring unknown OSC 777: {raw_data}') log_error(f'Ignoring unknown OSC 777: {raw_data}')
@ -932,7 +953,6 @@ class Window:
if cmd is not None and osc_code == 99: if cmd is not None and osc_code == 99:
self.prev_osc99_cmd = cmd self.prev_osc99_cmd = cmd
# screen callbacks {{{
def use_utf8(self, on: bool) -> None: def use_utf8(self, on: bool) -> None:
get_boss().child_monitor.set_iutf8_winid(self.id, on) get_boss().child_monitor.set_iutf8_winid(self.id, on)