Skip to content

Commit ac1f464

Browse files
committed
Server,Session(feat): add start_server, lock_session
why: Complete server lifecycle and session locking for programmatic use. what: - Add Server.start_server() wrapping start-server (idempotent) - Add Session.lock_session() wrapping lock-session - Add tests for both
1 parent 774a0ac commit ac1f464

4 files changed

Lines changed: 38 additions & 0 deletions

File tree

src/libtmux/server.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,21 @@ def list_commands(self, *, command_name: str | None = None) -> list[str]:
673673

674674
return proc.stdout
675675

676+
def start_server(self) -> None:
677+
"""Start the tmux server if not already running.
678+
679+
Wraps ``$ tmux start-server``. Usually not needed since the server
680+
starts automatically on first session creation.
681+
682+
Examples
683+
--------
684+
>>> server.start_server()
685+
"""
686+
proc = self.cmd("start-server")
687+
688+
if proc.stderr:
689+
raise exc.LibTmuxException(proc.stderr)
690+
676691
def show_messages(self) -> list[str]:
677692
"""Show server message log via ``$ tmux show-messages``.
678693

src/libtmux/session.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,18 @@ def cmd(
245245
Commands (tmux-like)
246246
"""
247247

248+
def lock_session(self) -> None:
249+
"""Lock this session via ``$ tmux lock-session``.
250+
251+
Examples
252+
--------
253+
>>> session.lock_session()
254+
"""
255+
proc = self.cmd("lock-session")
256+
257+
if proc.stderr:
258+
raise exc.LibTmuxException(proc.stderr)
259+
248260
def detach_client(
249261
self,
250262
*,

tests/test_server.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,12 @@ def test_tmux_bin_invalid_path_raise_if_dead() -> None:
461461
s.raise_if_dead()
462462

463463

464+
def test_start_server(server: Server) -> None:
465+
"""Test Server.start_server() runs without error."""
466+
server.new_session(session_name="startsvr_test")
467+
server.start_server() # idempotent — already running
468+
469+
464470
def test_bind_unbind_key(server: Server) -> None:
465471
"""Test Server.bind_key() and unbind_key() cycle."""
466472
server.new_session(session_name="bind_test")

tests/test_session.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,11 @@ def patched_cmd(cmd_name: str, *args: t.Any, **kwargs: t.Any) -> tmux_cmd:
576576
test_session.attach()
577577

578578

579+
def test_lock_session(session: Session) -> None:
580+
"""Test Session.lock_session() runs without error."""
581+
session.lock_session()
582+
583+
579584
def test_detach_client(
580585
control_mode: t.Callable[..., t.Any],
581586
session: Session,

0 commit comments

Comments
 (0)