From a0ac36784f2d0b943dc6e9b8a2fa8490a9eb935f Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 12 Nov 2026 08:47:19 +0530 Subject: [PATCH] Cleanup previous PR --- docs/changelog.rst | 4 +-- kitty/session.py | 20 +++++++++++--- kitty_tests/sessions.py | 59 ++++++++++++++++++++++++++++++++++------- 3 files changed, 69 insertions(+), 14 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 61f2f3e6e..27bb0e5c0 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -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 diff --git a/kitty/session.py b/kitty/session.py index 121f2d1f5..7042a652b 100644 --- a/kitty/session.py +++ b/kitty/session.py @@ -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'), diff --git a/kitty_tests/sessions.py b/kitty_tests/sessions.py index a5aacd07c..f8e8059be 100644 --- a/kitty_tests/sessions.py +++ b/kitty_tests/sessions.py @@ -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: