libvt.contrib.tmux.capture

Use build_tmux_capture_command() to capture the pane’s written extent and trailing styles together on tmux 3.4 and later. For raw captures, use the same pane, line range and line flags with capture-pane -e -p -N -T and capture-pane -e -p -N.

>>> from libvt.contrib.tmux import decode_tmux_capture
>>> capture = decode_tmux_capture(
...     b"\n",
...     rows=1, cols=3, trim_trailing=True,
...     untrimmed_capture=b"\x1b[41m   \n",
... )
>>> capture.snapshot.lines
('',)
>>> capture.snapshot[0, 2].style.bg.name
'RED'
>>> capture.completeness.exact_cells
True

An older tmux can return the same trailing spaces for a written blank row and an erased row. The decoder retains the available styles, and reports trailing_cells in completeness.unexposed_fields when it cannot establish which spaces were written. A -T capture without its untrimmed companion likewise reports uncertainty when a row stops short of the pane width.

Preserve empty physical rows when passing a sequence of lines. A capture with missing viewport, requested history, or requested saved-primary rows reports capture_rows and clears exact_cells and exact_wraps. The generation bracket reports the metadata comparison independently.

Continue after a capture

exact_cells describes the captured frame. Later input can depend on earlier bytes. unexposed_fields reports state outside the capture’s visible cells.

  • preceding_graphic is the glyph that a following REP (CSI Ps b) repeats. A cursor movement can clear it without changing the visible cells.

  • grapheme_continuation is the state used to join a following combining mark or emoji component to the preceding glyph.

  • utf8_prefix is an incomplete UTF-8 code point at the checkpoint.

TmuxPaneSync feeds the pending escape bytes tmux returns through capture-pane -P -C. The native command can return an empty prefix while the UTF-8 decoder still has buffered bytes. Resume from a known input boundary when continuation must be exact. A redraw that supplies complete encoded text and establishes its own first graphic can serve as that checkpoint.

tmux capture-pane reconstruction and chained command bundles.

libvt.contrib.tmux.capture.TMUX_METADATA_FORMAT: str = <...truncated, 594 chars>
data
data
libvt.contrib.tmux.capture.TMUX_METADATA_FORMAT: str = <...truncated, 594 chars>

Format string passed to tmux display-message -p for pane metadata.

class libvt.contrib.tmux.capture.CaptureCompleteness
class libvt.contrib.tmux.capture.CaptureCompleteness

Bases: object

Explicit fidelity report for a decoded tmux capture-pane snapshot.

Cell fidelity describes the captured frame. The preceding graphic for REP, the active grapheme, and an incomplete UTF-8 code point can remain unknown even when the cells are exact.

class libvt.contrib.tmux.capture.TmuxCaptureCommand
class libvt.contrib.tmux.capture.TmuxCaptureCommand

Bases: object

Chained tmux capture command specification and bundle parser.

class libvt.contrib.tmux.capture.TmuxCaptureResult
class libvt.contrib.tmux.capture.TmuxCaptureResult

Bases: object

Decoded tmux capture bundle ready for inspection or live streaming.

Examples

>>> from libvt.contrib.tmux.capture import decode_tmux_capture
>>> from libvt.testing import serialize_vt_snapshot
>>> res = decode_tmux_capture(
...     ["\x1b[1;32mPASS\x1b[0m suite", "done"],
...     rows=2,
...     cols=12,
... )
>>> print(serialize_vt_snapshot(res.snapshot))
size=12x2 cursor=(0,0,vis=True) alt=False modes=[]
┌────────────┐
│PASS suite  │
│done        │
└────────────┘
styles:
  r0[0..4]: bold fg=green
class libvt.contrib.tmux.capture.TmuxLineFlags
class libvt.contrib.tmux.capture.TmuxLineFlags

Bases: IntFlag

Per-line flags emitted by tmux capture-pane -F (tmux >= 3.7).

class libvt.contrib.tmux.capture.TmuxPaneMetadata
class libvt.contrib.tmux.capture.TmuxPaneMetadata

Bases: object

Out-of-band tmux pane state queried via TMUX_METADATA_FORMAT.

