|
23 | 23 | from libtmux.pane import Pane |
24 | 24 |
|
25 | 25 | from . import exc |
26 | | -from .common import PaneDict, WindowOptionDict, handle_option_error |
| 26 | +from .common import AsyncTmuxCmd, PaneDict, WindowOptionDict, handle_option_error |
27 | 27 |
|
28 | 28 | if t.TYPE_CHECKING: |
29 | 29 | from .server import Server |
@@ -175,6 +175,55 @@ def cmd( |
175 | 175 |
|
176 | 176 | return self.server.cmd(cmd, *args, target=target) |
177 | 177 |
|
| 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 | + |
178 | 227 | """ |
179 | 228 | Commands (tmux-like) |
180 | 229 | """ |
|
0 commit comments