Skip to content

Commit 7df463a

Browse files
committed
Pane(feat[clear_history]): add clear_history() wrapping tmux clear-history
why: clear-history is useful for clearing pane scrollback buffers, especially important for test isolation and monitoring workflows. what: - Add clear_history() method with clear_pane (-H, 3.4+) parameter - Version-gate -H flag with has_gte_version("3.4") - Add test verifying history is cleared after sending commands
1 parent 186ebd1 commit 7df463a

2 files changed

Lines changed: 63 additions & 0 deletions

File tree

src/libtmux/pane.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,6 +1139,41 @@ def enter(self) -> Pane:
11391139
self.cmd("send-keys", "Enter")
11401140
return self
11411141

1142+
def clear_history(self, *, clear_pane: bool | None = None) -> None:
1143+
"""Clear pane history buffer via ``$ tmux clear-history``.
1144+
1145+
Parameters
1146+
----------
1147+
clear_pane : bool, optional
1148+
Also clear the visible pane content (``-H`` flag).
1149+
Requires tmux 3.4+.
1150+
1151+
.. versionadded:: 0.45
1152+
1153+
Examples
1154+
--------
1155+
>>> pane.clear_history()
1156+
"""
1157+
import warnings
1158+
1159+
from libtmux.common import has_gte_version
1160+
1161+
tmux_args: tuple[str, ...] = ()
1162+
1163+
if clear_pane:
1164+
if has_gte_version("3.4", tmux_bin=self.server.tmux_bin):
1165+
tmux_args += ("-H",)
1166+
else:
1167+
warnings.warn(
1168+
"clear_pane requires tmux 3.4+, ignoring",
1169+
stacklevel=2,
1170+
)
1171+
1172+
proc = self.cmd("clear-history", *tmux_args)
1173+
1174+
if proc.stderr:
1175+
raise exc.LibTmuxException(proc.stderr)
1176+
11421177
def clear(self) -> Pane:
11431178
"""Clear pane."""
11441179
self.send_keys("reset")

tests/test_pane.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,3 +738,31 @@ def test_split_percentage_size_mutual_exclusion(session: Session) -> None:
738738
assert pane is not None
739739
with pytest.raises(ValueError, match="Cannot specify both"):
740740
pane.split(size=10, percentage=50)
741+
742+
743+
def test_clear_history(session: Session) -> None:
744+
"""Test Pane.clear_history()."""
745+
env = shutil.which("env")
746+
assert env is not None
747+
748+
window = session.new_window(
749+
window_name="test_clearhist",
750+
window_shell=f"{env} PS1='$ ' sh",
751+
)
752+
pane = window.active_pane
753+
assert pane is not None
754+
755+
retry_until(lambda: "$" in "\n".join(pane.capture_pane()), 2, raises=True)
756+
757+
# Send some commands to build up history
758+
pane.send_keys("echo line1", enter=True)
759+
pane.send_keys("echo line2", enter=True)
760+
retry_until(lambda: "line2" in "\n".join(pane.capture_pane()), 3, raises=True)
761+
762+
# Clear history
763+
pane.clear_history()
764+
765+
# The scrollback should be cleared (visible content may still show current)
766+
history = pane.capture_pane(start=-100)
767+
# After clearing, scrollback history should be much shorter
768+
assert len(history) <= 30 # reasonable bound after clear

0 commit comments

Comments
 (0)