Skip to content

Commit d8957d4

Browse files
committed
Window(feat[last_pane]): call last-pane directly instead of select-pane -l
why: last-pane is a real tmux command (since 1.4) with its own flags (det:Z). Call it directly and expose its unique parameters. what: - Change Window.last_pane() to call tmux last-pane directly - Add detach (-d), keep_zoom (-Z), disable_input (-e) parameters - Raise exc.LibTmuxException on stderr (never silently return None) - Add test verifying last pane selection
1 parent 3421d7d commit d8957d4

2 files changed

Lines changed: 66 additions & 3 deletions

File tree

src/libtmux/window.py

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -403,9 +403,54 @@ def resize(
403403
self.refresh()
404404
return self
405405

406-
def last_pane(self) -> Pane | None:
407-
"""Return last pane."""
408-
return self.select_pane("-l")
406+
def last_pane(
407+
self,
408+
*,
409+
detach: bool | None = None,
410+
keep_zoom: bool | None = None,
411+
disable_input: bool | None = None,
412+
) -> Pane | None:
413+
"""Select the last (previously active) pane via ``$ tmux last-pane``.
414+
415+
Parameters
416+
----------
417+
detach : bool, optional
418+
Do not make the pane active (``-d`` flag).
419+
keep_zoom : bool, optional
420+
Keep the window zoomed if zoomed (``-Z`` flag).
421+
disable_input : bool, optional
422+
Disable input to the pane (``-e`` flag).
423+
424+
Returns
425+
-------
426+
:class:`Pane` or None
427+
The selected pane, or None if no last pane exists.
428+
429+
Examples
430+
--------
431+
>>> pane1 = window.active_pane
432+
>>> pane2 = window.split()
433+
>>> pane2.select()
434+
Pane(...)
435+
>>> result = window.last_pane()
436+
"""
437+
tmux_args: tuple[str, ...] = ()
438+
439+
if detach:
440+
tmux_args += ("-d",)
441+
442+
if keep_zoom:
443+
tmux_args += ("-Z",)
444+
445+
if disable_input:
446+
tmux_args += ("-e",)
447+
448+
proc = self.cmd("last-pane", *tmux_args)
449+
450+
if proc.stderr:
451+
raise exc.LibTmuxException(proc.stderr)
452+
453+
return self.active_pane
409454

410455
def select_layout(
411456
self,

tests/test_window.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,24 @@ def test_select_layout_next_previous(session: Session) -> None:
814814
assert layout_after_prev == layout_before
815815

816816

817+
def test_last_pane(session: Session) -> None:
818+
"""Test Window.last_pane() selects the previously active pane."""
819+
window = session.new_window(window_name="test_last_pane")
820+
pane1 = window.active_pane
821+
assert pane1 is not None
822+
pane2 = pane1.split()
823+
824+
# Select pane2 then pane1 to establish history
825+
pane2.select()
826+
pane1.select()
827+
828+
# last_pane should go back to pane2
829+
result = window.last_pane()
830+
assert result is not None
831+
pane2.refresh()
832+
assert pane2.pane_active == "1"
833+
834+
817835
def test_next_layout(session: Session) -> None:
818836
"""Test Window.next_layout() cycles to the next layout."""
819837
window = session.new_window(window_name="test_next_layout")

0 commit comments

Comments
 (0)