@@ -448,7 +448,6 @@ def _reattach(builder):
448448
449449 If not, ``tmux attach-session`` loads the client to the target session.
450450 """
451-
452451 for plugin in builder .plugins :
453452 plugin .reattach (builder .session )
454453 proc = builder .session .cmd ('display-message' , '-p' , "'#S'" )
@@ -462,6 +461,85 @@ def _reattach(builder):
462461 builder .session .attach_session ()
463462
464463
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+
465543def load_workspace (
466544 config_file ,
467545 socket_name = None ,
@@ -470,6 +548,7 @@ def load_workspace(
470548 colors = None ,
471549 detached = False ,
472550 answer_yes = False ,
551+ append = False ,
473552):
474553 """
475554 Load a tmux "workspace" session via tmuxp file.
@@ -490,7 +569,11 @@ def load_workspace(
490569 detached : bool
491570 Force detached state. default False.
492571 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.
494577
495578 Notes
496579 -----
@@ -595,9 +678,8 @@ def load_workspace(
595678
596679 session_name = sconfig ['session_name' ]
597680
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 :
601683 if not detached and (
602684 answer_yes
603685 or click .confirm (
@@ -610,38 +692,37 @@ def load_workspace(
610692 return
611693
612694 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 )
626698
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 )
633704
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 )
639706
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 )
642711
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 )
645726
646727 except exc .TmuxpException as e :
647728 import traceback
@@ -663,11 +744,8 @@ def load_workspace(
663744 else :
664745 sys .exit ()
665746
666- # Runs after before_script
667- for plugin in builder .plugins :
668- plugin .before_script (builder .session )
747+ return _setup_plugins (builder )
669748
670- return builder .session
671749
672750
673751@click .group (context_settings = {'obj' : {}})
@@ -923,6 +1001,12 @@ def command_freeze(session_name, socket_name, socket_path, force):
9231001@click .option (
9241002 '-d' , 'detached' , help = 'Load the session without attaching it' , is_flag = True
9251003)
1004+ @click .option (
1005+ '-a' ,
1006+ 'append' ,
1007+ help = 'Load configuration, appending windows to the current session' ,
1008+ is_flag = True
1009+ )
9261010@click .option (
9271011 'colors' ,
9281012 '-2' ,
@@ -945,6 +1029,7 @@ def command_load(
9451029 new_session_name ,
9461030 answer_yes ,
9471031 detached ,
1032+ append ,
9481033 colors ,
9491034 log_file ,
9501035):
@@ -983,6 +1068,7 @@ def command_load(
9831068 'answer_yes' : answer_yes ,
9841069 'colors' : colors ,
9851070 'detached' : detached ,
1071+ 'append' : append ,
9861072 }
9871073
9881074 if not config :
0 commit comments