From 1e0b3f808f0885f4a7467767fd2744c298ea7475 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 22 Aug 2025 21:45:18 +0530 Subject: [PATCH] Move serialization code together --- kitty/window.py | 101 ++++++++++++++++++++++++------------------------ 1 file changed, 51 insertions(+), 50 deletions(-) diff --git a/kitty/window.py b/kitty/window.py index 8a3927e55..ba127f0c1 100644 --- a/kitty/window.py +++ b/kitty/window.py @@ -820,55 +820,6 @@ def title(self) -> str: def __repr__(self) -> str: return f'Window(title={self.title}, id={self.id})' - def as_dict(self, is_focused: bool = False, is_self: bool = False, is_active: bool = False) -> WindowDict: - 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, - 'last_reported_cmdline': self.last_cmd_cmdline, - 'last_cmd_exit_status': self.last_cmd_exit_status, - 'env': self.child.environ or self.child.final_env, - 'foreground_processes': self.child.foreground_processes, - 'is_self': is_self, - 'at_prompt': self.at_prompt, - 'lines': self.screen.lines, - 'columns': self.screen.columns, - 'user_vars': self.user_vars, - 'created_at': self.created_at, - 'in_alternate_screen': self.screen.is_using_alternate_linebuf(), - } - - def serialize_state(self) -> dict[str, Any]: - ans = { - 'version': 1, - 'id': self.id, - 'child_title': self.child_title, - 'override_title': self.override_title, - 'default_title': self.default_title, - 'title_stack': list(self.title_stack), - 'allow_remote_control': self.allow_remote_control, - 'remote_control_passwords': self.remote_control_passwords, - 'cwd': self.child.current_cwd or self.child.cwd, - 'env': self.child.environ, - 'cmdline': self.child.cmdline, - 'last_reported_cmdline': self.last_cmd_cmdline, - 'last_cmd_exit_status': self.last_cmd_exit_status, - 'margin': self.margin.serialize(), - 'user_vars': self.user_vars, - 'padding': self.padding.serialize(), - } - if self.window_custom_type: - 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.user_vars - return ans - @property def overlay_parent(self) -> Optional['Window']: tab = self.tabref() @@ -1957,6 +1908,56 @@ def current_mouse_position(self) -> Optional['MousePosition']: ' Return the last position at which a mouse event was received by this window ' return get_mouse_data_for_window(self.os_window_id, self.tab_id, self.id) + # Serialization {{{ + def as_dict(self, is_focused: bool = False, is_self: bool = False, is_active: bool = False) -> WindowDict: + 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, + 'last_reported_cmdline': self.last_cmd_cmdline, + 'last_cmd_exit_status': self.last_cmd_exit_status, + 'env': self.child.environ or self.child.final_env, + 'foreground_processes': self.child.foreground_processes, + 'is_self': is_self, + 'at_prompt': self.at_prompt, + 'lines': self.screen.lines, + 'columns': self.screen.columns, + 'user_vars': self.user_vars, + 'created_at': self.created_at, + 'in_alternate_screen': self.screen.is_using_alternate_linebuf(), + } + + def serialize_state(self) -> dict[str, Any]: + ans = { + 'version': 1, + 'id': self.id, + 'child_title': self.child_title, + 'override_title': self.override_title, + 'default_title': self.default_title, + 'title_stack': list(self.title_stack), + 'allow_remote_control': self.allow_remote_control, + 'remote_control_passwords': self.remote_control_passwords, + 'cwd': self.child.current_cwd or self.child.cwd, + 'env': self.child.environ, + 'cmdline': self.child.cmdline, + 'last_reported_cmdline': self.last_cmd_cmdline, + 'last_cmd_exit_status': self.last_cmd_exit_status, + 'margin': self.margin.serialize(), + 'user_vars': self.user_vars, + 'padding': self.padding.serialize(), + } + if self.window_custom_type: + 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.user_vars + return ans + @property def cwd_for_serialization(self) -> str: cwd = self.get_cwd_of_child(oldest=False) or self.get_cwd_of_child(oldest=True) or self.child.cwd @@ -2055,7 +2056,7 @@ def make_exe_absolute(cmd: list[str], pid: int) -> None: ans.insert(1, unserialize_launch_flag + json.dumps(unserialize_data)) ans.extend(cmd) return ans - + # }}} # actions {{{