(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.
