From c767dd0d0dab7f320879fd35c635478b10519404 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 27 Jul 2023 13:47:22 +0530 Subject: [PATCH] kitty @ ls: Add user variables set on windows to the output These can be set by programs running in the window using the OSC 1337 escape code, as specified here: https://iterm2.com/documentation-scripting-fundamentals.html#setting-user-defined-variables Fixes #6502 --- docs/changelog.rst | 5 +++++ kitty/window.py | 41 +++++++++++++++++++++++++++-------------- 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 0c820b509..019b263f0 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -35,6 +35,11 @@ mouse anywhere in the current command to move the cursor there. See Detailed list of changes ------------------------------------- +0.30.0 [future] +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +- kitty @ ls: Add user variables set on windows to the output (:iss:`6502`) + 0.29.2 [2023-07-27] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/kitty/window.py b/kitty/window.py index 825a9854e..fe1323bd5 100644 --- a/kitty/window.py +++ b/kitty/window.py @@ -204,6 +204,7 @@ class WindowDict(TypedDict): is_self: bool lines: int columns: int + user_vars: Dict[str, Union[str, Dict[str, str]]] class PipeData(TypedDict): @@ -634,21 +635,31 @@ class Window: def __repr__(self) -> str: return f'Window(title={self.title}, id={self.id})' + @property + def serializeable_user_vars(self) -> Dict[str, Union[str, Dict[str, str]]]: + from base64 import standard_b64encode + def s(x: bytes) -> Union[str, Dict[str, str]]: + with suppress(UnicodeDecodeError): + return x.decode('utf-8') + return {'value': standard_b64encode(x).decode('ascii'), 'encoding': 'base64'} + return {k: s(v) for k, v in self.user_vars.items()} + def as_dict(self, is_focused: bool = False, is_self: bool = False, is_active: bool = False) -> WindowDict: - return dict( - id=self.id, - is_focused=is_focused, - is_active=is_active, - title=self.title, - pid=self.child.pid, - cwd=self.child.current_cwd or self.child.cwd, - cmdline=self.child.cmdline, - env=self.child.environ, - foreground_processes=self.child.foreground_processes, - is_self=is_self, - lines=self.screen.lines, - columns=self.screen.columns, - ) + return { + 'id': self.id, + 'is_focused': is_focused, + 'is_active': is_active, + 'title': self.title, + 'pid': self.child.pid, + 'cwd': self.child.current_cwd or self.child.cwd, + 'cmdline': self.child.cmdline, + 'env': self.child.environ, + 'foreground_processes': self.child.foreground_processes, + 'is_self': is_self, + 'lines': self.screen.lines, + 'columns': self.screen.columns, + 'user_vars': self.serializeable_user_vars, + } def serialize_state(self) -> Dict[str, Any]: ans = { @@ -670,6 +681,8 @@ class Window: ans['window_custom_type'] = self.window_custom_type if self.overlay_type is not OverlayType.transient: ans['overlay_type'] = self.overlay_type.value + if self.user_vars: + ans['user_vars'] = self.serializeable_user_vars return ans @property