Skip to content

Commit 7a4fe80

Browse files
committed
refactor!(cmd): Explicit target keyword argument
1 parent 654f32f commit 7a4fe80

File tree

4 files changed

+82
-67
lines changed

4 files changed

+82
-67
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

+21-27
Original file line numberDiff line numberDiff line change
@@ -171,27 +171,17 @@ def cmd(
171171
172172
Notes
173173
-----
174+
.. versionchanged:: 0.34
175+
176+
Passing target by ``-t`` is ignored. Use ``target`` keyword argument instead.
177+
174178
.. versionchanged:: 0.8
175179
176180
Renamed from ``.tmux`` to ``.cmd``.
177181
"""
178-
# if -t is not set in any arg yet
179-
if not any("-t" in str(x) for x in args):
180-
# warnings.warn(
181-
# "Use `target=...` instead of passing `-t` as an argument.",
182-
# category=DeprecationWarning,
183-
# stacklevel=2,
184-
# )
185-
# insert -t immediately after 1st arg, as per tmux format
186-
new_args: t.Tuple[str, ...] = ()
187-
assert isinstance(self.session_id, str)
188-
new_args += (
189-
"-t",
190-
self.session_id,
191-
)
192-
new_args += args
193-
args = new_args
194-
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)
195185

196186
"""
197187
Commands (tmux-like)
@@ -368,9 +358,9 @@ def select_window(self, target_window: t.Union[str, int]) -> "Window":
368358
# Note that we also provide the session ID here, since cmd()
369359
# will not automatically add it as there is already a '-t'
370360
# argument provided.
371-
target = f"-t{self.session_id}:{target_window}"
361+
target = f"{self.session_id}:{target_window}"
372362

373-
proc = self.cmd("select-window", target)
363+
proc = self.cmd("select-window", target=target)
374364

375365
if proc.stderr:
376366
raise exc.LibTmuxException(proc.stderr)
@@ -507,7 +497,7 @@ def switch_client(self) -> "Session":
507497
------
508498
:exc:`exc.LibTmuxException`
509499
"""
510-
proc = self.cmd("switch-client", "-t%s" % self.session_id)
500+
proc = self.cmd("switch-client", target=self.session_id)
511501

512502
if proc.stderr:
513503
raise exc.LibTmuxException(proc.stderr)
@@ -665,9 +655,13 @@ def new_window(
665655
"Direction flag ignored, requires tmux 3.1 or newer.",
666656
)
667657

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}"
668662
if target_window:
669663
if has_gte_version("3.2"):
670-
window_args += (f"-t{target_window}",)
664+
target = target_window
671665
else:
672666
logger.warning(
673667
"Window target ignored, requires tmux 3.1 or newer.",
@@ -679,7 +673,7 @@ def new_window(
679673
if window_shell:
680674
window_args += (window_shell,)
681675

682-
cmd = self.cmd("new-window", *window_args)
676+
cmd = self.cmd("new-window", *window_args, target=target)
683677

684678
if cmd.stderr:
685679
raise exc.LibTmuxException(cmd.stderr)
@@ -708,11 +702,11 @@ def kill_window(self, target_window: t.Optional[str] = None) -> None:
708702
"""
709703
if target_window:
710704
if isinstance(target_window, int):
711-
target = "-t%s:%d" % (self.window_name, target_window)
705+
target = "%s:%d" % (self.window_name, target_window)
712706
else:
713-
target = "-t%s" % target_window
707+
target = "%s" % target_window
714708

715-
proc = self.cmd("kill-window", target)
709+
proc = self.cmd("kill-window", target=target)
716710

717711
if proc.stderr:
718712
raise exc.LibTmuxException(proc.stderr)
@@ -809,7 +803,7 @@ def attach_session(self) -> "Session":
809803
category=DeprecationWarning,
810804
stacklevel=2,
811805
)
812-
proc = self.cmd("attach-session", "-t%s" % self.session_id)
806+
proc = self.cmd("attach-session", target=self.session_id)
813807

