Skip to content

Commit df7df77

Browse files
committed
Server.acmd
1 parent 94869ee commit df7df77

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

src/libtmux/server.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
from . import exc, formats
2424
from .common import (
25+
AsyncTmuxCmd,
2526
EnvironmentMixin,
2627
PaneDict,
2728
SessionDict,
@@ -247,6 +248,73 @@ def cmd(
247248

248249
return tmux_cmd(*svr_args, *cmd_args)
249250

251+
async def acmd(
252+
self,
253+
cmd: str,
254+
*args: t.Any,
255+
target: t.Optional[t.Union[str, int]] = None,
256+
) -> AsyncTmuxCmd:
257+
"""Execute tmux command respective of socket name and file, return output.
258+
259+
Examples
260+
--------
261+
>>> server.acmd('display-message', 'hi')
262+
<libtmux.common.AsyncTmuxCmd object at ...>
263+
264+
New session:
265+
266+
>>> server.acmd('new-session', '-d', '-P', '-F#{session_id}').stdout[0]
267+
'$2'
268+
269+
>>> session.acmd('new-window', '-P').stdout[0]
270+
'libtmux...:2.0'
271+
272+
Output of `tmux -L ... new-window -P -F#{window_id}` to a `Window` object:
273+
274+
>>> Window.from_window_id(window_id=await session.acmd(
275+
... 'new-window', '-P', '-F#{window_id}').stdout[0], server=session.server)
276+
Window(@4 3:..., Session($1 libtmux_...))
277+
278+
Create a pane from a window:
279+
280+
>>> window.acmd('split-window', '-P', '-F#{pane_id}').stdout[0]
281+
'%5'
282+
283+
Output of `tmux -L ... split-window -P -F#{pane_id}` to a `Pane` object:
284+
285+
>>> Pane.from_pane_id(pane_id=window.cmd(
286+
... 'split-window', '-P', '-F#{pane_id}').stdout[0], server=window.server)
287+
Pane(%... Window(@... ...:..., Session($1 libtmux_...)))
288+
289+
Parameters
290+
----------
291+
target : str, optional
292+
Optional custom target.
293+
294+
Returns
295+
-------
296+
:class:`common.AsyncTmuxCmd`
297+
"""
298+
svr_args: list[t.Union[str, int]] = [cmd]
299+
cmd_args: list[t.Union[str, int]] = []
300+
if self.socket_name:
301+
svr_args.insert(0, f"-L{self.socket_name}")
302+
if self.socket_path:
303+
svr_args.insert(0, f"-S{self.socket_path}")
304+
if self.config_file:
305+
svr_args.insert(0, f"-f{self.config_file}")
306+
if self.colors:
307+
if self.colors == 256:
308+
svr_args.insert(0, "-2")
309+
elif self.colors == 88:
310+
svr_args.insert(0, "-8")
311+
else:
312+
raise exc.UnknownColorOption
313+
314+
cmd_args = ["-t", str(target), *args] if target is not None else [*args]
315+
316+
return await AsyncTmuxCmd.run(*svr_args, *cmd_args)
317+
250318
@property
251319
def attached_sessions(self) -> list[Session]:
252320
"""Return active :class:`Session`s.

0 commit comments

Comments
 (0)