Skip to content

Commit 6d3c49a

Browse files
authored
fix!(tmux_cmd): remove only trailing new lines in stdout (#405)
2 parents 94f507e + 16cb0e7 commit 6d3c49a

File tree

4 files changed

+34
-5
lines changed

4 files changed

+34
-5
lines changed

Diff for: CHANGES

+6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ $ pip install --user --upgrade --pre libtmux
1212

1313
- _Insert changes/features/fixes for next release here_
1414

15+
### Breaking changes
16+
17+
- Fixes #402: {func}`common.tmux_cmd` will only strip _trailing_ empty lines. Before this change,
18+
all empty lines were filtered out. This will lead to a more accurate behavior when using
19+
{meth}`Pane.capture_pane`. Credit: @rockandska, via #405.
20+
1521
### Documentation
1622

1723
- Move to sphinx-autoissues, #406

Diff for: libtmux/common.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -270,16 +270,18 @@ def __init__(self, *args: t.Any, **kwargs: t.Any) -> None:
270270

271271
stdout_str = console_to_str(stdout)
272272
stdout_split = stdout_str.split("\n")
273-
stdout_filtered = list(filter(None, stdout_split)) # filter empty values
273+
# remove trailing newlines from stdout
274+
while stdout_split and stdout_split[-1] == "":
275+
stdout_split.pop()
274276

275277
stderr_str = console_to_str(stderr)
276278
stderr_split = stderr_str.split("\n")
277279
self.stderr = list(filter(None, stderr_split)) # filter empty values
278280

279-
if "has-session" in cmd and len(self.stderr) and not stdout_filtered:
281+
if "has-session" in cmd and len(self.stderr) and not stdout_split:
280282
self.stdout = [self.stderr[0]]
281283
else:
282-
self.stdout = stdout_filtered
284+
self.stdout = stdout_split
283285

284286
logger.debug("self.stdout for {}: \n{}".format(" ".join(cmd), self.stdout))
285287

Diff for: libtmux/session.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ def new_window(
201201
start_directory: None = None,
202202
attach: bool = True,
203203
window_index: str = "",
204-
window_shell: None = None,
204+
window_shell: t.Optional[str] = None,
205205
) -> Window:
206206
"""
207207
Return :class:`Window` from ``$ tmux new-window``.
@@ -219,7 +219,7 @@ def new_window(
219219
window_index : str
220220
create the new window at the given index position. Default is empty
221221
string which will create the window in the next available position.
222-
window_shell : str
222+
window_shell : str, optional
223223
execute a command on starting the window. The window will close
224224
when the command exits.
225225

Diff for: tests/test_pane.py

+21
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,24 @@ def test_set_width(session: Session) -> None:
6363
assert int(pane1["pane_width"]) == 10
6464

6565
pane1.reset()
66+
67+
68+
def test_capture_pane(session: Session) -> None:
69+
session.new_window(
70+
attach=True,
71+
window_name="capture_pane",
72+
window_shell='env -i PS1="$ " /usr/bin/env bash --norc --noprofile'
73+
)
74+
pane = session.attached_window.attached_pane
75+
assert pane is not None
76+
pane_contents = "\n".join(pane.capture_pane())
77+
assert pane_contents == '$'
78+
pane.send_keys(
79+
r'printf "\n%s\n" "Hello World !"',
80+
literal=True,
81+
suppress_history=False
82+
)
83+
pane_contents = "\n".join(pane.capture_pane())
84+
assert pane_contents == r'$ printf "\n%s\n" "Hello World !"{}'.format(
85+
'\n\nHello World !\n$'
86+
)

0 commit comments

Comments
 (0)