Skip to content

Commit bfc6023

Browse files
committed
start_directory example showing concatenation. Tests for it. Fix #11.
1 parent 88fde2a commit bfc6023

File tree

9 files changed

+146
-26
lines changed

9 files changed

+146
-26
lines changed

CHANGES

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ Here you can find the recent changes to tmuxp.
1111
- [internal] :class:`Server` support for ``-2`` with ``colors=256`` and
1212
``colors=8``.
1313
- [cli] ``$ tmuxp -2`` for forcing 256 colors and ``tmuxp -8`` for forcing 88.
14+
- [config] [tests] Concatenation with ``start_directory`` via
15+
:meth:`Config.trickle()` if window ``start_directory`` is alphanumeric /
16+
relative (doesn't start with ``/``). See :ref:`Examples` in *start directory*.
1417

1518
2013-10-31
1619
----------

examples/start_directory.json

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,35 @@
55
"pwd",
66
"echo 'it trickles down from session-level'"
77
],
8+
"window_name": "should be /var/"
9+
},
10+
{
11+
"panes": [
12+
"pwd",
13+
"echo 'window start_directory concatenates to session start_directory'",
14+
"echo 'if it is not absolute'"
15+
],
16+
"start_directory": "log",
817
"window_name": "should be /var/log"
918
},
1019
{
1120
"panes": [
1221
"pwd",
13-
"echo 'has precedence'"
22+
"echo 'has precedence'",
23+
"echo 'remember to quote ~ in YAML'"
24+
],
25+
"start_directory": "~",
26+
"window_name": "should be ~"
27+
},
28+
{
29+
"panes": [
30+
"pwd",
31+
"echo 'absolute paths also have precedence'"
1432
],
15-
"start_directory": "$HOME",
16-
"window_name": "should be $HOME"
33+
"start_directory": "/proc",
34+
"window_name": "should be /proc"
1735
}
1836
],
1937
"session_name": "start directory",
20-
"start_directory": "/var/log"
38+
"start_directory": "/var/"
2139
}

examples/start_directory.yaml

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
session_name: start directory
2-
start_directory: /var/log
2+
start_directory: /var/
33
windows:
4-
- window_name: should be /var/log
4+
- window_name: should be /var/
55
panes:
66
- pwd
77
- echo 'it trickles down from session-level'
8-
- window_name: should be $HOME
9-
start_directory: $HOME
8+
- window_name: should be /var/log
9+
start_directory: log
10+
panes:
11+
- pwd
12+
- echo 'window start_directory concatenates to session start_directory'
13+
- echo 'if it is not absolute'
14+
- window_name: should be ~
15+
start_directory: '~'
1016
panes:
1117
- pwd
1218
- echo 'has precedence'
19+
- echo 'remember to quote ~ in YAML'
20+
- window_name: should be /proc
21+
start_directory: /proc
22+
panes:
23+
- pwd
24+
- echo 'absolute paths also have precedence'

tmuxp/cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,6 @@ def load_workspace(config_file, args):
232232
t = Server(
233233
socket_name=args.socket_name,
234234
socket_path=args.socket_path,
235-
colors=args.colors
236235
)
237236

238237
try:
@@ -831,7 +830,8 @@ def get_parser():
831830
parser.add_argument(
832831
'-v', '--version', action='version',
833832
version='tmuxp %s' % __version__,
834-
help='Prints the tmuxp version')
833+
help='Prints the tmuxp version',
834+
)
835835

836836
return parser
837837

