@@ -848,6 +848,10 @@ def startup(config_dir):
848
848
help = "Use vi-mode in ptpython/ptipython" ,
849
849
default = False ,
850
850
)
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
+ )
851
855
def command_shell (
852
856
session_name ,
853
857
window_name ,
@@ -857,6 +861,8 @@ def command_shell(
857
861
shell ,
858
862
use_pythonrc ,
859
863
use_vi_mode ,
864
+ detached ,
865
+ answer_yes ,
860
866
):
861
867
"""Launch python shell for tmux server, session, window and pane.
862
868
@@ -866,15 +872,56 @@ def command_shell(
866
872
session)
867
873
- ``server.attached_session``, ``session.attached_window``, ``window.attached_pane``
868
874
"""
875
+ print (f"detached: { detached } " )
869
876
server = Server (socket_name = socket_name , socket_path = socket_path )
870
877
871
878
util .raise_if_tmux_not_running (server = server )
872
879
873
880
current_pane = util .get_current_pane (server = server )
874
881
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
878
925
879
926
window = util .get_window (
880
927
session = session , window_name = window_name , current_pane = current_pane
0 commit comments