From fdb7636a45e0aea78fe81b2dd22bc1f0e5ab6279 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Sat, 7 Nov 2020 08:47:10 -0600 Subject: [PATCH] Progress --- tmuxp/cli.py | 53 ++++++++++++++++++++++++++++++++++++++++++++++++--- tmuxp/util.py | 4 ++++ 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/tmuxp/cli.py b/tmuxp/cli.py index 6b1ac0cf5df..6b37a298c19 100644 --- a/tmuxp/cli.py +++ b/tmuxp/cli.py @@ -700,6 +700,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, @@ -709,6 +713,8 @@ def command_shell( shell, use_pythonrc, use_vi_mode, + detached, + answer_yes, ): """Launch python shell for tmux server, session, window and pane. @@ -718,15 +724,56 @@ 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) current_pane = util.get_current_pane(server=server) - session = util.get_session( - server=server, session_name=session_name, current_pane=current_pane - ) + try: + current_session = session = util.get_session( + server=server, current_pane=current_pane + ) + except Exception: + current_session = None + + 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 + + if current_session is not None and current_session.id != session.id: + print('in') + if not detached and ( + answer_yes + or click.confirm( + 'Switch / attach to %s and run shell from there?' + % click.style(session_name, fg='green'), + default=True, + ) + ): + if current_session.id != session.id: + session.attached_window.attached_pane.send_keys( + 'tmuxp shell', enter=True + ) + if 'TMUX' in os.environ: + session.switch_client() + else: + session.attach_session() + return + + 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 6cf66b68cec..d0a2876277f 100644 --- a/tmuxp/util.py +++ b/tmuxp/util.py @@ -125,10 +125,14 @@ 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