Skip to content

Commit

Permalink
Server.acmd
Browse files Browse the repository at this point in the history
  • Loading branch information
tony committed Dec 25, 2024
1 parent 94869ee commit df7df77
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions src/libtmux/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

from . import exc, formats
from .common import (
AsyncTmuxCmd,
EnvironmentMixin,
PaneDict,
SessionDict,
Expand Down Expand Up @@ -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')
<libtmux.common.AsyncTmuxCmd object at ...>
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.
Expand Down

0 comments on commit df7df77

Please sign in to comment.