feat(session): restore the last session from a keybinding
Besides the --session=last startup option, the saved state can now be brought back in a running kitty with restore_last_session, mapped to kitty_mod+alt+r. The option types are regenerated so the shortcut is actually part of the default configuration.
This commit is contained in:
7 files changed
+62
-1
No files matched your search
@@ -48,6 +48,10 @@ To restore it, start kitty with:
|
||||
|
||||
kitty --session=last
|
||||
|
||||
The same saved state can be restored inside a running kitty with the
|
||||
:ac:`restore_last_session` action, which is mapped to :kbd:`kitty_mod+alt+r`
|
||||
by default and adds the saved tabs to the current window.
|
||||
|
||||
Restoring is never automatic: without this option the saved state is ignored,
|
||||
so an unexpected state is never imposed on you. A state file that is corrupt,
|
||||
unreadable or too large is discarded and kitty starts normally. :code:`cd`
|
||||
|
||||
@@ -3959,6 +3959,13 @@ class Boss:
|
||||
def goto_session(self, *cmdline: str) -> None:
|
||||
goto_session(self, cmdline)
|
||||
|
||||
@ac('session', 'Restore the tabs and windows of the session saved when kitty last quit. See :ref:`sessions`.')
|
||||
def restore_last_session(self) -> None:
|
||||
from .session_restore import restore_last_session_into
|
||||
|
||||
if not restore_last_session_into(self):
|
||||
self.ring_bell_if_allowed()
|
||||
|
||||
@ac('session', 'Save the current kitty state as a session file. See :ref:`save_as_session`.')
|
||||
def save_as_session(self, *cmdline: str) -> None:
|
||||
save_as_session(self, cmdline)
|
||||
|
||||
@@ -4649,6 +4649,23 @@ shortcuts. For a full description see :doc:`hints kitten </kittens/hints>`.
|
||||
""") # }}}
|
||||
|
||||
|
||||
# shortcuts.session {{{
|
||||
agr('shortcuts.session', 'Sessions')
|
||||
|
||||
map(
|
||||
'Restore the last session',
|
||||
'restore_last_session --allow-fallback=shifted,ascii kitty_mod+alt+r restore_last_session',
|
||||
long_text="""
|
||||
Restores the tabs, windows, layouts and working directories of the session that
|
||||
was saved when kitty last quit, without restarting kitty. The state is written
|
||||
by :code:`quit`; see :ref:`sessions`. If there is nothing to restore, a bell is
|
||||
rung instead.
|
||||
""",
|
||||
)
|
||||
|
||||
egr() # }}}
|
||||
|
||||
|
||||
# shortcuts.misc {{{
|
||||
agr('shortcuts.misc', 'Miscellaneous')
|
||||
|
||||
|
||||
Generated
+1
@@ -4,6 +4,7 @@
|
||||
#include "to-c.h"
|
||||
|
||||
|
||||
|
||||
static void
|
||||
convert_from_python_font_size(PyObject *val, Options *opts) {
|
||||
opts->font_size = PyFloat_AsDouble(val);
|
||||
|
||||
Generated
+3
-1
@@ -1024,12 +1024,14 @@ defaults.map = [
|
||||
KeyDefinition(is_sequence=True, trigger=SingleKey(mods=256, key=112), rest=(SingleKey(key=110),), options=KeyMapOptions(when_focus_on='', new_mode='', mode='', on_unknown='beep', on_action='keep', timeout=None, allow_fallback=(KeyFallbackType.shifted, KeyFallbackType.alternate)), definition='kitten hints --type linenum'),
|
||||
# open_selected_hyperlink
|
||||
KeyDefinition(is_sequence=True, trigger=SingleKey(mods=256, key=112), rest=(SingleKey(key=121),), options=KeyMapOptions(when_focus_on='', new_mode='', mode='', on_unknown='beep', on_action='keep', timeout=None, allow_fallback=(KeyFallbackType.shifted, KeyFallbackType.alternate)), definition='kitten hints --type hyperlink'),
|
||||
# restore_last_session
|
||||
KeyDefinition(trigger=SingleKey(mods=258, key=114), options=KeyMapOptions(when_focus_on='', new_mode='', mode='', on_unknown='beep', on_action='keep', timeout=None, allow_fallback=(KeyFallbackType.shifted, KeyFallbackType.alternate)), definition='restore_last_session'),
|
||||
# show_kitty_doc
|
||||
KeyDefinition(trigger=SingleKey(mods=256, key=57364), definition='show_kitty_doc overview'),
|
||||
# command_palette
|
||||
KeyDefinition(trigger=SingleKey(mods=256, key=57366), definition='command_palette'),
|
||||
# file_manager
|
||||
KeyDefinition(trigger=SingleKey(mods=5, key=101), definition='launch --type=window-dock --dock-edge=right --dock-size=60 kitten file-manager'),
|
||||
KeyDefinition(trigger=SingleKey(mods=5, key=101), definition='launch --type=window-dock --dock-edge=left --dock-size=60 kitten file-manager'),
|
||||
# toggle_fullscreen
|
||||
KeyDefinition(trigger=SingleKey(mods=256, key=57374), definition='toggle_fullscreen'),
|
||||
# toggle_maximized
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
import json
|
||||
import os
|
||||
import time
|
||||
from contextlib import suppress
|
||||
from typing import TYPE_CHECKING, Any
|
||||
|
||||
from .config import atomic_save
|
||||
@@ -135,3 +136,27 @@ def save_session_of(boss: 'BossType') -> None:
|
||||
save_last_session(session)
|
||||
except Exception as e:
|
||||
log_error(f'Failed to save the last session: {e}')
|
||||
|
||||
|
||||
def restore_last_session_into(boss: 'BossType') -> bool:
|
||||
"""Add the tabs and windows of the last saved session to a running kitty.
|
||||
|
||||
Returns False when there is nothing to restore. The saved state is a JSON document, so it is
|
||||
handed to the session parser through a short lived file rather than duplicating the parsing.
|
||||
"""
|
||||
raw = load_last_session()
|
||||
if not raw:
|
||||
return False
|
||||
import tempfile
|
||||
|
||||
from .session import create_session
|
||||
|
||||
fd, path = tempfile.mkstemp(prefix='last-session-', suffix='.kitty-session')
|
||||
try:
|
||||
with open(fd, 'w', encoding='utf-8') as f:
|
||||
f.write(raw)
|
||||
create_session(boss, path)
|
||||
finally:
|
||||
with suppress(OSError):
|
||||
os.unlink(path)
|
||||
return True
|
||||
@@ -138,3 +138,8 @@ class TestSessionRestore(BaseTest):
|
||||
self.ae(len(sessions), 1)
|
||||
self.ae(sessions[0].session_name, '')
|
||||
self.assertTrue(any('no saved session' in msg for msg in self.error_messages))
|
||||
|
||||
def test_restore_action_without_state_returns_false(self) -> None:
|
||||
from kitty.session_restore import restore_last_session_into
|
||||
|
||||
self.assertFalse(restore_last_session_into(None)) # type: ignore
|
||||
Reference in new issue
Block a user