Skip to content

fix(tmux_cmd): remove only trailing new lines in stdout #405

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ $ pip install --user --upgrade --pre libtmux

- _Insert changes/features/fixes for next release here_

### Breaking changes

- Fixes #402: {func}`common.tmux_cmd` will only strip _trailing_ empty lines. Before this change,
all empty lines were filtered out. This will lead to a more accurate behavior when using
{meth}`Pane.capture_pane`. Credit: @rockandska, via #405.

### Documentation

- Move to sphinx-autoissues, #406
Expand Down
8 changes: 5 additions & 3 deletions libtmux/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,16 +270,18 @@ def __init__(self, *args: t.Any, **kwargs: t.Any) -> None:

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

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

if "has-session" in cmd and len(self.stderr) and not stdout_filtered:
if "has-session" in cmd and len(self.stderr) and not stdout_split:
self.stdout = [self.stderr[0]]
else:
self.stdout = stdout_filtered
self.stdout = stdout_split

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

Expand Down
4 changes: 2 additions & 2 deletions libtmux/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def new_window(
start_directory: None = None,
attach: bool = True,
window_index: str = "",
window_shell: None = None,
window_shell: t.Optional[str] = None,
) -> Window:
"""
Return :class:`Window` from ``$ tmux new-window``.
Expand All @@ -219,7 +219,7 @@ def new_window(
window_index : str
create the new window at the given index position. Default is empty
string which will create the window in the next available position.
window_shell : str
window_shell : str, optional
execute a command on starting the window. The window will close
when the command exits.
Expand Down
21 changes: 21 additions & 0 deletions tests/test_pane.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,24 @@ def test_set_width(session: Session) -> None:
assert int(pane1["pane_width"]) == 10

pane1.reset()


def test_capture_pane(session: Session) -> None:
session.new_window(
attach=True,
window_name="capture_pane",
window_shell='env -i PS1="$ " /usr/bin/env bash --norc --noprofile'
)
pane = session.attached_window.attached_pane
assert pane is not None
pane_contents = "\n".join(pane.capture_pane())
assert pane_contents == '$'
pane.send_keys(
r'printf "\n%s\n" "Hello World !"',
literal=True,
suppress_history=False
)
pane_contents = "\n".join(pane.capture_pane())
assert pane_contents == r'$ printf "\n%s\n" "Hello World !"{}'.format(
'\n\nHello World !\n$'
)