Debian Codegraph @

docs/topics/clients.md

raw symbols SCIP
1 (clients)=
2
3 # Clients
4
5 A tmux {term}`Client` is an attached terminal — the side of the tmux
6 connection a user sees. The same tmux server can host many clients at
7 once (one per `$ tmux attach` from different terminals), and each
8 client has its own view of the active session, window, and pane.
9
10 {class}`~libtmux.Client` is the libtmux object for that attached terminal.
11 It sits outside the
12 {class}`~libtmux.server.Server` → {class}`~libtmux.session.Session` →
13 {class}`~libtmux.window.Window` → {class}`~libtmux.pane.Pane`
14 ownership hierarchy: a client *points at* a Session/Window/Pane it is
15 currently viewing, but is not owned by them.
16
17 Most code reads a client's current attachment once and branches on it;
18 the details about staleness and refresh below rarely matter in practice.
19
20 ## View, not identity
21
22 You rarely need this detail unless you're tracking a client across
23 several user commands, but it's worth understanding why certain fields
24 go stale. The fields that look like foreign keys — `client_session`,
25 `session_id`, `window_id`, and `pane_id` — are snapshots of where the
26 client was attached when libtmux read it. They go stale the instant the
27 user runs `switch-client`, `select-window`, or `select-pane`. The
28 client's *identity* is `client_name` (the tty path on Unix), which is
29 stable for the lifetime of the attachment.
30
31 | Field | What it is | Stable? |
32 |-------|------------|---------|
33 | `client_name` | tty path tmux assigned at attach time | Yes — identity |
34 | `session_id` / `window_id` / `pane_id` | the client's *attached view* when read | No — snapshot |
35 | `client_session` | session name of the same attached view | No — snapshot |
36 | `client_pid` / `client_tty` / `client_user` | terminal-level facts | Yes — identity-adjacent |
37
38 ## Live attachment lookup
39
40 When you want the *current* attachment — not the snapshot — use the
41 three `attached_*` properties. Each calls
42 {meth}`~libtmux.Client.refresh` to query the current state (one tmux
43 round-trip) and then resolves the typed
44 {class}`~libtmux.Session`, {class}`~libtmux.Window`, or
45 {class}`~libtmux.Pane` it's viewing. This costs a little — you're
46 asking for the live state — but you get the current view in return:
47
48 ```python
49 >>> with control_mode() as ctl:
50 ... client = server.clients.get(client_name=ctl.client_name)
51 ... attached = client.attached_session
52 >>> attached is not None
53 True
54 ```
55
56 {attr}`~libtmux.Client.attached_window` follows the client's attached
57 session to its
58 {attr}`~libtmux.session.Session.active_window`, and
59 {attr}`~libtmux.Client.attached_pane` follows that window to its
60 {attr}`~libtmux.window.Window.active_pane`. The three properties chain,
61 so reading {attr}`~libtmux.Client.attached_pane` does one
62 `list-clients` refresh, then walks to the active window and its active pane.
63
64 ```python
65 >>> with control_mode() as ctl:
66 ... client = server.clients.get(client_name=ctl.client_name)
67 ... pane = client.attached_pane
68 >>> pane is None or pane.pane_id.startswith('%')
69 True
70 ```
71
72 ## Iterating attached clients
73
74 If you need to find or filter clients, you iterate over or query the
75 server's client collection. {attr}`~libtmux.Server.clients` returns a
76 {class}`~libtmux._internal.query_list.QueryList` of every client tmux
77 reports through `list-clients`. Filter or
78 {meth}`get() <libtmux._internal.query_list.QueryList.get>` it the same way as
79 {attr}`~libtmux.Server.sessions`:
80
81 ```python
82 >>> with control_mode() as ctl:
83 ... attached = [
84 ... c
85 ... for c in server.clients
86 ... if c.client_name == ctl.client_name
87 ... ]
88 >>> bool(attached)
89 True
90 ```
91
92 For filtering clients, use `server.clients.filter(...)` or iterate over
93 {attr}`~libtmux.Server.clients` directly; see {ref}`native-filtering` if
94 you want tmux's native format-based filtering on sessions, windows,
95 panes, and buffers.
96
97 ## Missing live attachments
98
99 When a client detaches or its view becomes stale, the `attached_*`
100 properties return `None` so you can branch on truthiness without a
101 `try`/`except` block. This happens in three cases:
102
103 - the snapshot `session_id` is empty (e.g. the client is at the tmux
104 command prompt rather than viewing a session),
105 - the snapshot `session_id` no longer names a live session (the
106 session was killed between the client read and access), or
107 - the client has detached and `list-clients` no longer reports it.
108
109 Calling {meth}`~libtmux.Client.refresh` directly still raises
110 {exc}`~libtmux.exc.TmuxObjectDoesNotExist` on a detached client; the
111 `attached_*` properties catch that case and return `None` for you.
112
113 ## See also
114
115 - {doc}`/api/libtmux.client` — autodoc reference
116 - {ref}`about` — where {class}`~libtmux.Client` fits in the overall object model
117 - {ref}`native-filtering` — tmux-native filtering for sessions, windows, panes,
118 and buffers