Cleanup previous PR

This commit is contained in:
Kovid Goyal 2026-11-12 08:47:19 +05:30
parent c5e6bdf1fe
commit a0ac36784f
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
3 changed files with 69 additions and 14 deletions

View file

@ -134,8 +134,8 @@ Detailed list of changes
focusing the wrong tab when some OS Windows or tabs had nothing to save,
also affecting :ref:`restore_after_logout`
- Preserve the session file and show an error when :ac:`save_as_session` with
``--match`` has no windows to save
- :ac:`save_as_session`: Show an error instead of overwriting the session file
with an empty one when there are no windows that can be saved (:pull:`10852`)
- Fix the scrolled back view jumping when new output arrives and
``clear_terminal last_command`` not working or moving the view when scrolled

View file

@ -687,7 +687,7 @@ 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:.`.
If no matching windows can be saved, the action reports an error and cancels the save.
If none of the matching windows can be saved, an error is shown and any existing session file is left unchanged.
--base-dir
@ -706,6 +706,16 @@ saved on Wayland as it does not allow applications to position their windows.
"""
def show_nothing_to_save_error(boss: BossType, opts: SaveAsSessionOptions) -> None:
if opts.match and next(boss.match_windows(opts.match), None) is None:
msg = _('No windows match: {0}').format(opts.match)
elif opts.match:
msg = _('None of the matching windows can be saved in a session, for example, because they are running UI kittens.')
else:
msg = _('None of the windows can be saved in a session, for example, because they are running UI kittens.')
boss.show_error(_('No windows to save'), msg)
def save_as_session_part2(boss: BossType, opts: SaveAsSessionOptions, path: str, path_input_by_user: bool = False) -> None:
if not path:
return
@ -721,8 +731,9 @@ def save_as_session_part2(boss: BossType, opts: SaveAsSessionOptions, path: str,
if path_input_by_user and '.' not in os.path.basename(path):
path += '.kitty-session'
session = '\n'.join(boss.serialize_state_as_session(path, opts))
if opts.match and not session:
boss.show_error(_('No windows to save'), _('No windows matching {0} could be saved.').format(opts.match))
if not session:
# Never overwrite an existing session file with an empty one
show_nothing_to_save_error(boss, opts)
return
os.makedirs(os.path.dirname(path), exist_ok=True)
atomic_save(session.encode(), path)
@ -756,6 +767,9 @@ def save_as_session(boss: BossType, cmdline: Sequence[str]) -> None:
path = seen_session_paths.get(sn) or ''
if path:
save_as_session_part2(boss, opts, path)
elif next(boss.serialize_state_as_session('', opts), None) is None:
# Fail before asking the user for a path that will not be used
show_nothing_to_save_error(boss, opts)
else:
boss.get_save_filepath(
_('Enter the path at which to save the session, usually session files are given the .kitty-session file extension'),

View file

@ -87,24 +87,65 @@ class TestSessions(BaseTest):
(s,) = parse_session('\n'.join(lines), self.set_options())
self.ae((len(s.tabs), s.focus_tab_spec), (2, '1'))
def test_save_as_session_empty_match(self) -> None:
def test_save_as_session_nothing_to_save(self) -> None:
boss = Mock(active_session='work')
serialized: tuple[str, ...] = ('launch',)
matched: tuple[str, ...] = ()
boss.serialize_state_as_session.side_effect = lambda *a: iter(serialized)
boss.match_windows.side_effect = lambda *a: iter(matched)
def save(*args: str) -> None:
for m in (boss.show_error, boss.match_windows, boss.get_save_filepath):
m.reset_mock()
save_as_session(boss, args)
def assert_error(*fragments: str) -> None:
self.ae(boss.show_error.call_count, 1)
title, msg = boss.show_error.call_args.args
self.ae(title, 'No windows to save')
for frag in fragments:
self.assertIn(frag, msg)
match = '--match=title:^editor$'
with TemporaryDirectory() as tmp:
existing = Path(tmp, 'work.kitty-session')
new = Path(tmp, 'new', 'work.kitty-session')
with patch.dict('kitty.session.seen_session_paths', work=str(existing)):
boss.serialize_state_as_session.return_value = iter(('launch',))
save_as_session(boss, ['--save-only', match, '.'])
self.ae(existing.read_bytes(), b'launch')
boss.serialize_state_as_session.return_value = iter(())
with self.subTest(destination='active session'):
save_as_session(boss, [match, '.'])
save('--save-only', match, '.')
self.ae(existing.read_bytes(), b'launch')
with self.subTest(destination='new file'):
save_as_session(boss, ['--save-only', match, str(new)])
boss.show_error.assert_not_called()
with self.subTest(destination='prompt for path, windows closed before answer'):
save(match)
boss.get_save_filepath.assert_called_once()
callback = boss.get_save_filepath.call_args.args[1]
serialized = ()
callback(str(new))
self.assertFalse(new.parent.exists())
self.ae(boss.show_error.call_count, 2)
assert_error('No windows match: title:^editor$')
with self.subTest(destination='active session, no windows match'):
save(match, '.')
self.ae(existing.read_bytes(), b'launch')
assert_error('No windows match: title:^editor$')
boss.match_windows.assert_called_with('title:^editor$')
matched = ('window',)
with self.subTest(destination='active session, matched windows not saveable'):
save(match, '.')
self.ae(existing.read_bytes(), b'launch')
assert_error('None of the matching windows')
with self.subTest(destination='active session, no --match'):
save('--save-only', '.')
self.ae(existing.read_bytes(), b'launch')
assert_error('None of the windows')
boss.match_windows.assert_not_called()
with self.subTest(destination='new file'):
save('--save-only', match, str(new))
self.assertFalse(new.parent.exists())
assert_error('None of the matching windows')
with self.subTest(destination='prompt for path, nothing to save'):
save(match)
boss.get_save_filepath.assert_not_called()
assert_error('None of the matching windows')
boss.edit_file.assert_not_called()
def test_scrollback_for_restore(self) -> None: