Skip to content

Add options for shell command #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions libtmux/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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``.
Expand Down Expand Up @@ -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 <window_name>

: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`

Expand Down Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion libtmux/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand Down Expand Up @@ -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`

Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 12 additions & 0 deletions tests/test_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down