Skip to content

Commit f1f984b

Browse files
committed
Server(feat[if_shell]): add if_shell() wrapping tmux if-shell
why: if-shell enables conditional tmux command execution based on shell command exit status, useful for scripted environment setup. what: - Add Server.if_shell() with shell_command, tmux_command, else_command, background (-b), target_pane (-t) parameters - Add tests for true branch and false-with-else branch
1 parent 74a2b82 commit f1f984b

2 files changed

Lines changed: 70 additions & 0 deletions

File tree

src/libtmux/server.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,54 @@ def list_buffers(self) -> list[str]:
718718

719719
return proc.stdout
720720

721+
def if_shell(
722+
self,
723+
shell_command: str,
724+
tmux_command: str,
725+
*,
726+
else_command: str | None = None,
727+
background: bool | None = None,
728+
target_pane: str | None = None,
729+
) -> None:
730+
"""Execute a tmux command conditionally via ``$ tmux if-shell``.
731+
732+
Parameters
733+
----------
734+
shell_command : str
735+
Shell command whose exit status determines which tmux command runs.
736+
tmux_command : str
737+
Tmux command to run if *shell_command* succeeds (exit 0).
738+
else_command : str, optional
739+
Tmux command to run if *shell_command* fails (non-zero exit).
740+
background : bool, optional
741+
Run the shell command in the background (``-b`` flag).
742+
target_pane : str, optional
743+
Target pane for format expansion (``-t`` flag).
744+
745+
Examples
746+
--------
747+
>>> server.if_shell('true', 'set -g @if_test yes')
748+
>>> server.cmd('show-options', '-gv', '@if_test').stdout[0]
749+
'yes'
750+
"""
751+
tmux_args: tuple[str, ...] = ()
752+
753+
if background:
754+
tmux_args += ("-b",)
755+
756+
if target_pane is not None:
757+
tmux_args += ("-t", target_pane)
758+
759+
tmux_args += (shell_command, tmux_command)
760+
761+
if else_command is not None:
762+
tmux_args += (else_command,)
763+
764+
proc = self.cmd("if-shell", *tmux_args)
765+
766+
if proc.stderr:
767+
raise exc.LibTmuxException(proc.stderr)
768+
721769
def source_file(
722770
self,
723771
path: StrPath,

tests/test_server.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,28 @@ def test_list_buffers(server: Server) -> None:
605605
assert len(result) >= 2
606606

607607

608+
def test_if_shell_true(server: Server) -> None:
609+
"""Test Server.if_shell() with true condition."""
610+
server.new_session(session_name="ifshell_test")
611+
server.if_shell("true", "set -g @if_test_true yes")
612+
613+
result = server.cmd("show-options", "-gv", "@if_test_true")
614+
assert result.stdout[0] == "yes"
615+
616+
617+
def test_if_shell_false_with_else(server: Server) -> None:
618+
"""Test Server.if_shell() with false condition and else branch."""
619+
server.new_session(session_name="ifshell_else")
620+
server.if_shell(
621+
"false",
622+
"set -g @if_else_test yes",
623+
else_command="set -g @if_else_test no",
624+
)
625+
626+
result = server.cmd("show-options", "-gv", "@if_else_test")
627+
assert result.stdout[0] == "no"
628+
629+
608630
def test_source_file(server: Server, tmp_path: pathlib.Path) -> None:
609631
"""Test Server.source_file() sources a config file."""
610632
server.new_session(session_name="source_test")

0 commit comments

Comments
 (0)