Skip to content

Commit 8970723

Browse files
committed
Pane(fix[capture]): trim trailing whitespace consistently
why: Control mode and subprocess should return matching capture-pane output. what: - Trim trailing whitespace-only lines in control protocol and Pane.capture_pane - Add regression fixtures for whitespace tails across engines
1 parent 7e6f5fd commit 8970723

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

src/libtmux/_internal/engines/control_protocol.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424

2525

2626
def _trim_lines(lines: list[str]) -> list[str]:
27-
"""Remove trailing empty strings to mirror subprocess behaviour."""
27+
"""Remove trailing empty or whitespace-only lines to mirror subprocess behaviour."""
2828
trimmed = list(lines)
29-
while trimmed and trimmed[-1] == "":
29+
while trimmed and trimmed[-1].strip() == "":
3030
trimmed.pop()
3131
return trimmed
3232

src/libtmux/pane.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,9 +363,9 @@ def capture_pane(
363363
output = self.cmd(*cmd).stdout
364364

365365
def _trim(lines: list[str]) -> list[str]:
366-
# Match engine trimming: remove only empty strings, not whitespace-only
366+
# Match engine trimming: remove trailing empty or whitespace-only lines
367367
trimmed = list(lines)
368-
while trimmed and trimmed[-1] == "":
368+
while trimmed and trimmed[-1].strip() == "":
369369
trimmed.pop()
370370
return trimmed
371371

tests/test_control_mode_regressions.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ class AttachFixture(t.NamedTuple):
6464
),
6565
id="many_blanks",
6666
),
67+
pytest.param(
68+
TrailingOutputFixture(
69+
test_id="whitespace_tail",
70+
raw_lines=["line1", " ", " ", ""],
71+
expected_stdout=["line1"],
72+
),
73+
id="whitespace_tail",
74+
),
6775
]
6876

6977

tests/test_pane.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,23 @@ def test_capture_pane(session: Session) -> None:
9696
assert pane_contents == expected_full
9797

9898

99+
@pytest.mark.engines(["subprocess", "control"])
100+
def test_capture_pane_trims_whitespace_tail(session: Session) -> None:
101+
"""capture-pane should drop trailing whitespace-only lines for all engines."""
102+
pane = session.active_pane
103+
assert pane is not None
104+
105+
pane.send_keys('printf "line1\\n \\n"', literal=True, suppress_history=False)
106+
wait_for_line(pane, lambda line: "line1" in line)
107+
108+
lines = pane.capture_pane()
109+
assert lines
110+
# The last line should not be empty/whitespace-only
111+
assert lines[-1].strip() != ""
112+
# Ensure the whitespace-only line was trimmed
113+
assert "line1" in "\n".join(lines)
114+
115+
99116
def test_capture_pane_start(session: Session) -> None:
100117
"""Assert Pane.capture_pane() with ``start`` param."""
101118
env = shutil.which("env")

0 commit comments

Comments
 (0)