Skip to content

Commit 654ad41

Browse files
grimpytony
authored andcommitted
Add options for shell options
Make it possible to pass window_name and shell command to server.new_session Make it possible to pass shell command to window.split_window Signed-off-by: Jo De Boeck <[email protected]>
1 parent 3eb1dfb commit 654ad41

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

libtmux/server.py

+20
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,8 @@ def new_session(self,
396396
kill_session=False,
397397
attach=False,
398398
start_directory=None,
399+
window_name=None,
400+
shell=None,
399401
*args,
400402
**kwargs):
401403
"""Return :class:`Session` from ``$ tmux new-session``.
@@ -428,6 +430,18 @@ def new_session(self,
428430
new session is created.
429431
:type start_directory: str
430432
433+
:param window_name: window name::
434+
435+
$ tmux new-session -n <window_name>
436+
437+
:type session_name: str
438+
:param shell: execute a command on starting the session. The
439+
window will close when the command exits.
440+
NOTE: When this command exits the window will close. This feature
441+
is useful for long-running processes where the closing of the
442+
window upon completion is desired.
443+
:type window_command: str
444+
431445
:raises: :exc:`exc.BadSessionName`
432446
:rtype: :class:`Session`
433447
@@ -464,11 +478,17 @@ def new_session(self,
464478
if start_directory:
465479
tmux_args += ('-c', start_directory)
466480

481+
if window_name:
482+
tmux_args += ('-n', window_name)
483+
467484
# tmux 2.6 gives unattached sessions a tiny default area
468485
# no need send in -x/-y if they're in a client already, though
469486
if has_gte_version('2.6') and 'TMUX' not in os.environ:
470487
tmux_args += ('-x', 800, '-y', 600)
471488

489+
if shell:
490+
tmux_args += (shell, )
491+
472492
proc = self.cmd(
473493
'new-session',
474494
*tmux_args

libtmux/window.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,8 @@ def split_window(
351351
target=None,
352352
start_directory=None,
353353
attach=True,
354-
vertical=True
354+
vertical=True,
355+
shell=None
355356
):
356357
"""Split window and return the created :class:`Pane`.
357358
@@ -379,6 +380,12 @@ def split_window(
379380
:type target: bool
380381
:param vertical: split vertically
381382
:type vertical: bool
383+
:param shell: execute a command on splitting the window. The
384+
pane will close when the command exits.
385+
NOTE: When this command exits the pane will close. This feature
386+
is useful for long-running processes where the closing of the
387+
window upon completion is desired.
388+
:type shell: str
382389
383390
:rtype: :class:`Pane`
384391
@@ -414,6 +421,9 @@ def split_window(
414421
if not attach:
415422
tmux_args += ('-d',)
416423

424+
if shell:
425+
tmux_args += (shell, )
426+
417427
pane = self.cmd(
418428
'split-window',
419429
*tmux_args

tests/test_server.py

+11
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,14 @@ def test_new_session(server):
8686
mysession = server.new_session("test_new_session")
8787
assert mysession.get("session_name") == "test_new_session"
8888
assert server.has_session("test_new_session")
89+
90+
91+
def test_new_session_shell(server):
92+
"""Server.new_session creates and returns valid session running with specified command"""
93+
cmd = 'sleep 1m'
94+
mysession = server.new_session("test_new_session", shell=cmd)
95+
window = mysession.list_windows()[0]
96+
pane = window.list_panes()[0]
97+
assert mysession.get("session_name") == "test_new_session"
98+
assert server.has_session("test_new_session")
99+
assert pane.get('pane_start_command') == cmd

tests/test_window.py

+12
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,18 @@ def test_split_window(session):
108108
assert float(window.panes[0].height) <= ((float(window.width) + 1) / 2)
109109

110110

111+
def test_split_window_shell(session):
112+
"""Window.split_window() splits window, returns new Pane, vertical. Test shell command"""
113+
window_name = 'test split window'
114+
cmd = 'sleep 1m'
115+
window = session.new_window(window_name=window_name, attach=True)
116+
pane = window.split_window(shell=cmd)
117+
assert len(window.panes) == 2
118+
assert isinstance(pane, Pane)
119+
assert float(window.panes[0].height) <= ((float(window.width) + 1) / 2)
120+
assert pane.get('pane_start_command') == cmd
121+
122+
111123
def test_split_window_horizontal(session):
112124
"""Window.split_window() splits window, returns new Pane, horizontal. """
113125
window_name = 'test split window'

0 commit comments

Comments
 (0)