libvt.contrib.tmux.control

A pane size change starts the same native refresh as resync(). Send the commands returned by handle_event() and wait for LIVE before reading the new dimensions. The terminal keeps its last frame until the new capture installs.

Keep a scrollback bookmark across updates

Store an absolute row with a SyncToken from the same terminal. A native refresh retains the current visible rows and the requested history that tmux still holds. Once tmux has evicted earlier history, the capture cannot recover the previous absolute origin. The same row number may resolve to different text after a refresh.

>>> from libvt import Terminal
>>> from libvt.terminal import ResyncReason, SyncToken
>>> def bookmarked_text(term: Terminal, token: SyncToken, row: int) -> str | None:
...     delta = term.changes_since(token)
...     expired = (ResyncReason.EPOCH_CHANGED, ResyncReason.TERMINAL_MISMATCH)
...     if delta.resync_reason in expired or delta.snapshot.active_is_alt:
...         return None
...     try:
...         return delta.snapshot.at_abs(row).text
...     except IndexError:
...         return None
>>> term = Terminal(rows=2, cols=8, scrollback=3)
>>> term.feed(b"r0\r\nr1\r\nr2")
>>> bookmark = (term.sync_token(), term.committed.scrolled_total + 1)
>>> bookmarked_text(term, *bookmark)
'r2'
>>> term.resize(rows=2, cols=10)
>>> bookmarked_text(term, *bookmark) is None
True

Use the same check with sync.terminal after a native tmux refresh. An immutable snapshot retained with the bookmark still resolves rows in its original frame.

tmux -C / -CC byte-oriented control-mode parser and pane sync.

class libvt.contrib.tmux.control.TmuxAttachPhase
class libvt.contrib.tmux.control.TmuxAttachPhase

Bases: StrEnum

Lifecycle phase of sans-I/O two-phase TmuxPaneSync.

References

Examples

>>> from libvt.contrib.tmux.control import TmuxAttachPhase
>>> TmuxAttachPhase.LIVE.value
'live'
class libvt.contrib.tmux.control.TmuxCommandBlock
class libvt.contrib.tmux.control.TmuxCommandBlock

Bases: object

Completed %begin .. %end / %error control-mode reply block.

References

Examples

>>> from libvt.contrib.tmux.control import TmuxCommandBlock
>>> TmuxCommandBlock(timestamp=1, command_number=2, flags=1, lines=(b"ok",)).lines
(b'ok',)
class libvt.contrib.tmux.control.TmuxContinue
class libvt.contrib.tmux.control.TmuxContinue

Bases: object

%continue %<pane_id> flow-control notification.

References

Examples

>>> from libvt.contrib.tmux.control import TmuxContinue
>>> TmuxContinue(pane_id="%0").pane_id
'%0'
class libvt.contrib.tmux.control.TmuxControlParser
class libvt.contrib.tmux.control.TmuxControlParser

Bases: object

Streaming sans-I/O byte parser for tmux -C / -CC stdout.

Operates strictly on raw bytes so multi-byte UTF-8 sequences split across consecutive %output or %extended-output lines reassemble cleanly inside Terminal without U+FFFD loss, and strips optional tmux -CC DCS 1000p (ESC P 1000p .. ST) framing automatically.

Parameters:
  • max_line_bytes (int, default 4_194_304) – Maximum bytes buffered for a single unterminated wire line.

  • max_block_bytes (int, default 33_554_432) – Maximum cumulative bytes retained inside an open %begin block.

Raises:

ValueError – If max_line_bytes or max_block_bytes is less than 64.

References

Examples

>>> from libvt.contrib.tmux.control import TmuxControlParser
>>> p = TmuxControlParser()
>>> p.feed(b"%output %0 hi\\012\n")
[TmuxOutput(pane_id='%0', data=b'hi\n')]
class libvt.contrib.tmux.control.TmuxExit
class libvt.contrib.tmux.control.TmuxExit

Bases: object

%exit [reason] control client termination notification.

References

Examples

>>> from libvt.contrib.tmux.control import TmuxExit
>>> TmuxExit(reason="detached").reason
'detached'
class libvt.contrib.tmux.control.TmuxExtendedOutput
class libvt.contrib.tmux.control.TmuxExtendedOutput

Bases: object

Unescaped %extended-output %<pane_id> <age> : <data> notification.

References

Examples

>>> from libvt.contrib.tmux.control import TmuxExtendedOutput
>>> TmuxExtendedOutput(pane_id="%1", age_ms=25, data=b"x").age_ms
25
class libvt.contrib.tmux.control.TmuxLayoutChange
class libvt.contrib.tmux.control.TmuxLayoutChange

Bases: object

