Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ what was asked for.
| `src/libvt/parser.py`, `modes.py`, `style.py`, `config.py`, `profiles.py` | VT500 byte parser, CSI subparameters, modes, SGR styles, `Profile` / `Policy` / `Limits`, and host presets |
| `src/libvt/_ucd.py`, `_grid.py`, `screen.py`, `terminal.py` | Unicode 17 width/clustering tables, packed viewport & RLE scrollback rows, `Screen` / `ScreenSet`, `Terminal`, `Snapshot`, and `Formatter` |
| `src/libvt/status.py`, `events.py`, `input.py`, `codec.py` | Rex `OSC 7501` program status store, open `Event` hierarchy, mode-aware input encoders, and cold-path VT sequence explainer |
| `src/libvt/graphics.py`, `contrib/tmux/` | Zero-dependency `SIXEL` / Kitty `APC G` placement tracker and decoupled `tmux` `profile` / `capture` / `control` modules |
| `src/libvt/graphics.py`, `contrib/tmux/` | Zero-dependency `SIXEL` / Kitty `APC G` placement tracker and decoupled `tmux` `profile` / `capture` / `window` / `control` modules |
| `src/libvt/aio.py`, `__main__.py`, `exc.py` | `AsyncTerminalStream`, POSIX `open_pty()`, `python -m libvt` CLI (`explain`, `render`, `strip`), and exceptions |
| `src/libvt/testing.py` | Zero-dependency `assert_screen`, `assert_screen_box`, `FlightRecorder` diagnostics, and boxed VT + `SGR` `vt_screen` / `serialize_vt_snapshot` / `format_vt_diff_lines` utilities |
| `src/libvt/pytest_plugin.py` | Import-light `pytest11` plugin (`libvt[pytest]`): `vt_terminal`, `vt_factory`, `vt_recorder`, `vt_parser`, and `vt_pty` |
Expand Down
16 changes: 16 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,22 @@
_Notes on the upcoming release will go here._
<!-- END PLACEHOLDER - ADD NEW CHANGELOG ENTRIES BELOW THIS LINE -->

### What's new

#### Compose a screen from snapshots and text (#137)

Use {meth}`~libvt.screen.Screen.blit` to copy a rectangular region from a
screen or snapshot, retaining cell styles, hyperlinks, and written spaces.
Place text by display column with {meth}`~libvt.screen.Screen.put_text`,
then freeze the result with {meth}`~libvt.screen.Screen.snapshot`.

#### Capture a split `tmux` window as one snapshot (#137)

Capture a detached window from `tmux` command output, or combine existing
pane snapshots with {func}`~libvt.contrib.tmux.window.composite_tmux_window`.
The snapshot includes split borders, pane border labels, and the active pane's
cursor. A zoomed pane fills the window.

## libvt 0.0.1a4 (2026-11-29)

`libvt` 0.0.1a4 adds tool-agnostic boxed VT and `SGR` snapshot serialization,
Expand Down
15 changes: 15 additions & 0 deletions docs/api/contrib_tmux.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@
Accepted input representation for ``tmux capture-pane`` lines or raw text.
```

## `libvt.contrib.tmux.window`

```{eval-rst}
.. automodule:: libvt.contrib.tmux.window
:members:
:no-private-members:
:exclude-members: TmuxPaneSource

.. py:type:: TmuxPaneSource
:module: libvt.contrib.tmux.window
:canonical: Snapshot | Screen | Terminal | TmuxCaptureResult | object

Accepted per-pane state source for :func:`~libvt.contrib.tmux.window.composite_tmux_window`.
```

## `libvt.contrib.tmux.control`

```{eval-rst}
Expand Down
142 changes: 139 additions & 3 deletions src/libvt/_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,15 @@ class FrozenRow:
cells: tuple[str, ...] | None = None
wrapped: bool = False

@property
def text_used(self) -> int:
"""Return written-text column high-water mark."""
return len(self.cells) if self.cells is not None else len(self.text)

