diff --git a/CHANGES b/CHANGES index 710c016b6..5dde2fac7 100644 --- a/CHANGES +++ b/CHANGES @@ -14,6 +14,11 @@ $ pip install --user --upgrade --pre libtmux +### What's new + +- Server.new_session: Accept `x` and `y`, thanks + @rockandska (#469) + ## libtmux 0.19.1 (2022-01-07) ### Fixes diff --git a/src/libtmux/server.py b/src/libtmux/server.py index b1e275605..a281d202b 100644 --- a/src/libtmux/server.py +++ b/src/libtmux/server.py @@ -29,6 +29,11 @@ session_check_name, ) +if t.TYPE_CHECKING: + from typing_extensions import Literal, TypeAlias + + DashLiteral: TypeAlias = Literal["-"] + logger = logging.getLogger(__name__) @@ -351,6 +356,8 @@ def new_session( start_directory: t.Optional[str] = None, window_name: t.Optional[str] = None, window_command: t.Optional[str] = None, + x: t.Optional[t.Union[int, "DashLiteral"]] = None, + y: t.Optional[t.Union[int, "DashLiteral"]] = None, *args: t.Any, **kwargs: t.Any, ) -> Session: @@ -388,11 +395,17 @@ def new_session( :: $ tmux new-session -n - window_command : str + window_command : str, optional execute a command on starting the session. The window will close when the command exits. NOTE: When this command exits the window will close. This feature is useful for long-running processes where the closing of the window upon completion is desired. + x : [int, str], optional + Force the specified width instead of the tmux default for a + dettached session + y : [int, str], optional + Force the specified height instead of the tmux default for a + dettached session Returns ------- @@ -455,10 +468,11 @@ def new_session( if window_name: tmux_args += ("-n", window_name) - # tmux 2.6 gives unattached sessions a tiny default area - # no need send in -x/-y if they're in a client already, though - if has_gte_version("2.6") and "TMUX" not in os.environ: - tmux_args += ("-x", 800, "-y", 600) + if x is not None: + tmux_args += ("-x", x) + + if y is not None: + tmux_args += ("-y", y) if window_command: tmux_args += (window_command,) diff --git a/tests/test_server.py b/tests/test_server.py index 9742f9ad7..711964e27 100644 --- a/tests/test_server.py +++ b/tests/test_server.py @@ -3,7 +3,7 @@ import pytest -from libtmux.common import has_gte_version +from libtmux.common import has_gte_version, has_version from libtmux.server import Server from libtmux.session import Session @@ -127,6 +127,23 @@ def test_new_session_shell(server: Server) -> None: assert pane_start_command == cmd +@pytest.mark.skipif(has_version("3.2"), reason="Wrong width returned with 3.2") +def test_new_session_width_height(server: Server) -> None: + """Server.new_session creates and returns valid session running with + specified width /height""" + cmd = "/usr/bin/env PS1='$ ' sh" + mysession = server.new_session( + "test_new_session_width_height", + window_command=cmd, + x=32, + y=32, + ) + window = mysession.windows[0] + pane = window.panes[0] + assert pane.display_message("#{window_width}", get_text=True)[0] == "32" + assert pane.display_message("#{window_height}", get_text=True)[0] == "32" + + def test_no_server_sessions() -> None: server = Server(socket_name="test_attached_session_no_server") assert server.sessions == []