From 9eab168f4f3a76fdec578b0fd93998616390a7cb Mon Sep 17 00:00:00 2001 From: rockandska Date: Mon, 22 Aug 2022 14:10:21 +0200 Subject: [PATCH 1/5] fix(tmux_cmd): remove only trailing new lines in stdout --- libtmux/common.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/libtmux/common.py b/libtmux/common.py index 5217bc44e..df61b6986 100644 --- a/libtmux/common.py +++ b/libtmux/common.py @@ -270,16 +270,21 @@ 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 + for v in reversed(stdout_split): + if v == '': + stdout_split.pop() + else: + break 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)) From ce3bd14b21daa44b96356dc32e7c435e00f0182b Mon Sep 17 00:00:00 2001 From: rockandska Date: Mon, 22 Aug 2022 17:27:23 +0200 Subject: [PATCH 2/5] fix(new_window): correct type for window_shell --- libtmux/session.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libtmux/session.py b/libtmux/session.py index e2d1b614d..f996286de 100644 --- a/libtmux/session.py +++ b/libtmux/session.py @@ -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``. @@ -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. From 4c48599ed7443246b3d1586270b26d15482cfd44 Mon Sep 17 00:00:00 2001 From: rockandska Date: Mon, 22 Aug 2022 17:27:59 +0200 Subject: [PATCH 3/5] test(test_pane): add capture_pane test --- tests/test_pane.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/test_pane.py b/tests/test_pane.py index 6a1df4f9e..c8697c5d6 100644 --- a/tests/test_pane.py +++ b/tests/test_pane.py @@ -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$' + ) From 2346df4d1285a169a699e28184f9817a3642affb Mon Sep 17 00:00:00 2001 From: rockandska Date: Sun, 28 Aug 2022 19:38:22 +0200 Subject: [PATCH 4/5] chore(tmux_cmd): Tweak filtering of trailing newlines Co-authored-by: Tony Narlock --- libtmux/common.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/libtmux/common.py b/libtmux/common.py index df61b6986..3c4337caa 100644 --- a/libtmux/common.py +++ b/libtmux/common.py @@ -271,11 +271,8 @@ def __init__(self, *args: t.Any, **kwargs: t.Any) -> None: stdout_str = console_to_str(stdout) stdout_split = stdout_str.split("\n") # remove trailing newlines from stdout - for v in reversed(stdout_split): - if v == '': - stdout_split.pop() - else: - break + while stdout_split and stdout_split[-1] == "": + stdout_split.pop() stderr_str = console_to_str(stderr) stderr_split = stderr_str.split("\n") From 16cb0e78746be186a4e4aa00268203e3dd5ba372 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Sun, 28 Aug 2022 12:44:51 -0500 Subject: [PATCH 5/5] docs(CHANGES): Note tmux_cmd behavior update for trailing newlines --- CHANGES | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGES b/CHANGES index 5b5b70ea9..b8f51b2c2 100644 --- a/CHANGES +++ b/CHANGES @@ -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