diff --git a/libtmux/server.py b/libtmux/server.py index 948e234f0..dee5f6d3d 100644 --- a/libtmux/server.py +++ b/libtmux/server.py @@ -409,6 +409,8 @@ def new_session(self, kill_session=False, attach=False, start_directory=None, + window_name=None, + shell=None, *args, **kwargs): """Return :class:`Session` from ``$ tmux new-session``. @@ -441,6 +443,18 @@ def new_session(self, new session is created. :type start_directory: str + :param window_name: window name:: + + $ tmux new-session -n + + :type session_name: str + :param shell: execute a command on starting the session. The + window will close when the command exits. + NOTE: When this command exits the window will close. This feature + is useful for long-running processes where the closing of the + window upon completion is desired. + :type window_command: str + :raises: :exc:`exc.BadSessionName` :rtype: :class:`Session` @@ -477,11 +491,17 @@ def new_session(self, if start_directory: tmux_args += ('-c', start_directory) + if window_name: + tmux_args += ('-n', window_name) + # tmux 2.6 gives unattached sessions a tiny default area # no need send in -x/-y if they're in a client already, though if has_gte_version('2.6') and 'TMUX' not in os.environ: tmux_args += ('-x', 800, '-y', 600) + if shell: + tmux_args += (shell, ) + proc = self.cmd( 'new-session', *tmux_args diff --git a/libtmux/window.py b/libtmux/window.py index b1fd77ba1..dd2933e73 100644 --- a/libtmux/window.py +++ b/libtmux/window.py @@ -351,7 +351,8 @@ def split_window( target=None, start_directory=None, attach=True, - vertical=True + vertical=True, + shell=None ): """Split window and return the created :class:`Pane`. @@ -379,6 +380,12 @@ def split_window( :type target: bool :param vertical: split vertically :type vertical: bool + :param shell: execute a command on splitting the window. The + pane will close when the command exits. + NOTE: When this command exits the pane will close. This feature + is useful for long-running processes where the closing of the + window upon completion is desired. + :type shell: str :rtype: :class:`Pane` @@ -414,6 +421,9 @@ def split_window( if not attach: tmux_args += ('-d',) + if shell: + tmux_args += (shell, ) + pane = self.cmd( 'split-window', *tmux_args diff --git a/tests/test_server.py b/tests/test_server.py index b5068eb88..65e8ac5fc 100644 --- a/tests/test_server.py +++ b/tests/test_server.py @@ -86,3 +86,14 @@ def test_new_session(server): mysession = server.new_session("test_new_session") assert mysession.get("session_name") == "test_new_session" assert server.has_session("test_new_session") + + +def test_new_session_shell(server): + """Server.new_session creates and returns valid session running with specified command""" + cmd = 'sleep 1m' + mysession = server.new_session("test_new_session", shell=cmd) + window = mysession.list_windows()[0] + pane = window.list_panes()[0] + assert mysession.get("session_name") == "test_new_session" + assert server.has_session("test_new_session") + assert pane.get('pane_start_command') == cmd diff --git a/tests/test_window.py b/tests/test_window.py index d4b059012..4a9b2ded1 100644 --- a/tests/test_window.py +++ b/tests/test_window.py @@ -108,6 +108,18 @@ def test_split_window(session): assert float(window.panes[0].height) <= ((float(window.width) + 1) / 2) +def test_split_window_shell(session): + """Window.split_window() splits window, returns new Pane, vertical. Test shell command""" + window_name = 'test split window' + cmd = 'sleep 1m' + window = session.new_window(window_name=window_name, attach=True) + pane = window.split_window(shell=cmd) + assert len(window.panes) == 2 + assert isinstance(pane, Pane) + assert float(window.panes[0].height) <= ((float(window.width) + 1) / 2) + assert pane.get('pane_start_command') == cmd + + def test_split_window_horizontal(session): """Window.split_window() splits window, returns new Pane, horizontal. """ window_name = 'test split window'