Skip to content

Commit 8c4ca4d

Browse files
committed
Session(feat[new_window]): add kill-existing and select-existing flags
why: new-window supports flags for replacing existing windows at a target index and selecting existing windows by name that were not exposed. what: - Add kill_existing (-k) and select_existing (-S) parameters to new_window() - -k destroys existing window at target index before creating new one - -S selects existing window with matching name instead of creating new - Add tests for both flags
1 parent f279d1a commit 8c4ca4d

2 files changed

Lines changed: 51 additions & 0 deletions

File tree

src/libtmux/session.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,8 @@ def new_window(
461461
environment: dict[str, str] | None = None,
462462
direction: WindowDirection | None = None,
463463
target_window: str | None = None,
464+
kill_existing: bool | None = None,
465+
select_existing: bool | None = None,
464466
) -> Window:
465467
"""Create new window, returns new :class:`Window`.
466468
@@ -491,6 +493,16 @@ def new_window(
491493
492494
target_window : str, optional
493495
Used by :meth:`Window.new_window` to specify the target window.
496+
kill_existing : bool, optional
497+
Destroy the window at the target index if it already exists
498+
(``-k`` flag).
499+
500+
.. versionadded:: 0.45
501+
select_existing : bool, optional
502+
If a window with the given name already exists, select it instead
503+
of creating a new one (``-S`` flag).
504+
505+
.. versionadded:: 0.45
494506
495507
.. versionchanged:: 0.28.0
496508
@@ -555,6 +567,12 @@ def new_window(
555567
if direction is not None:
556568
window_args += (WINDOW_DIRECTION_FLAG_MAP[direction],)
557569

570+
if kill_existing:
571+
window_args += ("-k",)
572+
573+
if select_existing:
574+
window_args += ("-S",)
575+
558576
target: str | None = None
559577
if window_index is not None:
560578
# empty string for window_index will use the first one available

tests/test_session.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,3 +574,36 @@ def patched_cmd(cmd_name: str, *args: t.Any, **kwargs: t.Any) -> tmux_cmd:
574574
)
575575
with raises_ctx:
576576
test_session.attach()
577+
578+
579+
def test_new_window_kill_existing(session: Session) -> None:
580+
"""Test Session.new_window() with kill_existing flag."""
581+
# Create a window at a specific index
582+
w1 = session.new_window(window_name="kill_orig", window_index="5")
583+
assert w1.window_name == "kill_orig"
584+
assert w1.window_index == "5"
585+
586+
# Create another window at the same index with kill_existing
587+
w2 = session.new_window(
588+
window_name="kill_replace",
589+
window_index="5",
590+
kill_existing=True,
591+
)
592+
assert w2.window_name == "kill_replace"
593+
assert w2.window_index == "5"
594+
595+
# Original window should be gone
596+
session.refresh()
597+
names = [w.window_name for w in session.windows]
598+
assert "kill_orig" not in names
599+
assert "kill_replace" in names
600+
601+
602+
def test_new_window_select_existing(session: Session) -> None:
603+
"""Test Session.new_window() with select_existing flag — new name."""
604+
# With a unique name and select_existing, a new window is created normally
605+
w = session.new_window(
606+
window_name="selexist_new",
607+
select_existing=True,
608+
)
609+
assert w.window_name == "selexist_new"

0 commit comments

Comments
 (0)