libvt.contrib.tmux.capture.build_tmux_capture_command(target=None, *, tmux_version='3.8a', include_saved_primary=True, history_lines=None, nonce=None)
function[source]
function[source]
libvt.contrib.tmux.capture.build_tmux_capture_command(target=None, *, tmux_version='3.8a', include_saved_primary=True, history_lines=None, nonce=None)

Build a single-invocation tmux command chaining metadata and capture.

Chains metadata and capture-pane -e -p -N in one tmux command queue. From tmux 3.4, a companion -T capture establishes the written extent while the untrimmed capture retains trailing cell styles. From tmux 3.7 the captures use -F for wrap flags. Older versions add a -J capture to infer wraps. A closing POST footer preserves trailing blank viewport rows in line-based stdout.

Parameters:
  • target (str | None) – Optional tmux target pane (e.g. "%0"); when None, -t is omitted so tmux targets the current pane.

  • tmux_version (tuple[int, int] | str) – Target tmux version as (major, minor) or tmux -V string (e.g. "3.8a", "3.7", "3.6", "3.3a").

  • include_saved_primary (bool) – Include capture-pane -a -q sections to restore backgrounded primary history when alternate_on is active.

  • history_lines (int | None) – Maximum scrollback lines to capture above the viewport (None for -S - full history, 0 for -S 0 viewport-only, or N > 0 for -S -N).

  • nonce (str | None) – Explicit ASCII boundary token matching [0-9A-Za-z_-]{8,128}; generated via secrets.token_hex() when None.

Returns:

Command specification with argv and parse().

Return type:

TmuxCaptureCommand

Raises:
  • TypeError – If history_lines is not an int or None.

  • ValueError – If target, tmux_version, history_lines, or nonce is invalid.

References

Examples

>>> from libvt.contrib.tmux.capture import build_tmux_capture_command
>>> cmd = build_tmux_capture_command("%1", tmux_version="3.8a", nonce="CAP_NONCE_1")
>>> cmd.flagged, cmd.trim_trailing, cmd.has_joined
(True, True, False)
libvt.contrib.tmux.capture.decode_tmux_capture(captured, *, metadata=None, rows=24, cols=80, scrollback=None, history_lines=None, flagged=False, trim_trailing=False, untrimmed_capture=None, joined_capture=None, saved_primary_capture=None, saved_primary_flagged=None, saved_primary_trim_trailing=None, saved_primary_untrimmed_capture=None, saved_primary_joined_capture=None, post_metadata=None, tmux_version=None, profile=TMUX_PROFILE, policy=TMUX_CAPTURE_POLICY, limits=DEFAULT_LIMITS)
function[source]
function[source]
libvt.contrib.tmux.capture.decode_tmux_capture(captured, *, metadata=None, rows=24, cols=80, scrollback=None, history_lines=None, flagged=False, trim_trailing=False, untrimmed_capture=None, joined_capture=None, saved_primary_capture=None, saved_primary_flagged=None, saved_primary_trim_trailing=None, saved_primary_untrimmed_capture=None, saved_primary_joined_capture=None, post_metadata=None, tmux_version=None, profile=TMUX_PROFILE, policy=TMUX_CAPTURE_POLICY, limits=DEFAULT_LIMITS)

Reconstruct a Terminal from tmux capture-pane.

Handles bare SO/SI (0x0e/0x0f) ACS box-drawing runs without polluting the reconstructed terminal’s live G1 charset, decodes tmux’s CSI 5:3 m overline serialization strictly on the capture scratch grid, expands GRID_FLAG_TAB \t cells against #{pane_tabs} while preserving active SGR background/attributes, carries SGR across rows just like tmux’s grid_string_cells(&gc), reconstructs pending_wrap from cursor_x >= pane_width, and optionally populates backgrounded primary history when alternate_on is active.

