Skip to content

Commit 7efb867

Browse files
committed
Server,Session,Window: Add .acmd
1 parent a19a9f0 commit 7efb867

File tree

3 files changed

+109
-3
lines changed

3 files changed

+109
-3
lines changed

src/libtmux/server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ async def acmd(
276276
Output of `tmux -L ... new-window -P -F#{window_id}` to a `Window` object:
277277
278278
>>> async def test_new_window():
279-
... result = await server.acmd('new-window', '-P', '-F#{window_id}')
279+
... result = await session.acmd('new-window', '-P', '-F#{window_id}')
280280
... window_id = result.stdout[0]
281281
... window = Window.from_window_id(window_id=window_id, server=server)
282282
... print(window)
@@ -294,7 +294,7 @@ async def acmd(
294294
Output of `tmux -L ... split-window -P -F#{pane_id}` to a `Pane` object:
295295
296296
>>> async def test_pane():
297-
... result = await server.acmd('split-window', '-P', '-F#{pane_id}')
297+
... result = await window.acmd('split-window', '-P', '-F#{pane_id}')
298298
... pane_id = result.stdout[0]
299299
... pane = Pane.from_pane_id(pane_id=pane_id, server=server)
300300
... print(pane)

src/libtmux/session.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
from . import exc
2323
from .common import (
24+
AsyncTmuxCmd,
2425
EnvironmentMixin,
2526
WindowDict,
2627
handle_option_error,
@@ -186,6 +187,62 @@ def cmd(
186187
target = self.session_id
187188
return self.server.cmd(cmd, *args, target=target)
188189

190+
async def acmd(
191+
self,
192+
cmd: str,
193+
*args: t.Any,
194+
target: t.Optional[t.Union[str, int]] = None,
195+
) -> AsyncTmuxCmd:
196+
"""Execute tmux subcommand within session context.
197+
198+
Automatically binds target by adding ``-t`` for object's session ID to the
199+
command. Pass ``target`` to keyword arguments to override.
200+
201+
Examples
202+
--------
203+
>>> import asyncio
204+
>>> async def test_acmd():
205+
... result = await session.acmd('new-window', '-P')
206+
... print(result.stdout[0])
207+
>>> asyncio.run(test_acmd())
208+
libtmux...:....0
209+
210+
From raw output to an enriched `Window` object:
211+
212+
>>> async def test_from_window():
213+
... window_id_result = await session.acmd(
214+
... 'new-window', '-P', '-F#{window_id}'
215+
... )
216+
... return Window.from_window_id(
217+
... window_id=window_id_result.stdout[0],
218+
... server=session.server
219+
... )
220+
>>> asyncio.run(test_from_window())
221+
Window(@... ...:..., Session($1 libtmux_...))
222+
223+
Parameters
224+
----------
225+
target : str, optional
226+
Optional custom target override. By default, the target is the session ID.
227+
228+
Returns
229+
-------
230+
:meth:`server.cmd`
231+
232+
Notes
233+
-----
234+
.. versionchanged:: 0.34
235+
236+
Passing target by ``-t`` is ignored. Use ``target`` keyword argument instead.
237+
238+
.. versionchanged:: 0.8
239+
240+
Renamed from ``.tmux`` to ``.cmd``.
241+
"""
242+
if target is None:
243+
target = self.session_id
244+
return await self.server.acmd(cmd, *args, target=target)
245+
189246
"""
190247
Commands (tmux-like)
191248
"""

src/libtmux/window.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from libtmux.pane import Pane
2424

2525
from . import exc
26-
from .common import PaneDict, WindowOptionDict, handle_option_error
26+
from .common import AsyncTmuxCmd, PaneDict, WindowOptionDict, handle_option_error
2727

2828
if t.TYPE_CHECKING:
2929
from .server import Server
@@ -175,6 +175,55 @@ def cmd(
175175

176176
return self.server.cmd(cmd, *args, target=target)
177177

178+
async def acmd(
179+
self,
180+
cmd: str,
181+
*args: t.Any,
182+
target: t.Optional[t.Union[str, int]] = None,
183+
) -> AsyncTmuxCmd:
184+
"""Execute tmux subcommand within window context.
185+
186+
Automatically binds target by adding ``-t`` for object's window ID to the
187+
command. Pass ``target`` to keyword arguments to override.
188+
189+
Examples
190+
--------
191+
Create a pane from a window:
192+
193+
>>> import asyncio
194+
>>> async def test_acmd():
195+
... result = await window.acmd('split-window', '-P', '-F#{pane_id}')
196+
... print(result.stdout[0])
197+
>>> asyncio.run(test_acmd())
198+
%...
199+
200+
Magic, directly to a `Pane`:
201+
202+
>>> async def test_from_pane():
203+
... pane_id_result = await session.acmd(
204+
... 'split-window', '-P', '-F#{pane_id}'
205+
... )
206+
... return Pane.from_pane_id(
207+
... pane_id=pane_id_result.stdout[0],
208+
... server=session.server
209+
... )
210+
>>> asyncio.run(test_from_pane())
211+
Pane(%... Window(@... ...:..., Session($1 libtmux_...)))
212+
213+
Parameters
214+
----------
215+
target : str, optional
216+
Optional custom target override. By default, the target is the window ID.
217+
218+
Returns
219+
-------
220+
:meth:`server.cmd`
221+
"""
222+
if target is None:
223+
target = self.window_id
224+
225+
return await self.server.acmd(cmd, *args, target=target)
226+
178227
"""
179228
Commands (tmux-like)
180229
"""

0 commit comments

Comments
 (0)