tmuxp/config.py

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -190,24 +190,38 @@ def trickle(config):
190190
``shell_command_before``.
191191
'''
192192

193-
session_start_directory = config['start_directory'] if 'start_directory' in config else None
193+
if 'start_directory' in config:
194+
session_start_directory = config['start_directory']
195+
else:
196+
session_start_directory = None
194197

195198
for windowconfig in config['windows']:
196-
if not 'start_directory' in windowconfig and session_start_directory:
197-
windowconfig['start_directory'] = session_start_directory
198-
for paneconfig in windowconfig['panes']:
199-
commands_before = config[
200-
'shell_command_before'] if 'shell_command_before' in config else []
201-
commands_before.extend(windowconfig[
202-
'shell_command_before']) if 'shell_command_before' in windowconfig else None
203-
commands_before.extend(paneconfig[
204-
'shell_command_before']) if 'shell_command_before' in paneconfig else None
205199

200+
# Prepend start_directory to relative window commands
201+
if session_start_directory:
202+
203+
if not 'start_directory' in windowconfig:
204+
windowconfig['start_directory'] = session_start_directory
205+
else:
206+
if not any(windowconfig['start_directory'].startswith(a) for a in ['~', '/']):
207+
windowconfig['start_directory'] = os.path.join(session_start_directory, windowconfig['start_directory'])
208+
209+
for paneconfig in windowconfig['panes']:
210+
commands_before = []
211+
212+
# Prepend shell_command_before to commands
213+
if 'shell_command_before' in config:
214+
commands_before = config['shell_command_before']
215+
if 'shell_command_before' in windowconfig:
216+
commands_before.extend(windowconfig['shell_command_before'])
217+
if 'shell_command_before' in paneconfig:
218+
commands_before.extend(paneconfig['shell_command_before'])
206219
if 'shell_command' not in paneconfig:
207220
paneconfig['shell_command'] = list()
208221

209-
commands_before.extend(paneconfig[
210-
'shell_command']) if paneconfig['shell_command'] else None
222+
if paneconfig['shell_command']:
223+
commands_before.extend(paneconfig['shell_command'])
224+
211225
paneconfig['shell_command'] = commands_before
212226

213227
return config

tmuxp/session.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ def by(val, *args):
5959
return list(filter(by, self.server._sessions))[0]
6060
except IndexError as e:
6161
logger.error(e)
62-
logger.error(self._session_name)
6362
logger.error(self.server._sessions)
6463

6564
def tmux(self, *args, **kwargs):

tmuxp/testsuite/test_config.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,80 @@ def test_shell_command_before(self):
583583
self.assertDictEqual(test_config, self.config_after)
584584

585585

586+
587+
class TrickleRelativeStartDirectory(unittest.TestCase):
588+
config_expanded = { # shell_command_before is string in some areas
589+
'session_name': 'sampleconfig',
590+
'start_directory': '/var',
591+
'windows': [
592+
{
593+
'window_name': 'editor',
594+
'start_directory': 'log',
595+
'panes': [
596+
{
597+
'shell_command': ['vim'],
598+
},
599+
{
600+
'shell_command': ['cowsay "hey"']
601+
},
602+
],
603+
'layout': 'main-verticle'
604+
},
605+
{
606+
'window_name': 'logging',
607+
'start_directory': '~',
608+
'panes': [
609+
{
610+
'shell_command': ['tail -F /var/log/syslog'],
611+
},
612+
{
613+
}
614+
]
615+
},
616+
]
617+
}
618+
619+
config_after = { # shell_command_before is string in some areas
620+
'session_name': 'sampleconfig',
621+
'start_directory': '/var',
622+
'windows': [
623+
{
624+
'window_name': 'editor',
625+
'start_directory': '/var/log',
626+
'panes': [
627+
{
628+
'shell_command': ['vim'],
629+
},
630+
{
631+
'shell_command': [
632+
'cowsay "hey"'
633+
]
634+
},
635+
],
636+
'layout': 'main-verticle'
637+
},
638+
{
639+
'start_directory': '~',
640+
'window_name': 'logging',
641+
'panes': [
642+
{
643+
'shell_command': ['tail -F /var/log/syslog'],
644+
},
645+
{
646+
'shell_command': []
647+
}
648+
]
649+
},
650+
]
651+
}
652+
653+
def test_shell_command_before(self):
654+
self.maxDiff = None
655+
656+
test_config = config.trickle(self.config_expanded)
657+
self.maxDiff = None
658+
self.assertDictEqual(test_config, self.config_after)
659+
586660
class ConfigConsistency(unittest.TestCase):
587661

588662
delete_this = '''

tmuxp/window.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ def move_window(self, destination):
278278
proc = self.tmux(
279279
'move-window',
280280
'-s%s:%s' % (self.get('session_id'), self.get('window_index')),
281-
'-t%s' % destination,
281+
'-t%s:%s' % (self.get('session_id'), destination),
282282
)
283283

284284
if proc.stderr:

tmuxp/workspacebuilder.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ def build(self, session=None):
127127
)
128128

129129
assert(self.sconf['session_name'] == session.get('session_name'))
130+
assert(len(self.sconf['session_name']) > 0)
130131

131132
self.session = session
132133

@@ -165,6 +166,7 @@ def iter_create_windows(self, s):
165166
if i == int(1): # if first window, use window 1
166167
w1 = s.attached_window()
167168
w1.move_window(99)
169+
pass
168170
w = s.new_window(
169171
window_name=window_name,
170172
start_directory=wconf['start_directory'] if 'start_directory' in wconf else None,
@@ -246,8 +248,6 @@ def freeze(session):
246248
'cd ' + p.get('pane_current_path'))
247249
pconf['shell_command'].append(p.get('pane_current_command'))
248250
wconf['panes'].append(pconf)
249-
# logger.error(p)
250-
# logger.error(dict(p))
251251

252252
sconf['windows'].append(wconf)
253253

0 commit comments

Comments
 (0)