From 71b4978b216bed690c5d6a509fe989516fb2d66b Mon Sep 17 00:00:00 2001 From: rockandska Date: Fri, 13 Jan 2023 12:07:07 +0100 Subject: [PATCH 1/4] feat(new_session): add width/height parameters --- src/libtmux/server.py | 19 ++++++++++++++----- tests/test_server.py | 22 +++++++++++++++++++++- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/libtmux/server.py b/src/libtmux/server.py index b1e275605..f53aed19f 100644 --- a/src/libtmux/server.py +++ b/src/libtmux/server.py @@ -351,6 +351,8 @@ def new_session( start_directory: t.Optional[str] = None, window_name: t.Optional[str] = None, window_command: t.Optional[str] = None, + window_width: t.Optional[int] = None, + window_height: t.Optional[int] = None, *args: t.Any, **kwargs: t.Any, ) -> Session: @@ -388,11 +390,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. + window_width : int, optional + Force the specified width instead of the tmux default for a + dettached session + window_height : int, optional + Force the specified height instead of the tmux default for a + dettached session Returns ------- @@ -455,10 +463,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 window_width is not None: + tmux_args += ("-x", window_width) + + if window_height is not None: + tmux_args += ("-y", window_height) if window_command: tmux_args += (window_command,) diff --git a/tests/test_server.py b/tests/test_server.py index 9742f9ad7..35674a178 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,26 @@ 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, + window_width=32, + window_height=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 == [] From b92637fb50fbdc6cad9ee4a747208d7703b20a9e Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Sun, 15 Jan 2023 07:03:19 -0600 Subject: [PATCH 2/4] docs(CHANGES): Note window_height and window_width --- CHANGES | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGES b/CHANGES index 710c016b6..b17b66b8f 100644 --- a/CHANGES +++ b/CHANGES @@ -14,6 +14,9 @@ $ pip install --user --upgrade --pre libtmux +- Server.new_session: Accept `window_height` and `window_width`, thanks + @rockandska (#469) + ## libtmux 0.19.1 (2022-01-07) ### Fixes From d5afed97befe1095c5c88d833ced2ebb8a96b4e9 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Sun, 15 Jan 2023 07:13:09 -0600 Subject: [PATCH 3/4] refactor(Server.new_sesion): Use x and y for param names --- CHANGES | 4 +++- src/libtmux/server.py | 16 ++++++++-------- tests/test_server.py | 9 +++------ 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/CHANGES b/CHANGES index b17b66b8f..5dde2fac7 100644 --- a/CHANGES +++ b/CHANGES @@ -14,7 +14,9 @@ $ pip install --user --upgrade --pre libtmux -- Server.new_session: Accept `window_height` and `window_width`, thanks +### What's new + +- Server.new_session: Accept `x` and `y`, thanks @rockandska (#469) ## libtmux 0.19.1 (2022-01-07) diff --git a/src/libtmux/server.py b/src/libtmux/server.py index f53aed19f..a663b99bb 100644 --- a/src/libtmux/server.py +++ b/src/libtmux/server.py @@ -351,8 +351,8 @@ def new_session( start_directory: t.Optional[str] = None, window_name: t.Optional[str] = None, window_command: t.Optional[str] = None, - window_width: t.Optional[int] = None, - window_height: t.Optional[int] = None, + x: t.Optional[int] = None, + y: t.Optional[int] = None, *args: t.Any, **kwargs: t.Any, ) -> Session: @@ -395,10 +395,10 @@ def new_session( 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. - window_width : int, optional + x : int, optional Force the specified width instead of the tmux default for a dettached session - window_height : int, optional + y : int, optional Force the specified height instead of the tmux default for a dettached session @@ -463,11 +463,11 @@ def new_session( if window_name: tmux_args += ("-n", window_name) - if window_width is not None: - tmux_args += ("-x", window_width) + if x is not None: + tmux_args += ("-x", x) - if window_height is not None: - tmux_args += ("-y", window_height) + 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 35674a178..711964e27 100644 --- a/tests/test_server.py +++ b/tests/test_server.py @@ -127,10 +127,7 @@ 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" -) +@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""" @@ -138,8 +135,8 @@ def test_new_session_width_height(server: Server) -> None: mysession = server.new_session( "test_new_session_width_height", window_command=cmd, - window_width=32, - window_height=32, + x=32, + y=32, ) window = mysession.windows[0] pane = window.panes[0] From 465bd0719dc050c08c1c26fb1d172c868a827cdf Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Sun, 15 Jan 2023 07:21:04 -0600 Subject: [PATCH 4/4] feat(Server.new_session): Support '-' --- src/libtmux/server.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/libtmux/server.py b/src/libtmux/server.py index a663b99bb..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,8 +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[int] = None, - y: t.Optional[int] = None, + x: t.Optional[t.Union[int, "DashLiteral"]] = None, + y: t.Optional[t.Union[int, "DashLiteral"]] = None, *args: t.Any, **kwargs: t.Any, ) -> Session: @@ -395,10 +400,10 @@ def new_session( 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, optional + x : [int, str], optional Force the specified width instead of the tmux default for a dettached session - y : int, optional + y : [int, str], optional Force the specified height instead of the tmux default for a dettached session