diff --git a/tmuxp/cli.py b/tmuxp/cli.py index c457a6fd83..94b987f5c3 100644 --- a/tmuxp/cli.py +++ b/tmuxp/cli.py @@ -848,6 +848,10 @@ def startup(config_dir): help="Use vi-mode in ptpython/ptipython", default=False, ) +@click.option("--yes", "-y", "answer_yes", help="yes", is_flag=True) +@click.option( + "-d", "detached", help="Load the session without attaching it", is_flag=True +) def command_shell( session_name, window_name, @@ -857,6 +861,8 @@ def command_shell( shell, use_pythonrc, use_vi_mode, + detached, + answer_yes, ): """Launch python shell for tmux server, session, window and pane. @@ -866,15 +872,38 @@ def command_shell( session) - ``server.attached_session``, ``session.attached_window``, ``window.attached_pane`` """ + print(f"detached: {detached}") server = Server(socket_name=socket_name, socket_path=socket_path) - util.raise_if_tmux_not_running(server=server) + if not util.is_server_running(server=server): + if answer_yes or click.confirm( + "No tmux server running, create?", + default=True, + ): + session = server.new_session( + session_name=session_name or "tmuxp shell", + window_command=" ".join(sys.argv), + ) + session.attach_session() + else: + current_pane = util.get_current_pane(server=server) - current_pane = util.get_current_pane(server=server) + try: + session = util.get_session( + server=server, session_name=session_name, current_pane=current_pane + ) + except exc.TmuxpException: + if answer_yes or click.confirm( + "Session %s does not exist. Create?" % session_name + if session_name is not None + else "Session does not exist. Create?" + ): + session = server.new_session(session_name=session_name) + else: + return - session = util.get_session( - server=server, session_name=session_name, current_pane=current_pane - ) + if current_pane["session_id"] != session.id: + current_pane = None window = util.get_window( session=session, window_name=window_name, current_pane=current_pane diff --git a/tmuxp/util.py b/tmuxp/util.py index 118345a611..a15dbb3783 100644 --- a/tmuxp/util.py +++ b/tmuxp/util.py @@ -92,6 +92,15 @@ def raise_if_tmux_not_running(server): raise e +def is_server_running(server): + try: + raise_if_tmux_not_running(server=server) + except LibTmuxException: + return False + + return True + + def get_current_pane(server): """Return Pane if one found in env""" if os.getenv("TMUX_PANE") is not None: @@ -132,9 +141,13 @@ def get_window(session, window_name=None, current_pane=None): if not window: raise exc.TmuxpException("Window not found: %s" % window_name) elif current_pane is not None: + print("get_window: current_pane") + window = session.find_where({"window_id": current_pane["window_id"]}) else: + print("get_window: else") window = session.list_windows()[0] + print(f"get_window: {window}") return window