Skip to content

Commit bc2a0a8

Browse files
committed
docs: document Server.connect() method
- Add Server.connect() feature to CHANGES file - Add comprehensive example to quickstart.md showing connect() as cleaner alternative to has_session/new_session pattern - Add control-mode compatibility example in topics/control_mode.md - All doctests pass (29 tests in quickstart and control_mode docs)
1 parent c046d1b commit bc2a0a8

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

CHANGES

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ _Future release notes will be placed here_
4040
notification parsing (layout changes, unlinked windows, client detach/session change,
4141
session rename, paste-buffer events), and stats while keeping existing
4242
`Server/Session/Window/Pane.cmd` return type (`tmux_cmd`) stable. (#605)
43+
- `Server.connect()`: New convenience method for session management. Returns an
44+
existing session if found, otherwise creates a new detached session. Simplifies
45+
common session reuse patterns and works transparently with both subprocess and
46+
control-mode engines.
4347

4448
### Compatibility
4549

docs/quickstart.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,41 @@ Session($1 foo)
280280

281281
to give us a `session` object to play with.
282282

283+
## Connect to or create a session
284+
285+
A simpler approach is to use {meth}`Server.connect()`, which returns an existing
286+
session or creates it if it doesn't exist:
287+
288+
```python
289+
>>> session = server.connect('my_project')
290+
>>> session.name
291+
'my_project'
292+
293+
>>> # Calling again returns the same session
294+
>>> session2 = server.connect('my_project')
295+
>>> session2.session_id == session.session_id
296+
True
297+
```
298+
299+
This is particularly useful for:
300+
301+
- Development workflows where you want to reuse existing sessions
302+
- Scripts that should create a session on first run, then reattach
303+
- Working with both subprocess and control-mode engines transparently
304+
305+
Compare with the traditional approach:
306+
307+
```python
308+
>>> # Traditional: check then create
309+
>>> if server.has_session('my_project'):
310+
... session = server.sessions.get(session_name='my_project')
311+
... else:
312+
... session = server.new_session('my_project')
313+
314+
>>> # Simpler with connect()
315+
>>> session = server.connect('my_project')
316+
```
317+
283318
## Playing with our tmux session
284319

285320
We now have access to `session` from above with all of the methods

docs/topics/control_mode.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,29 @@ Control mode creates a bootstrap tmux session named ``libtmux_control_mode``.
4646
If your code enumerates sessions, filter it out.
4747
:::
4848

49+
## Session management with Control Mode
50+
51+
The {meth}`Server.connect()` method works seamlessly with control mode:
52+
53+
```python
54+
from libtmux._internal.engines.control_mode import ControlModeEngine
55+
from libtmux.server import Server
56+
57+
engine = ControlModeEngine()
58+
server = Server(engine=engine)
59+
60+
# Reuses session if it exists, creates if it doesn't
61+
session = server.connect("dev-session")
62+
print(session.name)
63+
64+
# Calling again returns the same session
65+
session2 = server.connect("dev-session")
66+
assert session2.session_id == session.session_id
67+
```
68+
69+
This works transparently with both control mode and subprocess engines, making it
70+
easy to switch between them without changing your code.
71+
4972
## Parsing notifications directly
5073

5174
The protocol parser can be used without tmux to understand the wire format.

0 commit comments

Comments
 (0)