Skip to content

Commit 7610e51

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 62f63be commit 7610e51

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
@@ -322,8 +322,14 @@ def capture_pane(
322322
self,
323323
start: t.Literal["-"] | int | None = None,
324324
end: t.Literal["-"] | int | None = None,
325+
*,
326+
escape_sequences: bool = False,
327+
escape_non_printable: bool = False,
328+
join_wrapped: bool = False,
329+
preserve_trailing: bool = False,
330+
trim_trailing: bool = False,
325331
) -> list[str]:
326-
"""Capture text from pane.
332+
r"""Capture text from pane.
327333
328334
``$ tmux capture-pane`` to pane.
329335
``$ tmux capture-pane -S -10`` to pane.
@@ -346,17 +352,74 @@ def capture_pane(
346352
Negative numbers are lines in the history.
347353
``-`` is the end of the visible pane.
348354
Default: None
355+
escape_sequences : bool, optional
356+
Include ANSI escape sequences for text and background attributes
357+
(``-e`` flag). Useful for capturing colored output.
358+
Default: False
359+
escape_non_printable : bool, optional
360+
Escape non-printable characters as octal ``\\xxx`` format
361+
(``-C`` flag). Useful for binary-safe capture.
362+
Default: False
363+
join_wrapped : bool, optional
364+
Join wrapped lines and preserve trailing spaces (``-J`` flag).
365+
Lines that were wrapped by tmux will be joined back together.
366+
Default: False
367+
preserve_trailing : bool, optional
368+
Preserve trailing spaces at each line's end (``-N`` flag).
369+
Default: False
370+
trim_trailing : bool, optional
371+
Trim trailing positions with no characters (``-T`` flag).
372+
Only includes characters up to the last used cell.
373+
Requires tmux 3.4+. If used with tmux < 3.4, a warning
374+
is issued and the flag is ignored.
375+
Default: False
349376
350377
Returns
351378
-------
352379
list[str]
353380
Captured pane content.
381+
382+
Examples
383+
--------
384+
>>> pane = window.split(shell='sh')
385+
>>> pane.capture_pane()
386+
['$']
387+
388+
>>> pane.send_keys('echo "Hello world"', enter=True)
389+
390+
>>> pane.capture_pane()
391+
['$ echo "Hello world"', 'Hello world', '$']
392+
393+
>>> print(chr(10).join(pane.capture_pane()))
394+
$ echo "Hello world"
395+
Hello world
396+
$
354397
"""
398+
import warnings
399+
400+
from libtmux.common import has_gte_version
401+
355402
cmd = ["capture-pane", "-p"]
356403
if start is not None:
357404
cmd.extend(["-S", str(start)])
358405
if end is not None:
359406
cmd.extend(["-E", str(end)])
407+
if escape_sequences:
408+
cmd.append("-e")
409+
if escape_non_printable:
410+
cmd.append("-C")
411+
if join_wrapped:
412+
cmd.append("-J")
413+
if preserve_trailing:
414+
cmd.append("-N")
415+
if trim_trailing:
416+
if has_gte_version("3.4"):
417+
cmd.append("-T")
418+
else:
419+
warnings.warn(
420+
"trim_trailing requires tmux 3.4+, ignoring",
421+
stacklevel=2,
422+
)
360423
return self.cmd(*cmd).stdout
361424

362425
def capture_frame(

0 commit comments

Comments
 (0)