Debian Codegraph @

docs/topics/traversal.md

raw symbols SCIP
1 (traversal)=
2
3 # Traversal
4
5 When you navigate a tmux server with libtmux, you move through a hierarchy of
6 related objects: a {class}`~libtmux.Server` holds {class}`~libtmux.Session`
7 objects, each session holds {class}`~libtmux.Window` objects, and each window
8 holds {class}`~libtmux.Pane` objects. Every object knows both its parents and
9 its children, so you can traverse in either direction — reach for
10 {attr}`session.windows <libtmux.Session.windows>` to list the windows under a
11 session, or {attr}`pane.session <libtmux.Pane.session>` to jump
12 from a pane back up to the session that contains it.
13
14 Most of the time you call a handful of properties like
15 {attr}`session.windows <libtmux.Session.windows>` and
16 {attr}`pane.session <libtmux.Pane.session>` and never look further. This works
17 out of the box, with no setup. The filtering and relationship checks later on
18 the page are there for the rarer cases where you need to find a specific object
19 by name or pattern, or confirm how two objects relate.
20
21 Under the hood this all rides on libtmux's object abstraction of {term}`target`s
22 (the `-t` argument) and the permanent internal IDs tmux assigns to each object,
23 but you rarely have to think about that layer to move around.
24
25 Open two terminals:
26
27 Terminal one: start tmux in a separate terminal:
28
29 ```console
30 $ tmux
31 ```
32
33 Terminal two, `python` or `ptpython` if you have it:
34
35 ```console
36 $ python
37 ```
38
39 ## Setup
40
41 First, create a test session:
42
43 ```python
44 >>> session = server.new_session() # Create a test session using existing server
45 ```
46
47 ## Server level
48
49 The {class}`~libtmux.Server` sits at the top of the hierarchy. Start by viewing
50 its representation:
51
52 ```python
53 >>> server # doctest: +ELLIPSIS
54 Server(socket_name=...)
55 ```
56
57 Get all sessions in the server:
58
59 ```python
60 >>> server.sessions # doctest: +ELLIPSIS
61 [Session($... ...)]
62 ```
63
64 Get all windows across all sessions:
65
66 ```python
67 >>> server.windows # doctest: +ELLIPSIS
68 [Window(@... ..., Session($... ...))]
69 ```
70
71 Get all panes across all windows:
72
73 ```python
74 >>> server.panes # doctest: +ELLIPSIS
75 [Pane(%... Window(@... ..., Session($... ...)))]
76 ```
77
78 Each of these properties queries tmux fresh every time you access it, so the
79 result always reflects the server's current state. That freshness costs a tmux
80 round-trip per access — worth it for correctness, but if you iterate over the
81 same collection repeatedly, bind it to a variable once instead of re-reading the
82 property inside a loop.
83
84 ## Session level
85
86 A {class}`~libtmux.Session` groups windows. Get the first one:
87
88 ```python
89 >>> session = server.sessions[0]
90 >>> session # doctest: +ELLIPSIS
91 Session($... ...)
92 ```
93
94 Get windows in a session:
95
96 ```python
97 >>> session.windows # doctest: +ELLIPSIS
98 [Window(@... ..., Session($... ...))]
99 ```
100
101 Get active window and pane:
102
103 ```python
104 >>> session.active_window # doctest: +ELLIPSIS
105 Window(@... ..., Session($... ...))
106
107 >>> session.active_pane # doctest: +ELLIPSIS
108 Pane(%... Window(@... ..., Session($... ...)))
109 ```
110
111 ## Window level
112
113 A {class}`~libtmux.Window` groups panes. Get one and inspect its properties:
114
115 ```python
116 >>> window = session.windows[0]
117 >>> window.window_index # doctest: +ELLIPSIS
118 '...'
119 ```
120
121 Traverse upward to the window's parent session:
122
123 ```python
124 >>> window.session # doctest: +ELLIPSIS
125 Session($... ...)
126 >>> window.session.session_id == session.session_id
127 True
128 ```
129
130 Get panes in a window:
131
132 ```python
133 >>> window.panes # doctest: +ELLIPSIS
134 [Pane(%... Window(@... ..., Session($... ...)))]
135 ```
136
137 Get active pane:
138
139 ```python
140 >>> window.active_pane # doctest: +ELLIPSIS
141 Pane(%... Window(@... ..., Session($... ...)))
142 ```
143
144 ## Pane level
145
146 A {class}`~libtmux.Pane` is the leaf of the hierarchy. From a pane you can walk
147 all the way back up to its window, session, and server:
148
149 ```python
150 >>> pane = window.panes[0]
151 >>> pane.window.window_id == window.window_id
152 True
153 >>> pane.session.session_id == session.session_id
154 True
155 >>> pane.server is server
156 True
157 ```
158
159 ## Filtering and finding objects
160
161 Sometimes a property like {attr}`session.windows <libtmux.Session.windows>`
162 hands you more objects than you
163 want, and you need the one — or the few — matching a name, an index, or a
164 pattern. Every libtmux collection lets you narrow it down: call
165 {meth}`~libtmux._internal.query_list.QueryList.filter` to keep the objects that
166 match a condition, or {meth}`~libtmux._internal.query_list.QueryList.get` to
167 pull out a single object (it raises if it finds zero or more than one). You match
168 on the same attributes the objects already expose — `window_name`,
169 `window_index`, `pane_id`, and so on.
170
171 This is the opt-in part of the page. If the plain properties above already get
172 you to the object you want, you can stop here. For comprehensive coverage of all
173 lookup operators, see {ref}`querylist-filtering`. For tmux-native filters that
174 return only matching rows on large servers, see {ref}`native-filtering`.
175
176 ### Basic filtering
177
178 Find windows by exact attribute match:
179
180 ```python
181 >>> session.windows.filter(window_index=window.window_index) # doctest: +ELLIPSIS
182 [Window(@... ..., Session($... ...))]
183 ```
184
185 Get a specific pane by ID:
186
187 ```python
188 >>> window.panes.get(pane_id=pane.pane_id) # doctest: +ELLIPSIS
189 Pane(%... Window(@... ..., Session($... ...)))
190 ```
191
192 ### Partial matching
193
194 Use lookup suffixes like `__contains`, `__startswith`, `__endswith`:
195
196 ```python
197 >>> # Create windows to demonstrate filtering
198 >>> w1 = session.new_window(window_name="app-frontend")
199 >>> w2 = session.new_window(window_name="app-backend")
200 >>> w3 = session.new_window(window_name="logs")
201
202 >>> # Find windows starting with 'app-'
203 >>> session.windows.filter(window_name__startswith='app-') # doctest: +ELLIPSIS
204 [Window(@... ...:app-frontend, Session($... ...)), Window(@... ...:app-backend, Session($... ...))]
205
206 >>> # Find windows containing 'end'
207 >>> session.windows.filter(window_name__contains='end') # doctest: +ELLIPSIS
208 [Window(@... ...:app-frontend, Session($... ...)), Window(@... ...:app-backend, Session($... ...))]
209
210 >>> # Clean up
211 >>> w1.kill()
212 >>> w2.kill()
213 >>> w3.kill()
214 ```
215
216 ### Case-insensitive matching
217
218 Prefix any lookup with `i` for case-insensitive matching:
219
220 ```python
221 >>> # Create windows with mixed case
222 >>> w1 = session.new_window(window_name="MyApp")
223 >>> w2 = session.new_window(window_name="myapp-worker")
224
225 >>> # Case-insensitive search
226 >>> session.windows.filter(window_name__istartswith='myapp') # doctest: +ELLIPSIS
227 [Window(@... ...:MyApp, Session($... ...)), Window(@... ...:myapp-worker, Session($... ...))]
228
229 >>> # Clean up
230 >>> w1.kill()
231 >>> w2.kill()
232 ```
233
234 ### Regex filtering
235
236 For complex patterns, use `__regex` or `__iregex`:
237
238 ```python
239 >>> # Create versioned windows
240 >>> w1 = session.new_window(window_name="release-v1-0")
241 >>> w2 = session.new_window(window_name="release-v2-0")
242 >>> w3 = session.new_window(window_name="dev")
243
244 >>> # Match version pattern
245 >>> session.windows.filter(window_name__regex=r'v\d+-\d+') # doctest: +ELLIPSIS
246 [Window(@... ...:release-v1-0, Session($... ...)), Window(@... ...:release-v2-0, Session($... ...))]
247
248 >>> # Clean up
249 >>> w1.kill()
250 >>> w2.kill()
251 >>> w3.kill()
252 ```
253
254 ### Chaining filters
255
256 Multiple conditions can be combined:
257
258 ```python
259 >>> # Create windows for chaining example
260 >>> w1 = session.new_window(window_name="api-prod")
261 >>> w2 = session.new_window(window_name="api-staging")
262 >>> w3 = session.new_window(window_name="web-prod")
263
264 >>> # Multiple conditions in one call (AND)
265 >>> session.windows.filter(
266 ... window_name__startswith='api',
267 ... window_name__endswith='prod'
268 ... ) # doctest: +ELLIPSIS
269 [Window(@... ...:api-prod, Session($... ...))]
270
271 >>> # Chained calls (also AND)
272 >>> session.windows.filter(
273 ... window_name__contains='api'
274 ... ).filter(
275 ... window_name__contains='staging'
276 ... ) # doctest: +ELLIPSIS
277 [Window(@... ...:api-staging, Session($... ...))]
278
279 >>> # Clean up
280 >>> w1.kill()
281 >>> w2.kill()
282 >>> w3.kill()
283 ```
284
285 ### Get with default
286
287 Avoid exceptions when an object might not exist:
288
289 ```python
290 >>> # Returns None instead of raising ObjectDoesNotExist
291 >>> session.windows.get(window_name="nonexistent", default=None) is None
292 True
293 ```
294
295 ## Checking relationships
296
297 Two questions come up often: does an object belong to a collection (membership),
298 and do two handles point at the same tmux entity (identity)? Python's `in`
299 operator answers the first — whether an object is part of a collection:
300
301 ```python
302 >>> window in session.windows
303 True
304 >>> pane in window.panes
305 True
306 >>> session in server.sessions
307 True
308 ```
309
310 Comparing IDs answers the second — here, whether the window you hold is the
311 session's active window:
312
313 ```python
314 >>> window.window_id == session.active_window.window_id
315 True
316 ```
317
318 And whether the pane you hold is the window's active pane:
319
320 ```python
321 >>> pane.pane_id == window.active_pane.pane_id
322 True
323 ```
324
325 [target]: http://man.openbsd.org/OpenBSD-5.9/man1/tmux.1#COMMANDS