implement --base-dir and --focus-tab to save_as_session

This commit is contained in:
Jackie Li
2025-10-26 11:08:43 +00:00
parent 75537187a8
commit 47b4c94c61
2 changed files with 22 additions and 1 deletions

View File

@@ -627,6 +627,16 @@ If specified, only save all windows (and their parent tabs/OS Windows) that matc
search expression. See :ref:`search_syntax` for details on the search language. In particular if
you want to only save windows that are present in the currently active session,
use :code:`--match=session:.`.
--base-dir
When specified, relative session filenames will be saved to this directory instead of the current
working directory. Absolute paths are not affected by this option.
--focus-tab
type=bool-set
When enabled, add a focus_tab command to the saved session file to preserve the currently active tab.
'''
@@ -634,6 +644,10 @@ def save_as_session_part2(boss: BossType, opts: SaveAsSessionOptions, path: str)
if not path:
return
from .config import atomic_save
# Handle --base-dir option
if opts.base_dir and not os.path.isabs(path):
base_dir = os.path.abspath(os.path.expanduser(opts.base_dir))
path = os.path.join(base_dir, path)
path = os.path.abspath(os.path.expanduser(path))
session = '\n'.join(boss.serialize_state_as_session(path, opts))
os.makedirs(os.path.dirname(path), exist_ok=True)

View File

@@ -1317,7 +1317,10 @@ class TabManager: # {{{
hmap[at.id] = len(self.active_tab_history) + 1
def skey(tab: Tab) -> int:
return hmap.get(tab.id, -1)
for tab in sorted(self, key=skey):
active_tab_index = -1
for i, tab in enumerate(sorted(self, key=skey)):
if tab is self.active_tab:
active_tab_index = i
ans.extend(tab.serialize_state_as_session(session_path, matched_windows, ser_opts))
if ans:
prefix = [] if is_first else ['', '', 'new_os_window']
@@ -1326,6 +1329,10 @@ class TabManager: # {{{
if self.wm_name and self.wm_name != appname:
prefix.append(f'os_window_name {self.wm_name}')
ans = prefix + ans
# Add focus_tab command if --focus-tab option is enabled
if ser_opts.focus_tab and active_tab_index >= 0:
ans.append('')
ans.append(f'focus_tab {active_tab_index}')
return ans
@property