mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-22 00:08:04 +02:00
Implement startup sessions
This commit is contained in:
92
kitty/session.py
Normal file
92
kitty/session.py
Normal file
@@ -0,0 +1,92 @@
|
||||
#!/usr/bin/env python
|
||||
# vim:fileencoding=utf-8
|
||||
# License: GPL v3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
|
||||
|
||||
import shlex
|
||||
|
||||
from .config import to_layout_names
|
||||
from .constants import shell_path
|
||||
from .layout import all_layouts
|
||||
|
||||
|
||||
class Tab:
|
||||
|
||||
def __init__(self, opts):
|
||||
self.windows = []
|
||||
self.active_window_idx = 0
|
||||
self.enabled_layouts = opts.enabled_layouts
|
||||
self.layout = (self.enabled_layouts or ['tall'])[0]
|
||||
self.cwd = None
|
||||
|
||||
|
||||
class Session:
|
||||
|
||||
def __init__(self):
|
||||
self.tabs = []
|
||||
self.active_tab_idx = 0
|
||||
|
||||
def add_tab(self, opts):
|
||||
self.tabs.append(Tab(opts))
|
||||
|
||||
def set_layout(self, val):
|
||||
if val not in all_layouts:
|
||||
raise ValueError('{} is not a valid layout'.format(val))
|
||||
self.tabs[-1].layout = val
|
||||
|
||||
def add_window(self, cmd):
|
||||
if cmd:
|
||||
cmd = shlex.split(cmd) if isinstance(cmd, str) else cmd
|
||||
else:
|
||||
cmd = None
|
||||
self.tabs[-1].windows.append(cmd)
|
||||
|
||||
def focus(self):
|
||||
self.active_tab_idx = max(0, len(self.tabs) - 1)
|
||||
self.tabs[-1].active_window_idx = max(0, len(self.tabs.windows) - 1)
|
||||
|
||||
def set_enabled_layouts(self, raw):
|
||||
self.tabs[-1].enabled_layouts = to_layout_names(raw)
|
||||
|
||||
def set_cwd(self, val):
|
||||
self.tabs[-1].cwd = val
|
||||
|
||||
|
||||
def parse_session(raw, opts):
|
||||
ans = Session()
|
||||
for line in raw.splitlines():
|
||||
line = line.strip()
|
||||
if line and not line.startswith('#'):
|
||||
cmd, rest = line.partition(' ')[::2]
|
||||
cmd, rest = cmd.strip(), rest.strip()
|
||||
if cmd == 'new_tab':
|
||||
ans.add_tab(opts)
|
||||
elif cmd == 'layout':
|
||||
ans.set_layout(rest)
|
||||
elif cmd == 'launch':
|
||||
ans.add_window(rest)
|
||||
elif cmd == 'focus':
|
||||
ans.focus()
|
||||
elif cmd == 'enabled_layouts':
|
||||
ans.set_enabled_layouts(rest)
|
||||
elif cmd == 'cd':
|
||||
ans.set_cwd(rest)
|
||||
else:
|
||||
raise ValueError('Unknown command in session file: {}'.format(cmd))
|
||||
|
||||
|
||||
def create_session(opts, args):
|
||||
if args.session:
|
||||
with open(args.session) as f:
|
||||
return parse_session(f.read(), opts)
|
||||
ans = Session()
|
||||
if args.window_layout:
|
||||
if args.window_layout not in opts.enabled_layouts:
|
||||
opts.enabled_layouts.insert(0, args.window_layout)
|
||||
current_layout = args.window_layout
|
||||
else:
|
||||
current_layout = opts.enabled_layouts[0] if opts.enabled_layouts else 'tall'
|
||||
ans.add_tab(opts)
|
||||
ans.tabs[-1].layout = current_layout
|
||||
cmd = args.args or [shell_path]
|
||||
ans.add_window(cmd)
|
||||
return ans
|
||||
Reference in New Issue
Block a user