Skip to content

Commit 1624abe

Browse files
committed
attempt at fixing #309 layout issues with tmux 2.6
even after setting -x and -y with new-session, select-layout won't take effect. However, it is necessary in 2.6 to get pane creation in detached mode to work. the only way found for tmux select-layout to take effect and prior behavior to have viewed the window you are running "select-layout" for in the client. a solution has been devised to connect the client to the session, then run select-layout on each window in the session. However, it's still not enough to load the session, it seems the window must be selected by the client at least once for the select-layout to take effect. so a hook against the session is created for the session that selects each window, runs select layout at the -t target for the window id. and after that, the hook is neatly unset when that's finished.
1 parent acde0f0 commit 1624abe

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

tmuxp/cli.py

+42-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,48 @@ 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+
hook_cmd.append('selectl -t {}'.format(window.id))
304+
hook_cmd.append('selectw -p'.format(window.id))
305+
306+
# unset the hook immediately after executing
307+
hook_cmd.append(
308+
'set-hook -u -t {target_session} client-attached'.format(
309+
target_session=builder.session.id
310+
)
311+
)
312+
313+
# join the hook's commands with semicolons
314+
hook_cmd = '{}'.format('; '.join(hook_cmd))
315+
316+
# append the hook command
317+
cmd.append(hook_cmd)
318+
319+
# create the hook
320+
builder.session.cmd(*cmd)
281321
builder.session.attach_session()
322+
282323
except exc.TmuxpException as e:
283324
import traceback
284325

0 commit comments

Comments
 (0)