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
7 changes: 7 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ Deleting an attribute on {class}`~libvt.snapshot.Snapshot` now raises

### Documentation

#### Observe a pane and its attached tmux client (#141)

The {ref}`tmux screen guide <topics-tmux-screens>` compares pane captures,
composited layouts, control streams, and attached-client copy-mode views,
and demonstrates pane-local click translation and explicit focus checks after
resize.

#### Drive a Python child from the README (#131)

The README quickstart uses {func}`~libvt.aio.open_pty` and
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,10 @@ styles:
Escape, and Ctrl-C outcomes).

Read `tmux` captures as screen cells and decode control-mode output with
[`libvt.contrib.tmux`](https://libvt.git-pull.com/api/contrib/tmux/). Full
guides and API reference are at [libvt.git-pull.com](https://libvt.git-pull.com/).
[`libvt.contrib.tmux`][contrib_tmux]. See
[Which screen am I testing?][tmux_screens] for attached-client and copy-mode
checks. Full guides and API reference are at
[libvt.git-pull.com](https://libvt.git-pull.com/).

## License

Expand All @@ -268,3 +270,5 @@ MIT License. See [LICENSE](LICENSE).
[strip]: https://libvt.git-pull.com/api/terminal/#libvt.terminal.strip
[explain]: https://libvt.git-pull.com/api/codec/#libvt.codec.explain
[fzf_cookbook]: https://libvt.git-pull.com/topics/testing/#topics-testing-fzf
[contrib_tmux]: https://libvt.git-pull.com/api/contrib/tmux/
[tmux_screens]: https://libvt.git-pull.com/topics/tmux-screens/
13 changes: 11 additions & 2 deletions docs/topics/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
# Topics

Guides covering `libvt`'s parser and screen design, immutable snapshots,
terminal testing systems, synchronized redraws and `asyncio` adapters, terminal
protocols and input encoding, and migrating from `pyte`.
terminal testing systems, tmux screen and attached-client observation,
synchronized redraws and `asyncio` adapters, terminal protocols and input
encoding, and migrating from `pyte`.

::::{grid} 1 1 2 2
:gutter: 2 2 3 3
Expand All @@ -27,6 +28,13 @@ Immutable {class}`~libvt.snapshot.Snapshot` captures, {attr}`~libvt.terminal.Ter
Three decoupled testing systems built on snapshots: {mod}`libvt.testing`, {mod}`libvt.pytest_plugin` (`libvt[pytest]`), and {mod}`libvt.contrib.syrupy` (`libvt[syrupy]`).
:::

:::{grid-item-card} Which screen am I testing?
:link: tmux-screens
:link-type: doc
Compare a tmux pane with an attached client in copy mode and route a mouse
click after a resize.
:::

:::{grid-item-card} Synchronized Output & `asyncio`
:link: sync-and-asyncio
:link-type: doc
Expand Down Expand Up @@ -55,6 +63,7 @@ Differences in terminal construction, coordinates, alternate screens, and query
architecture
snapshots
testing
tmux-screens
sync-and-asyncio
protocols-and-input
migrating-from-pyte
Expand Down
135 changes: 135 additions & 0 deletions docs/topics/tmux-screens.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
(topics-tmux-screens)=

# Which screen am I testing?

Choose the screen whose behavior your test checks. Each tmux observation layer
shows a different buffer:

| Layer | API | What it observes |
| --- | --- | --- |
| Single pane grid | {func}`~libvt.contrib.tmux.capture.build_tmux_capture_command` | Application primary grid and scrollback ({meth}`~libvt.snapshot.Snapshot.viewport` pages history statelessly; `-a` reads saved primary behind `?1049`). |
| Multi-pane layout | {func}`~libvt.contrib.tmux.window.composite_tmux_window` | Composited application grids across panes without tmux copy-mode overlays or status chrome. |
| Control stream | {mod}`libvt.contrib.tmux.control` | Incremental pane output and layout notifications streamed from `tmux -CC`. |
| Attached client | {func}`~libvt.aio.open_pty` | Full rendered terminal window including copy mode, selection highlights, borders, and the status line. |

Use an attached client when checking what a person sees or clicks. On an
attached client, {meth}`~libvt.aio.PtySession.wait_for` and
{attr}`~libvt.terminal.Terminal.committed` inspect completed frames across
`?2026` synchronized updates, whereas {meth}`~libvt.terminal.Terminal.snapshot`
reads the immediate working buffer (see
{ref}`testing-working-and-committed-frames`).

## Compare a pane with its copy-mode viewport

The example opens an archive on the left and a mouse-aware picker on the right.
The archive prints 32 ledger entries and `ARCHIVE LIVE`. After tmux enters
copy mode and moves to the start of history, the two observations differ:

| Observation | Text in the archive pane |
| --- | --- |
| Application grid from {func}`~libvt.contrib.tmux.capture.build_tmux_capture_command` | `ARCHIVE LIVE` |
| Rendering from a tmux client attached through {func}`~libvt.aio.open_pty` | `Ledger entry 00` |

The example runs the capture bundle against the same server and crops the
attached {class}`~libvt.snapshot.Snapshot` by cell coordinates. The one-row
offset accounts for the status line above the window. The example selects at
the start of the ledger and checks for the configured selection colors in the
attached frame.

```{literalinclude} ../../examples/tmux_screens.py
:language: python
:start-at: Compare the application grid
:end-before: Read the mode grid
:dedent: 16
```

When `tmux list-commands capture-pane` advertises `-M`, use it to read the
frozen copy-mode grid even after the live application writes new output behind
it. Read `#{scroll_position}` while the pane is in copy mode and use an
inclusive range from `-scroll` through `pane_height - 1 - scroll`. Without `-M`
and that range, `capture-pane` can return rows from the live grid instead. An
attached client also observes presentation details such as selection colors and
the copy-mode position indicator.

```{literalinclude} ../../examples/tmux_screens.py
:language: python
:start-at: Read the mode grid
:end-before: Leave copy mode
:dedent: 16
```

On builds that omit `-M`, use the attached snapshot for visible copy-mode rows.

## Translate a click after resize

Cancel copy mode, call {meth}`~libvt.aio.PtySession.resize` on the attached
PTY, and wait for tmux to report the new client and window dimensions. Query
`#{pane_top}` and `#{pane_left}` again. For this single-client, unpanned window
with status at the top, add the one-row status line and the pane origin to the
target's local row and column. Here `PIN` holds the target's `(row, column)`
inside the picker.

```{literalinclude} ../../examples/tmux_screens.py
:language: python
:start-at: outer_row = 1 + top + PIN[0]
:end-at: outer_col = left + PIN[1]
:dedent: 20
```

Use zero-based rows and columns for {class}`~libvt.input.MouseEvent`. The
encoder converts them to one-based SGR mouse wire coordinates (`+1` on both
axes):

```python
>>> from libvt import Terminal
>>> from libvt.input import MouseAction, MouseButton, MouseEvent
>>> term = Terminal(rows=14, cols=68)
>>> term.feed(b"\x1b[?1000;1006h")
>>> status_rows, pane_top, pane_left = 1, 0, 35
>>> pin_row, pin_col = 2, 3
>>> outer_row = status_rows + pane_top + pin_row
>>> outer_col = pane_left + pin_col
>>> term.encode(MouseEvent(MouseButton.LEFT, MouseAction.PRESS, outer_row, outer_col))
b'\x1b[<0;39;4M'
```

Check the target cell against the attached snapshot before clicking. Borders
and status cells belong to tmux, and any change to window layout, status
position, or viewport offset changes the mapping.

A delivered mouse click does not guarantee that keyboard focus moved to the
clicked pane. Default tmux `MouseDown1Pane` bindings run `select-pane` before
forwarding the click, so the picker gains focus automatically. Custom tmux
bindings (for example `bind -T root MouseDown1Pane send-keys -M`) or other
multiplexers can deliver mouse events to an unfocused pane while leaving
keyboard focus elsewhere. Check the active pane after clicking and issue an
explicit focus command (such as `select-pane`) before sending a dependent key
with {meth}`~libvt.aio.PtySession.send_key`.

The example sends both button edges with
{meth}`~libvt.aio.PtySession.send_event`, waits for the picker to report the
pane-local coordinates, verifies focus (issuing `select-pane` under
`--no-focus-on-click`), and then sends the key:

```{literalinclude} ../../examples/tmux_screens.py
:language: python
:start-at: MouseEvent accepts zero-based
:end-before: The child has received
:dedent: 16
```

## Run the example

From the repository root with `libvt` installed, run the
{download}`example <../../examples/tmux_screens.py>` on POSIX with tmux 3.1 or
newer that advertises `capture-pane -N`. The example starts a private server
and shuts it down on exit.

```console
$ python examples/tmux_screens.py
```

Use `--tmux` to select another binary. Pass `--stale-origin` to see the picker
receive column 11 in place of column 3 after the split's origin moves, or pass
`--no-focus-on-click` to see a delivered click leave focus on the archive pane
until `select-pane` transfers it explicitly.
Loading
Loading