Skip to content

Commit 9a6083b

Browse files
committed
Detect current shell based on TMUX_PANE
1 parent c68c9d3 commit 9a6083b

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

tmuxp/cli.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -674,14 +674,27 @@ def startup(config_dir):
674674
def command_shell(session_name, window_name, socket_name, socket_path, command):
675675
server = Server(socket_name=socket_name, socket_path=socket_path)
676676

677+
current_pane = None
678+
if os.getenv('TMUX_PANE') is not None:
679+
current_pane = next(
680+
(
681+
p
682+
for p in server._list_panes()
683+
if p.get('pane_id') == os.getenv('TMUX_PANE')
684+
),
685+
None,
686+
)
687+
677688
try:
678689
if session_name:
679690
session = server.find_where({'session_name': session_name})
691+
elif current_pane is not None:
692+
session = server.find_where({'session_id': current_pane['session_id']})
680693
else:
681694
session = server.list_sessions()[0]
682695

683696
if not session:
684-
raise exc.TmuxpException('Session not found.')
697+
raise exc.TmuxpException('Session not found: %s' % session_name)
685698
except exc.TmuxpException as e:
686699
print(e)
687700
return
@@ -690,14 +703,25 @@ def command_shell(session_name, window_name, socket_name, socket_path, command):
690703
if window_name:
691704
window = session.find_where({'window_name': window_name})
692705
if not window:
693-
raise exc.TmuxpException('Window not found.')
706+
raise exc.TmuxpException('Window not found: %s' % window_name)
707+
elif current_pane is not None:
708+
window = session.find_where({'window_id': current_pane['window_id']})
694709
else:
695710
window = session.list_windows()[0]
696711

697712
except exc.TmuxpException as e:
698713
print(e)
699714
return
700715

716+
try:
717+
if current_pane is not None:
718+
pane = window.find_where({'pane_id': current_pane['pane_id']}) # NOQA: F841
719+
else:
720+
pane = window.attached_pane # NOQA: F841
721+
except exc.TmuxpException as e:
722+
print(e)
723+
return
724+
701725
if command is not None:
702726
exec(command)
703727
else:

0 commit comments

Comments
 (0)