| 1 | # Design decisions |
| 2 | |
| 3 | This page explains the "why" behind libtmux's shape: the four core choices it |
| 4 | makes about representing tmux to your Python code. You don't need any of it to |
| 5 | get started — the defaults work out of the box, and most code never thinks about |
| 6 | the rationale below. Read on when a choice starts to matter to you: why |
| 7 | {attr}`session.windows <libtmux.Session.windows>` is a live collection, why |
| 8 | properties read cleanly off an object, and what to expect from a pre-1.0 API. |
| 9 | |
| 10 | ## Why ORM-style objects |
| 11 | |
| 12 | Most of your code just writes {attr}`session.windows <libtmux.Session.windows>` |
| 13 | and gets a live, filterable collection back — you rarely think about why it's |
| 14 | shaped that way. This section is for when you're curious about the design. |
| 15 | |
| 16 | tmux organizes terminals in a strict hierarchy: {class}`~libtmux.Server` → |
| 17 | {class}`~libtmux.Session` → {class}`~libtmux.Window` → |
| 18 | {class}`~libtmux.Pane`. Each level owns the next. libtmux mirrors that |
| 19 | hierarchy with Python objects that maintain the same parent-child |
| 20 | relationships, so navigating tmux feels like navigating Python. |
| 21 | |
| 22 | What you get is a relational structure you can walk in either direction: |
| 23 | {attr}`session.windows <libtmux.Session.windows>` lists a session's windows, |
| 24 | {attr}`pane.window <libtmux.Pane.window>` points back up to the pane's parent. |
| 25 | The alternative — a flat command-builder API |
| 26 | (`tmux("new-session", "-s", "foo")`) — hands back raw strings and leaves you to |
| 27 | track which windows belong to which session yourself. |
| 28 | |
| 29 | The trade-off is that an object is a snapshot. If tmux state changes out from |
| 30 | under you — another client splits a window, a process exits — your object can go |
| 31 | stale, and you reach for {meth}`~libtmux.Session.refresh` to re-read it. You |
| 32 | trade that occasional refresh for an API that reads like the hierarchy it |
| 33 | models. |
| 34 | |
| 35 | ## Why format strings |
| 36 | |
| 37 | tmux exposes object properties through its format system (`-F` flags). For |
| 38 | example, `tmux list-sessions -F '#{session_id}:#{session_name}'` returns |
| 39 | structured data. |
| 40 | |
| 41 | libtmux queries through this system instead of parsing human-readable `tmux ls` |
| 42 | output because: |
| 43 | |
| 44 | - **Stability**: format variables are part of tmux's documented interface |
| 45 | - **Precision**: no regex fragility from parsing prose output |
| 46 | - **Completeness**: formats expose properties (like `session_id`) that don't appear in default output |
| 47 | |
| 48 | The cost is a tmux round-trip on the live collections: reading a property like |
| 49 | {attr}`session.windows <libtmux.Session.windows>` runs a subprocess against the |
| 50 | server each time you access it, not a cached value (the scalar fields like |
| 51 | {attr}`session.session_name <libtmux.Session.session_name>` are different — |
| 52 | read once when the object is built). What it buys is a value |
| 53 | you can trust — pulled straight from tmux's own reporting, not reconstructed by |
| 54 | guessing at the layout of display text. The format constants that make this work |
| 55 | live in {mod}`libtmux.formats`. |
| 56 | |
| 57 | ## Why typed data rows |
| 58 | |
| 59 | Advanced — for contributors and lower-level query work. Most code uses the ORM |
| 60 | objects above and never touches this layer directly. |
| 61 | |
| 62 | {mod}`libtmux.neo` provides a modern |
| 63 | [dataclass](https://docs.python.org/3/library/dataclasses.html)-based interface |
| 64 | alongside the legacy dict-style objects. The motivation: |
| 65 | |
| 66 | - **Type safety**: dataclass fields have declared types, enabling |
| 67 | [mypy](https://mypy-lang.org/) checks and IDE completion |
| 68 | - **Predictability**: attribute access (`session.session_name`) instead of dict access (`session["session_name"]`) |
| 69 | - **Migration path**: the two interfaces coexist, allowing gradual adoption without breaking existing code |
| 70 | |
| 71 | Coexistence is the honest trade-off: two interfaces are more surface area to |
| 72 | learn than one. The payoff is that you can adopt the typed path incrementally, |
| 73 | file by file, without a flag-day rewrite. |
| 74 | |
| 75 | ## Pre-1.0 API evolution |
| 76 | |
| 77 | libtmux is pre-1.0. This is a deliberate choice — the API is still maturing. |
| 78 | What this means in practice for code you write today: |
| 79 | |
| 80 | - **Minor versions** (0.x → 0.y) may include breaking changes |
| 81 | - **Patch versions** (0.x.y → 0.x.z) are bug fixes only |
| 82 | - **Pin your dependency**: use `libtmux>=0.55,<0.56` or `libtmux~=0.55.0` |
| 83 | |
| 84 | Breaking changes always get: |
| 85 | 1. A deprecation warning for at least one minor release |
| 86 | 2. Documentation in the [changelog](../history.md) and [deprecations](../project/deprecations.md) |
| 87 | 3. Migration guidance |
| 88 | |
| 89 | See [Public API](../project/public-api.md) for the stability contract. |