From df7df77441412a45665ab4cb777d170a20e6cead Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Wed, 25 Dec 2024 07:00:30 -0600 Subject: [PATCH] Server.acmd --- src/libtmux/server.py | 68 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/src/libtmux/server.py b/src/libtmux/server.py index 89ea2b90b..8a85056e0 100644 --- a/src/libtmux/server.py +++ b/src/libtmux/server.py @@ -22,6 +22,7 @@ from . import exc, formats from .common import ( + AsyncTmuxCmd, EnvironmentMixin, PaneDict, SessionDict, @@ -247,6 +248,73 @@ def cmd( return tmux_cmd(*svr_args, *cmd_args) + async def acmd( + self, + cmd: str, + *args: t.Any, + target: t.Optional[t.Union[str, int]] = None, + ) -> AsyncTmuxCmd: + """Execute tmux command respective of socket name and file, return output. + + Examples + -------- + >>> server.acmd('display-message', 'hi') + + + New session: + + >>> server.acmd('new-session', '-d', '-P', '-F#{session_id}').stdout[0] + '$2' + + >>> session.acmd('new-window', '-P').stdout[0] + 'libtmux...:2.0' + + Output of `tmux -L ... new-window -P -F#{window_id}` to a `Window` object: + + >>> Window.from_window_id(window_id=await session.acmd( + ... 'new-window', '-P', '-F#{window_id}').stdout[0], server=session.server) + Window(@4 3:..., Session($1 libtmux_...)) + + Create a pane from a window: + + >>> window.acmd('split-window', '-P', '-F#{pane_id}').stdout[0] + '%5' + + Output of `tmux -L ... split-window -P -F#{pane_id}` to a `Pane` object: + + >>> Pane.from_pane_id(pane_id=window.cmd( + ... 'split-window', '-P', '-F#{pane_id}').stdout[0], server=window.server) + Pane(%... Window(@... ...:..., Session($1 libtmux_...))) + + Parameters + ---------- + target : str, optional + Optional custom target. + + Returns + ------- + :class:`common.AsyncTmuxCmd` + """ + svr_args: list[t.Union[str, int]] = [cmd] + cmd_args: list[t.Union[str, int]] = [] + if self.socket_name: + svr_args.insert(0, f"-L{self.socket_name}") + if self.socket_path: + svr_args.insert(0, f"-S{self.socket_path}") + if self.config_file: + svr_args.insert(0, f"-f{self.config_file}") + if self.colors: + if self.colors == 256: + svr_args.insert(0, "-2") + elif self.colors == 88: + svr_args.insert(0, "-8") + else: + raise exc.UnknownColorOption + + cmd_args = ["-t", str(target), *args] if target is not None else [*args] + + return await AsyncTmuxCmd.run(*svr_args, *cmd_args) + @property def attached_sessions(self) -> list[Session]: """Return active :class:`Session`s.