Skip to content

Commit

Permalink
fix kill_window
Browse files Browse the repository at this point in the history
  • Loading branch information
Moysenko committed Oct 22, 2023
1 parent b2302c8 commit b172f49
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/libtmux/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,25 +523,29 @@ def new_window(
server=self.server, window_id=window_formatters["window_id"]
)

def kill_window(self, target_window: t.Optional[str] = None) -> None:
def kill_window(self, target_window: t.Optional[t.Union[str, int]] = None) -> None:
"""Close a tmux window, and all panes inside it, ``$ tmux kill-window``
Kill the current window or the window at ``target-window``. removing it
from any sessions to which it is linked.
Parameters
----------
target_window : str, optional
window to kill
target_window : str, int, optional
window to kill. Stands for a window id in the format "@{id}",
if str, a window index if int, and an attached window if none.
"""

if target_window:
kill_cmd = ["kill-window"]

if target_window is not None:
if isinstance(target_window, int):
target = "-t%s:%d" % (self.window_name, target_window)
target = "-t%s:%d" % (self.session_name, target_window)
else:
target = "-t%s" % target_window

proc = self.cmd("kill-window", target)
kill_cmd.append(target)

proc = self.cmd(*kill_cmd)

if proc.stderr:
raise exc.LibTmuxException(proc.stderr)
Expand Down
21 changes: 21 additions & 0 deletions tests/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import pytest

from libtmux import exc
from libtmux._internal.query_list import ObjectDoesNotExist
from libtmux.common import has_gte_version, has_lt_version
from libtmux.pane import Pane
from libtmux.server import Server
Expand Down Expand Up @@ -321,3 +322,23 @@ def test_new_window_with_environment_logs_warning_for_old_tmux(
assert any(
"Cannot set up environment" in record.msg for record in caplog.records
), "Warning missing"


@pytest.mark.parametrize(
"target_window_mapper",
[
lambda w: int(w.index),
lambda w: w.id,
lambda w: None,
]
)
def test_kill_window_by_id(
session: Session,
target_window_mapper: t.Callable[[Window], t.Optional[t.Union[int, str]]],
) -> None:
# kill by window index
session.new_window()
w = session.attached_window
session.kill_window(target_window_mapper(w))
with pytest.raises(ObjectDoesNotExist):
w.refresh()

0 comments on commit b172f49

Please sign in to comment.