@@ -848,6 +848,10 @@ def startup(config_dir):
848848 help = "Use vi-mode in ptpython/ptipython" ,
849849 default = False ,
850850)
851+ @click .option ("--yes" , "-y" , "answer_yes" , help = "yes" , is_flag = True )
852+ @click .option (
853+ "-d" , "detached" , help = "Load the session without attaching it" , is_flag = True
854+ )
851855def command_shell (
852856 session_name ,
853857 window_name ,
@@ -857,6 +861,8 @@ def command_shell(
857861 shell ,
858862 use_pythonrc ,
859863 use_vi_mode ,
864+ detached ,
865+ answer_yes ,
860866):
861867 """Launch python shell for tmux server, session, window and pane.
862868
@@ -866,15 +872,56 @@ def command_shell(
866872 session)
867873 - ``server.attached_session``, ``session.attached_window``, ``window.attached_pane``
868874 """
875+ print (f"detached: { detached } " )
869876 server = Server (socket_name = socket_name , socket_path = socket_path )
870877
871878 util .raise_if_tmux_not_running (server = server )
872879
873880 current_pane = util .get_current_pane (server = server )
874881
875- session = util .get_session (
876- server = server , session_name = session_name , current_pane = current_pane
877- )
882+ try :
883+ current_session = session = util .get_session (
884+ server = server , current_pane = current_pane
885+ )
886+ except Exception :
887+ current_session = None
888+
889+ try :
890+ session = util .get_session (
891+ server = server , session_name = session_name , current_pane = current_pane
892+ )
893+ except exc .TmuxpException :
894+ if answer_yes or click .confirm (
895+ "Session %s does not exist. Create?" % session_name
896+ if session_name is not None
897+ else "Session does not exist. Create?"
898+ ):
899+ session = server .new_session (session_name = session_name )
900+ else :
901+ return
902+
903+ if current_session is not None and current_session .id != session .id :
904+ print ("in" )
905+ if not detached and (
906+ answer_yes
907+ or click .confirm (
908+ "Switch / attach to %s and run shell from there?"
909+ % click .style (session_name , fg = "green" ),
910+ default = True ,
911+ )
912+ ):
913+ if current_session .id != session .id :
914+ session .attached_window .attached_pane .send_keys (
915+ "tmuxp shell" , enter = True
916+ )
917+ if "TMUX" in os .environ :
918+ session .switch_client ()
919+ else :
920+ session .attach_session ()
921+ return
922+
923+ if current_pane ["session_id" ] != session .id :
924+ current_pane = None
878925
879926 window = util .get_window (
880927 session = session , window_name = window_name , current_pane = current_pane
0 commit comments