Skip to content

Commit 28b2ddc

Browse files
committed
feat: Basic dataclass
1 parent 4636913 commit 28b2ddc

26 files changed

+1849
-1223
lines changed

CHANGES

+31
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,37 @@ $ pip install --user --upgrade --pre libtmux
1414

1515
<!-- Maintainers and contributors: Insert change notes for the next release above -->
1616

17+
### Breaking changes
18+
19+
- Finding objects / relations
20+
21+
- 0.16 and below: `session._windows()`, `session.list_windows()`, etc.
22+
23+
0.17 and after: {attr}`session.windows <libtmux.Session.windows>`
24+
25+
- 0.16 and below: `session.find_where({'window_name': my_window})`
26+
27+
0.17 and after: {meth}`session.windows.get(window_name=my_window, default=None) <libtmux.Session.windows>`
28+
29+
- If not found and not `default`, raises {exc}`~libtmux._internal.query_list.ObjectDoesNotExist`
30+
- If multiple objects found, raises {exc}`~libtmux._internal.query_list.MultipleObjectsReturned`
31+
32+
- 0.16 and below: `session.where({'window_name': my_window})`
33+
34+
0.17 and after: {meth}`session.windows.filter(window_name=my_window) <libtmux.Session.windows>`
35+
36+
- Accessing attributes
37+
38+
- 0.16 and below: `window['id']`
39+
40+
0.17 and after: `window.id`
41+
- 0.16 and below: `window.get('id')`
42+
43+
0.17 and after: `window.id`
44+
- 0.16 and below: `window.get('id', None)`
45+
46+
0.17 and after: `getattr(window, 'id', None)`
47+
1748
### New features
1849

1950
#### Detect if server active (#448)

MIGRATION

+33
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,39 @@ well.
1919
[tracker]: https://github.com/tmux-python/libtmux/discussions
2020
```
2121

22+
## 0.17.x: Simplified attributes
23+
24+
### Finding objects / relations
25+
26+
- 0.16 and below: `session._windows()`, `session.list_windows()`, etc.
27+
28+
0.17 and after: {attr}`session.windows <libtmux.Session.windows>`
29+
30+
- 0.16 and below: `session.find_where({'window_name': my_window})`
31+
32+
0.17 and after: {meth}`session.windows.get(window_name=my_window, default=None) <libtmux.Session.windows>`
33+
34+
- If not found and not `default`, raises {exc}`~libtmux._internal.query_list.ObjectDoesNotExist`
35+
- If multiple objects found, raises {exc}`~libtmux._internal.query_list.MultipleObjectsReturned`
36+
37+
38+
39+
- 0.16 and below: `session.where({'window_name': my_window})`
40+
41+
0.17 and after: {meth}`session.windows.filter(window_name=my_window) <libtmux.Session.windows>`
42+
43+
### Accessing attributes
44+
45+
- 0.16 and below: `window['id']`
46+
47+
0.17 and after: `window.id`
48+
- 0.16 and below: `window.get('id')`
49+
50+
0.17 and after: `window.id`
51+
- 0.16 and below: `window.get('id', None)`
52+
53+
0.17 and after: `getattr(window, 'id', None)`
54+
2255
## Next release
2356

2457
_Migration instructions for the upcoming release will be added here_

README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,14 @@ current tmux server / session / window pane.
6969
List sessions:
7070

7171
```python
72-
>>> server.list_sessions()
72+
>>> server.sessions
7373
[Session($1 ...), Session($0 ...)]
7474
```
7575

7676
Find session:
7777

7878
```python
79-
>>> server.get_by_id('$1')
79+
>>> server.sessions.filter(session_id='$1')[0]
8080
Session($1 ...)
8181
```
8282

@@ -85,7 +85,7 @@ Find session by dict lookup:
8585
```python
8686
>>> server.sessions[0].rename_session('foo')
8787
Session($1 foo)
88-
>>> server.find_where({ "session_name": "foo" })
88+
>>> server.sessions.filter(session_name="foo")[0]
8989
Session($1 foo)
9090
```
9191

@@ -150,12 +150,14 @@ Type inside the pane (send key strokes):
150150

151151
>>> pane.send_keys('echo hey', enter=False)
152152
>>> pane.enter()
153+
Pane(%1 ...)
153154
```
154155

155156
Grab the output of pane:
156157

