Skip to content

Commit 47e11f8

Browse files
committed
refactor(config): Extract expand_command
1 parent 9535fc0 commit 47e11f8

File tree

1 file changed

+30
-30
lines changed

1 file changed

+30
-30
lines changed

tmuxp/config.py

+30-30
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
~~~~~~~~~~~~
55
66
"""
7-
import copy
87
import logging
98
import os
9+
from typing import Dict
1010

1111
from . import exc
1212

@@ -168,6 +168,32 @@ def inline(sconf):
168168
return sconf
169169

170170

171+
def expand_cmd(p: Dict) -> Dict:
172+
if isinstance(p, str):
173+
p = {"shell_command": [p]}
174+
elif not p:
175+
p = {"shell_command": []}
176+
177+
assert isinstance(p, dict)
178+
if "shell_command" in p:
179+
cmd = p["shell_command"]
180+
181+
if isinstance(p["shell_command"], str):
182+
cmd = [cmd]
183+
184+
if not cmd or any(a == cmd for a in [None, "blank", "pane"]):
185+
cmd = []
186+
187+
if isinstance(cmd, list) and len(cmd) == int(1):
188+
if any(a in cmd for a in [None, "blank", "pane"]):
189+
cmd = []
190+
191+
p["shell_command"] = cmd
192+
else:
193+
p["shell_command"] = []
194+
return p
195+
196+
171197
def expand(sconf, cwd=None, parent=None):
172198
"""Return config with shorthand and inline properties expanded.
173199
@@ -278,35 +304,9 @@ def expand(sconf, cwd=None, parent=None):
278304
sconf["windows"] = [expand(window, parent=sconf) for window in sconf["windows"]]
279305
elif "panes" in sconf:
280306
pane_configs = sconf["panes"]
281-
for pconf in pane_configs:
282-
p_index = pane_configs.index(pconf)
283-
p = copy.deepcopy(pconf)
284-
pconf = pane_configs[p_index] = {}
285-
286-
if isinstance(p, str):
287-
p = {"shell_command": [p]}
288-
elif not p:
289-
p = {"shell_command": []}
290-
291-
assert isinstance(p, dict)
292-
if "shell_command" in p:
293-
cmd = p["shell_command"]
294-
295-
if isinstance(p["shell_command"], str):
296-
cmd = [cmd]
297-
298-
if not cmd or any(a == cmd for a in [None, "blank", "pane"]):
299-
cmd = []
300-
301-
if isinstance(cmd, list) and len(cmd) == int(1):
302-
if any(a in cmd for a in [None, "blank", "pane"]):
303-
cmd = []
304-
305-
p["shell_command"] = cmd
306-
else:
307-
p["shell_command"] = []
308-
309-
pconf.update(p)
307+
for pane_idx, pconf in enumerate(pane_configs):
308+
pane_configs[pane_idx] = {}
309+
pane_configs[pane_idx].update(expand_cmd(pconf))
310310
sconf["panes"] = [expand(pane, parent=sconf) for pane in pane_configs]
311311

312312
return sconf

0 commit comments

Comments
 (0)