Skip to content

Commit 25bc68b

Browse files
committed
Merge pull request #108 from tony/envvars_expand
Expand environmental variables with curly brackets
2 parents 150fd9c + 19c41a1 commit 25bc68b

File tree

7 files changed

+159
-5
lines changed

7 files changed

+159
-5
lines changed

CHANGES

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ CURRENT
88
-------
99

1010
- [internal]: Renamed ``config.expandpath`` to ``config.expandshell``.
11+
- [internal] [tests]: compat 2.7/3.3 wrapper for ``EnvironmentVarGuard`` for
12+
testing.
13+
- [shell_command_before]: You can now use environment variables inside of
14+
``start_directory``, ``before_script``, ``shell_command_before``,
15+
``session_name`` and ``window_name``.
16+
- [docs] [examples]: add example for environmental variables,
17+
``examples/env-variables.json`` and ``examples/env-variables.yaml``.
1118

1219
0.8.1
1320
-----

doc/examples.rst

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,50 @@ JSON
162162
.. literalinclude:: ../examples/start-directory.json
163163
:language: json
164164

165+
Environment variables
166+
---------------------
167+
168+
tmuxp will replace environment variables wrapped in curly brackets
169+
for the following variables:
170+
171+
- ``start_directory``
172+
- ``before_script``
173+
- ``session_name``
174+
- ``window_name``
175+
- ``before_shell_command``
176+
177+
tmuxp replaces these variables before-hand with variables in the
178+
terminal ``tmuxp`` invokes in.
179+
180+
In this case of this example, assuming the username "user"::
181+
182+
$ MY_ENV_VAR=foo tmuxp load examples/env-variables.yaml
183+
184+
and your session name will be ``session - user (foo)``.
185+
186+
Shell variables in ``shell_command`` do not support this type of
187+
concatenation. ``shell_command`` and ``before_shell_command`` both
188+
support normal shell variables, since they are sent into panes
189+
automatically via ``send-key`` in ``tmux(1)``. See ``ls $PWD`` in
190+
example.
191+
192+
If you have a special case and would like to see behavior changed,
193+
please make a ticket on the `issue tracker`_.
194+
195+
.. _issue tracker: https://github.com/tony/tmuxp/issues
196+
197+
YAML
198+
~~~~
199+
200+
.. literalinclude:: ../examples/env-variables.yaml
201+
:language: yaml
202+
203+
JSON
204+
~~~~
205+
206+
.. literalinclude:: ../examples/env-variables.json
207+
:language: json
208+
165209
Focusing
166210
--------
167211

examples/env-variables.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"before_script": "{MY_ENV_VAR}/test3.sh",
3+
"windows": [
4+
{
5+
"panes": [
6+
{
7+
"shell_command": [
8+
"tail -F /var/log/syslog"
9+
]
10+
}
11+
],
12+
"start_directory": "/var/log",
13+
"window_name": "editor"
14+
},
15+
{
16+
"panes": [
17+
{
18+
"shell_command": [
19+
"htop",
20+
"ls $PWD"
21+
]
22+
}
23+
],
24+
"window_name": "logging for {USER}",
25+
"automatic_rename": true
26+
}
27+
],
28+
"shell_command_before": "echo {PWD}",
29+
"start_directory": "{PWD}/test",
30+
"session_name": "session - {USER} ({MY_ENV_VAR})"
31+
}

examples/env-variables.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
start_directory: "{PWD}/test"
2+
shell_command_before: "echo {PWD}"
3+
before_script: "{MY_ENV_VAR}/test3.sh"
4+
session_name: session - {USER} ({MY_ENV_VAR})
5+
windows:
6+
- window_name: editor
7+
panes:
8+
- shell_command:
9+
- tail -F /var/log/syslog
10+
start_directory: /var/log
11+
- window_name: logging for {USER}
12+
automatic_rename: true
13+
panes:
14+
- shell_command:
15+
- htop
16+
- ls $PWD

tmuxp/_compat.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
from string import lower as ascii_lowercase
3434
import urlparse
3535

36+
from test import test_support as support
37+
3638
exec('def reraise(tp, value, tb=None):\n raise tp, value, tb')
3739

3840
def implements_to_string(cls):
@@ -71,6 +73,8 @@ def console_to_str(s):
7173
import urllib.parse as urlparse
7274
from urllib.request import urlretrieve
7375

76+
from test import support
77+
7478
console_encoding = sys.__stdout__.encoding
7579

7680
implements_to_string = _identity

tmuxp/config.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,11 @@ def expand(sconf, cwd=None, parent=None):
185185
if not cwd:
186186
cwd = os.getcwd()
187187

