Skip to content

Commit 1e0aeb1

Browse files
committed
refactor!(cmd): Explicit target keyword argument
1 parent 42dcfc7 commit 1e0aeb1

File tree

4 files changed

+92
-65
lines changed

4 files changed

+92
-65
lines changed

src/libtmux/pane.py

+17-10
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,16 @@ def session(self) -> "Session":
117117
Commands (pane-scoped)
118118
"""
119119

120-
def cmd(self, cmd: str, *args: t.Any) -> tmux_cmd:
120+
def cmd(
121+
self,
122+
cmd: str,
123+
*args: t.Any,
124+
target: t.Optional[t.Union[str, int]] = None,
125+
) -> tmux_cmd:
121126
"""Execute tmux subcommand within pane context.
122127
123-
Automatically adds ``-t`` for object's pane ID to the command. Pass ``-t``
124-
in args to override.
128+
Automatically binds target by adding ``-t`` for object's pane ID to the
129+
command. Pass ``target`` to keyword arguments to override.
125130
126131
Examples
127132
--------
@@ -134,14 +139,19 @@ def cmd(self, cmd: str, *args: t.Any) -> tmux_cmd:
134139
... 'split-window', '-P', '-F#{pane_id}').stdout[0], server=pane.server)
135140
Pane(%... Window(@... ...:..., Session($1 libtmux_...)))
136141
142+
Parameters
143+
----------
144+
target : str, optional
145+
Optional custom target override. By default, the target is the pane ID.
146+
137147
Returns
138148
-------
139149
:meth:`server.cmd`
140150
"""
141-
if not any("-t" in str(x) for x in args):
142-
args = ("-t", self.pane_id, *args)
151+
if target is None:
152+
target = self.pane_id
143153

144-
return self.server.cmd(cmd, *args)
154+
return self.server.cmd(cmd, *args, target=target)
145155

146156
"""
147157
Commands (tmux-like)
@@ -620,9 +630,6 @@ def split(
620630
if not attach:
621631
tmux_args += ("-d",)
622632

623-
if target is not None:
624-
tmux_args += (f"-t{target}",)
625-
626633
if environment:
627634
if has_gte_version("3.0"):
628635
for k, v in environment.items():
@@ -635,7 +642,7 @@ def split(
635642
if shell:
636643
tmux_args += (shell,)
637644

638-
pane_cmd = self.cmd("split-window", *tmux_args)
645+
pane_cmd = self.cmd("split-window", *tmux_args, target=target)
639646

640647
# tmux < 1.7. This is added in 1.7.
641648
if pane_cmd.stderr:

src/libtmux/server.py

+26-18
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,12 @@ def raise_if_dead(self) -> None:
174174
#
175175
# Command
176176
#
177-
def cmd(self, cmd: str, *args: t.Any) -> tmux_cmd:
177+
def cmd(
178+
self,
179+
cmd: str,
180+
*args: t.Any,
181+
target: t.Optional[t.Union[str, int]] = None,
182+
) -> tmux_cmd:
178183
"""Execute tmux command respective of socket name and file, return output.
179184
180185
Examples
@@ -207,6 +212,11 @@ def cmd(self, cmd: str, *args: t.Any) -> tmux_cmd:
207212
... 'split-window', '-P', '-F#{pane_id}').stdout[0], server=window.server)
208213
Pane(%... Window(@... ...:..., Session($1 libtmux_...)))
209214
215+
Parameters
216+
----------
217+
target : str, optional
218+
Optional custom target.
219+
210220
Returns
211221
-------
212222
:class:`common.tmux_cmd`
@@ -217,22 +227,25 @@ def cmd(self, cmd: str, *args: t.Any) -> tmux_cmd:
217227
218228
Renamed from ``.tmux`` to ``.cmd``.
219229
"""
220-
cmd_args: t.List[t.Union[str, int]] = [cmd, *args]
230+
svr_args: t.List[t.Union[str, int]] = [cmd]
231+
cmd_args: t.List[t.Union[str, int]] = []
221232
if self.socket_name:
222-
cmd_args.insert(0, f"-L{self.socket_name}")
233+
svr_args.insert(0, f"-L{self.socket_name}")
223234
if self.socket_path:
224-
cmd_args.insert(0, f"-S{self.socket_path}")
235+
svr_args.insert(0, f"-S{self.socket_path}")
225236
if self.config_file:
226-
cmd_args.insert(0, f"-f{self.config_file}")
237+
svr_args.insert(0, f"-f{self.config_file}")
227238
if self.colors:
228239
if self.colors == 256:
229-
cmd_args.insert(0, "-2")
240+
svr_args.insert(0, "-2")
230241
elif self.colors == 88:
231-
cmd_args.insert(0, "-8")
242+
svr_args.insert(0, "-8")
232243
else:
233244
raise exc.UnknownColorOption()
234245

235-
return tmux_cmd(*cmd_args)
246+
cmd_args = ["-t", str(target), *args] if target is not None else [*args]
247+
248+
return tmux_cmd(*svr_args, *cmd_args)
236249

237250
@property
238251
def attached_sessions(self) -> t.List[Session]:
@@ -274,7 +287,7 @@ def has_session(self, target_session: str, exact: bool = True) -> bool:
274287
if exact and has_gte_version("2.1"):
275288
target_session = f"={target_session}"
276289

277-
proc = self.cmd("has-session", "-t%s" % target_session)
290+
proc = self.cmd("has-session", target=target_session)
278291

279292
if not proc.returncode:
280293
return True
@@ -318,7 +331,7 @@ def kill_session(self, target_session: t.Union[str, int]) -> "Server":
318331
------
319332
:exc:`exc.BadSessionName`
320333
"""
321-
proc = self.cmd("kill-session", "-t%s" % target_session)
334+
proc = self.cmd("kill-session", target=target_session)
322335

323336
if proc.stderr:
324337
raise exc.LibTmuxException(proc.stderr)
@@ -339,7 +352,7 @@ def switch_client(self, target_session: str) -> None:
339352
"""
340353
session_check_name(target_session)
341354

342-
proc = self.cmd("switch-client", "-t%s" % target_session)
355+
proc = self.cmd("switch-client", target=target_session)
343356

344357
if proc.stderr:
345358
raise exc.LibTmuxException(proc.stderr)
@@ -357,12 +370,7 @@ def attach_session(self, target_session: t.Optional[str] = None) -> None:
357370
:exc:`exc.BadSessionName`
358371
"""
359372
session_check_name(target_session)
360-
361-
tmux_args: t.Tuple[str, ...] = ()
362-
if target_session:
363-
tmux_args += ("-t%s" % target_session,)
364-
365-
proc = self.cmd("attach-session", *tmux_args)
373+
proc = self.cmd("attach-session", target=target_session)
366374

367375
if proc.stderr:
368376
raise exc.LibTmuxException(proc.stderr)
@@ -456,7 +464,7 @@ def new_session(
456464

457465
if self.has_session(session_name):
458466
if kill_session:
459-
self.cmd("kill-session", "-t%s" % session_name)
467+
self.cmd("kill-session", target=session_name)
460468
logger.info("session %s exists. killed it." % session_name)
461469
else:
462470
raise exc.TmuxSessionExists(

src/libtmux/session.py

+31-25
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)