Skip to content

Commit 4244049

Browse files
committed
feat(Window.split_window): set up environment
This allows to set up a custom environment when creating a new pane.
1 parent 9878f60 commit 4244049

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

src/libtmux/window.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import shlex
1010
import typing as t
1111

12-
from libtmux.common import tmux_cmd
12+
from libtmux.common import has_gte_version, tmux_cmd
1313
from libtmux.pane import Pane
1414

1515
from . import exc, formats
@@ -442,6 +442,7 @@ def split_window(
442442
vertical: bool = True,
443443
shell: t.Optional[str] = None,
444444
percent: t.Optional[int] = None,
445+
environment: t.Optional[t.Dict[str, str]] = None,
445446
) -> Pane:
446447
"""
447448
Split window and return the created :class:`Pane`.
@@ -520,6 +521,15 @@ def split_window(
520521
if not attach:
521522
tmux_args += ("-d",)
522523

524+
if environment:
525+
if has_gte_version("3.0"):
526+
for k, v in environment.items():
527+
tmux_args += (f"-e{k}={v}",)
528+
else:
529+
logger.warning(
530+
"Cannot set up environment as tmux 3.0 or newer is required."
531+
)
532+
523533
if shell:
524534
tmux_args += (shell,)
525535

tests/test_window.py

+55
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Test for libtmux Window object."""
22
import logging
3+
import shutil
4+
import time
35
import typing as t
46

57
import pytest
@@ -310,3 +312,56 @@ def test_empty_window_name(session: Session) -> None:
310312
"#{==:#{session_name}," + session.name + "}",
311313
)
312314
assert "''" in cmd.stdout
315+
316+
317+
@pytest.mark.skipif(
318+
has_lt_version("3.0"),
319+
reason="needs -e flag for split-window which was introduced in 3.0",
320+
)
321+
@pytest.mark.parametrize(
322+
"environment",
323+
[
324+
{"ENV_VAR": "pane"},
325+
{"ENV_VAR_1": "pane_1", "ENV_VAR_2": "pane_2"},
326+
],
327+
)
328+
def test_split_window_with_environment(
329+
session: Session,
330+
environment: t.Dict[str, str],
331+
) -> None:
332+
env = shutil.which("env")
333+
assert env is not None, "Cannot find usable `env` in Path."
334+
335+
window = session.new_window(window_name="split_window_with_environment")
336+
pane = window.split_window(
337+
shell=f"{env} PS1='$ ' sh",
338+
environment=environment,
339+
)
340+
assert pane is not None
341+
# wait a bit for the prompt to be ready as the test gets flaky otherwise
342+
time.sleep(0.05)
343+
for k, v in environment.items():
344+
pane.send_keys(f"echo ${k}")
345+
assert pane.capture_pane()[-2] == v
346+
347+
348+
@pytest.mark.skipif(
349+
has_gte_version("3.0"),
350+
reason="3.0 has the -e flag on split-window",
351+
)
352+
def test_split_window_with_environment_logs_warning_for_old_tmux(
353+
session: Session,
354+
caplog: pytest.LogCaptureFixture,
355+
) -> None:
356+
env = shutil.which("env")
357+
assert env is not None, "Cannot find usable `env` in Path."
358+
359+
window = session.new_window(window_name="split_window_with_environment")
360+
window.split_window(
361+
shell=f"{env} PS1='$ ' sh",
362+
environment={"ENV_VAR": "pane"},
363+
)
364+
365+
assert any(
366+
"Cannot set up environment" in record.msg for record in caplog.records
367+
), "Warning missing"

0 commit comments

Comments
 (0)