Skip to content

feat(new_session): add width/height parameters #469

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ $ pip install --user --upgrade --pre libtmux

<!-- Maintainers and contributors: Insert change notes for the next release above -->

### What's new

- Server.new_session: Accept `x` and `y`, thanks
@rockandska (#469)

## libtmux 0.19.1 (2022-01-07)

### Fixes
Expand Down
24 changes: 19 additions & 5 deletions src/libtmux/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
session_check_name,
)

if t.TYPE_CHECKING:
from typing_extensions import Literal, TypeAlias

DashLiteral: TypeAlias = Literal["-"]

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -388,11 +395,17 @@ def new_session(
::

$ tmux new-session -n <window_name>
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
-------
Expand Down Expand Up @@ -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,)
Expand Down
19 changes: 18 additions & 1 deletion tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 == []
Expand Down