24
24
25
25
from . import exc , formats
26
26
from .common import (
27
+ AsyncTmuxCmd ,
27
28
EnvironmentMixin ,
28
29
PaneDict ,
29
30
SessionDict ,
@@ -199,8 +200,12 @@ def cmd(
199
200
200
201
Output of `tmux -L ... new-window -P -F#{window_id}` to a `Window` object:
201
202
202
- >>> Window.from_window_id(window_id=session.cmd(
203
- ... 'new-window', '-P', '-F#{window_id}').stdout[0], server=session.server)
203
+ >>> Window.from_window_id(
204
+ ... window_id=session.cmd(
205
+ ... 'new-window', '-P', '-F#{window_id}'
206
+ ... ).stdout[0],
207
+ ... server=session.server,
208
+ ... )
204
209
Window(@4 3:..., Session($1 libtmux_...))
205
210
206
211
Create a pane from a window:
@@ -211,7 +216,9 @@ def cmd(
211
216
Output of `tmux -L ... split-window -P -F#{pane_id}` to a `Pane` object:
212
217
213
218
>>> Pane.from_pane_id(pane_id=window.cmd(
214
- ... 'split-window', '-P', '-F#{pane_id}').stdout[0], server=window.server)
219
+ ... 'split-window', '-P', '-F#{pane_id}').stdout[0],
220
+ ... server=window.server
221
+ ... )
215
222
Pane(%... Window(@... ...:..., Session($1 libtmux_...)))
216
223
217
224
Parameters
@@ -249,6 +256,90 @@ def cmd(
249
256
250
257
return tmux_cmd (* svr_args , * cmd_args )
251
258
259
+ async def acmd (
260
+ self ,
261
+ cmd : str ,
262
+ * args : t .Any ,
263
+ target : str | int | None = None ,
264
+ ) -> AsyncTmuxCmd :
265
+ """Execute tmux command respective of socket name and file, return output.
266
+
267
+ Examples
268
+ --------
269
+ >>> import asyncio
270
+ >>> async def test_acmd():
271
+ ... result = await server.acmd('display-message', 'hi')
272
+ ... print(result.stdout)
273
+ >>> asyncio.run(test_acmd())
274
+ []
275
+
276
+ New session:
277
+
278
+ >>> async def test_new_session():
279
+ ... result = await server.acmd(
280
+ ... 'new-session', '-d', '-P', '-F#{session_id}'
281
+ ... )
282
+ ... print(result.stdout[0])
283
+ >>> asyncio.run(test_new_session())
284
+ $...
285
+
286
+ Output of `tmux -L ... new-window -P -F#{window_id}` to a `Window` object:
287
+
288
+ >>> async def test_new_window():
289
+ ... result = await session.acmd('new-window', '-P', '-F#{window_id}')
290
+ ... window_id = result.stdout[0]
291
+ ... window = Window.from_window_id(window_id=window_id, server=server)
292
+ ... print(window)
293
+ >>> asyncio.run(test_new_window())
294
+ Window(@... ...:..., Session($... libtmux_...))
295
+
296
+ Create a pane from a window:
297
+
298
+ >>> async def test_split_window():
299
+ ... result = await server.acmd('split-window', '-P', '-F#{pane_id}')
300
+ ... print(result.stdout[0])
301
+ >>> asyncio.run(test_split_window())
302
+ %...
303
+
304
+ Output of `tmux -L ... split-window -P -F#{pane_id}` to a `Pane` object:
305
+
306
+ >>> async def test_pane():
307
+ ... result = await window.acmd('split-window', '-P', '-F#{pane_id}')
308
+ ... pane_id = result.stdout[0]
309
+ ... pane = Pane.from_pane_id(pane_id=pane_id, server=server)
310
+ ... print(pane)
311
+ >>> asyncio.run(test_pane())
312
+ Pane(%... Window(@... ...:..., Session($1 libtmux_...)))
313
+
314
+ Parameters
315
+ ----------
316
+ target : str, optional
317
+ Optional custom target.
318
+
319
+ Returns
320
+ -------
321
+ :class:`common.AsyncTmuxCmd`
322
+ """
323
+ svr_args : list [str | int ] = [cmd ]
324
+ cmd_args : list [str | int ] = []
325
+ if self .socket_name :
326
+ svr_args .insert (0 , f"-L{ self .socket_name } " )
327
+ if self .socket_path :
328
+ svr_args .insert (0 , f"-S{ self .socket_path } " )
329
+ if self .config_file :
330
+ svr_args .insert (0 , f"-f{ self .config_file } " )
331
+ if self .colors :
332
+ if self .colors == 256 :
333
+ svr_args .insert (0 , "-2" )
334
+ elif self .colors == 88 :
335
+ svr_args .insert (0 , "-8" )
336
+ else :
337
+ raise exc .UnknownColorOption
338
+
339
+ cmd_args = ["-t" , str (target ), * args ] if target is not None else [* args ]
340
+
341
+ return await AsyncTmuxCmd .run (* svr_args , * cmd_args )
342
+
252
343
@property
253
344
def attached_sessions (self ) -> list [Session ]:
254
345
"""Return active :class:`Session`s.
0 commit comments