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 |
Application primary grid and scrollback ( |
|
Multi-pane layout |
Composited application grids across panes without tmux copy-mode overlays or status chrome. |
|
Control stream |
Incremental pane output and layout notifications streamed from |
|
Attached client |
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, wait_for() and
committed inspect completed frames across
?2026 synchronized updates, whereas snapshot()
reads the immediate working buffer (see
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 |
|
Rendering from a tmux client attached through |
|
The example runs the capture bundle against the same server and crops the
attached 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.
# Compare the application grid with the rendered client.
await tm("copy-mode", "-t", archive)
await tm("send-keys", "-X", "-t", archive, "history-top")
await tm("send-keys", "-X", "-t", archive, "begin-selection")
await tm("send-keys", "-X", "-t", archive, "cursor-right")
pane_y, pane_x, height, width = await geometry(archive)
# This example puts one status row above the window.
client_y = 1 + pane_y
rows = slice(client_y, client_y + height)
cols = slice(pane_x, pane_x + width)
ledger = tuple(f"Ledger entry {index:02}" for index in range(height))
visible = await client.wait_for(
lambda snap: (
snap[client_y, pane_x].style.bg == AnsiColor.BLUE
and snap[client_y, pane_x].style.fg == AnsiColor.YELLOW
and all(
row.startswith(label)
for row, label in zip(
snap[rows, cols].lines, ledger, strict=True
)
)
),
timeout=6,
)
_check(visible[client_y, pane_x].text == "L", "Missing selected L")
_check(
visible[client_y, pane_x + 2].style.bg != AnsiColor.BLUE,
"Selection extends past the marked cells",
)
app = await capture(archive)
_check(
"ARCHIVE LIVE" in app and "Ledger entry 00" not in app,
"Pane capture did not show the live archive",
)
sys.stdout.write(
"app pane: ARCHIVE LIVE\nattached copy mode: Ledger entry 00\n"
)
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.
# Read the mode grid at the current scroll offset.
if "M" in flags:
# Advance the live pane while copy mode stays frozen on history.
trigger.write_text("go", encoding="ascii")
deadline = asyncio.get_running_loop().time() + 5
while "ARCHIVE UPDATED" not in await capture(archive):
if asyncio.get_running_loop().time() >= deadline:
msg = "Live archive did not update behind copy mode"
raise AssertionError(msg)
await asyncio.sleep(0.02)
scroll = int(
await tm(
"display-message", "-p", "-t", archive, "#{scroll_position}"
)
)
mode_rows = await tm(
"capture-pane",
"-p",
"-M",
"-t",
archive,
"-S",
str(-scroll),
"-E",
str(height - 1 - scroll),
)
live_rows = await tm(
"capture-pane",
"-p",
"-t",
archive,
"-S",
str(-scroll),
"-E",
str(height - 1 - scroll),
)
attached_rows = visible[rows, cols].lines
# Compare ledger labels before any copy-mode decorations.
label_width = len(ledger[0])
_check(
tuple(row[:label_width] for row in mode_rows.splitlines())
== tuple(row[:label_width] for row in attached_rows),
"Copy-mode rows differ from the attached viewport",
)
_check(
"Ledger entry 00" not in live_rows,
"Plain capture-pane matched frozen copy-mode history",
)
sys.stdout.write("mode capture at scroll offset: Ledger entry 00\n")
On builds that omit -M, use the attached snapshot for visible copy-mode rows.
Translate a click after resize¶
Cancel copy mode, call 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.
outer_row = 1 + top + PIN[0]
outer_col = left + PIN[1]
Use zero-based rows and columns for MouseEvent. The
encoder converts them to one-based SGR mouse wire coordinates (+1 on both
axes):
>>> 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 send_key().
The example sends both button edges with
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:
# MouseEvent accepts zero-based outer coordinates and encodes them.
for edge in (MouseAction.PRESS, MouseAction.RELEASE):
client.send_event(
MouseEvent(MouseButton.LEFT, edge, outer_row, outer_col)
)
press_row = 1 + top + 4
release_row = 1 + top + 5
receipt = await client.wait_for(
lambda s: (
(
line := s[release_row, left : left + pane_cols].lines[0]
).startswith("Picker release ")
and line.endswith(";")
),
timeout=6,
)
want = f"Picker release row={PIN[0]} col={PIN[1]}"
actual = receipt[release_row, left : left + pane_cols].lines[0]
actual = actual.removesuffix(";")
_check(
actual == want,
f"Picker reported {actual.removeprefix('Picker ')}, wanted {want}",
)
press_line = receipt[press_row, left : left + pane_cols].lines[0]
_check(
press_line == f"Picker press row={PIN[0]} col={PIN[1]};",
f"Picker missed the button press: {press_line!r}",
)
active = await tm("display-message", "-p", "-t", "desk:", "#{pane_id}")
if no_focus_on_click:
_check(
active == archive,
f"Expected focus to stay on archive, got {active}",
)
await tm("select-pane", "-t", picker)
active = await tm(
"display-message", "-p", "-t", "desk:", "#{pane_id}"
)
_check(
active == picker, f"Expected the picker to own focus, got {active}"
)
if no_focus_on_click:
sys.stdout.write(
"focus after click: stayed on archive; "
"transferred via select-pane\n"
)
client.send_key("v")
await client.wait_for("Picker key v", timeout=6)
Run the example¶
From the repository root with libvt installed, run the
example 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.
$ 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.