Skip to content

Commit d2c254b

Browse files
committed
Merge pull request #145 from ikirudennis/new-window-command
Add new-window command functionality
2 parents e9a11f4 + e015b71 commit d2c254b

File tree

4 files changed

+62
-3
lines changed

4 files changed

+62
-3
lines changed

tmuxp/session.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ def new_window(self,
124124
window_name=None,
125125
start_directory=None,
126126
attach=True,
127-
window_index=''):
127+
window_index='',
128+
window_shell=None):
128129
"""Return :class:`Window` from ``$ tmux new-window``.
129130
130131
.. note::
@@ -144,6 +145,16 @@ def new_window(self,
144145
:type start_directory: string
145146
:param attach: make new window the current window after creating it,
146147
default True.
148+
:param window_index: create the new window at the given index position.
149+
Default is empty string which will create the window in the next
150+
available position.
151+
:type window_index: string
152+
:param window_shell: execute a command on starting the window. The
153+
window will close when the command exits.
154+
NOTE: When this command exits the window will close. This feature
155+
is useful for long-running processes where the closing of the
156+
window upon completion is desired.
157+
:type window_command: string
147158
:param type: bool
148159
:rtype: :class:`Window`
149160
@@ -177,6 +188,9 @@ def new_window(self,
177188
'-t%s:%s' % (self.get('session_id'), window_index),
178189
)
179190

191+
if window_shell:
192+
window_args += (window_shell,)
193+
180194
proc = self.cmd('new-window', *window_args)
181195

182196
if proc.stderr:

tmuxp/testsuite/workspacebuilder.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,39 @@ def test_window_options(self):
311311
window_count += 1
312312
w.select_layout(wconf['layout'])
313313

314+
def test_window_shell(self):
315+
yaml_config = """
316+
session_name: test window options
317+
start_directory: '~'
318+
windows:
319+
- layout: main-horizontal
320+
options:
321+
main-pane-height: 5
322+
panes:
323+
- pane
324+
- pane
325+
- pane
326+
window_name: editor
327+
window_shell: top
328+
"""
329+
s = self.session
330+
sconfig = kaptan.Kaptan(handler='yaml')
331+
sconfig = sconfig.import_config(yaml_config).get()
332+
sconfig = config.expand(sconfig)
333+
334+
builder = WorkspaceBuilder(sconf=sconfig)
335+
336+
for w, wconf in builder.iter_create_windows(s):
337+
if 'window_shell' in wconf:
338+
self.assertEqual(wconf['window_shell'], text_type('top'))
339+
for i in range(10):
340+
self.session.server._update_windows()
341+
if w['window_name'] != 'top':
342+
break
343+
time.sleep(.2)
344+
345+
self.assertNotEqual(w.get('window_name'), text_type('top'))
346+
314347

315348
class EnvironmentVariables(TmuxTestCase):
316349

@@ -337,7 +370,7 @@ def test_environment_variables(self):
337370

338371
self.assertEqual('BAR', self.session.show_environment('FOO'))
339372
self.assertEqual('/tmp', self.session.show_environment('PATH'))
340-
373+
341374
class WindowAutomaticRename(TmuxTestCase):
342375

343376
yaml_config = """

tmuxp/window.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,13 @@ def by(val, *args):
6262
return False
6363
return True
6464

65-
return list(filter(by, self.server._windows))[0]
65+
ret = list(filter(by, self.server._windows))
66+
# If a window_shell option was configured which results in
67+
# a short-lived process, the window id is @0. Use that instead of
68+
# self._window_id
69+
if len(ret) == 0 and self.server._windows[0]['window_id'] == '@0':
70+
ret = self.server._windows
71+
return ret[0]
6672

6773
def cmd(self, cmd, *args, **kwargs):
6874
"""Return :meth:`Server.cmd` defaulting ``target_window`` as target.

tmuxp/workspacebuilder.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,17 @@ def iter_create_windows(self, s):
199199
else:
200200
sd = None
201201

202+
if 'window_shell' in wconf:
203+
ws = wconf['window_shell']
204+
else:
205+
ws = None
206+
202207
w = s.new_window(
203208
window_name=window_name,
204209
start_directory=sd,
205210
attach=False, # do not move to the new window
206211
window_index=wconf.get('window_index', ''),
212+
window_shell=ws,
207213
)
208214

209215
if i == int(1) and w1: # if first window, use window 1

0 commit comments

Comments
 (0)