Parameters:
  • captured (CaptureInput) – Output of tmux capture-pane -S - -E - -e -p -N [-T] [-F] as str, bytes, or a line sequence (e.g. libtmux Pane.capture_pane).

  • metadata (TmuxPaneMetadata | str | None) – Parsed TmuxPaneMetadata or raw format line emitted by display-message -p TMUX_METADATA_FORMAT; when None, rows, cols, and scrollback defaults are used.

  • rows (int) – Fallback pane height when metadata is None.

  • cols (int) – Fallback pane width when metadata is None.

  • scrollback (int | None) – Explicit scrollback line cap; when None, defaults to metadata.history_limit if metadata is provided, else 2000.

  • history_lines (int | None) – Maximum scrollback lines requested via capture-pane -S (None for full history -S -, 0 for viewport-only -S 0, or N for -S -N).

  • flagged (bool) – True when captured includes capture-pane -F line prefixes.

  • trim_trailing (bool) – True when captured used capture-pane -N -T (tmux >= 3.4) to record the written extent. Styled cells beyond it need a companion.

  • untrimmed_capture (CaptureInput | None) – Optional capture-pane -e -p -N output from the same pane state. Use the same line range and -F setting as captured and omit -T. Supplies trailing cell styles when trim_trailing is True.

  • joined_capture (CaptureInput | None) – Optional capture-pane -S - -E - -e -p -J output used to infer soft wraps on tmux < 3.8 when flagged=False.

  • saved_primary_capture (CaptureInput | None) – Optional capture-pane -a -q -S - -E - -e -p -N [-T] [-F] output restoring the backgrounded primary screen and scrollback when alternate_on is True.

  • saved_primary_flagged (bool | None) – Whether saved_primary_capture carries -F prefixes (defaults to flagged).

  • saved_primary_trim_trailing (bool | None) – Whether saved_primary_capture used -T (defaults to trim_trailing).

  • saved_primary_untrimmed_capture (CaptureInput | None) – Untrimmed companion from the same saved primary grid and line range.

  • saved_primary_joined_capture (CaptureInput | None) – Optional capture-pane -a -q -S - -E - -e -p -J companion capture for saved_primary_capture when saved_primary_flagged is False.

  • post_metadata (TmuxPaneMetadata | str | None) – Optional second metadata sample taken immediately after capture-pane to verify pane_output_generation stability.

  • tmux_version (tuple[int, int] | str | None) – Release that produced the capture. Before tmux 3.5, a widening emoji selector can shift the next captured cell. An unknown release receives the same qualification in the completeness report.

  • profile (Profile) – Compatibility profile for the reconstructed terminal.

  • policy (Policy) – Security policy for the reconstructed terminal.

  • limits (Limits) – Resource ceilings for the reconstructed terminal.

Returns:

Reconstructed terminal, immutable snapshot, completeness report, and parsed per-line flags.

Return type:

TmuxCaptureResult

Raises:

ValueError – When an untrimmed companion has no corresponding -T capture.

References

Examples

>>> from libvt.contrib.tmux.capture import decode_tmux_capture
>>> from libvt.testing import serialize_vt_snapshot
>>> res = decode_tmux_capture(["\x1b[32mready\x1b[0m", "done"], rows=2, cols=10)
>>> print(serialize_vt_snapshot(res.snapshot))
size=10x2 cursor=(0,0,vis=True) alt=False modes=[]
┌──────────┐
│ready     │
│done      │
└──────────┘
styles:
  r0[0..5]: fg=green
libvt.contrib.tmux.capture.parse_flagged_line(line)
function[source]
function[source]
libvt.contrib.tmux.capture.parse_flagged_line(line)

Split a tmux capture-pane -F line into (flags, body).

Parameters:

line (str) – Single line emitted by tmux capture-pane -F (without trailing \n).

Returns:

Parsed TmuxLineFlags and remaining SGR/text payload.

Return type:

tuple[TmuxLineFlags, str]

Raises:

ValueError – If line does not start with a valid <flags> space-delimited prefix.

References

Examples

>>> from libvt.contrib.tmux.capture import TmuxLineFlags, parse_flagged_line
>>> flags, body = parse_flagged_line("PW $ echo hi")
>>> flags == (TmuxLineFlags.START_PROMPT | TmuxLineFlags.WRAPPED), body
(True, '$ echo hi')
type libvt.contrib.tmux.capture.CaptureInput = str | bytes | bytearray | collections.abc.Sequence[str]
type
type
type libvt.contrib.tmux.capture.CaptureInput = str | bytes | bytearray | collections.abc.Sequence[str]

Accepted input representation for tmux capture-pane lines or raw text.