Skip to content

Commit 0a52a69

Browse files
committed
refactor!: Move freezing code to workspace.freezer
1 parent 7cf5850 commit 0a52a69

File tree

5 files changed

+68
-69
lines changed

5 files changed

+68
-69
lines changed

docs/api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ If you need an internal API stabilized please [file an issue](https://github.com
127127
## Workspace Freezer
128128

129129
```{eval-rst}
130-
.. automethod:: tmuxp.workspace.builder.freeze
130+
.. automethod:: tmuxp.workspace.freezer.freeze
131131
```
132132

133133
```{eval-rst}

src/tmuxp/cli/freeze.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from .. import util
1212
from ..workspace import config
13-
from ..workspace.builder import freeze
13+
from ..workspace.freezer import freeze
1414
from .utils import get_config_dir, prompt, prompt_choices, prompt_yes_no
1515

1616
if t.TYPE_CHECKING:

src/tmuxp/workspace/builder.py

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -494,69 +494,3 @@ def find_current_attached_session(self):
494494

495495
def first_window_pass(self, i, session, append):
496496
return len(session.windows) == 1 and i == 1 and not append
497-
498-
499-
def freeze(session):
500-
"""
501-
Freeze live tmux session and Return session config :py:obj:`dict`.
502-
503-
Parameters
504-
----------
505-
session : :class:`libtmux.Session`
506-
session object
507-
508-
Returns
509-
-------
510-
dict
511-
tmuxp compatible workspace config
512-
"""
513-
sconf = {"session_name": session["session_name"], "windows": []}
514-
515-
for w in session.windows:
516-
wconf = {
517-
"options": w.show_window_options(),
518-
"window_name": w.name,
519-
"layout": w.layout,
520-
"panes": [],
521-
}
522-
if w.get("window_active", "0") == "1":
523-
wconf["focus"] = "true"
524-
525-
# If all panes have same path, set 'start_directory' instead
526-
# of using 'cd' shell commands.
527-
def pane_has_same_path(p):
528-
return w.panes[0].current_path == p.current_path
529-
530-
if all(pane_has_same_path(p) for p in w.panes):
531-
wconf["start_directory"] = w.panes[0].current_path
532-
533-
for p in w.panes:
534-
pconf = {"shell_command": []}
535-
536-
if "start_directory" not in wconf:
537-
pconf["shell_command"].append("cd " + p.current_path)
538-
539-
if p.get("pane_active", "0") == "1":
540-
pconf["focus"] = "true"
541-
542-
current_cmd = p.current_command
543-
544-
def filter_interpretters_and_shells():
545-
return current_cmd.startswith("-") or any(
546-
current_cmd.endswith(cmd) for cmd in ["python", "ruby", "node"]
547-
)
548-
549-
if filter_interpretters_and_shells():
550-
current_cmd = None
551-
552-
if current_cmd:
553-
pconf["shell_command"].append(current_cmd)
554-
else:
555-
if not len(pconf["shell_command"]):
556-
pconf = "pane"
557-
558-
wconf["panes"].append(pconf)
559-
560-
sconf["windows"].append(wconf)
561-
562-
return sconf

src/tmuxp/workspace/freezer.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
def freeze(session):
2+
"""
3+
Freeze live tmux session and Return session config :py:obj:`dict`.
4+
5+
Parameters
6+
----------
7+
session : :class:`libtmux.Session`
8+
session object
9+
10+
Returns
11+
-------
12+
dict
13+
tmuxp compatible workspace config
14+
"""
15+
sconf = {"session_name": session["session_name"], "windows": []}
16+
17+
for w in session.windows:
18+
wconf = {
19+
"options": w.show_window_options(),
20+
"window_name": w.name,
21+
"layout": w.layout,
22+
"panes": [],
23+
}
24+
if w.get("window_active", "0") == "1":
25+
wconf["focus"] = "true"
26+
27+
# If all panes have same path, set 'start_directory' instead
28+
# of using 'cd' shell commands.
29+
def pane_has_same_path(p):
30+
return w.panes[0].current_path == p.current_path
31+
32+
if all(pane_has_same_path(p) for p in w.panes):
33+
wconf["start_directory"] = w.panes[0].current_path
34+
35+
for p in w.panes:
36+
pconf = {"shell_command": []}
37+
38+
if "start_directory" not in wconf:
39+
pconf["shell_command"].append("cd " + p.current_path)
40+
41+
if p.get("pane_active", "0") == "1":
42+
pconf["focus"] = "true"
43+
44+
current_cmd = p.current_command
45+
46+
def filter_interpretters_and_shells():
47+
return current_cmd.startswith("-") or any(
48+
current_cmd.endswith(cmd) for cmd in ["python", "ruby", "node"]
49+
)
50+
51+
if filter_interpretters_and_shells():
52+
current_cmd = None
53+
54+
if current_cmd:
55+
pconf["shell_command"].append(current_cmd)
56+
else:
57+
if not len(pconf["shell_command"]):
58+
pconf = "pane"
59+
60+
wconf["panes"].append(pconf)
61+
62+
sconf["windows"].append(wconf)
63+
64+
return sconf

tests/workspace/test_freezer.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
from tmuxp.config_reader import ConfigReader
55
from tmuxp.workspace import config
6-
from tmuxp.workspace.builder import WorkspaceBuilder, freeze
6+
from tmuxp.workspace.builder import WorkspaceBuilder
7+
from tmuxp.workspace.freezer import freeze
78

89
from ..fixtures import utils as test_utils
910

0 commit comments

Comments
 (0)