@@ -448,7 +448,6 @@ def _reattach(builder):
448
448
449
449
If not, ``tmux attach-session`` loads the client to the target session.
450
450
"""
451
-
452
451
for plugin in builder .plugins :
453
452
plugin .reattach (builder .session )
454
453
proc = builder .session .cmd ('display-message' , '-p' , "'#S'" )
@@ -462,6 +461,85 @@ def _reattach(builder):
462
461
builder .session .attach_session ()
463
462
464
463
464
+ def _load_attached (builder , detached ):
465
+ """
466
+ Load config in new session and attach
467
+
468
+ Parameters
469
+ ----------
470
+ builder: :class:`workspacebuilder.WorkspaceBuilder`
471
+ detached : bool
472
+ """
473
+ builder .build ()
474
+
475
+ if 'TMUX' in os .environ : # tmuxp ran from inside tmux
476
+ # unset TMUX, save it, e.g. '/tmp/tmux-1000/default,30668,0'
477
+ tmux_env = os .environ .pop ('TMUX' )
478
+
479
+ if has_gte_version ('2.6' ):
480
+ set_layout_hook (builder .session , 'client-session-changed' )
481
+
482
+ builder .session .switch_client () # switch client to new session
483
+
484
+ os .environ ['TMUX' ] = tmux_env # set TMUX back again
485
+ else :
486
+ if has_gte_version ('2.6' ):
487
+ # if attaching for first time
488
+ set_layout_hook (builder .session , 'client-attached' )
489
+
490
+ # for cases where user switches client for first time
491
+ set_layout_hook (builder .session , 'client-session-changed' )
492
+
493
+ if not detached :
494
+ builder .session .attach_session ()
495
+
496
+
497
+ def _load_detached (builder ):
498
+ """
499
+ Load config in new session but don't attach
500
+
501
+ Parameters
502
+ ----------
503
+ builder: :class:`workspacebuilder.WorkspaceBuilder`
504
+ """
505
+ builder .build ()
506
+
507
+ if has_gte_version ('2.6' ): # prepare for both cases
508
+ set_layout_hook (builder .session , 'client-attached' )
509
+ set_layout_hook (builder .session , 'client-session-changed' )
510
+
511
+ print ('Session created in detached state.' )
512
+
513
+
514
+ def _load_append_windows_to_current_session (builder ):
515
+ """
516
+ Load config as new windows in current session
517
+
518
+ Parameters
519
+ ----------
520
+ builder: :class:`workspacebuilder.WorkspaceBuilder`
521
+ """
522
+ current_attached_session = builder .find_current_attached_session ()
523
+ builder .build (current_attached_session , append = True )
524
+ if has_gte_version ('2.6' ): # prepare for both cases
525
+ set_layout_hook (builder .session , 'client-attached' )
526
+ set_layout_hook (builder .session , 'client-session-changed' )
527
+
528
+
529
+ def _setup_plugins (builder ):
530
+ """
531
+ Runs after before_script
532
+
533
+ Parameters
534
+ ----------
535
+ builder: :class:`workspacebuilder.WorkspaceBuilder`
536
+ """
537
+ for plugin in builder .plugins :
538
+ plugin .before_script (builder .session )
539
+
540
+ return builder .session
541
+
542
+
465
543
def load_workspace (
466
544
config_file ,
467
545
socket_name = None ,
@@ -470,6 +548,7 @@ def load_workspace(
470
548
colors = None ,
471
549
detached = False ,
472
550
answer_yes = False ,
551
+ append = False ,
473
552
):
474
553
"""
475
554
Load a tmux "workspace" session via tmuxp file.
@@ -490,7 +569,11 @@ def load_workspace(
490
569
detached : bool
491
570
Force detached state. default False.
492
571
answer_yes : bool
493
- Assume yes when given prompt. default False.
572
+ Assume yes when given prompt to attach in new session.
573
+ Default False.
574
+ append : bool
575
+ Assume current when given prompt to append windows in same session.
576
+ Default False.
494
577
495
578
Notes
496
579
-----
@@ -595,9 +678,8 @@ def load_workspace(
595
678
596
679
session_name = sconfig ['session_name' ]
597
680
598
- # if the session already exists, prompt the user to attach. tmuxp doesn't
599
- # support incremental session building or appending (yet, PR's welcome!)
600
- if builder .session_exists (session_name ):
681
+ # if the session already exists, prompt the user to attach
682
+ if builder .session_exists (session_name ) and not append :
601
683
if not detached and (
602
684
answer_yes
603
685
or click .confirm (
@@ -610,38 +692,37 @@ def load_workspace(
610
692
return
611
693
612
694
try :
613
- builder .build () # load tmux session via workspace builder
614
-
615
- if 'TMUX' in os .environ : # tmuxp ran from inside tmux
616
- if not detached and (
617
- answer_yes or click .confirm ('Already inside TMUX, switch to session?' )
618
- ):
619
- # unset TMUX, save it, e.g. '/tmp/tmux-1000/default,30668,0'
620
- tmux_env = os .environ .pop ('TMUX' )
621
-
622
- if has_gte_version ('2.6' ):
623
- set_layout_hook (builder .session , 'client-session-changed' )
624
-
625
- builder .session .switch_client () # switch client to new session
695
+ if detached :
696
+ _load_detached (builder )
697
+ return _setup_plugins (builder )
626
698
627
- os .environ ['TMUX' ] = tmux_env # set TMUX back again
628
- return builder .session
629
- else : # session created in the background, from within tmux
630
- if has_gte_version ('2.6' ): # prepare for both cases
631
- set_layout_hook (builder .session , 'client-attached' )
632
- set_layout_hook (builder .session , 'client-session-changed' )
699
+ if append :
700
+ if 'TMUX' in os .environ : # tmuxp ran from inside tmux
701
+ _load_append_windows_to_current_session (builder )
702
+ else :
703
+ _load_attached (builder , detached )
633
704
634
- sys .exit ('Session created in detached state.' )
635
- else : # tmuxp ran from inside tmux
636
- if has_gte_version ('2.6' ):
637
- # if attaching for first time
638
- set_layout_hook (builder .session , 'client-attached' )
705
+ return _setup_plugins (builder )
639
706
640
- # for cases where user switches client for first time
641
- set_layout_hook (builder .session , 'client-session-changed' )
707
+ # append and answer_yes have no meaning if specified together
708
+ elif answer_yes :
709
+ _load_attached (builder , detached )
710
+ return _setup_plugins (builder )
642
711
643
- if not detached :
644
- builder .session .attach_session ()
712
+ if 'TMUX' in os .environ : # tmuxp ran from inside tmux
713
+ msg = "Already inside TMUX, switch to session? yes/no\n " \
714
+ "Or (a)ppend windows in the current active session?\n [y/n/a]"
715
+ options = ['y' , 'n' , 'a' ]
716
+ choice = click .prompt (msg , value_proc = _validate_choices (options ))
717
+
718
+ if choice == 'y' :
719
+ _load_attached (builder , detached )
720
+ elif choice == 'a' :
721
+ _load_append_windows_to_current_session (builder )
722
+ else :
723
+ _load_detached (builder )
724
+ else :
725
+ _load_attached (builder , detached )
645
726
646
727
except exc .TmuxpException as e :
647
728
import traceback
@@ -663,11 +744,8 @@ def load_workspace(
663
744
else :
664
745
sys .exit ()
665
746
666
- # Runs after before_script
667
- for plugin in builder .plugins :
668
- plugin .before_script (builder .session )
747
+ return _setup_plugins (builder )
669
748
670
- return builder .session
671
749
672
750
673
751
@click .group (context_settings = {'obj' : {}})
@@ -923,6 +1001,12 @@ def command_freeze(session_name, socket_name, socket_path, force):
923
1001
@click .option (
924
1002
'-d' , 'detached' , help = 'Load the session without attaching it' , is_flag = True
925
1003
)
1004
+ @click .option (
1005
+ '-a' ,
1006
+ 'append' ,
1007
+ help = 'Load configuration, appending windows to the current session' ,
1008
+ is_flag = True
1009
+ )
926
1010
@click .option (
927
1011
'colors' ,
928
1012
'-2' ,
@@ -945,6 +1029,7 @@ def command_load(
945
1029
new_session_name ,
946
1030
answer_yes ,
947
1031
detached ,
1032
+ append ,
948
1033
colors ,
949
1034
log_file ,
950
1035
):
@@ -983,6 +1068,7 @@ def command_load(
983
1068
'answer_yes' : answer_yes ,
984
1069
'colors' : colors ,
985
1070
'detached' : detached ,
1071
+ 'append' : append ,
986
1072
}
987
1073
988
1074
if not config :
0 commit comments