Skip to content

Commit fc1967d

Browse files
committed
docs(topics): add control-mode engine guide (#605)
1 parent 9efc8b0 commit fc1967d

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed

docs/api/index.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55
# API Reference
66

7+
:::{note}
8+
Looking for the new control-mode engine? See {ref}`control-mode` for an
9+
experimental, protocol-focused entry point that still preserves the public
10+
``tmux_cmd`` return type.
11+
:::
12+
713
```{toctree}
814
915
properties

docs/quickstart.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,23 @@ in your server object. `libtmux.Server(socket_name='mysocket')` is
157157
equivalent to `$ tmux -L mysocket`.
158158
:::
159159

160+
### Optional: Control mode (experimental)
161+
162+
Control mode keeps a persistent tmux client open and streams
163+
notifications. Enable it by injecting a control-mode engine:
164+
165+
```python
166+
from libtmux._internal.engines.control_mode import ControlModeEngine
167+
from libtmux.server import Server
168+
169+
engine = ControlModeEngine()
170+
server = Server(engine=engine)
171+
session = server.new_session(session_name="ctrl")
172+
print(session.name)
173+
```
174+
175+
See {ref}`control-mode` for details, caveats, and notification handling.
176+
160177
`server` is now a living object bound to the tmux server's Sessions,
161178
Windows and Panes.
162179

docs/topics/control_mode.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
---
2+
orphan: true
3+
---
4+
5+
(control-mode)=
6+
7+
# Control Mode Engine (experimental)
8+
9+
:::{warning}
10+
This is an **experimental API**. Names and behavior may change between releases.
11+
Use with caution and pin your libtmux version if you depend on it.
12+
:::
13+
14+
libtmux can drive tmux through a persistent Control Mode client. This keeps a
15+
single connection open, pipelines commands, and surfaces tmux notifications in a
16+
typed stream.
17+
18+
## Why use Control Mode?
19+
20+
- Lower overhead than spawning a tmux process per command
21+
- Access to live notifications: layout changes, window/link events, client
22+
detach/attach, paste buffer updates, and more
23+
- Structured command results with timing/flag metadata
24+
25+
## Using ControlModeEngine
26+
27+
```python
28+
from __future__ import annotations
29+
30+
from libtmux._internal.engines.control_mode import ControlModeEngine
31+
from libtmux.server import Server
32+
33+
engine = ControlModeEngine(command_timeout=5)
34+
server = Server(engine=engine)
35+
36+
session = server.new_session(session_name="ctrl-demo")
37+
print(session.name)
38+
39+
# Consume notifications (non-blocking example)
40+
for notif in engine.iter_notifications(timeout=0.1):
41+
print(notif.kind, notif.data)
42+
```
43+
44+
:::{note}
45+
Control mode creates a bootstrap tmux session named ``libtmux_control_mode``.
46+
If your code enumerates sessions, filter it out.
47+
:::
48+
49+
## Parsing notifications directly
50+
51+
The protocol parser can be used without tmux to understand the wire format.
52+
53+
```python
54+
>>> from libtmux._internal.engines.control_protocol import ControlProtocol
55+
>>> proto = ControlProtocol()
56+
>>> proto.feed_line("%layout-change @1 abcd efgh 0")
57+
>>> notif = proto.get_notification()
58+
>>> notif.kind.name
59+
'WINDOW_LAYOUT_CHANGED'
60+
>>> notif.data['window_layout']
61+
'abcd'
62+
```
63+
64+
## Fallback engine
65+
66+
If control mode is unavailable, ``SubprocessEngine`` matches the same
67+
``Engine`` interface but runs one tmux process per command:
68+
69+
```python
70+
from libtmux._internal.engines.subprocess_engine import SubprocessEngine
71+
from libtmux.server import Server
72+
73+
server = Server(engine=SubprocessEngine())
74+
print(server.list_sessions()) # legacy behavior
75+
```
76+
77+
## Key behaviors
78+
79+
- Commands still return ``tmux_cmd`` objects for compatibility, but extra
80+
metadata (``exit_status``, ``cmd_id``, ``tmux_time``) is attached.
81+
- Notifications are queued; drops are counted when consumers fall behind.
82+
- Timeouts raise ``ControlModeTimeout`` and restart the control client.

docs/topics/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ Explore libtmux’s core functionalities and underlying principles at a high lev
1010
1111
context_managers
1212
traversal
13+
control_mode
1314
```

0 commit comments

Comments
 (0)