814808
if proc.stderr:
815809
raise exc.LibTmuxException(proc.stderr)
@@ -830,7 +824,7 @@ def kill_session(self) -> None:
830824
category=DeprecationWarning,
831825
stacklevel=2,
832826
)
833-
proc = self.cmd("kill-session", "-t%s" % self.session_id)
827+
proc = self.cmd("kill-session", target=self.session_id)
834828

835829
if proc.stderr:
836830
raise exc.LibTmuxException(proc.stderr)

src/libtmux/window.py

+18-12
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,12 @@ def cmd(
141141
self,
142142
cmd: str,
143143
*args: t.Any,
144+
target: t.Optional[t.Union[str, int]] = None,
144145
) -> tmux_cmd:
145146
"""Execute tmux subcommand within window context.
146147
147-
Automatically adds ``-t`` for object's indow ID to the command. Pass ``-t``
148-
in args to override.
148+
Automatically binds target by adding ``-t`` for object's window ID to the
149+
command. Pass ``target`` to keyword arguments to override.
149150
150151
Examples
151152
--------
@@ -160,14 +161,19 @@ def cmd(
160161
... 'split-window', '-P', '-F#{pane_id}').stdout[0], server=session.server)
161162
Pane(%... Window(@... ...:..., Session($1 libtmux_...)))
162163
164+
Parameters
165+
----------
166+
target : str, optional
167+
Optional custom target override. By default, the target is the window ID.
168+
163169
Returns
164170
-------
165171
:meth:`server.cmd`
166172
"""
167-
if not any("-t" in str(x) for x in args):
168-
args = ("-t", self.window_id, *args)
173+
if target is None:
174+
target = self.window_id
169175

170-
return self.server.cmd(cmd, *args)
176+
return self.server.cmd(cmd, *args, target=target)
171177

172178
"""
173179
Commands (tmux-like)
@@ -188,9 +194,9 @@ def select_pane(self, target_pane: t.Union[str, int]) -> t.Optional["Pane"]:
188194
:class:`Pane`
189195
"""
190196
if target_pane in ["-l", "-U", "-D", "-L", "-R"]:
191-
proc = self.cmd("select-pane", "-t%s" % self.window_id, target_pane)
197+
proc = self.cmd("select-pane", target_pane, target=self.window_id)
192198
else:
193-
proc = self.cmd("select-pane", "-t%s" % target_pane)
199+
proc = self.cmd("select-pane", target=target_pane)
194200

195201
if proc.stderr:
196202
raise exc.LibTmuxException(proc.stderr)
@@ -367,12 +373,12 @@ def select_layout(self, layout: t.Optional[str] = None) -> "Window":
367373
'custom'
368374
custom dimensions (see :term:`tmux(1)` manpages).
369375
"""
370-
cmd = ["select-layout", f"-t{self.session_id}:{self.window_index}"]
376+
cmd = ["select-layout"]
371377

372378
if layout: # tmux allows select-layout without args
373379
cmd.append(layout)
374380

375-
proc = self.cmd(*cmd)
381+
proc = self.cmd(*cmd, target=f"{self.session_id}:{self.window_index}")
376382

377383
if proc.stderr:
378384
raise exc.LibTmuxException(proc.stderr)
@@ -404,9 +410,9 @@ def set_window_option(self, option: str, value: t.Union[int, str]) -> "Window":
404410

405411
cmd = self.cmd(
406412
"set-window-option",
407-
f"-t{self.session_id}:{self.window_index}",
408413
option,
409414
value,
415+
target=f"{self.session_id}:{self.window_index}",
410416
)
411417

412418
if isinstance(cmd.stderr, list) and len(cmd.stderr):
@@ -608,7 +614,7 @@ def move_window(
608614
proc = self.cmd(
609615
"move-window",
610616
f"-s{self.session_id}:{self.window_index}",
611-
f"-t{session}:{destination}",
617+
target=f"{session}:{destination}",
612618
)
613619

614620
if proc.stderr:
@@ -916,7 +922,7 @@ def kill_window(self) -> None:
916922
)
917923
proc = self.cmd(
918924
"kill-window",
919-
f"-t{self.session_id}:{self.window_index}",
925+
target=f"{self.session_id}:{self.window_index}",
920926
)
921927

922928
if proc.stderr:

0 commit comments

Comments
 (0)