@property
def used(self) -> int:
"""Return count of written columns in this sealed row."""
text_cols = len(self.cells) if self.cells is not None else len(self.text)
text_cols = self.text_used
run_end = self.runs[-1][0] if self.runs else 0
ext_end = (self.exts[-1][0] + 1) if self.exts else 0
return max(text_cols, run_end, ext_end)
Expand Down Expand Up @@ -497,10 +502,12 @@ def _heal_wide_boundary(self, col: int) -> None:
if col < 0 or col >= cols:
return
role = raw_width_bits(self._attrs[col])
healed_pair_start = -1
healed_pair_end = -1
if role == int(WidthRole.WIDE):
self._chars[col] = " "
self._attrs[col] &= _WIDTH_CLEAR_MASK
healed_pair_start = col
healed_pair_end = col + 1
if col + 1 < cols:
self._chars[col + 1] = " "
Expand All @@ -509,14 +516,15 @@ def _heal_wide_boundary(self, col: int) -> None:
elif role == int(WidthRole.SPACER_TAIL):
self._chars[col] = " "
self._attrs[col] &= _WIDTH_CLEAR_MASK
healed_pair_start = col
healed_pair_end = col + 1
if col - 1 >= 0:
self._chars[col - 1] = " "
self._attrs[col - 1] &= _WIDTH_CLEAR_MASK
healed_pair_start = col - 1
if healed_pair_end != -1:
if self._text_used == healed_pair_end:
while self._text_used > 0 and self._chars[self._text_used - 1] == " ":
self._text_used -= 1
self._text_used = healed_pair_start
while (
self._used > self._text_used
and self._chars[self._used - 1] == " "
Expand Down Expand Up @@ -627,6 +635,134 @@ def copy_region_from(self, src_row: _Row, left: int, right: int) -> None:
self._used = max(self._used, self._text_used)
self.stamp()

def blit_from(
self,
src_row: RowLike,
*,
dst_col: int,
src_col: int,
width: int,
) -> None:
"""Copy *width* cells from ``src_row[src_col..]`` into ``self[dst_col..]``."""
if width <= 0:
return
if src_row is self:
src_row = self.clone()
dst_end = dst_col + width
if self._has_wide and self._attrs is not None:
if raw_width_bits(self._attrs[dst_col]) == int(WidthRole.SPACER_TAIL):
self._heal_wide_boundary(dst_col)
if raw_width_bits(self._attrs[dst_end - 1]) == int(WidthRole.WIDE):
self._heal_wide_boundary(dst_end - 1)

old_text_used = self._text_used
old_used = self._used
src_end = src_col + width

if (
isinstance(src_row, _Row)
and not src_row._has_wide
and src_end <= len(src_row._chars)
):
self._chars[dst_col:dst_end] = src_row._chars[src_col:src_end]
if src_row._attrs is None:
s_u = src_row._uniform_attr
if self._attrs is None:
if s_u != self._uniform_attr:
attrs = self._ensure_attrs()
attrs[dst_col:dst_end] = array.array("Q", [s_u]) * width
else:
self._attrs[dst_col:dst_end] = array.array("Q", [s_u]) * width
else:
attrs = self._ensure_attrs()
attrs[dst_col:dst_end] = src_row._attrs[src_col:src_end]

if self._ext:
for dc in range(dst_col, dst_end):
self._ext.pop(dc, None)
if src_row._ext:
if self._ext is None:
self._ext = {}
shift = dst_col - src_col
for sc, ce in src_row._ext.items():
if src_col <= sc < src_end:
self._ext[sc + shift] = ce
if self._ext is not None and not self._ext:
self._ext = None
else:
src_cols = src_row.cols
for j in range(width):
sc = src_col + j
dc = dst_col + j
if sc < 0 or sc >= src_cols:
self._set_raw(dc, " ", 0, WidthRole.NARROW, None)
continue
ch = src_row.char_at(sc)
word = src_row.attr_at(sc)
ext = src_row.ext_at(sc)
rb = raw_width_bits(word)
if rb == int(WidthRole.SPACER_HEAD):
ch = " "
rb = int(WidthRole.NARROW)
elif rb == int(WidthRole.SPACER_TAIL):
if (
j == 0
or sc - 1 < 0
or raw_width_bits(src_row.attr_at(sc - 1))
!= int(WidthRole.WIDE)
):
ch = " "
rb = int(WidthRole.NARROW)
elif rb == int(WidthRole.WIDE) and (
j == width - 1
or sc + 1 >= src_cols
or raw_width_bits(src_row.attr_at(sc + 1))
!= int(WidthRole.SPACER_TAIL)
):
ch = " "
rb = int(WidthRole.NARROW)
base = word & _PEN_BASE_MASK
self._set_raw(dc, ch, base, WidthRole(rb), ext)

src_t = min(max(src_row.text_used, src_col), src_end)
while src_t > src_col and raw_width_bits(src_row.attr_at(src_t - 1)) == int(
WidthRole.SPACER_HEAD
):
src_t -= 1
if (
src_t == src_end
and src_t > src_col
and raw_width_bits(src_row.attr_at(src_t - 1)) == int(WidthRole.WIDE)
):
src_t -= 1
if src_t == src_col + 1 and raw_width_bits(src_row.attr_at(src_col)) == int(
WidthRole.SPACER_TAIL
):
src_t = src_col

if old_text_used > dst_end:
new_t = old_text_used
elif src_t > src_col:
new_t = dst_col + (src_t - src_col)
else:
new_t = min(old_text_used, dst_col)
self._text_used = new_t

floor_u = max(min(old_used, dst_col), new_t)
if old_used > dst_end:
self._used = max(old_used, new_t)
else:
new_u = max(dst_end, floor_u)
while (
new_u > floor_u
and self._chars[new_u - 1] == " "
and self.attr_at(new_u - 1) == 0
and self.ext_at(new_u - 1) is None
):
new_u -= 1
self._used = new_u
self.stamp()

def write_narrow(
self,
col: int,
Expand Down
61 changes: 60 additions & 1 deletion src/libvt/contrib/tmux/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Decoupled ``tmux`` profile, ``capture-pane``, and control-mode integration."""
"""Decoupled ``tmux`` profile, ``capture-pane``, window, and control-mode adapters."""

from __future__ import annotations

Expand Down Expand Up @@ -41,6 +41,20 @@
parse_tmux_version,
tmux_profile,
)
from libvt.contrib.tmux.window import (
TMUX_WINDOW_METADATA_FORMAT,
TMUX_WINDOW_PANE_FORMAT,
TmuxPaneSource,
TmuxWindowCaptureCommand,
TmuxWindowCaptureResult,
TmuxWindowLayout,
TmuxWindowListCommand,
TmuxWindowPane,
build_tmux_window_capture_command,
build_tmux_window_list_command,
composite_tmux_window,
parse_tmux_window_layout,
)

_LAZY_EXPORTS: dict[str, tuple[str, str]] = {
"CaptureCompleteness": ("libvt.contrib.tmux.capture", "CaptureCompleteness"),
Expand All @@ -49,6 +63,14 @@
"TMUX_METADATA_FORMAT": ("libvt.contrib.tmux.capture", "TMUX_METADATA_FORMAT"),
"TMUX_OBSERVER_POLICY": ("libvt.contrib.tmux.profile", "TMUX_OBSERVER_POLICY"),
"TMUX_PROFILE": ("libvt.contrib.tmux.profile", "TMUX_PROFILE"),
"TMUX_WINDOW_METADATA_FORMAT": (
"libvt.contrib.tmux.window",
"TMUX_WINDOW_METADATA_FORMAT",
),
"TMUX_WINDOW_PANE_FORMAT": (
"libvt.contrib.tmux.window",
"TMUX_WINDOW_PANE_FORMAT",
),
"TmuxAttachPhase": ("libvt.contrib.tmux.control", "TmuxAttachPhase"),
"TmuxCaptureCommand": ("libvt.contrib.tmux.capture", "TmuxCaptureCommand"),
"TmuxCaptureResult": ("libvt.contrib.tmux.capture", "TmuxCaptureResult"),
Expand All @@ -63,19 +85,44 @@
"TmuxNotification": ("libvt.contrib.tmux.control", "TmuxNotification"),
"TmuxOutput": ("libvt.contrib.tmux.control", "TmuxOutput"),
"TmuxPaneMetadata": ("libvt.contrib.tmux.capture", "TmuxPaneMetadata"),
"TmuxPaneSource": ("libvt.contrib.tmux.window", "TmuxPaneSource"),
"TmuxPaneSync": ("libvt.contrib.tmux.control", "TmuxPaneSync"),
"TmuxPause": ("libvt.contrib.tmux.control", "TmuxPause"),
"TmuxSubscriptionChanged": (
"libvt.contrib.tmux.control",
"TmuxSubscriptionChanged",
),
"TmuxWindowCaptureCommand": (
"libvt.contrib.tmux.window",
"TmuxWindowCaptureCommand",
),
"TmuxWindowCaptureResult": (
"libvt.contrib.tmux.window",
"TmuxWindowCaptureResult",
),
"TmuxWindowLayout": ("libvt.contrib.tmux.window", "TmuxWindowLayout"),
"TmuxWindowListCommand": ("libvt.contrib.tmux.window", "TmuxWindowListCommand"),
"TmuxWindowPane": ("libvt.contrib.tmux.window", "TmuxWindowPane"),
"build_tmux_capture_command": (
"libvt.contrib.tmux.capture",
"build_tmux_capture_command",
),
"build_tmux_window_capture_command": (
"libvt.contrib.tmux.window",
"build_tmux_window_capture_command",
),
"build_tmux_window_list_command": (
"libvt.contrib.tmux.window",
"build_tmux_window_list_command",
),
"composite_tmux_window": ("libvt.contrib.tmux.window", "composite_tmux_window"),
"decode_tmux_capture": ("libvt.contrib.tmux.capture", "decode_tmux_capture"),
"parse_flagged_line": ("libvt.contrib.tmux.capture", "parse_flagged_line"),
"parse_tmux_version": ("libvt.contrib.tmux.profile", "parse_tmux_version"),
"parse_tmux_window_layout": (
"libvt.contrib.tmux.window",
"parse_tmux_window_layout",
),
"tmux_profile": ("libvt.contrib.tmux.profile", "tmux_profile"),
"unescape_tmux_control_output": (
"libvt.contrib.tmux.control",
Expand All @@ -88,6 +135,8 @@
"TMUX_METADATA_FORMAT",
"TMUX_OBSERVER_POLICY",
"TMUX_PROFILE",
"TMUX_WINDOW_METADATA_FORMAT",
"TMUX_WINDOW_PANE_FORMAT",
"CaptureCompleteness",
"CaptureInput",
"TmuxAttachPhase",
Expand All @@ -104,13 +153,23 @@
"TmuxNotification",
"TmuxOutput",
"TmuxPaneMetadata",
"TmuxPaneSource",
"TmuxPaneSync",
"TmuxPause",
"TmuxSubscriptionChanged",
"TmuxWindowCaptureCommand",
"TmuxWindowCaptureResult",
"TmuxWindowLayout",
"TmuxWindowListCommand",
"TmuxWindowPane",
"build_tmux_capture_command",
"build_tmux_window_capture_command",
"build_tmux_window_list_command",
"composite_tmux_window",
"decode_tmux_capture",
"parse_flagged_line",
"parse_tmux_version",
"parse_tmux_window_layout",
"tmux_profile",
"unescape_tmux_control_output",
]
Expand Down
Loading
Loading