Skip to content

Commit 7f1d077

Browse files
authored
Merge pull request #312 from tony/client-attach-layout-hook
attempt at fixing #309 layout issues with tmux 2.6
2 parents acde0f0 + 6e08e03 commit 7f1d077

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

tmuxp/cli.py

+43-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import click
1515
import kaptan
1616
from click.exceptions import FileError
17-
from libtmux.common import has_minimum_version, which
17+
from libtmux.common import has_minimum_version, which, has_gte_version
1818
from libtmux.exc import TmuxCommandNotFound
1919
from libtmux.server import Server
2020

@@ -278,7 +278,49 @@ def reattach(session):
278278
sys.exit('Session created in detached state.')
279279

280280
if not detached:
281+
282+
if has_gte_version('2.6'):
283+
# tmuxp issue: https://github.com/tony/tmuxp/issues/309
284+
# tmux issue: https://github.com/tmux/tmux/issues/1106
285+
#
286+
# tmux now requires that the window be viewed with the client
287+
# before select-layout adjustments can be meaningful
288+
#
289+
# To handle this, let's create a temporary hook for this
290+
# session to iterage and run select-layout on all windows
291+
# after client attaches.
292+
cmd = [
293+
'set-hook',
294+
'-t', builder.session.id,
295+
'client-attached'
296+
]
297+
hook_cmd = []
298+
for window in builder.session.windows:
299+
# unfortunately, select-layout won't work unless
300+
# we've literally selected the window at least once
301+
# with the client
302+
hook_cmd.append('selectw -t {}'.format(window.id))
303+
# edit: removed -t, or else it won't respect main-pane-w/h
304+
hook_cmd.append('selectl'.format(window.id))
305+
hook_cmd.append('selectw -p'.format(window.id))
306+
307+
# unset the hook immediately after executing
308+
hook_cmd.append(
309+
'set-hook -u -t {target_session} client-attached'.format(
310+
target_session=builder.session.id
311+
)
312+
)
313+
314+
# join the hook's commands with semicolons
315+
hook_cmd = '{}'.format('; '.join(hook_cmd))
316+
317+
# append the hook command
318+
cmd.append(hook_cmd)
319+
320+
# create the hook
321+
builder.session.cmd(*cmd)
281322
builder.session.attach_session()
323+
282324
except exc.TmuxpException as e:
283325
import traceback
284326

0 commit comments

Comments
 (0)