From 53dbdeeca553b6b17d6d817c93cf85b4aa114668 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 12 Aug 2025 11:37:58 +0530 Subject: [PATCH] Function to serialize tab state as session --- kitty/tabs.py | 18 ++++++++++++++++++ kitty/window.py | 4 ++-- kitty/window_list.py | 6 ++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/kitty/tabs.py b/kitty/tabs.py index dcd0211bb..fd187383e 100644 --- a/kitty/tabs.py +++ b/kitty/tabs.py @@ -288,6 +288,24 @@ def serialize_state(self) -> dict[str, Any]: 'name': self.name, } + def serialize_state_as_session(self) -> list[str]: + import shlex + self._current_layout_name + ans = [ + f'new_tab {self.name}'.rstrip(), + f'layout {self._current_layout_name}', + f'enabled_layouts {",".join(self.enabled_layouts)}' + ] + launch_cmds = [] + for g in self.windows.iter_groups_in_activation_order(): + gw: list[str] = [] + for window in g: + lc = window.as_launch_command(is_overlay=bool(gw)) + if lc: + gw.append(shlex.join(lc)) + launch_cmds.extend(gw) + return (ans + launch_cmds) if launch_cmds else [] + def active_window_changed(self) -> None: w = self.active_window set_active_window(self.os_window_id, self.id, 0 if w is None else w.id) diff --git a/kitty/window.py b/kitty/window.py index 8c4875c3e..fb64eeb07 100644 --- a/kitty/window.py +++ b/kitty/window.py @@ -1943,7 +1943,7 @@ 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) - def as_launch_command(self) -> list[str]: + def as_launch_command(self, is_overlay: bool = False) -> list[str]: ' Return a launch command that can be used to serialize this window. Empty list indicates not serializable. ' if self.actions_on_close or self.actions_on_focus_change or self.actions_on_removal: # such windows are typically UI kittens. The actions are not @@ -1996,7 +1996,7 @@ def as_launch_command(self) -> list[str]: lpos = (f'{ypos}-{xpos}' if ypos else xpos) if xpos else ypos ans.append(f'--logo-position={lpos}') - if self.overlay_parent is not None: + if is_overlay: t = 'overlay-main' if self.overlay_type is OverlayType.main else 'overlay' ans.append(f'--type={t}') diff --git a/kitty/window_list.py b/kitty/window_list.py index 0fba377c1..9cba0e185 100644 --- a/kitty/window_list.py +++ b/kitty/window_list.py @@ -333,6 +333,12 @@ def make_previous_group_active(self, which: int = 1, notify: bool = True) -> Non return self.set_active_group_idx(len(self.groups) - 1, notify=notify) + def iter_groups_in_activation_order(self) -> Iterator[WindowGroup]: + imap = {gid: i for i, gid in enumerate(self.active_group_history)} + def skey(g: WindowGroup) -> int: + return imap.get(g.id, -1) + yield from sorted(self.groups, key=skey) + @property def num_groups(self) -> int: return len(self.groups)