Skip to content

Commit 420830f

Browse files
committed
Pane(feat[capture_pane]): Add 5 new flag parameters
why: Expose more tmux capture-pane capabilities for advanced use cases like capturing colored output, handling wrapped lines, and controlling trailing space behavior. what: - Add escape_sequences parameter (-e flag) for ANSI escape sequences - Add escape_non_printable parameter (-C flag) for octal escapes - Add join_wrapped parameter (-J flag) for joining wrapped lines - Add preserve_trailing parameter (-N flag) for trailing spaces - Add trim_trailing parameter (-T flag) with tmux 3.4+ version check - Issue warning when trim_trailing used with tmux < 3.4
1 parent bd13b1e commit 420830f

File tree

1 file changed

+64
-1
lines changed

1 file changed

+64
-1
lines changed

src/libtmux/pane.py

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,14 @@ def capture_pane(
320320
self,
321321
start: t.Literal["-"] | int | None = None,
322322
end: t.Literal["-"] | int | None = None,
323+
*,
324+
escape_sequences: bool = False,
325+
escape_non_printable: bool = False,
326+
join_wrapped: bool = False,
327+
preserve_trailing: bool = False,
328+
trim_trailing: bool = False,
323329
) -> list[str]:
324-
"""Capture text from pane.
330+
r"""Capture text from pane.
325331
326332
``$ tmux capture-pane`` to pane.
327333
``$ tmux capture-pane -S -10`` to pane.
@@ -344,17 +350,74 @@ def capture_pane(
344350
Negative numbers are lines in the history.
345351
``-`` is the end of the visible pane.
346352
Default: None
353+
escape_sequences : bool, optional
354+
Include ANSI escape sequences for text and background attributes
355+
(``-e`` flag). Useful for capturing colored output.
356+
Default: False
357+
escape_non_printable : bool, optional
358+
Escape non-printable characters as octal ``\\xxx`` format
359+
(``-C`` flag). Useful for binary-safe capture.
360+
Default: False
361+
join_wrapped : bool, optional
362+
Join wrapped lines and preserve trailing spaces (``-J`` flag).
363+
Lines that were wrapped by tmux will be joined back together.
364+
Default: False
365+
preserve_trailing : bool, optional
366+
Preserve trailing spaces at each line's end (``-N`` flag).
367+
Default: False
368+
trim_trailing : bool, optional
369+
Trim trailing positions with no characters (``-T`` flag).
370+
Only includes characters up to the last used cell.
371+
Requires tmux 3.4+. If used with tmux < 3.4, a warning
372+
is issued and the flag is ignored.
373+
Default: False
347374
348375
Returns
349376
-------
350377
list[str]
351378
Captured pane content.
379+
380+
Examples
381+
--------
382+
>>> pane = window.split(shell='sh')
383+
>>> pane.capture_pane()
384+
['$']
385+
386+
>>> pane.send_keys('echo "Hello world"', enter=True)
387+
388+
>>> pane.capture_pane()
389+
['$ echo "Hello world"', 'Hello world', '$']
390+
391+
>>> print(chr(10).join(pane.capture_pane()))
392+
$ echo "Hello world"
393+
Hello world
394+
$
352395
"""
396+
import warnings
397+
398+
from libtmux.common import has_gte_version
399+
353400
cmd = ["capture-pane", "-p"]
354401
if start is not None:
355402
cmd.extend(["-S", str(start)])
356403
if end is not None:
357404
cmd.extend(["-E", str(end)])
405+
if escape_sequences:
406+
cmd.append("-e")
407+
if escape_non_printable:
408+
cmd.append("-C")
409+
if join_wrapped:
410+
cmd.append("-J")
411+
if preserve_trailing:
412+
cmd.append("-N")
413+
if trim_trailing:
414+
if has_gte_version("3.4"):
415+
cmd.append("-T")
416+
else:
417+
warnings.warn(
418+
"trim_trailing requires tmux 3.4+, ignoring",
419+
stacklevel=2,
420+
)
358421
return self.cmd(*cmd).stdout
359422

360423
def send_keys(

0 commit comments

Comments
 (0)