Avoid unneccessarily duplicating --cwd argument for all launch commands in a tab during serialization

This commit is contained in:
Kovid Goyal
2025-08-17 11:41:02 +05:30
parent 67b2e859f5
commit f8c3a721f7
2 changed files with 16 additions and 6 deletions

View File

@@ -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 []

View File

@@ -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: