@@ -141,11 +141,13 @@ def panes(self) -> QueryList["Pane"]:
141141 #
142142 # Command
143143 #
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 :
145147 """Execute tmux subcommand within session context.
146148
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.
149151
150152 Examples
151153 --------
@@ -158,28 +160,28 @@ def cmd(self, cmd: str, *args: t.Any) -> tmux_cmd:
158160 ... 'new-window', '-P', '-F#{window_id}').stdout[0], server=session.server)
159161 Window(@... ...:..., Session($1 libtmux_...))
160162
163+ Parameters
164+ ----------
165+ target : str, optional
166+ Optional custom target override. By default, the target is the session ID.
167+
161168 Returns
162169 -------
163170 :meth:`server.cmd`
164171
165172 Notes
166173 -----
174+ .. versionchanged:: 0.34
175+
176+ Passing target by ``-t`` is ignored. Use ``target`` keyword argument instead.
177+
167178 .. versionchanged:: 0.8
168179
169180 Renamed from ``.tmux`` to ``.cmd``.
170181 """
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 )
183185
184186 """
185187 Commands (tmux-like)
@@ -356,9 +358,9 @@ def select_window(self, target_window: t.Union[str, int]) -> "Window":
356358 # Note that we also provide the session ID here, since cmd()
357359 # will not automatically add it as there is already a '-t'
358360 # argument provided.
359- target = f"-t { self .session_id } :{ target_window } "
361+ target = f"{ self .session_id } :{ target_window } "
360362
361- proc = self .cmd ("select-window" , target )
363+ proc = self .cmd ("select-window" , target = target )
362364
363365 if proc .stderr :
364366 raise exc .LibTmuxException (proc .stderr )
@@ -495,7 +497,7 @@ def switch_client(self) -> "Session":
495497 ------
496498 :exc:`exc.LibTmuxException`
497499 """
498- proc = self .cmd ("switch-client" , "-t%s" % self .session_id )
500+ proc = self .cmd ("switch-client" , target = self .session_id )
499501
500502 if proc .stderr :
501503 raise exc .LibTmuxException (proc .stderr )
@@ -653,9 +655,13 @@ def new_window(
653655 "Direction flag ignored, requires tmux 3.1 or newer." ,
654656 )
655657
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 } "
656662 if target_window :
657663 if has_gte_version ("3.2" ):
658- window_args += ( f"-t { target_window } " ,)
664+ target = target_window
659665 else :
660666 logger .warning (
661667 "Window target ignored, requires tmux 3.1 or newer." ,
@@ -667,7 +673,7 @@ def new_window(
667673 if window_shell :
668674 window_args += (window_shell ,)
669675
670- cmd = self .cmd ("new-window" , * window_args )
676+ cmd = self .cmd ("new-window" , * window_args , target = target )
671677
672678 if cmd .stderr :
673679 raise exc .LibTmuxException (cmd .stderr )
@@ -696,11 +702,11 @@ def kill_window(self, target_window: t.Optional[str] = None) -> None:
696702 """
697703 if target_window :
698704 if isinstance (target_window , int ):
699- target = "-t %s:%d" % (self .window_name , target_window )
705+ target = "%s:%d" % (self .window_name , target_window )
700706 else :
701- target = "-t %s" % target_window
707+ target = "%s" % target_window
702708
703- proc = self .cmd ("kill-window" , target )
709+ proc = self .cmd ("kill-window" , target = target )
704710
705711 if proc .stderr :
706712 raise exc .LibTmuxException (proc .stderr )
@@ -797,7 +803,7 @@ def attach_session(self) -> "Session":
797803 category = DeprecationWarning ,
798804 stacklevel = 2 ,
799805 )
800- proc = self .cmd ("attach-session" , "-t%s" % self .session_id )
806+ proc = self .cmd ("attach-session" , target = self .session_id )
801807
802808 if proc .stderr :
803809 raise exc .LibTmuxException (proc .stderr )
@@ -818,7 +824,7 @@ def kill_session(self) -> None:
818824 category = DeprecationWarning ,
819825 stacklevel = 2 ,
820826 )
821- proc = self .cmd ("kill-session" , "-t%s" % self .session_id )
827+ proc = self .cmd ("kill-session" , target = self .session_id )
822828
823829 if proc .stderr :
824830 raise exc .LibTmuxException (proc .stderr )
0 commit comments