157158
```python
158159
>>> pane.clear() # clear the pane
160+
Pane(%1 ...)
159161
>>> pane.send_keys("cowsay 'hello'", enter=True)
160162
>>> print('\n'.join(pane.cmd('capture-pane', '-p').stdout)) # doctest: +SKIP
161163
$ cowsay 'hello'

docs/about.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ How is libtmux able to keep references to panes, windows and sessions?
8585
> Window index refers to the # of the window in the session.
8686
>
8787
> To assert pane, window and session data, libtmux will use
88-
> {meth}`Server.list_sessions()`, {meth}`Session.list_windows()`,
89-
> {meth}`Window.list_panes()` to update objects.
88+
> {meth}`Server.sessions()`, {meth}`Session.windows()`,
89+
> {meth}`Window.panes()` to update objects.
9090
9191
## Naming conventions
9292

docs/internals/dataclasses.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Dataclass helpers - `libtmux._internal.dataclasses`
2+
3+
```{eval-rst}
4+
.. automodule:: libtmux._internal.dataclasses
5+
:members:
6+
:special-members:
7+
8+
```

docs/internals/index.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
# Internals
44

55
:::{warning}
6+
Be careful with these! Internal APIs are **not** covered by version policies. They can break or be removed between minor versions!
67

7-
These APIs are internal and not covered by versioning policy.
8-
8+
If you need an internal API stabilized please [file an issue](https://github.com/tmux-python/libtmux/issues).
99
:::
1010

1111
## Environmental variables
@@ -23,3 +23,10 @@ to split `tmux(1)`'s formatting information.
2323

2424
If you find any compatibility problems with the default, or better yet find a string copacetic
2525
many environments and tmux releases, note it at <https://github.com/tmux-python/libtmux/discussions/355>.
26+
27+
## Changes
28+
29+
```{toctree}
30+
dataclasses
31+
query_list
32+
``

docs/internals/query_list.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# List querying - `libtmux._internal.query_list`
2+
3+
```{eval-rst}
4+
.. automodule:: libtmux._internal.query_list
5+
:members:
6+
```

docs/quickstart.md

+16-12
Original file line numberDiff line numberDiff line change
@@ -125,33 +125,33 @@ Windows and Panes.
125125
If you have multiple tmux sessions open, you can see that all of the
126126
methods in {class}`Server` are available.
127127

128-
We can list sessions with {meth}`Server.list_sessions`:
128+
We can list sessions with {meth}`Server.sessions`:
129129

130130
```python
131-
>>> server.list_sessions()
131+
>>> server.sessions
132132
[Session($1 ...), Session($0 ...)]
133133
```
134134

135135
This returns a list of {class}`Session` objects you can grab. We can
136136
find our current session with:
137137

138138
```python
139-
>>> server.list_sessions()[0]
139+
>>> server.sessions[0]
140140
Session($1 ...)
141141
```
142142

143143
However, this isn't guaranteed, libtmux works against current tmux information, the
144144
session's name could be changed, or another tmux session may be created,
145-
so {meth}`Server.get_by_id` and {meth}`Server.find_where` exists as a lookup.
145+
so {meth}`Server.sessions` and {meth}`Server.windows` exists as a lookup.
146146

147147
## Get session by ID
148148

149149
tmux sessions use the `$[0-9]` convention as a way to identify sessions.
150150

151-
`$1` is whatever the ID `list_sessions()` returned above.
151+
`$1` is whatever the ID `sessions()` returned above.
152152

153153
```python
154-
>>> server.get_by_id('$1')
154+
>>> server.sessions.filter(session_id='$1')[0]
155155
Session($1 ...)
156156
```
157157

@@ -163,13 +163,16 @@ You may `session = server.get_by_id('$<yourId>')` to use the session object.
163163
>>> server.sessions[0].rename_session('foo')
164164
Session($1 foo)
165165

166-
>>> server.find_where({ "session_name": "foo" })
166+
>>> server.sessions.filter(session_name="foo")[0]
167+
Session($1 foo)
168+
169+
>>> server.sessions.get(session_name="foo")
167170
Session($1 foo)
168171
```
169172

170-
With `find_where`, pass in a dict and return the first object found. In
173+
With `filter`, pass in attributes and return a list of matches. In
171174
this case, a {class}`Server` holds a collection of child {class}`Session`.
172-
{class}`Session` and {class}`Window` both utilize `find_where` to sift
175+
{class}`Session` and {class}`Window` both utilize `filter` to sift
173176
through Windows and Panes, respectively.
174177

175178
So you may now use:
@@ -178,7 +181,7 @@ So you may now use:
178181
>>> server.sessions[0].rename_session('foo')
179182
Session($1 foo)
180183

181-
>>> session = server.find_where({ "session_name": "foo" })
184+
>>> session = server.sessions.get(session_name="foo")
182185
>>> session
183186
Session($1 foo)
184187
```
@@ -213,7 +216,7 @@ Let's delete that window ({meth}`Session.kill_window`).
213216
Method 1: Use passthrough to tmux's `target` system.
214217

