Skip to content

Commit aaa562c

Browse files
committed
fix(load): Accept multiple workspaces
1 parent e6c3d41 commit aaa562c

File tree

1 file changed

+35
-25
lines changed

1 file changed

+35
-25
lines changed

src/tmuxp/cli/load.py

+35-25
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from libtmux.common import has_gte_version
1717
from libtmux.server import Server
1818
from libtmux.session import Session
19+
from tmuxp.types import StrPath
1920

2021
from .. import config, config_reader, exc, log, util
2122
from ..workspacebuilder import WorkspaceBuilder
@@ -29,13 +30,17 @@
2930
)
3031

3132
if t.TYPE_CHECKING:
32-
from typing_extensions import Literal, TypeAlias
33+
from typing_extensions import Literal, NotRequired, TypeAlias, TypedDict
3334

3435
CLIColorsLiteral: TypeAlias = Literal[56, 88]
3536

37+
class OptionOverrides(TypedDict):
38+
detached: NotRequired[bool]
39+
new_session_name: NotRequired[t.Optional[str]]
40+
3641

3742
class CLILoadNamespace(argparse.Namespace):
38-
config_file: str
43+
config_files: t.List[str]
3944
socket_name: t.Optional[str]
4045
socket_path: t.Optional[str]
4146
tmux_config_file: t.Optional[str]
@@ -251,7 +256,7 @@ def _setup_plugins(builder: WorkspaceBuilder) -> Session:
251256

252257

253258
def load_workspace(
254-
config_file: t.Union[pathlib.Path, str],
259+
config_file: StrPath,
255260
socket_name: t.Optional[str] = None,
256261
socket_path: None = None,
257262
tmux_config_file: None = None,
@@ -266,8 +271,8 @@ def load_workspace(
266271
267272
Parameters
268273
----------
269-
config_file : str
270-
absolute path to config file
274+
config_file : list of str
275+
paths or session names to workspace files
271276
socket_name : str, optional
272277
``tmux -L <socket-name>``
273278
socket_path: str, optional
@@ -356,7 +361,7 @@ def load_workspace(
356361
Accessed April 8th, 2018.
357362
"""
358363
# get the canonical path, eliminating any symlinks
359-
if isinstance(config_file, str):
364+
if isinstance(config_file, (str, os.PathLike)):
360365
config_file = pathlib.Path(config_file)
361366

362367
tmuxp_echo(
@@ -486,8 +491,9 @@ def config_file_completion(ctx, params, incomplete):
486491

487492

488493
def create_load_subparser(parser: argparse.ArgumentParser) -> argparse.ArgumentParser:
489-
config_file = parser.add_argument(
490-
"config_file",
494+
config_files = parser.add_argument(
495+
"config_files",
496+
nargs="+",
491497
metavar="config-file",
492498
help="filepath to session or filename of session if in tmuxp config directory",
493499
)
@@ -568,7 +574,7 @@ def create_load_subparser(parser: argparse.ArgumentParser) -> argparse.ArgumentP
568574
try:
569575
import shtab
570576

571-
config_file.complete = shtab.FILE # type: ignore
577+
config_files.complete = shtab.FILE # type: ignore
572578
tmux_config_file.complete = shtab.FILE # type: ignore
573579
log_file.complete = shtab.FILE # type: ignore
574580
except ImportError:
@@ -623,27 +629,31 @@ def command_load(
623629
"append": args.append,
624630
}
625631

626-
if args.config_file is None:
632+
if args.config_files is None or len(args.config_files) == 0:
627633
tmuxp_echo("Enter at least one config")
628634
if parser is not None:
629635
parser.print_help()
630636
sys.exit()
637+
return
631638

632-
config_file = scan_config(args.config_file, config_dir=get_config_dir())
639+
last_idx = len(args.config_files) - 1
640+
original_options = tmux_options.copy()
633641

634-
if isinstance(config_file, str):
635-
load_workspace(config_file, **tmux_options)
636-
elif isinstance(config_file, tuple):
637-
config = list(config_file)
638-
# Load each configuration but the last to the background
639-
for cfg in config[:-1]:
640-
opt = tmux_options.copy()
641-
opt.update({"detached": True, "new_session_name": None})
642-
load_workspace(cfg, **opt)
642+
for idx, config_file in enumerate(args.config_files):
643+
config_file = scan_config(config_file, config_dir=get_config_dir())
643644

644-
# todo: obey the -d in the cli args only if user specifies
645-
load_workspace(config_file[-1], **tmux_options)
646-
else:
647-
raise NotImplementedError(
648-
f"config {type(config_file)} with {config_file} not valid"
645+
detached = tmux_options.pop("detached", original_options.get("detached", False))
646+
new_session_name = tmux_options.pop(
647+
"new_session_name", original_options.get("new_session_name")
648+
)
649+
650+
if last_idx > 0 and idx < last_idx:
651+
detached = True
652+
new_session_name = None
653+
654+
load_workspace(
655+
config_file,
656+
detached=detached,
657+
new_session_name=new_session_name,
658+
**tmux_options,
649659
)

0 commit comments

Comments
 (0)