From d1b5e8394d729d2af00ccc35514aabb86b18e413 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Wed, 2 Dec 2026 19:09:02 -0600 Subject: [PATCH 1/2] tmux(fix[capture]): Preserve metadata presence why: Missing paste and tab metadata differ from known disabled values. what: - Represent unknown paste mode and cleared rulers separately. - Mark stored-tab expansions as uncertain. - Document how to handle missing metadata after capture. Fixes #79 Fixes #103 --- MIGRATION | 20 ++++++++++++++ docs/topics/sync-and-asyncio.md | 12 +++++++++ src/libvt/contrib/tmux/capture.py | 39 ++++++++++++++++++--------- tests/test_tmux.py | 45 ++++++++++++++++++++++++++++++- 4 files changed, 102 insertions(+), 14 deletions(-) diff --git a/MIGRATION b/MIGRATION index 74e6af7f..3f368a53 100644 --- a/MIGRATION +++ b/MIGRATION @@ -86,6 +86,26 @@ Use an explicit `max_bytes=64` to retain the former minimum. An eight-byte limit now retains eight bytes. Pass zero to count bytes without storing them. Negative limits raise `ValueError` before a PTY child starts. +### Missing tmux paste and tab metadata + +Read `result.metadata.bracketed_paste_flag` before sending a paste to +a late-attached pane. To distinguish unknown from disabled, branch on +`None` first. The input encoder still emits plain text for both cases. + +```python +# Before +paste_disabled = not result.metadata.bracketed_paste_flag + +# After +flag = result.metadata.bracketed_paste_flag +paste_disabled = flag is False +paste_unknown = flag is None +``` + +A known empty ruler is now `()`. A missing ruler remains `None`. +Inspect `result.completeness.unexposed_fields` for +`historical_tabstops` before relying on reconstructed cell coordinates. + ## libvt 0.0.1a2 (2026-11-28) ### Curated PEP 562 `libvt` root exports (#43) diff --git a/docs/topics/sync-and-asyncio.md b/docs/topics/sync-and-asyncio.md index c660396d..aca89c56 100644 --- a/docs/topics/sync-and-asyncio.md +++ b/docs/topics/sync-and-asyncio.md @@ -221,6 +221,18 @@ For this input `result.completeness.unexposed_fields` includes Use an attached-client screen to assert positions in a pane with uncertain widths. +Before sending a paste to a late-attached pane, check +{attr}`~libvt.contrib.tmux.capture.TmuxPaneMetadata.bracketed_paste_flag`. +The value is `None` when the server omits both the direct flag and the +private modes list. For example, tmux 3.6b omits the paste flag and +3.7 supplies it. + +A capture restores the current tab stops for future input. If a row +contains a stored tab, tmux omits the stops in effect when that row was +written. Use an attached-client screen to assert the column following +such a tab. The capture lists `historical_tabstops` in +`completeness.unexposed_fields`. + ## Concurrency and thread ownership Every method call and attribute read on a {class}`~libvt.terminal.Terminal` diff --git a/src/libvt/contrib/tmux/capture.py b/src/libvt/contrib/tmux/capture.py index 21896328..c8d61b13 100644 --- a/src/libvt/contrib/tmux/capture.py +++ b/src/libvt/contrib/tmux/capture.py @@ -217,8 +217,9 @@ class TmuxPaneMetadata: ``DECAWM`` autowrap mode (``#{wrap_flag}``). keypad_cursor_flag : bool ``DECCKM`` application cursor keys (``#{keypad_cursor_flag}``). - bracketed_paste_flag : bool - ``?2004`` bracketed paste mode (``#{bracket_paste_flag}``). + bracketed_paste_flag : bool | None + Whether mode 2004 is enabled, from ``#{bracket_paste_flag}`` or + the private modes list. ``None`` means the server supplied neither. mouse_standard_flag : bool ``?1000`` VT200 mouse reporting (``#{mouse_standard_flag}``). mouse_button_flag : bool @@ -232,8 +233,8 @@ class TmuxPaneMetadata: private_modes : frozenset[int] DEC private mode numbers parsed from ``#{pane_private_modes}``. pane_tabs : tuple[int, ...] | None - Sorted zero-based tabstop columns from ``#{pane_tabs}``, or ``None`` to - use standard 8-column tabstops. + Current zero-based tabstop columns. An empty tuple means all stops + were cleared. With no field (``None``), capture uses 8-column stops. history_size : int Current scrollback line count (``#{history_size}``). history_limit : int @@ -273,7 +274,7 @@ class TmuxPaneMetadata: origin_flag: bool = False wrap_flag: bool = True keypad_cursor_flag: bool = False - bracketed_paste_flag: bool = False + bracketed_paste_flag: bool | None = None mouse_standard_flag: bool = False mouse_button_flag: bool = False mouse_any_flag: bool = False @@ -292,6 +293,11 @@ class TmuxPaneMetadata: cwd: str = "" title: str = "" + def __post_init__(self) -> None: + """Use the private modes list when the direct paste flag is missing.""" + if self.bracketed_paste_flag is None and self.private_modes: + object.__setattr__(self, "bracketed_paste_flag", 2004 in self.private_modes) + @property def pending_wrap(self) -> bool: """Return ``True`` when ``cursor_x >= cols`` encodes ``pending_wrap``. @@ -344,7 +350,8 @@ def from_format_line(cls, line: str) -> TmuxPaneMetadata: cursor_blink_raw = "" keypad_raw = "" key_mode_raw = "" - if cleaned.count("\t") >= 30: + separators = cleaned.count("\t") + if separators >= 30: cand = cleaned.split("\t", 30) if cand[25].strip() in ("", "default", "block", "underline", "bar"): parts = cand @@ -398,7 +405,7 @@ def from_format_line(cls, line: str) -> TmuxPaneMetadata: tabs_raw = parts[20].strip() tabs: tuple[int, ...] | None = None - if tabs_raw: + if separators >= 20: parsed_tabs: list[int] = [] for tok in tabs_raw.split(","): t_val = _parse_ascii_uint(tok.strip(), max_val=65_535) @@ -425,7 +432,7 @@ def from_format_line(cls, line: str) -> TmuxPaneMetadata: origin_flag=_opt_bool(parts[11]), wrap_flag=_opt_bool(parts[12], default=True), keypad_cursor_flag=_opt_bool(parts[13]), - bracketed_paste_flag=_opt_bool(parts[14]) or (2004 in priv_modes), + bracketed_paste_flag=_opt_bool(parts[14]) if parts[14].strip() else None, mouse_standard_flag=_opt_bool(parts[15]) or (1000 in priv_modes), mouse_button_flag=_opt_bool(parts[16]) or (1002 in priv_modes), mouse_any_flag=_opt_bool(parts[17]) or (1003 in priv_modes), @@ -459,7 +466,9 @@ class CaptureCompleteness: exact_cells : bool Whether the capture can certify cell positions and styles. A widening emoji presentation selector from a server older than 3.5 leaves its - native columns unverified. + native columns unverified. A stored tab also makes this ``False`` + because tmux exposes the current tab stops but omits the tab width + used when the pane wrote that row. exact_wraps : bool ``True`` when soft-wrap flags came from ``-F`` or an exact ``-J`` companion capture. @@ -1514,7 +1523,7 @@ def decode_tmux_capture( term.modes.set_mode(Mode.DECTCEM, enabled=meta.cursor_visible) term.modes.set_mode(Mode.DECCKM, enabled=meta.keypad_cursor_flag) term.modes.set_mode(Mode.APPLICATION_KEYPAD, enabled=meta.keypad_flag) - term.modes.set_mode(Mode.BRACKETED_PASTE, enabled=meta.bracketed_paste_flag) + term.modes.set_mode(Mode.BRACKETED_PASTE, enabled=meta.bracketed_paste_flag is True) term.modes.set_mode(Mode.FOCUS_EVENT, enabled=meta.focus_events_flag) mouse_flags = { code @@ -1534,6 +1543,7 @@ def decode_tmux_capture( 47, 1047, 1049, + 2004, 2026, ): raw_m = DEC_PRIVATE_MASK | priv_code @@ -1572,14 +1582,17 @@ def decode_tmux_capture( else: term._committed_generation += 1 - uncalibrated_tabs = saw_tabs and meta.pane_tabs is None unexposed = set(_BASE_UNEXPOSED_FIELDS) if meta.cursor_shape is not None: unexposed.discard("cursor_shape") - if uncalibrated_tabs: + if meta.pane_tabs is None: unexposed.add("pane_tabs") if unverified_width: unexposed.add("grapheme_widths") + if saw_tabs: + unexposed.add("historical_tabstops") + if meta.bracketed_paste_flag is None: + unexposed.add("bracketed_paste_flag") if not exact_wraps: unexposed.add("soft_wraps") if meta.alternate_on and saved_primary_capture is None: @@ -1598,7 +1611,7 @@ def decode_tmux_capture( == (meta.cols, meta.rows, meta.alternate_on, meta.history_size) ) completeness = CaptureCompleteness( - exact_cells=not (uncalibrated_tabs or unverified_width), + exact_cells=not (saw_tabs or unverified_width), exact_wraps=exact_wraps, exact_generation_bracket=exact_gen, unexposed_fields=frozenset(unexposed), diff --git a/tests/test_tmux.py b/tests/test_tmux.py index 75d6d19d..8e186aaa 100644 --- a/tests/test_tmux.py +++ b/tests/test_tmux.py @@ -22,6 +22,7 @@ unescape_tmux_control_output, ) from libvt.exc import ProtocolError +from libvt.input import PasteEvent from libvt.modes import Mode from libvt.style import AnsiColor, IndexedColor from tests import new_tmux_socket_name @@ -156,7 +157,9 @@ def test_capture_decoder_acs_tabs_joined_wraps_and_prompt_zones() -> None: ["a\tb"], metadata=TmuxPaneMetadata(cols=8, rows=1, pane_tabs=(4,)), ) - assert calibrated_res.completeness.exact_cells is True + assert calibrated_res.snapshot.lines == ("a b",) + assert calibrated_res.completeness.exact_cells is False + assert "historical_tabstops" in calibrated_res.completeness.unexposed_fields assert "pane_tabs" not in calibrated_res.completeness.unexposed_fields pz_snap = decode_tmux_capture( @@ -1322,3 +1325,43 @@ def test_tmux_control_buffers_accept_strided_and_empty_multidim_memoryviews() -> empty_2d.release() empty_2d_u16.release() + + +def test_capture_cleared_tabstops_and_missing_field() -> None: + """Resume from a cleared ruler and from metadata that omits the ruler.""" + short = "18\t2\t0\t0" + for line, stops, col in ((short + "\t" * 17, (), 17), (short, None, 8)): + cap = decode_tmux_capture(["", ""], metadata=line) + assert cap.metadata.pane_tabs == stops + assert ("pane_tabs" in cap.completeness.unexposed_fields) == (stops is None) + assert cap.completeness.exact_cells + cap.terminal.feed(b"\tX") + assert cap.terminal[0, col].text == "X" + assert cap.snapshot[0, col].text == " " + + +@pytest.mark.parametrize( + ("flag", "modes", "expected"), + [ + ("", "", None), + ("0", "2004", False), + ("1", "", True), + ("", "1004,2004", True), + ("", "1004", False), + ("", "1", None), + ], +) +def test_capture_bracketed_paste_state( + flag: str, modes: str, expected: bool | None +) -> None: + """Prefer an explicit flag and report when the server exposed no mode.""" + fields = [""] * 21 + fields[:4] = ["18", "2", "0", "0"] + fields[14], fields[19] = flag, modes + cap = decode_tmux_capture([""], metadata="\t".join(fields)) + assert cap.metadata.bracketed_paste_flag is expected + assert ("bracketed_paste_flag" in cap.completeness.unexposed_fields) == ( + expected is None + ) + encoded = cap.terminal.encode(PasteEvent("edit")) + assert encoded == (b"\x1b[200~edit\x1b[201~" if expected else b"edit") From f77a4ee90ea6e2552cf3df8336efba0acc0b4c1b Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Wed, 2 Dec 2026 19:38:23 -0600 Subject: [PATCH 2/2] CHANGES(docs): Explain missing pane metadata why: An omitted paste flag does not establish that paste mode is off. Stored tabs can outlive their original ruler. what: - Document unknown paste mode, cleared stops and uncertain stored widths. --- CHANGES | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/CHANGES b/CHANGES index a2d05dfc..3116853f 100644 --- a/CHANGES +++ b/CHANGES @@ -261,6 +261,18 @@ rec = FlightRecorder(max_bytes=-1) # retained 64 bytes rec = FlightRecorder(max_bytes=64) ``` +#### Read missing tmux pane metadata (#170) + +{attr}`~libvt.contrib.tmux.capture.TmuxPaneMetadata.bracketed_paste_flag` +is `None` when the server supplies neither a direct paste flag nor a +nonempty list of parsed private modes. +Use `flag is False` for a confirmed disabled mode and `flag is None` +for an unknown mode. + +An empty tab ruler now restores a pane with every tab stop cleared. +When a captured row contains a stored tab, `exact_cells` is false. +The server exposes today's stops and omits the width of the stored tab. + ## libvt 0.0.1a5 (2026-11-30) `libvt` 0.0.1a5 adds rectangular grid blitting, display-column text writes, and