@@ -141,11 +141,13 @@ def panes(self) -> QueryList["Pane"]:
141
141
#
142
142
# Command
143
143
#
144
- def cmd (self , cmd : str , * args : t .Any ) -> tmux_cmd :
144
+ def cmd (
145
+ self , cmd : str , * args : t .Any , target : t .Optional [t .Union [str , int ]] = None
146
+ ) -> tmux_cmd :
145
147
"""Execute tmux subcommand within session context.
146
148
147
- Automatically adds ``-t`` for object's session ID to the command. Pass ``-t``
148
- in args to override.
149
+ Automatically binds target by adding ``-t`` for object's session ID to the
150
+ command. Pass ``target`` to keyword arguments to override.
149
151
150
152
Examples
151
153
--------
@@ -158,28 +160,28 @@ def cmd(self, cmd: str, *args: t.Any) -> tmux_cmd:
158
160
... 'new-window', '-P', '-F#{window_id}').stdout[0], server=session.server)
159
161
Window(@... ...:..., Session($1 libtmux_...))
160
162
163
+ Parameters
164
+ ----------
165
+ target : str, optional
166
+ Optional custom target override. By default, the target is the session ID.
167
+
161
168
Returns
162
169
-------
163
170
:meth:`server.cmd`
164
171
165
172
Notes
166
173
-----
174
+ .. versionchanged:: 0.34
175
+
176
+ Passing target by ``-t`` is ignored. Use ``target`` keyword argument instead.
177
+
167
178
.. versionchanged:: 0.8
168
179
169
180
Renamed from ``.tmux`` to ``.cmd``.
170
181
"""
171
- # if -t is not set in any arg yet
172
- if not any ("-t" in str (x ) for x in args ):
173
- # insert -t immediately after 1st arg, as per tmux format
174
- new_args : t .Tuple [str , ...] = ()
175
- assert isinstance (self .session_id , str )
176
- new_args += (
177
- "-t" ,
178
- self .session_id ,
179
- )
180
- new_args += args
181
- args = new_args
182
- return self .server .cmd (cmd , * args )
182
+ if target is None :
183
+ target = self .session_id
184
+ return self .server .cmd (cmd , * args , target = target )
183
185
184
186
"""
185
187
Commands (tmux-like)
@@ -356,9 +358,9 @@ def select_window(self, target_window: t.Union[str, int]) -> "Window":
356
358
# Note that we also provide the session ID here, since cmd()
357
359
# will not automatically add it as there is already a '-t'
358
360
# argument provided.
359
- target = f"-t { self .session_id } :{ target_window } "
361
+ target = f"{ self .session_id } :{ target_window } "
360
362
361
- proc = self .cmd ("select-window" , target )
363
+ proc = self .cmd ("select-window" , target = target )
362
364
363
365
if proc .stderr :
364
366
raise exc .LibTmuxException (proc .stderr )
@@ -495,7 +497,7 @@ def switch_client(self) -> "Session":
495
497
------
496
498
:exc:`exc.LibTmuxException`
497
499
"""
498
- proc = self .cmd ("switch-client" , "-t%s" % self .session_id )
500
+ proc = self .cmd ("switch-client" , target = self .session_id )
499
501
500
502
if proc .stderr :
501
503
raise exc .LibTmuxException (proc .stderr )
@@ -653,9 +655,13 @@ def new_window(
653
655
"Direction flag ignored, requires tmux 3.1 or newer." ,
654
656
)
655
657
658
+ target : t .Optional [str ] = None
659
+ if window_index is not None :
660
+ # empty string for window_index will use the first one available
661
+ target = f"{ self .session_id } :{ window_index } "
656
662
if target_window :
657
663
if has_gte_version ("3.2" ):
658
- window_args += ( f"-t { target_window } " ,)
664
+ target = target_window
659
665
else :
660
666
logger .warning (
661
667
"Window target ignored, requires tmux 3.1 or newer." ,
@@ -667,7 +673,7 @@ def new_window(
667
673
if window_shell :
668
674
window_args += (window_shell ,)
669
675
670
- cmd = self .cmd ("new-window" , * window_args )
676
+ cmd = self .cmd ("new-window" , * window_args , target = target )
671
677
672
678
if cmd .stderr :
673
679
raise exc .LibTmuxException (cmd .stderr )
@@ -696,11 +702,11 @@ def kill_window(self, target_window: t.Optional[str] = None) -> None:
696
702
"""
697
703
if target_window :
698
704
if isinstance (target_window , int ):
699
- target = "-t %s:%d" % (self .window_name , target_window )
705
+ target = "%s:%d" % (self .window_name , target_window )
700
706
else :
701
- target = "-t %s" % target_window
707
+ target = "%s" % target_window
702
708
703
- proc = self .cmd ("kill-window" , target )
709
+ proc = self .cmd ("kill-window" , target = target )
704
710
705
711
if proc .stderr :
706
712
raise exc .LibTmuxException (proc .stderr )
@@ -797,7 +803,7 @@ def attach_session(self) -> "Session":
797
803
category = DeprecationWarning ,
798
804
stacklevel = 2 ,
799
805
)
800
- proc = self .cmd ("attach-session" , "-t%s" % self .session_id )
806
+ proc = self .cmd ("attach-session" , target = self .session_id )
801
807
802
808
if proc .stderr :
803
809
raise exc .LibTmuxException (proc .stderr )
@@ -818,7 +824,7 @@ def kill_session(self) -> None:
818
824
category = DeprecationWarning ,
819
825
stacklevel = 2 ,
820
826
)
821
- proc = self .cmd ("kill-session" , "-t%s" % self .session_id )
827
+ proc = self .cmd ("kill-session" , target = self .session_id )
822
828
823
829
if proc .stderr :
824
830
raise exc .LibTmuxException (proc .stderr )
0 commit comments