215218
```python
216-
>>> session.kill_window(window.id)
219+
>>> session.kill_window(window.window_id)
217220
```
218221

219222
The window in the bg dissappeared. This was the equivalent of
@@ -260,7 +263,7 @@ And kill:
260263
>>> window.kill_window()
261264
```
262265

263-
Use {meth}`Session.list_windows()` and {meth}`Session.find_where()` to list and sort
266+
Use {meth}`Session.windows` and {meth}`Session.windows.filter()` to list and sort
264267
through active {class}`Window`'s.
265268

266269
## Manipulating windows
@@ -346,6 +349,7 @@ using {meth}`Pane.enter()`:
346349

347350
```python
348351
>>> pane.enter()
352+
Pane(%1 ...)
349353
```
350354

351355
### Avoid cluttering shell history

docs/reference/properties.md

+17-38
Original file line numberDiff line numberDiff line change
@@ -49,25 +49,24 @@ Session($1 libtmux_...)
4949
Quick access to basic attributes:
5050

5151
```python
52-
>>> session.name
52+
>>> session.session_name
5353
'libtmux_...'
5454

55-
>>> session.id
55+
>>> session.session_id
5656
'$1'
5757
```
5858

5959
To see all attributes for a session:
6060

6161
```python
62-
>>> sorted(list(session._info.keys()))
62+
from libtmux.neo import Obj
63+
64+
>>> sorted(list(Obj.__dataclass_fields__.keys()))
6365
['session_attached', 'session_created', ...]
6466
```
6567

66-
Some may conflict with python API, to access them, you can use `.get()`, to get the count
67-
of sessions in a window:
68-
6968
```python
70-
>>> session.get('session_windows')
69+
>>> session.session_windows
7170
'...'
7271
```
7372

@@ -85,30 +84,23 @@ Window(@1 ...:..., Session($1 ...))
8584
Basics:
8685

8786
```python
88-
>>> window.name
87+
>>> window.window_name
8988
'...'
9089

91-
>>> window.id
90+
>>> window.window_id
9291
'@1'
9392

94-
>>> window.height
93+
>>> window.window_height
9594
'...'
9695

97-
>>> window.width
96+
>>> window.window_width
9897
'...'
9998
```
10099

101-
Everything available:
102-
103-
```python
104-
>>> sorted(list(window.keys()))
105-
['session_id', 'session_name', 'window_active', ..., 'window_width']
106-
```
107-
108-
Use `get()` for details not accessible via properties:
100+
Use attribute access for details not accessible via properties:
109101

110102
```python
111-
>>> window.get('window_panes')
103+
>>> window.window_panes
112104
'1'
113105
```
114106

@@ -126,33 +118,20 @@ Pane(%1 Window(@1 ...:..., Session($1 libtmux_...)))
126118
Basics:
127119

128120
```python
129-
>>> pane.current_command
121+
>>> pane.pane_current_command
130122
'...'
131123

132-
>>> type(pane.current_command)
124+
>>> type(pane.pane_current_command)
133125
<class 'str'>
134126

135-
>>> pane.height
127+
>>> pane.pane_height
136128
'...'
137129

138-
>>> pane.width
130+
>>> pane.pane_width
139131
'...'
140132

141-
>>> pane.index
133+
>>> pane.pane_index
142134
'0'
143135
```
144136

145-
Everything:
146-
147-
````python
148-
>>> sorted(list(pane._info.keys()))
149-
['alternate_on', 'alternate_saved_x', ..., 'wrap_flag']
150-
151-
Use `get()` for details keys:
152-
153-
```python
154-
>>> pane.get('pane_width')
155-
'...'
156-
````
157-
158137
[formats]: http://man.openbsd.org/OpenBSD-5.9/man1/tmux.1#FORMATS

0 commit comments

Comments
 (0)