Add a new `last_used_layout` function that can be mapped to a shortcut to switch to the previously used window layout

Fixes #870
This commit is contained in:
Kovid Goyal 2018-09-08 05:56:02 +05:30
parent 7438e0e997
commit f0357b061e
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
4 changed files with 56 additions and 5 deletions

View File

@ -3,6 +3,13 @@ Changelog
|kitty| is a feature full, cross-platform, *fast*, GPU based terminal emulator.
0.12.2 [future]
------------------------------
- Add a new ``last_used_layout`` function that can be mapped to a shortcut to
switch to the previously used window layout (:iss:`870`)
0.12.1 [2018-09-08]
------------------------------

View File

@ -282,7 +282,7 @@ def set_tab_title(boss, window, payload):
# }}}
# set_layout {{{
# goto_layout {{{
@cmd(
'Set the window layout',
'Set the window layout in the specified tab (or the active tab if not specified).'
@ -317,6 +317,34 @@ def goto_layout(boss, window, payload):
# }}}
# last_used_layout {{{
@cmd(
'Switch to the last used layout',
'Switch to the last used window layout in the specified tab (or the active tab if not specified).'
' You can use special match value :italic:`all` to set the layout in all tabs.',
options_spec=MATCH_TAB_OPTION,
)
def cmd_last_used_layout(global_opts, opts, args):
return {'match': opts.match}
def last_used_layout(boss, window, payload):
match = payload['match']
if match:
if match == 'all':
tabs = tuple(boss.all_tabs)
else:
tabs = tuple(boss.match_tabs(match))
if not tabs:
raise MatchError(match, 'tabs')
else:
tabs = [boss.tab_for_window(window) if window else boss.active_tab]
for tab in tabs:
if tab:
tab.last_used_layout()
# }}}
# close_window {{{
@cmd(
'Close the specified window(s)',

View File

@ -155,6 +155,11 @@ You can also create shortcuts to switch to specific layouts::
map ctrl+alt+t goto_layout tall
map ctrl+alt+s goto_layout stack
Similarly, to switch back to the previous layout::
map ctrl+alt+p last_used_layout
''')],
'shortcuts.fonts': [
_('Font sizes'), _('''\

View File

@ -46,10 +46,11 @@ class Tab: # {{{
self.windows = deque()
for i, which in enumerate('first second third fourth fifth sixth seventh eighth ninth tenth'.split()):
setattr(self, which + '_window', partial(self.nth_window, num=i))
self._last_used_layout = self._current_layout_name = None
if session_tab is None:
self.cwd = self.args.directory
sl = self.enabled_layouts[0]
self.current_layout = self.create_layout_object(sl)
self._set_current_layout(sl)
if special_window is None:
self.new_window(cwd_from=cwd_from)
else:
@ -57,9 +58,14 @@ class Tab: # {{{
else:
self.cwd = session_tab.cwd or self.args.directory
l0 = session_tab.layout
self.current_layout = self.create_layout_object(l0)
self._set_current_layout(l0)
self.startup(session_tab)
def _set_current_layout(self, layout_name):
self._last_used_layout = self._current_layout_name
self.current_layout = self.create_layout_object(layout_name)
self._current_layout_name = layout_name
def startup(self, session_tab):
for cmd in session_tab.windows:
if isinstance(cmd, (SpecialWindowInstance,)):
@ -154,7 +160,12 @@ class Tab: # {{{
else:
idx = -1
nl = self.enabled_layouts[(idx + 1) % len(self.enabled_layouts)]
self.current_layout = self.create_layout_object(nl)
self._set_current_layout(nl)
self.relayout()
def last_used_layout(self):
if len(self.enabled_layouts) > 1 and self._last_used_layout and self._last_used_layout != self._current_layout_name:
self._set_current_layout(self._last_used_layout)
self.relayout()
def goto_layout(self, layout_name, raise_exception=False):
@ -164,7 +175,7 @@ class Tab: # {{{
raise ValueError(layout_name)
log_error('Unknown or disabled layout: {}'.format(layout_name))
return
self.current_layout = self.create_layout_object(layout_name)
self._set_current_layout(layout_name)
self.relayout()
def resize_window_by(self, window_id, increment, is_horizontal):