%layout-change @<win> <layout> <visible_layout> <flags> notification.

References

Examples

>>> from libvt.contrib.tmux.control import TmuxLayoutChange
>>> TmuxLayoutChange(window_id="@0", layout="a", visible_layout="a").window_id
'@0'
class libvt.contrib.tmux.control.TmuxNotification
class libvt.contrib.tmux.control.TmuxNotification

Bases: object

Fallback %<name> ... control-mode notification record.

References

Examples

>>> from libvt.contrib.tmux.control import TmuxNotification
>>> TmuxNotification(name="sessions-changed", args=b"").name
'sessions-changed'
class libvt.contrib.tmux.control.TmuxOutput
class libvt.contrib.tmux.control.TmuxOutput

Bases: object

Unescaped %output %<pane_id> <data> byte notification.

References

Examples

>>> from libvt.contrib.tmux.control import TmuxOutput
>>> TmuxOutput(pane_id="%0", data=b"hi").pane_id
'%0'
class libvt.contrib.tmux.control.TmuxPaneSync
class libvt.contrib.tmux.control.TmuxPaneSync

Bases: object

Sans-I/O two-phase tmux -C / -CC pane-attachment coordinator.

Checks membership in the control client’s session, then implements a pause barrier and a single atomic capture-pane + capture-pane -P -p -C + refresh-client -A '%N:continue' command batch so tmux executes capture and resume in one cmdq_next turn without dropping live PTY bytes.

On a shared control client, feed each event to every pane sync. Send each returned string in one write followed by a newline. The two lines identify the reply owner and submit its command batch.

A topology change can put a live pane into TmuxAttachPhase.CHECKING_SCOPE while the session query runs. If the pane leaves the client’s session, phase becomes TmuxAttachPhase.OUT_OF_SCOPE and terminal retains its last view. Call resync() from a control client whose session contains the pane.

Parameters:
  • pane_id (str) – Target tmux pane identifier (e.g. "%0").

  • tmux_version (tuple[int, int] | str, default "3.8a") – Target tmux version string or (major, minor) tuple.

  • scrollback (int, default 2_000) – Scrollback capacity for the reconstructed Terminal.

  • history_lines (int | None, default None) – Maximum scrollback lines requested on each capture/resync (None for full history, 0 for viewport-only, or N for -S -N).

  • profile (Profile | None, default None) – Optional inner-pane profile (defaults to tmux_profile(tmux_version)).

  • policy (Policy | None, default None) – Optional security/reply policy (defaults to TMUX_OBSERVER_POLICY).

  • limits (Limits | None, default None) – Optional terminal resource limits.

Raises:

ValueError – If pane_id is not "%<ascii_digits>" or tmux_version is invalid.

References

Examples

>>> from libvt.contrib.tmux.control import TmuxAttachPhase, TmuxPaneSync
>>> sync = TmuxPaneSync("%0")
>>> "list-panes -s" in sync.begin_attach()
True
>>> sync.phase == TmuxAttachPhase.PAUSING
True
class libvt.contrib.tmux.control.TmuxPause
class libvt.contrib.tmux.control.TmuxPause

Bases: object

%pause %<pane_id> flow-control notification.

References

Examples

>>> from libvt.contrib.tmux.control import TmuxPause
>>> TmuxPause(pane_id="%0").pane_id
'%0'
class libvt.contrib.tmux.control.TmuxSubscriptionChanged
class libvt.contrib.tmux.control.TmuxSubscriptionChanged

Bases: object

%subscription-changed <name> $<sess> <win> <idx> <pane> : <value>.

References

Examples

>>> from libvt.contrib.tmux.control import TmuxSubscriptionChanged
>>> TmuxSubscriptionChanged(
...     name="s", session_id="$0", window_id="-", window_index="-",
...     pane_id="-", value="1",
... ).value
'1'
libvt.contrib.tmux.control.unescape_tmux_control_output(data)
function[source]
function[source]
libvt.contrib.tmux.control.unescape_tmux_control_output(data)

Decode tmux -CC %output octal escapes into raw PTY bytes.

tmux control mode escapes bytes < 0x20 and backslash (0x5c -> \134) as three-digit octal sequences \ooo while passing printable ASCII and UTF-8 bytes verbatim (including split multi-byte UTF-8 sequences).

Parameters:

data (collections.abc.Buffer | str) – Escaped payload following %output %<pane_id>.

Returns:

Unescaped raw VT byte stream ready for feed().

Return type:

bytes

References

Examples

>>> from libvt.contrib.tmux.control import unescape_tmux_control_output
>>> unescape_tmux_control_output(r"\033[32mok\033[0m\015\012")
b'\x1b[32mok\x1b[0m\r\n'

Union of typed events emitted by feed().