From f8c3a721f714af6c175f94986819e6958cb3363f Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 17 Aug 2025 11:41:02 +0530 Subject: [PATCH] Avoid unneccessarily duplicating --cwd argument for all launch commands in a tab during serialization --- kitty/tabs.py | 10 ++++++++-- kitty/window.py | 12 ++++++++---- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/kitty/tabs.py b/kitty/tabs.py index c8824c8be..3dc95dc2e 100644 --- a/kitty/tabs.py +++ b/kitty/tabs.py @@ -299,10 +299,15 @@ class Tab: # {{{ import shlex launch_cmds = [] active_idx = self.windows.active_group_idx - for i, g in enumerate(self.windows.iter_all_layoutable_groups()): + groups = tuple(self.windows.iter_all_layoutable_groups()) + cwds = {w.id: w.cwd_for_serialization for g in groups for w in g} + from collections import Counter + most_common_cwd, _ = Counter(cwds.values()).most_common(1)[0] + for i, g in enumerate(groups): gw: list[str] = [] for window in g: - lc = window.as_launch_command(ser_opts, is_overlay=bool(gw)) + cwd = cwds[window.id] + lc = window.as_launch_command(ser_opts, '' if cwd == most_common_cwd else cwd, is_overlay=bool(gw)) if lc: gw.append(shlex.join(lc)) if gw: @@ -316,6 +321,7 @@ class Tab: # {{{ f'layout {self._current_layout_name}', f'enabled_layouts {",".join(self.enabled_layouts)}', f'set_layout_state {json.dumps(self.current_layout.serialize(self.windows))}', + f'cd {most_common_cwd}', '' ] + launch_cmds return [] diff --git a/kitty/window.py b/kitty/window.py index e28d036df..d382f84a1 100644 --- a/kitty/window.py +++ b/kitty/window.py @@ -1947,16 +1947,20 @@ class Window: ' 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, ser_opts: SaveAsSessionOptions, is_overlay: bool = False) -> list[str]: + @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 + if self.screen.last_reported_cwd and self.at_prompt and not self.child_is_remote: + cwd = path_from_osc7_url(self.screen.last_reported_cwd) or cwd + return cwd + + def as_launch_command(self, ser_opts: SaveAsSessionOptions, cwd: str, 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 # serializable anyway, so skip. return [] ans = ['launch'] - cwd = self.get_cwd_of_child(oldest=False) or self.get_cwd_of_child(oldest=True) - if self.screen.last_reported_cwd and self.at_prompt and not self.child_is_remote: - cwd = path_from_osc7_url(self.screen.last_reported_cwd) or cwd if cwd: ans.append(f'--cwd={cwd}') if self.allow_remote_control: