feat(builder): allow environments for windows and panes · tmux-python/tmuxp@210333a · GitHub
Skip to content

Commit 210333a

Browse files
committed
feat(builder): allow environments for windows and panes
This fixes #832.
1 parent b7e7a79 commit 210333a

7 files changed

Lines changed: 197 additions & 13 deletions

File tree

‎CHANGES‎

Lines changed: 22 additions & 1 deletion

‎docs/configuration/examples.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,9 @@ please make a ticket on the [issue tracker][issue tracker].
262262

263263
## Environment variables
264264

265-
tmuxp will set session environment variables.
265+
tmuxp will set session, window and pane environment variables. Note that
266+
setting environment variables for windows and panes requires tmuxp 1.19 or
267+
newer.
266268

267269
````{tab} YAML
268270

‎examples/session-environment.json‎

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,32 @@
22
"environment": {
33
"EDITOR": "/usr/bin/vim",
44
"DJANGO_SETTINGS_MODULE": "my_app.settings.local",
5-
"SERVER_PORT": "8009",
6-
},
5+
"SERVER_PORT": "8009"
6+
},
77
"windows": [
88
{
99
"panes": [
10-
"./manage.py runserver 0.0.0.0:${SERVER_PORT}"
11-
],
10+
"./manage.py runserver 0.0.0.0:${SERVER_PORT}"
11+
],
1212
"window_name": "Django project"
13-
},
14-
],
13+
},
14+
{
15+
"environment": {
16+
"DJANGO_SETTINGS_MODULE": "my_app.settings.local",
17+
"SERVER_PORT": "8010"
18+
},
19+
"panes": [
20+
"./manage.py runserver 0.0.0.0:${SERVER_PORT}",
21+
{
22+
"environment": {
23+
"DJANGO_SETTINGS_MODULE": "my_app.settings.local-testing",
24+
"SERVER_PORT": "8011"
25+
},
26+
"shell_command": "./manage.py runserver 0.0.0.0:${SERVER_PORT}"
27+
}
28+
],
29+
"window_name": "Another Django project"
30+
}
31+
],
1532
"session_name": "Environment variables test"
16-
}
33+
}

‎examples/session-environment.yaml‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,13 @@ windows:
77
- window_name: Django project
88
panes:
99
- ./manage.py runserver 0.0.0.0:${SERVER_PORT}
10+
- window_name: Another Django project
11+
environment:
12+
DJANGO_SETTINGS_MODULE: my_app.settings.local
13+
SERVER_PORT: "8010"
14+
panes:
15+
- ./manage.py runserver 0.0.0.0:${SERVER_PORT}
16+
- environment:
17+
DJANGO_SETTINGS_MODULE: my_app.settings.local-testing
18+
SERVER_PORT: "8011"
19+
shell_command: ./manage.py runserver 0.0.0.0:${SERVER_PORT}

‎src/tmuxp/workspace/builder.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import logging
88
import time
99

10+
from libtmux.common import has_lt_version
1011
from libtmux.exc import TmuxSessionExists
1112
from libtmux.pane import Pane
1213
from libtmux.server import Server
@@ -346,12 +347,31 @@ def iter_create_windows(self, session, append=False):
346347
except (KeyError, IndexError):
347348
pass
348349

350+
environment = panes[0].get("environment", wconf.get("environment"))
351+
if environment and has_lt_version("3.0"):
352+
# Falling back to use the environment of the first pane for the window
353+
# creation is nice but yields misleading error messages.
354+
pane_env = panes[0].get("environment")
355+
win_env = wconf.get("environment")
356+
if pane_env and win_env:
357+
target = "panes and windows"
358+
elif pane_env:
359+
target = "panes"
360+
else:
361+
target = "windows"
362+
logging.warning(
363+
f"Cannot set environment for new {target}. "
364+
"You need tmux 3.0 or newer for this."
365+
)
366+
environment = None
367+
349368
w = session.new_window(
350369
window_name=window_name,
351370
start_directory=sd,
352371
attach=False, # do not move to the new window
353372
window_index=wconf.get("window_index", ""),
354373
window_shell=ws,
374+
environment=environment,
355375
)
356376

357377
if is_first_window_pass: # if first window, use window 1
@@ -418,11 +438,24 @@ def get_pane_shell():
418438
else:
419439
return None
420440

441+
environment = pconf.get("environment", wconf.get("environment"))
442+
if environment and has_lt_version("3.0"):
443+
# Just issue a warning when the environment comes from the pane
444+
# configuration as a warning for the window was already issued when
445+
# the window was created.
446+
if pconf.get("environment"):
447+
logging.warning(
448+
"Cannot set environment for new panes. "
449+
"You need tmux 3.0 or newer for this."
450+
)
451+
environment = None
452+
421453
p = w.split_window(
422454
attach=True,
423455
start_directory=get_pane_start_directory(),
424456
shell=get_pane_shell(),
425457
target=p.id,
458+
environment=environment,
426459
)
427460

428461
assert isinstance(p, Pane)
Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,32 @@
11
session_name: test env vars
2-
start_directory: '~'
2+
start_directory: "~"
33
environment:
4-
FOO: BAR
4+
FOO: SESSION
55
PATH: /tmp
66
windows:
7-
- window_name: editor
7+
- window_name: no_overrides
88
panes:
99
- pane
10+
- window_name: window_overrides
11+
environment:
12+
FOO: WINDOW
13+
panes:
14+
- pane
15+
- window_name: pane_overrides
16+
panes:
17+
- environment:
18+
FOO: PANE
19+
- window_name: both_overrides
20+
environment:
21+
FOO: WINDOW
22+
panes:
23+
- pane
24+
- environment:
25+
FOO: PANE
26+
# This test case it just needed for warnings issued in old versions of tmux.
27+
- window_name: both_overrides_on_first_pane
28+
environment:
29+
FOO: WINDOW
30+
panes:
31+
- environment:
32+
FOO: PANE

‎tests/workspace/test_builder.py‎

Lines changed: 79 additions & 1 deletion

0 commit comments

Comments
 (0)