Allow using the full launch command in session files
Note this is slightly backward incompatible.
This commit is contained in:
+18
-48
@@ -4,7 +4,7 @@
|
||||
|
||||
import shlex
|
||||
import sys
|
||||
from typing import Generator, List, Optional, Union
|
||||
from typing import Generator, List, Optional, Sequence, Union
|
||||
|
||||
from .cli_stub import CLIOptions
|
||||
from .config_data import to_layout_names
|
||||
@@ -14,7 +14,6 @@ from .options_stub import Options
|
||||
from .os_window_size import WindowSize, WindowSizeData, WindowSizes
|
||||
from .typing import SpecialWindowInstance
|
||||
from .utils import log_error, resolved_shell
|
||||
from .window import Watchers
|
||||
|
||||
|
||||
def get_os_window_sizing_data(opts: Options, session: Optional['Session'] = None) -> WindowSizeData:
|
||||
@@ -27,44 +26,31 @@ def get_os_window_sizing_data(opts: Options, session: Optional['Session'] = None
|
||||
|
||||
class Tab:
|
||||
|
||||
def __init__(self, opts: Options, name: str, watchers: Watchers):
|
||||
self.windows: List['SpecialWindowInstance'] = []
|
||||
def __init__(self, opts: Options, name: str):
|
||||
from .launch import LaunchSpec
|
||||
self.windows: List[Union[LaunchSpec, 'SpecialWindowInstance']] = []
|
||||
self.name = name.strip()
|
||||
self.active_window_idx = 0
|
||||
self.enabled_layouts = opts.enabled_layouts
|
||||
self.layout = (self.enabled_layouts or ['tall'])[0]
|
||||
self.cwd: Optional[str] = None
|
||||
self.next_title: Optional[str] = None
|
||||
self.watchers: Watchers = watchers.copy()
|
||||
|
||||
|
||||
class Session:
|
||||
|
||||
def __init__(self, default_title: Optional[str] = None):
|
||||
def __init__(self, default_title: Optional[str] = None, default_watchers: Sequence[str] = ()):
|
||||
self.tabs: List[Tab] = []
|
||||
self.default_watchers = list(default_watchers)
|
||||
self.active_tab_idx = 0
|
||||
self.default_title = default_title
|
||||
self.os_window_size: Optional[WindowSizes] = None
|
||||
self.os_window_class: Optional[str] = None
|
||||
self.watchers = Watchers()
|
||||
|
||||
def add_watchers_to_all_windows(self, watchers: Watchers) -> None:
|
||||
def add(w: 'SpecialWindowInstance') -> 'SpecialWindowInstance':
|
||||
if w.watchers is None:
|
||||
return w._replace(watchers=watchers)
|
||||
wt = w.watchers.copy()
|
||||
wt.add(watchers)
|
||||
return w._replace(watchers=wt)
|
||||
|
||||
for tab in self.tabs:
|
||||
tab.windows = [add(w) for w in tab.windows]
|
||||
|
||||
def add_tab(self, opts: Options, name: str = '') -> None:
|
||||
if self.tabs and not self.tabs[-1].windows:
|
||||
del self.tabs[-1]
|
||||
if self.tabs:
|
||||
self.tabs[-1].watchers = self.watchers.copy()
|
||||
self.tabs.append(Tab(opts, name, self.watchers))
|
||||
self.tabs.append(Tab(opts, name))
|
||||
|
||||
def set_next_title(self, title: str) -> None:
|
||||
self.tabs[-1].next_title = title.strip()
|
||||
@@ -75,22 +61,18 @@ class Session:
|
||||
self.tabs[-1].layout = val
|
||||
|
||||
def add_window(self, cmd: Union[None, str, List[str]]) -> None:
|
||||
if cmd:
|
||||
cmd = shlex.split(cmd) if isinstance(cmd, str) else cmd
|
||||
else:
|
||||
cmd = None
|
||||
from .tabs import SpecialWindow
|
||||
from .launch import parse_launch_args
|
||||
if isinstance(cmd, str):
|
||||
cmd = shlex.split(cmd)
|
||||
spec = parse_launch_args(cmd)
|
||||
if self.default_watchers:
|
||||
spec.opts.watcher = list(spec.opts.watcher) + self.default_watchers
|
||||
t = self.tabs[-1]
|
||||
watchers: Optional[Watchers] = None
|
||||
if self.watchers.has_watchers:
|
||||
watchers = self.watchers.copy()
|
||||
sw = SpecialWindow(cmd, cwd=t.cwd, override_title=t.next_title or self.default_title, watchers=watchers)
|
||||
t.windows.append(sw)
|
||||
spec.opts.cwd = spec.opts.cwd or t.cwd
|
||||
t.windows.append(spec)
|
||||
t.next_title = None
|
||||
|
||||
def add_special_window(self, sw: 'SpecialWindowInstance') -> None:
|
||||
if self.watchers.has_watchers:
|
||||
sw = sw._replace(watchers=self.watchers.copy())
|
||||
self.tabs[-1].windows.append(sw)
|
||||
|
||||
def focus(self) -> None:
|
||||
@@ -112,10 +94,7 @@ def parse_session(raw: str, opts: Options, default_title: Optional[str] = None)
|
||||
from .tabs import SpecialWindow
|
||||
for t in ans.tabs:
|
||||
if not t.windows:
|
||||
w: Optional[Watchers] = None
|
||||
if t.watchers.has_watchers:
|
||||
w = t.watchers.copy()
|
||||
t.windows.append(SpecialWindow(cmd=resolved_shell(opts), watchers=w, override_title=default_title))
|
||||
t.windows.append(SpecialWindow(cmd=resolved_shell(opts), override_title=default_title))
|
||||
return ans
|
||||
|
||||
ans = Session(default_title)
|
||||
@@ -133,9 +112,7 @@ def parse_session(raw: str, opts: Options, default_title: Optional[str] = None)
|
||||
ans.add_tab(opts, rest)
|
||||
elif cmd == 'new_os_window':
|
||||
yield finalize_session(ans)
|
||||
wt = ans.watchers
|
||||
ans = Session(default_title)
|
||||
ans.watchers = wt.copy()
|
||||
ans.add_tab(opts, rest)
|
||||
elif cmd == 'layout':
|
||||
ans.set_layout(rest)
|
||||
@@ -155,14 +132,6 @@ def parse_session(raw: str, opts: Options, default_title: Optional[str] = None)
|
||||
ans.os_window_size = WindowSizes(WindowSize(*w), WindowSize(*h))
|
||||
elif cmd == 'os_window_class':
|
||||
ans.os_window_class = rest
|
||||
elif cmd == 'watcher':
|
||||
from .launch import load_watch_modules
|
||||
if rest == 'clear':
|
||||
ans.watchers = Watchers()
|
||||
else:
|
||||
watchers = load_watch_modules((rest,))
|
||||
if watchers is not None:
|
||||
ans.watchers.add(watchers)
|
||||
else:
|
||||
raise ValueError('Unknown command in session file: {}'.format(cmd))
|
||||
yield finalize_session(ans)
|
||||
@@ -194,7 +163,8 @@ def create_sessions(
|
||||
else:
|
||||
yield from parse_session(session_data, opts, getattr(args, 'title', None))
|
||||
return
|
||||
ans = Session()
|
||||
default_watchers = args.watcher if args else ()
|
||||
ans = Session(default_watchers=default_watchers)
|
||||
current_layout = opts.enabled_layouts[0] if opts.enabled_layouts else 'tall'
|
||||
ans.add_tab(opts)
|
||||
ans.tabs[-1].layout = current_layout
|
||||
|
||||
Reference in New Issue
Block a user