16
16
from libtmux .common import has_gte_version
17
17
from libtmux .server import Server
18
18
from libtmux .session import Session
19
+ from tmuxp .types import StrPath
19
20
20
21
from .. import config , config_reader , exc , log , util
21
22
from ..workspacebuilder import WorkspaceBuilder
29
30
)
30
31
31
32
if t .TYPE_CHECKING :
32
- from typing_extensions import Literal , TypeAlias
33
+ from typing_extensions import Literal , NotRequired , TypeAlias , TypedDict
33
34
34
35
CLIColorsLiteral : TypeAlias = Literal [56 , 88 ]
35
36
37
+ class OptionOverrides (TypedDict ):
38
+ detached : NotRequired [bool ]
39
+ new_session_name : NotRequired [t .Optional [str ]]
40
+
36
41
37
42
class CLILoadNamespace (argparse .Namespace ):
38
- config_file : str
43
+ workspace_files : t . List [ str ]
39
44
socket_name : t .Optional [str ]
40
45
socket_path : t .Optional [str ]
41
46
tmux_config_file : t .Optional [str ]
@@ -251,7 +256,7 @@ def _setup_plugins(builder: WorkspaceBuilder) -> Session:
251
256
252
257
253
258
def load_workspace (
254
- config_file : t . Union [ pathlib . Path , str ] ,
259
+ workspace_file : StrPath ,
255
260
socket_name : t .Optional [str ] = None ,
256
261
socket_path : None = None ,
257
262
tmux_config_file : None = None ,
@@ -266,8 +271,8 @@ def load_workspace(
266
271
267
272
Parameters
268
273
----------
269
- config_file : str
270
- absolute path to config file
274
+ workspace_file : list of str
275
+ paths or session names to workspace files
271
276
socket_name : str, optional
272
277
``tmux -L <socket-name>``
273
278
socket_path: str, optional
@@ -356,18 +361,19 @@ def load_workspace(
356
361
Accessed April 8th, 2018.
357
362
"""
358
363
# 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 )
361
366
362
367
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 )
364
370
)
365
371
366
372
# 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 {}
368
374
369
375
# 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 ))
371
377
# Overwrite session name
372
378
if new_session_name :
373
379
sconfig ["session_name" ] = new_session_name
@@ -388,7 +394,7 @@ def load_workspace(
388
394
sconf = sconfig , plugins = load_plugins (sconfig ), server = t
389
395
)
390
396
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 )
392
398
return None
393
399
394
400
session_name = sconfig ["session_name" ]
@@ -486,8 +492,9 @@ def config_file_completion(ctx, params, incomplete):
486
492
487
493
488
494
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 = "+" ,
491
498
metavar = "config-file" ,
492
499
help = "filepath to session or filename of session if in tmuxp config directory" ,
493
500
)
@@ -568,7 +575,7 @@ def create_load_subparser(parser: argparse.ArgumentParser) -> argparse.ArgumentP
568
575
try :
569
576
import shtab
570
577
571
- config_file .complete = shtab .FILE # type: ignore
578
+ workspace_files .complete = shtab .FILE # type: ignore
572
579
tmux_config_file .complete = shtab .FILE # type: ignore
573
580
log_file .complete = shtab .FILE # type: ignore
574
581
except ImportError :
@@ -623,27 +630,31 @@ def command_load(
623
630
"append" : args .append ,
624
631
}
625
632
626
- if args .config_file is None :
633
+ if args .workspace_files is None or len ( args . workspace_files ) == 0 :
627
634
tmuxp_echo ("Enter at least one config" )
628
635
if parser is not None :
629
636
parser .print_help ()
630
637
sys .exit ()
638
+ return
631
639
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 ()
633
642
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 ())
643
645
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 ,
649
660
)
0 commit comments