Skip to content

Commit 334e87e

Browse files
committed
build(deps): libtmux: Use experimental branch w/ dataclasses
1 parent cf784c0 commit 334e87e

File tree

12 files changed

+151
-160
lines changed

12 files changed

+151
-160
lines changed

docs/api.md

-4
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ If you need an internal API stabilized please [file an issue](https://github.com
2323
.. automethod:: tmuxp.util.oh_my_zsh_auto_title
2424
```
2525

26-
```{eval-rst}
27-
.. automethod:: tmuxp.util.raise_if_tmux_not_running
28-
```
29-
3026
```{eval-rst}
3127
.. automethod:: tmuxp.util.get_current_pane
3228
```

docs/quickstart.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,10 @@ AL - [Abstraction Layer][abstraction layer]
145145
| {ref}`tmuxp python api <libtmux:api>` | {term}`tmux(1)` equivalent |
146146
| ------------------------------------- | -------------------------- |
147147
| {meth}`libtmux.Server.new_session` | `$ tmux new-session` |
148-
| {meth}`libtmux.Server.list_sessions` | `$ tmux list-sessions` |
149-
| {meth}`libtmux.Session.list_windows` | `$ tmux list-windows` |
148+
| {meth}`libtmux.Server.sessions` | `$ tmux list-sessions` |
149+
| {meth}`libtmux.Session.windows` | `$ tmux list-windows` |
150150
| {meth}`libtmux.Session.new_window` | `$ tmux new-window` |
151-
| {meth}`libtmux.Window.list_panes` | `$ tmux list-panes` |
151+
| {meth}`libtmux.Window.panes` | `$ tmux list-panes` |
152152
| {meth}`libtmux.Window.split_window` | `$ tmux split-window` |
153153
| {meth}`libtmux.Pane.send_keys` | `$ tmux send-keys` |
154154

poetry.lock

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ tmuxp = 'tmuxp:cli.cli'
4747

4848
[tool.poetry.dependencies]
4949
python = "^3.7"
50-
libtmux = "~0.16.1"
50+
libtmux = "0.17.0a1"
5151
colorama = ">=0.3.9"
5252
PyYAML = "^6.0"
5353

src/tmuxp/cli/freeze.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class CLIFreezeNamespace(argparse.Namespace):
3232

3333
def session_completion(ctx, params, incomplete):
3434
server = Server()
35-
choices = [session.name for session in server.list_sessions()]
35+
choices = [session.name for session in server.sessions]
3636
return sorted(str(c) for c in choices if str(c).startswith(incomplete))
3737

3838

@@ -100,7 +100,7 @@ def command_freeze(
100100

101101
try:
102102
if args.session_name:
103-
session = server.find_where({"session_name": args.session_name})
103+
session = server.sessions.get(session_name=args.session_name, default=None)
104104
else:
105105
session = util.get_session(server)
106106

src/tmuxp/cli/shell.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def command_shell(
142142
"""
143143
server = Server(socket_name=args.socket_name, socket_path=args.socket_path)
144144

145-
util.raise_if_tmux_not_running(server=server)
145+
server.raise_if_dead()
146146

147147
current_pane = util.get_current_pane(server=server)
148148

src/tmuxp/util.py

+38-42
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import sys
1212

1313
from libtmux._compat import console_to_str
14-
from libtmux.exc import LibTmuxException
1514

1615
from . import exc
1716

@@ -75,49 +74,31 @@ def oh_my_zsh_auto_title():
7574
)
7675

7776

78-
def raise_if_tmux_not_running(server):
79-
"""Raise exception if not running. More descriptive error if no server found."""
80-
try:
81-
server.sessions
82-
except LibTmuxException as e:
83-
if any(
84-
needle in str(e)
85-
for needle in ["No such file or directory", "no server running on"]
86-
):
87-
raise LibTmuxException(
88-
"no tmux session found. Start a tmux session and try again. \n"
89-
"Original error: " + str(e)
90-
)
91-
else:
92-
raise e
93-
94-
9577
def get_current_pane(server):
9678
"""Return Pane if one found in env"""
9779
if os.getenv("TMUX_PANE") is not None:
9880
try:
99-
return [
100-
p
101-
for p in server._list_panes()
102-
if p.get("pane_id") == os.getenv("TMUX_PANE")
103-
][0]
81+
return [p for p in server.panes if p.pane_id == os.getenv("TMUX_PANE")][0]
10482
except IndexError:
10583
pass
10684

10785

10886
def get_session(server, session_name=None, current_pane=None):
109-
if session_name:
110-
session = server.find_where({"session_name": session_name})
111-
elif current_pane is not None:
112-
session = server.find_where({"session_id": current_pane["session_id"]})
113-
else:
114-
current_pane = get_current_pane(server)
115-
if current_pane:
116-
session = server.find_where({"session_id": current_pane["session_id"]})
87+
try:
88+
if session_name:
89+
session = server.sessions.get(session_name=session_name)
90+
elif current_pane is not None:
91+
session = server.sessions.get(session_id=current_pane.session_id)
11792
else:
118-
session = server.list_sessions()[0]
119-
120-
if not session:
93+
current_pane = get_current_pane(server)
94+
if current_pane:
95+
session = server.sessions.get(session_id=current_pane.session_id)
96+
else:
97+
session = server.sessions[0]
98+
except Exception:
99+
session = None
100+
101+
if session is None:
121102
if session_name:
122103
raise exc.TmuxpException("Session not found: %s" % session_name)
123104
else:
@@ -127,26 +108,41 @@ def get_session(server, session_name=None, current_pane=None):
127108

128109

129110
def get_window(session, window_name=None, current_pane=None):
130-
if window_name:
131-
window = session.find_where({"window_name": window_name})
132-
if not window:
111+
try:
112+
if window_name:
113+
window = session.windows.get(window_name=window_name)
114+
elif current_pane is not None:
115+
window = session.windows.get(window_id=current_pane.window_id)
116+
else:
117+
window = session.windows[0]
118+
except Exception:
119+
window = None
120+
121+
if window is None:
122+
if window_name:
133123
raise exc.TmuxpException("Window not found: %s" % window_name)
134-
elif current_pane is not None:
135-
window = session.find_where({"window_id": current_pane["window_id"]})
136-
else:
137-
window = session.list_windows()[0]
124+
if current_pane:
125+
raise exc.TmuxpException("Window not found: %s" % current_pane)
126+
else:
127+
raise exc.TmuxpException("Window not found")
138128

139129
return window
140130

141131

142132
def get_pane(window, current_pane=None):
143133
try:
144134
if current_pane is not None:
145-
pane = window.find_where({"pane_id": current_pane["pane_id"]}) # NOQA: F841
135+
pane = window.panes.get(pane_id=current_pane.pane_id) # NOQA: F841
146136
else:
147137
pane = window.attached_pane # NOQA: F841
148138
except exc.TmuxpException as e:
149139
print(e)
150140
return
151141

142+
if pane is None:
143+
if current_pane:
144+
raise exc.TmuxpException("Pane not found: %s" % current_pane)
145+
else:
146+
raise exc.TmuxpException("Pane not found")
147+
152148
return pane

src/tmuxp/workspace/builder.py

+23-19
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,15 @@ class WorkspaceBuilder:
7171
>>> new_session.name == 'sample workspace'
7272
True
7373
74-
>>> len(new_session._windows)
74+
>>> len(new_session.windows)
7575
3
7676
7777
>>> sorted([window.name for window in new_session.windows])
7878
['editor', 'logging', 'test']
7979
8080
**Existing session:**
8181
82-
>>> len(session._windows)
82+
>>> len(session.windows)
8383
1
8484
8585
>>> builder.build(session=session)
@@ -89,7 +89,7 @@ class WorkspaceBuilder:
8989
>>> session.name == 'sample workspace'
9090
False
9191
92-
>>> len(session._windows)
92+
>>> len(session.windows)
9393
3
9494
9595
>>> sorted([window.name for window in session.windows])
@@ -174,7 +174,10 @@ def session_exists(self, session_name=None):
174174
if not exists:
175175
return exists
176176

177-
self.session = self.server.find_where({"session_name": session_name})
177+
try:
178+
self.session = self.server.sessions.filter(session_name=session_name)[0]
179+
except IndexError:
180+
return False
178181
return True
179182

180183
def build(self, session=None, append=False):
@@ -203,12 +206,17 @@ def build(self, session=None, append=False):
203206
)
204207

205208
if self.server.has_session(self.sconf["session_name"]):
206-
self.session = self.server.find_where(
207-
{"session_name": self.sconf["session_name"]}
208-
)
209-
raise TmuxSessionExists(
210-
"Session name %s is already running." % self.sconf["session_name"]
211-
)
209+
try:
210+
self.session = self.server.sessions.filter(
211+
session_name=self.sconf["session_name"]
212+
)[0]
213+
214+
raise TmuxSessionExists(
215+
"Session name %s is already running."
216+
% self.sconf["session_name"]
217+
)
218+
except IndexError:
219+
pass
212220
else:
213221
new_session_kwargs = {}
214222
if "start_directory" in self.sconf:
@@ -226,7 +234,7 @@ def build(self, session=None, append=False):
226234
self.session = session
227235
self.server = session.server
228236

229-
self.server._list_sessions()
237+
self.server.sessions
230238
assert self.server.has_session(session.name)
231239
assert session.id
232240

@@ -378,16 +386,13 @@ def iter_create_windows(self, session, append=False):
378386
session.attached_window.kill_window()
379387

380388
assert isinstance(w, Window)
381-
session.server._update_windows()
382389
if "options" in wconf and isinstance(wconf["options"], dict):
383390
for key, val in wconf["options"].items():
384391
w.set_window_option(key, val)
385392

386393
if "focus" in wconf and wconf["focus"]:
387394
w.select_window()
388395

389-
session.server._update_windows()
390-
391396
yield w, wconf
392397

393398
def iter_create_panes(self, w, wconf):
@@ -421,7 +426,6 @@ def iter_create_panes(self, w, wconf):
421426
else:
422427

423428
def get_pane_start_directory():
424-
425429
if "start_directory" in pconf:
426430
return pconf["start_directory"]
427431
elif "start_directory" in wconf:
@@ -430,7 +434,6 @@ def get_pane_start_directory():
430434
return None
431435

432436
def get_pane_shell():
433-
434437
if "shell" in pconf:
435438
return pconf["shell"]
436439
elif "window_shell" in wconf:
@@ -486,7 +489,8 @@ def get_pane_shell():
486489
time.sleep(sleep_after)
487490

488491
if "focus" in pconf and pconf["focus"]:
489-
w.select_pane(p["pane_id"])
492+
assert p.pane_id is not None
493+
w.select_pane(p.pane_id)
490494

491495
w.server._update_panes()
492496

@@ -519,8 +523,8 @@ def find_current_attached_session(self):
519523
return next(
520524
(
521525
s
522-
for s in self.server.list_sessions()
523-
if s["session_id"] == current_active_pane["session_id"]
526+
for s in self.server.sessions
527+
if s.session_id == current_active_pane.session_id
524528
),
525529
None,
526530
)

src/tmuxp/workspace/freezer.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -53,36 +53,36 @@ def freeze(session):
5353
dict
5454
tmuxp compatible workspace
5555
"""
56-
sconf = {"session_name": session["session_name"], "windows": []}
56+
sconf = {"session_name": session.session_name, "windows": []}
5757

5858
for w in session.windows:
5959
wconf = {
6060
"options": w.show_window_options(),
6161
"window_name": w.name,
62-
"layout": w.layout,
62+
"layout": w.window_layout,
6363
"panes": [],
6464
}
65-
if w.get("window_active", "0") == "1":
65+
if getattr(w, "window_active", "0") == "1":
6666
wconf["focus"] = "true"
6767

6868
# If all panes have same path, set 'start_directory' instead
6969
# of using 'cd' shell commands.
7070
def pane_has_same_path(p):
71-
return w.panes[0].current_path == p.current_path
71+
return w.panes[0].pane_current_path == p.pane_current_path
7272

7373
if all(pane_has_same_path(p) for p in w.panes):
74-
wconf["start_directory"] = w.panes[0].current_path
74+
wconf["start_directory"] = w.panes[0].pane_current_path
7575

7676
for p in w.panes:
7777
pconf = {"shell_command": []}
7878

7979
if "start_directory" not in wconf:
80-
pconf["shell_command"].append("cd " + p.current_path)
80+
pconf["shell_command"].append("cd " + p.pane_current_path)
8181

82-
if p.get("pane_active", "0") == "1":
82+
if getattr(p, "pane_active", "0") == "1":
8383
pconf["focus"] = "true"
8484

85-
current_cmd = p.current_command
85+
current_cmd = p.pane_current_command
8686

8787
def filter_interpretters_and_shells():
8888
return current_cmd.startswith("-") or any(

0 commit comments

Comments
 (0)