Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shell prompt: Offer to attach / create unset server, window, session, launch within #642

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 34 additions & 5 deletions tmuxp/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.

Expand All @@ -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
Expand Down
13 changes: 13 additions & 0 deletions tmuxp/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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

Expand Down