Skip to content

Commit 484a339

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

File tree

1 file changed

+41
-30
lines changed

1 file changed

+41
-30
lines changed

src/tmuxp/cli/load.py

Lines changed: 41 additions & 30 deletions
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+
workspace_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+
workspace_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+
workspace_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,18 +361,19 @@ def load_workspace(
356361
Accessed April 8th, 2018.
357362
"""
358363
# get the canonical path, eliminating any symlinks
359-
if isinstance(config_file, str):
360-
config_file = pathlib.Path(config_file)
364+
if isinstance(workspace_file, (str, os.PathLike)):
365+
workspace_file = pathlib.Path(workspace_file)
361366

362367
tmuxp_echo(
363-
style("[Loading] ", fg="green") + style(str(config_file), fg="blue", bold=True)
368+
style("[Loading] ", fg="green")
369+
+ style(str(workspace_file), fg="blue", bold=True)
364370
)
365371

366372
# ConfigReader allows us to open a yaml or json file as a dict
367-
raw_config = config_reader.ConfigReader._from_file(config_file) or {}
373+
raw_config = config_reader.ConfigReader._from_file(workspace_file) or {}
368374

369375
# shapes configurations relative to config / profile file location
370-
sconfig = config.expand(raw_config, cwd=os.path.dirname(config_file))
376+
sconfig = config.expand(raw_config, cwd=os.path.dirname(workspace_file))
371377
# Overwrite session name
372378
if new_session_name:
373379
sconfig["session_name"] = new_session_name
@@ -388,7 +394,7 @@ def load_workspace(
388394
sconf=sconfig, plugins=load_plugins(sconfig), server=t
389395
)
390396
except exc.EmptyConfigException:
391-
tmuxp_echo("%s is empty or parsed no config data" % config_file)
397+
tmuxp_echo("%s is empty or parsed no config data" % workspace_file)
392398
return None
393399

394400
session_name = sconfig["session_name"]
@@ -486,8 +492,9 @@ def config_file_completion(ctx, params, incomplete):
486492

487493

488494
def create_load_subparser(parser: argparse.ArgumentParser) -> argparse.ArgumentParser:
489-
config_file = parser.add_argument(
490-
"config_file",
495+
workspace_files = parser.add_argument(
496+
"workspace_files",
497+
nargs="+",
491498
metavar="config-file",
492499
help="filepath to session or filename of session if in tmuxp config directory",
493500
)
@@ -568,7 +575,7 @@ def create_load_subparser(parser: argparse.ArgumentParser) -> argparse.ArgumentP
568575
try:
569576
import shtab
570577

571-
config_file.complete = shtab.FILE # type: ignore
578+
workspace_files.complete = shtab.FILE # type: ignore
572579
tmux_config_file.complete = shtab.FILE # type: ignore
573580
log_file.complete = shtab.FILE # type: ignore
574581
except ImportError:
@@ -623,27 +630,31 @@ def command_load(
623630
"append": args.append,
624631
}
625632

626-
if args.config_file is None:
633+
if args.workspace_files is None or len(args.workspace_files) == 0:
627634
tmuxp_echo("Enter at least one config")
628635
if parser is not None:
629636
parser.print_help()
630637
sys.exit()
638+
return
631639

632-
config_file = scan_config(args.config_file, config_dir=get_config_dir())
640+
last_idx = len(args.workspace_files) - 1
641+
original_options = tmux_options.copy()
633642

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)
643+
for idx, workspace_file in enumerate(args.workspace_files):
644+
workspace_file = scan_config(workspace_file, config_dir=get_config_dir())
643645

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"
646+
detached = tmux_options.pop("detached", original_options.get("detached", False))
647+
new_session_name = tmux_options.pop(
648+
"new_session_name", original_options.get("new_session_name")
649+
)
650+
651+
if last_idx > 0 and idx < last_idx:
652+
detached = True
653+
new_session_name = None
654+
655+
load_workspace(
656+
workspace_file,
657+
detached=detached,
658+
new_session_name=new_session_name,
659+
**tmux_options,
649660
)

0 commit comments

Comments
 (0)