Skip to content

Commit ec70b75

Browse files
committed
#33 rewrite pane iteration on config.expand().
1 parent 3be7216 commit ec70b75

File tree

3 files changed

+138
-37
lines changed

3 files changed

+138
-37
lines changed

tmuxp/config.py

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
from __future__ import absolute_import, division, print_function, with_statement
1313
import os
14+
import copy
1415
import logging
1516
from . import exc
1617
from .util import basestring
@@ -198,50 +199,38 @@ def expand(sconf, cwd=None):
198199
]
199200
elif 'panes' in sconf:
200201

201-
for p in sconf['panes']:
202-
p_index = sconf['panes'].index(p)
202+
for pconf in sconf['panes']:
203+
p_index = sconf['panes'].index(pconf)
204+
p = copy.deepcopy(pconf)
205+
pconf = sconf['panes'][p_index] = {}
203206

204-
if not isinstance(p, dict) and not isinstance(p, list):
205-
p = sconf['panes'][p_index] = {
207+
if isinstance(p, basestring):
208+
p = {
206209
'shell_command': [p]
207210
}
208-
209-
if isinstance(p, dict) and not len(p):
210-
p = sconf['panes'][p_index] = {
211+
elif not p:
212+
p = {
211213
'shell_command': []
212214
}
213215

214-
if isinstance(p, basestring):
215-
216-
p = sconf['panes'][p_index] = {
217-
'shell_command': [p]
218-
}
219-
216+
assert isinstance(p, dict)
217+
assert 'shell_command' in p
220218
if 'shell_command' in p:
219+
cmd = p['shell_command']
221220

222221
if isinstance(p['shell_command'], basestring):
223-
p = sconf['panes'][p_index] = {
224-
'shell_command': [p['shell_command']]
225-
}
226-
227-
if p['shell_command'] is None:
228-
p = sconf['panes'][p_index] = {
229-
'shell_command': []
230-
}
231-
elif (
232-
isinstance(p['shell_command'], list) and (
233-
len(p['shell_command']) == int(1) and (
234-
any(
235-
a in p['shell_command']
236-
for a in [None, 'blank', 'pane']
237-
) or p['shell_command'][0] is None
238-
)
239-
)
240-
):
241-
p = sconf['panes'][p_index] = {
242-
'shell_command': []
243-
}
222+
cmd = [cmd]
223+
224+
if not cmd or any(a == cmd for a in [None, 'blank', 'pane']):
225+
cmd = []
226+
227+
if isinstance(cmd, list) and len(cmd) == int(1):
228+
if any(a in cmd for a in [None, 'blank', 'pane']):
229+
cmd = []
230+
231+
p['shell_command'] = cmd
244232

233+
pconf.update(p)
245234
sconf['panes'] = [expand(pane) for pane in sconf['panes']]
246235

247236
return sconf

tmuxp/testsuite/test_config.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,91 @@ def test_config(self):
277277
test_config = config.expand(self.before_config)
278278
self.assertDictEqual(test_config, self.after_config)
279279

280+
def test_no_window_name(self):
281+
"""Expand shell commands from string to list."""
282+
283+
unexpanded_yaml = """
284+
session_name: sampleconfig
285+
start_directory: '~'
286+
windows:
287+
- window_name: focused window
288+
layout: main-horizontal
289+
focus: true
290+
panes:
291+
- shell_command:
292+
- cd ~
293+
- shell_command:
294+
- cd /usr
295+
focus: true
296+
- shell_command:
297+
- cd ~
298+
- echo "moo"
299+
- top
300+
- window_name: window 2
301+
panes:
302+
- shell_command:
303+
- top
304+
focus: true
305+
- shell_command:
306+
- echo "hey"
307+
- shell_command:
308+
- echo "moo"
309+
- window_name: window 3
310+
panes:
311+
- shell_command: cd /
312+
focus: true
313+
- pane
314+
- pane
315+
"""
316+
317+
expanded_yaml = """
318+
session_name: sampleconfig
319+
start_directory: '~'
320+
windows:
321+
- window_name: focused window
322+
layout: main-horizontal
323+
focus: true
324+
panes:
325+
- shell_command:
326+
- cd ~
327+
- shell_command:
328+
- cd /usr
329+
focus: true
330+
- shell_command:
331+
- cd ~
332+
- echo "moo"
333+
- top
334+
- window_name: window 2
335+
panes:
336+
- shell_command:
337+
- top
338+
focus: true
339+
- shell_command:
340+
- echo "hey"
341+
- shell_command:
342+
- echo "moo"
343+
- window_name: window 3
344+
panes:
345+
- shell_command:
346+
- cd /
347+
focus: true
348+
- shell_command: []
349+
- shell_command: []
350+
"""
351+
352+
self.maxDiff = None
353+
354+
unexpanded_dict = kaptan.Kaptan(handler='yaml'). \
355+
import_config(unexpanded_yaml).get()
356+
357+
expanded_dict = kaptan.Kaptan(handler='yaml'). \
358+
import_config(expanded_yaml).get()
359+
360+
self.assertDictEqual(
361+
config.expand(unexpanded_dict),
362+
expanded_dict
363+
)
364+
280365

281366
class InlineTest(TestCase):
282367

tmuxp/testsuite/test_workspacebuilder.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,18 +133,25 @@ class FocusAndPaneIndexTest(TmuxTestCase):
133133
panes:
134134
- shell_command:
135135
- top
136-
1ocus: true
136+
focus: true
137137
- shell_command:
138138
- echo "hey"
139139
- shell_command:
140140
- echo "moo"
141-
141+
- window_name: window 3
142+
panes:
143+
- shell_command: cd /
144+
focus: true
145+
- pane
146+
- pane
142147
"""
143148

144-
def test_split_windows(self):
149+
def test_focus_pane_index(self):
145150
s = self.session
146151
sconfig = kaptan.Kaptan(handler='yaml')
147152
sconfig = sconfig.import_config(self.yaml_config).get()
153+
sconfig = config.expand(sconfig)
154+
sconfig = config.trickle(sconfig)
148155

149156
builder = WorkspaceBuilder(sconf=sconfig)
150157

@@ -186,6 +193,26 @@ def test_split_windows(self):
186193

187194
self.assertEqual(p.get('pane_current_path'), pane_path)
188195

196+
proc = self.session.tmux('show-option', '-gv', 'base-index')
197+
base_index = int(proc.stdout[0])
198+
self.session.server._update_windows()
199+
for w in self.session._windows:
200+
logger.error(w['window_index'])
201+
202+
window3 = self.session.findWhere({'window_index': str(base_index + 2)})
203+
self.assertIsInstance(window3, Window)
204+
205+
p = None
206+
pane_path = '/'
207+
for i in range(60):
208+
p = window3.attached_pane()
209+
p.server._update_panes()
210+
if p.get('pane_current_path') == pane_path:
211+
break
212+
time.sleep(.2)
213+
214+
self.assertEqual(p.get('pane_current_path'), pane_path)
215+
189216

190217
class WindowOptions(TmuxTestCase):
191218

0 commit comments

Comments
 (0)