188+
if 'session_name' in sconf:
189+
sconf['session_name'] = expandshell(sconf['session_name'])
190+
if 'window_name' in sconf:
191+
sconf['window_name'] = expandshell(sconf['window_name'])
192+
188193
# Any config section, session, window, pane that can contain the
189194
# 'shell_command' value
190195
if 'start_directory' in sconf:
@@ -202,12 +207,12 @@ def expand(sconf, cwd=None, parent=None):
202207
start_path = os.path.normpath(os.path.join(cwd, start_path))
203208
sconf['start_directory'] = start_path
204209

205-
206210
if 'before_script' in sconf:
207-
before_script = sconf['before_script']
208-
if any(before_script.startswith(a) for a in ['.', './']):
209-
before_script = os.path.normpath(os.path.join(cwd, before_script))
210-
sconf['before_script'] = before_script
211+
sconf['before_script'] = expandshell(sconf['before_script'])
212+
if any(sconf['before_script'].startswith(a) for a in ['.', './']):
213+
sconf['before_script'] = os.path.normpath(
214+
os.path.join(cwd, sconf['before_script'])
215+
)
211216

212217
if (
213218
'shell_command' in sconf and
@@ -221,6 +226,14 @@ def expand(sconf, cwd=None, parent=None):
221226
):
222227
sconf['shell_command_before'] = [sconf['shell_command_before']]
223228

229+
if (
230+
'shell_command_before' in sconf and
231+
isinstance(sconf['shell_command_before'], list)
232+
):
233+
sconf['shell_command_before'] = [
234+
expandshell(scmd) for scmd in sconf['shell_command_before']
235+
]
236+
224237
# recurse into window and pane config items
225238
if 'windows' in sconf:
226239
sconf['windows'] = [

tmuxp/testsuite/config.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818
import kaptan
1919

2020
from .. import config, exc
21+
from .._compat import support
2122
from .helpers import TestCase
2223

2324

25+
2426
logger = logging.getLogger(__name__)
2527
TMUXP_DIR = os.path.join(os.path.dirname(__file__), '.tmuxp')
2628
current_dir = os.path.abspath(os.path.dirname(__file__))
@@ -1061,6 +1063,42 @@ def test_no_window_name(self):
10611063
config.validate_schema(sconfig)
10621064

10631065

1066+
class ConfigExpandEnvironmentVariables(TestCase, unittest.TestCase):
1067+
def test_replaces_start_directory(self):
1068+
env_key = "TESTHEY92"
1069+
env_value = "HEYO1"
1070+
yaml_config = """
1071+
start_directory: {TEST_VAR}/test
1072+
shell_command_before: {TEST_VAR}/test2
1073+
before_script: {TEST_VAR}/test3
1074+
session_name: hi - {TEST_VAR}
1075+
windows:
1076+
- window_name: editor
1077+
panes:
1078+
- shell_command:
1079+
- tail -F /var/log/syslog
1080+
start_directory: /var/log
1081+
- window_name: logging @ {TEST_VAR}
1082+
automatic_rename: true
1083+
panes:
1084+
- shell_command:
1085+
- htop
1086+
""".format(
1087+
TEST_VAR="${%s}" % env_key
1088+
)
1089+
1090+
sconfig = kaptan.Kaptan(handler='yaml')
1091+
sconfig = sconfig.import_config(yaml_config).get()
1092+
1093+
with support.EnvironmentVarGuard() as env:
1094+
env.set(env_key, env_value)
1095+
sconfig = config.expand(sconfig)
1096+
self.assertEqual("%s/test" % env_value, sconfig['start_directory'])
1097+
self.assertIn("%s/test2" % env_value, sconfig['shell_command_before'])
1098+
self.assertEqual("%s/test3" % env_value, sconfig['before_script'])
1099+
self.assertEqual("hi - %s" % env_value, sconfig['session_name'])
1100+
self.assertEqual("logging @ %s" % env_value, sconfig['windows'][1]['window_name'])
1101+
10641102
def suite():
10651103
suite = unittest.TestSuite()
10661104
suite.addTest(unittest.makeSuite(ConfigBlankPanes))
@@ -1071,4 +1109,5 @@ def suite():
10711109
suite.addTest(unittest.makeSuite(ShellCommandBeforeTest))
10721110
suite.addTest(unittest.makeSuite(ShellCommandBeforeSession))
10731111
suite.addTest(unittest.makeSuite(TrickleRelativeStartDirectory))
1112+
suite.addTest(unittest.makeSuite(ConfigExpandEnvironmentVariables))
10741113
return suite

0 commit comments

Comments
 (0)