Skip to content

Commit 039a472

Browse files
committed
feat(Session.new_window): set up environment
This allows to set up a custom environment when creating a new window.
1 parent 07fa5a6 commit 039a472

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/libtmux/session.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ def new_window(
202202
attach: bool = True,
203203
window_index: str = "",
204204
window_shell: t.Optional[str] = None,
205+
environment: t.Optional[dict[str, str]] = None,
205206
) -> Window:
206207
"""
207208
Return :class:`Window` from ``$ tmux new-window``.
@@ -259,6 +260,10 @@ def new_window(
259260
% (self.id, window_index),
260261
)
261262

263+
if environment:
264+
for k, v in environment.items():
265+
window_args += ("-e%s=%s" % (k, v),)
266+
262267
if window_shell:
263268
window_args += (window_shell,)
264269

tests/test_session.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,16 @@ def test_cmd_inserts_sesion_id(session: Session) -> None:
257257
assert "-t" in cmd.cmd
258258
assert current_session_id in cmd.cmd
259259
assert cmd.cmd[-1] == last_arg
260+
261+
262+
def test_new_window_with_environment(session: Session) -> None:
263+
window = session.new_window(
264+
attach=True,
265+
window_name="window_with_environment",
266+
window_shell="/usr/bin/env PS1='$ ' bash --norc --noprofile",
267+
environment={"ENV_VAR": "window"},
268+
)
269+
pane = window.attached_pane
270+
assert pane is not None
271+
pane.send_keys("echo $ENV_VAR")
272+
assert pane.capture_pane() == ["$ echo $ENV_VAR", "window", "$"]

0 commit comments

Comments
 (0)