Skip to content

Commit 71b4978

Browse files
committed
feat(new_session): add width/height parameters
1 parent 8f6f132 commit 71b4978

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

src/libtmux/server.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,8 @@ def new_session(
351351
start_directory: t.Optional[str] = None,
352352
window_name: t.Optional[str] = None,
353353
window_command: t.Optional[str] = None,
354+
window_width: t.Optional[int] = None,
355+
window_height: t.Optional[int] = None,
354356
*args: t.Any,
355357
**kwargs: t.Any,
356358
) -> Session:
@@ -388,11 +390,17 @@ def new_session(
388390
::
389391
390392
$ tmux new-session -n <window_name>
391-
window_command : str
393+
window_command : str, optional
392394
execute a command on starting the session. The window will close
393395
when the command exits. NOTE: When this command exits the window
394396
will close. This feature is useful for long-running processes
395397
where the closing of the window upon completion is desired.
398+
window_width : int, optional
399+
Force the specified width instead of the tmux default for a
400+
dettached session
401+
window_height : int, optional
402+
Force the specified height instead of the tmux default for a
403+
dettached session
396404
397405
Returns
398406
-------
@@ -455,10 +463,11 @@ def new_session(
455463
if window_name:
456464
tmux_args += ("-n", window_name)
457465

458-
# tmux 2.6 gives unattached sessions a tiny default area
459-
# no need send in -x/-y if they're in a client already, though
460-
if has_gte_version("2.6") and "TMUX" not in os.environ:
461-
tmux_args += ("-x", 800, "-y", 600)
466+
if window_width is not None:
467+
tmux_args += ("-x", window_width)
468+
469+
if window_height is not None:
470+
tmux_args += ("-y", window_height)
462471

463472
if window_command:
464473
tmux_args += (window_command,)

tests/test_server.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import pytest
55

6-
from libtmux.common import has_gte_version
6+
from libtmux.common import has_gte_version, has_version
77
from libtmux.server import Server
88
from libtmux.session import Session
99

@@ -127,6 +127,26 @@ def test_new_session_shell(server: Server) -> None:
127127
assert pane_start_command == cmd
128128

129129

130+
@pytest.mark.skipif(
131+
has_version("3.2"),
132+
reason="Wrong width returned with 3.2"
133+
)
134+
def test_new_session_width_height(server: Server) -> None:
135+
"""Server.new_session creates and returns valid session running with
136+
specified width /height"""
137+
cmd = "/usr/bin/env PS1='$ ' sh"
138+
mysession = server.new_session(
139+
"test_new_session_width_height",
140+
window_command=cmd,
141+
window_width=32,
142+
window_height=32,
143+
)
144+
window = mysession.windows[0]
145+
pane = window.panes[0]
146+
assert pane.display_message("#{window_width}", get_text=True)[0] == "32"
147+
assert pane.display_message("#{window_height}", get_text=True)[0] == "32"
148+
149+
130150
def test_no_server_sessions() -> None:
131151
server = Server(socket_name="test_attached_session_no_server")
132152
assert server.sessions == []

0 commit comments

Comments
 (0)