Debian Codegraph @

docs/topics/architecture.md

raw symbols SCIP
1 (about)=
2
3 # Architecture
4
5 When you use libtmux, you work through a hierarchy of typed Python
6 objects — {class}`~libtmux.server.Server`, {class}`~libtmux.session.Session`,
7 {class}`~libtmux.window.Window`, and {class}`~libtmux.pane.Pane` — each a
8 proxy for the tmux entity it represents. You navigate from one to the
9 next (a server's sessions, a session's windows, a window's panes), and
10 every method you call turns into a tmux command directed at that exact
11 object.
12
13 You don't need anything on this page to use the API; the objects and
14 their methods work out of the box. This is reference material for when
15 you're curious how libtmux tracks those objects, keeps their identities
16 stable across refreshes, and lays out the code underneath. Skim the
17 first section and stop whenever you've seen enough.
18
19 Under the hood, libtmux is a [typed](https://docs.python.org/3/library/typing.html)
20 abstraction layer built on two tmux primitives: targets (`-t`), which
21 direct a command at an individual session, window, or pane, and
22 `FORMATS`, the template variables tmux exposes to describe each object's
23 properties.
24
25 ## Object hierarchy
26
27 libtmux mirrors tmux's object hierarchy as a typed Python ORM, so the
28 parent-child relationships you know from tmux carry over directly into
29 the objects you hold:
30
31 ```
32 Server
33 ├── Session
34 │ └── Window
35 │ └── Pane
36 └── Client (attached view)
37 ```
38
39 | Object | Child | Parent |
40 |--------|-------|--------|
41 | {class}`~libtmux.server.Server` | {class}`~libtmux.session.Session`, {class}`~libtmux.client.Client` | None |
42 | {class}`~libtmux.session.Session` | {class}`~libtmux.window.Window` | {class}`~libtmux.server.Server` |
43 | {class}`~libtmux.window.Window` | {class}`~libtmux.pane.Pane` | {class}`~libtmux.session.Session` |
44 | {class}`~libtmux.pane.Pane` | None | {class}`~libtmux.window.Window` |
45 | {class}`~libtmux.client.Client` | None | {class}`~libtmux.server.Server` |
46
47 The Session, Window, Pane, and Client classes share a common dataclass
48 base (`Obj`) defined in {mod}`libtmux.neo`, which fetches each object's
49 fields from tmux; the parent and child links above are plain properties
50 on each class.
51
52 One object breaks the ownership chain: {class}`~libtmux.client.Client` is a
53 *view*, not a child. Each attached terminal points at the
54 Session/Window/Pane it is currently displaying, but is not owned by
55 them — so its view can change the moment a user switches sessions. See
56 {ref}`clients` for the view-vs-identity distinction.
57
58 ## Internal identifiers
59
60 When tmux state changes, libtmux needs a way to recognize that the same
61 window is still the same window. tmux assigns each session, window, and
62 pane a unique ID for exactly this, and libtmux stores it as a dataclass
63 attribute on each object (`session_id`, `window_id`, `pane_id`) to track
64 objects reliably across state refreshes rather than relying on names or
65 indexes that shift around.
66
67 | Object | Prefix | Example |
68 |--------|--------|---------|
69 | {class}`~libtmux.server.Server` | N/A | Uses `socket-name` / `socket-path` |
70 | {class}`~libtmux.session.Session` | `$` | `$13` |
71 | {class}`~libtmux.window.Window` | `@` | `@3243` |
72 | {class}`~libtmux.pane.Pane` | `%` | `%5433` |
73
74 ## Core objects
75
76 These are the five classes you'll actually hold and call methods on.
77 Each level wraps the tmux commands and format queries for its tier of
78 the hierarchy:
79
80 - {class}`~libtmux.server.Server` — entry point, manages sessions, executes raw tmux commands
81 - {class}`~libtmux.session.Session` — manages windows within a session
82 - {class}`~libtmux.window.Window` — manages panes, handles layouts
83 - {class}`~libtmux.pane.Pane` — terminal instance, sends keys and captures output
84 - {class}`~libtmux.client.Client` — attached terminal viewing a session, window, and pane
85
86 ## Data flow
87
88 Every interaction follows the same round-trip: you act on a Python
89 object, libtmux talks to tmux, and the result comes back as more typed
90 objects. Reading state and changing it both cost a tmux call, which is
91 why an object can go stale and why you refresh it rather than trust a
92 cached value indefinitely.
93
94 1. User creates a {class}`~libtmux.Server` (connects to a running tmux server)
95 2. Queries use tmux format strings ({mod}`libtmux.constants`) to fetch state
96 3. Results are parsed into typed Python objects
97 4. Mutations dispatch tmux commands via the `cmd()` method
98 5. Objects refresh state from tmux on demand
99
100 ## Module map
101
102 The codebase splits along the same hierarchy: one module per tier, plus
103 shared plumbing. The first block is what you import and use day to day;
104 the second is lower-level and mostly of interest to contributors or
105 deeper integrations.
106
107 | Module | Role |
108 |--------|------|
109 | {mod}`libtmux.server` | Server connection and session management |
110 | {mod}`libtmux.session` | Session operations |
111 | {mod}`libtmux.window` | Window operations and pane management |
112 | {mod}`libtmux.pane` | Pane I/O and capture |
113 | {mod}`libtmux.client` | Attached-client view and live-attachment lookup |
114 | {mod}`libtmux.common` | Base classes, command execution |
115
116 The remaining modules are advanced — reach for them only when the core
117 objects don't cover your case:
118
119 | Module | Role |
120 |--------|------|
121 | {mod}`libtmux.neo` | Modern dataclass-based query interface |
122 | {mod}`libtmux.constants` | Format string constants |
123 | {mod}`libtmux.options` | tmux option get/set |
124 | {mod}`libtmux.hooks` | tmux hook management |
125 | {mod}`libtmux.exc` | Exception hierarchy |
126
127 ## Naming conventions
128
129 tmux commands use dashes (`new-window`). libtmux replaces these with
130 underscores (`new_window`) to follow Python naming conventions.
131
132 ## References
133
134 - [tmux man page](https://man.openbsd.org/tmux.1)
135 - [tmux source code](https://github.com/tmux/tmux)