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