|
7 | 7 |
|
8 | 8 | libtmux.server |
9 | 9 | ~~~~~~~~~~~~~~ |
| 10 | +
|
| 11 | +Examples |
| 12 | +-------- |
| 13 | +>>> from libtmux import Server |
| 14 | +>>> server = Server() # Create a new server instance |
| 15 | +>>> server.is_alive() # Check if tmux server is running |
| 16 | +True |
| 17 | +>>> # Clean up any existing test session first |
| 18 | +>>> if server.has_session("test_session"): |
| 19 | +... server.kill_session("test_session") |
| 20 | +>>> new_session = server.new_session(session_name="test_session") |
| 21 | +>>> new_session.name |
| 22 | +'test_session' |
| 23 | +>>> server.has_session("test_session") |
| 24 | +True |
| 25 | +>>> server.kill_session("test_session") # Clean up |
| 26 | +Server(socket_path=/tmp/tmux-.../default) |
10 | 27 | """ |
11 | 28 |
|
12 | 29 | from __future__ import annotations |
@@ -309,25 +326,33 @@ def attached_sessions(self) -> list[Session]: |
309 | 326 | return self.sessions.filter(session_attached__noeq="1") |
310 | 327 |
|
311 | 328 | def has_session(self, target_session: str, exact: bool = True) -> bool: |
312 | | - """Check if a session with the given name (or pattern) exists. |
| 329 | + """Return True if session exists. |
313 | 330 |
|
314 | 331 | Parameters |
315 | 332 | ---------- |
316 | | - target_session |
317 | | - The session name or pattern to check. |
318 | | - exact |
319 | | - Whether to require an exact match (tmux 2.1+). If True, prepends |
320 | | - ``=`` to the session name for an exact match. |
321 | | -
|
322 | | - Raises |
323 | | - ------ |
324 | | - exc.BadSessionName |
325 | | - If the `target_session` is invalid. |
| 333 | + target_session : str |
| 334 | + Target session name to check |
| 335 | + exact : bool, optional |
| 336 | + If True, match the name exactly. Otherwise, match as a pattern. |
326 | 337 |
|
327 | | - Returns |
328 | | - ------- |
329 | | - bool |
330 | | - True if the session exists, False otherwise. |
| 338 | + Examples |
| 339 | + -------- |
| 340 | + >>> server = Server() |
| 341 | + >>> # Clean up any existing test session |
| 342 | + >>> if server.has_session("test_session"): |
| 343 | + ... server.kill_session("test_session") |
| 344 | + >>> server.new_session(session_name="test_session") |
| 345 | + Session($... test_session) |
| 346 | + >>> server.has_session("test_session") |
| 347 | + True |
| 348 | + >>> server.has_session("nonexistent") |
| 349 | + False |
| 350 | + >>> server.has_session("test_session", exact=True) # Exact match |
| 351 | + True |
| 352 | + >>> server.has_session("test*", exact=False) # Pattern match |
| 353 | + True |
| 354 | + >>> server.kill_session("test_session") # Clean up |
| 355 | + Server(socket_path=/tmp/tmux-.../default) |
331 | 356 | """ |
332 | 357 | session_check_name(target_session) |
333 | 358 |
|
@@ -359,23 +384,26 @@ def kill(self) -> None: |
359 | 384 | self.cmd("kill-server") |
360 | 385 |
|
361 | 386 | def kill_session(self, target_session: str | int) -> Server: |
362 | | - """Kill a specific session on this server. |
| 387 | + """Kill a session by name. |
363 | 388 |
|
364 | 389 | Parameters |
365 | 390 | ---------- |
366 | | - target_session |
367 | | - The session name or ID to kill. Note that tmux uses fnmatch(3), |
368 | | - so 'asdf' might also match 'asdfasd'. |
| 391 | + target_session : str or int |
| 392 | + Name of the session or session ID to kill |
369 | 393 |
|
370 | | - Returns |
371 | | - ------- |
372 | | - Server |
373 | | - This server object (for chaining). |
374 | | -
|
375 | | - Raises |
376 | | - ------ |
377 | | - exc.LibTmuxException |
378 | | - If tmux reports an error (stderr output). |
| 394 | + Examples |
| 395 | + -------- |
| 396 | + >>> server = Server() |
| 397 | + >>> # Clean up any existing session first |
| 398 | + >>> if server.has_session("temp"): |
| 399 | + ... server.kill_session("temp") |
| 400 | + >>> session = server.new_session(session_name="temp") |
| 401 | + >>> server.has_session("temp") |
| 402 | + True |
| 403 | + >>> server.kill_session("temp") |
| 404 | + Server(socket_path=/tmp/tmux-.../default) |
| 405 | + >>> server.has_session("temp") |
| 406 | + False |
379 | 407 | """ |
380 | 408 | proc = self.cmd("kill-session", target=target_session) |
381 | 409 | if proc.stderr: |
@@ -437,58 +465,63 @@ def new_session( |
437 | 465 | *args: t.Any, |
438 | 466 | **kwargs: t.Any, |
439 | 467 | ) -> Session: |
440 | | - """Create a new tmux session and return the associated :class:`Session`. |
441 | | -
|
442 | | - Uses ``-P -F#{session_id}`` to capture the session ID in the output. |
| 468 | + """Create a new session. |
443 | 469 |
|
444 | 470 | Parameters |
445 | 471 | ---------- |
446 | 472 | session_name : str, optional |
447 | | - The name to assign to the new session (``-s`` flag). |
| 473 | + Name of the session |
448 | 474 | kill_session : bool, optional |
449 | | - If True, kills any existing session with the same name. |
| 475 | + Kill session if it exists |
450 | 476 | attach : bool, optional |
451 | | - If True, creates the new session in the foreground (no ``-d`` flag). |
| 477 | + Attach to session after creating it |
452 | 478 | start_directory : str, optional |
453 | | - Working directory for the new session (``-c`` flag). |
| 479 | + Working directory for the session |
454 | 480 | window_name : str, optional |
455 | | - If given, creates the session's first window with this name (``-n``). |
| 481 | + Name of the initial window |
456 | 482 | window_command : str, optional |
457 | | - A shell command to run immediately in the new window; the window |
458 | | - closes when the command exits. |
| 483 | + Command to run in the initial window |
459 | 484 | x : int or "-", optional |
460 | | - Forces the specified width for the new session if detached (``-x``). |
| 485 | + Width of new window |
461 | 486 | y : int or "-", optional |
462 | | - Forces the specified height for the new session if detached (``-y``). |
463 | | - environment : dict of str to str, optional |
464 | | - Environment variables to set for the session (tmux 3.2+). |
465 | | -
|
466 | | - Returns |
467 | | - ------- |
468 | | - Session |
469 | | - The newly created :class:`Session`. |
470 | | -
|
471 | | - Raises |
472 | | - ------ |
473 | | - exc.TmuxSessionExists |
474 | | - If a session with the given `session_name` already exists and |
475 | | - `kill_session` is False. |
476 | | - exc.BadSessionName |
477 | | - If `session_name` is invalid. |
478 | | - exc.LibTmuxException |
479 | | - If tmux reports an error (stderr output). |
| 487 | + Height of new window |
| 488 | + environment : dict, optional |
| 489 | + Dictionary of environment variables to set |
480 | 490 |
|
481 | 491 | Examples |
482 | 492 | -------- |
483 | | - Sessions can be created without a session name: |
484 | | -
|
485 | | - >>> server.new_session() |
486 | | - Session($2 2) |
487 | | -
|
488 | | - With a session name: |
| 493 | + >>> server = Server() |
| 494 | + >>> # Clean up any existing sessions first |
| 495 | + >>> for name in ["basic", "custom", "env_test"]: |
| 496 | + ... if server.has_session(name): |
| 497 | + ... server.kill_session(name) |
| 498 | + >>> # Create a basic session |
| 499 | + >>> session1 = server.new_session(session_name="basic") |
| 500 | + >>> session1.name |
| 501 | + 'basic' |
| 502 | +
|
| 503 | + >>> # Create session with custom window name |
| 504 | + >>> session2 = server.new_session( |
| 505 | + ... session_name="custom", |
| 506 | + ... window_name="editor" |
| 507 | + ... ) |
| 508 | + >>> session2.windows[0].name |
| 509 | + 'editor' |
489 | 510 |
|
490 | | - >>> server.new_session(session_name='my session') |
491 | | - Session($3 my session) |
| 511 | + >>> # Create session with environment variables |
| 512 | + >>> session3 = server.new_session( |
| 513 | + ... session_name="env_test", |
| 514 | + ... environment={"TEST_VAR": "test_value"} |
| 515 | + ... ) |
| 516 | + >>> session3.name |
| 517 | + 'env_test' |
| 518 | +
|
| 519 | + >>> # Clean up |
| 520 | + >>> for name in ["basic", "custom", "env_test"]: |
| 521 | + ... server.kill_session(name) |
| 522 | + Server(socket_path=/tmp/tmux-.../default) |
| 523 | + Server(socket_path=/tmp/tmux-.../default) |
| 524 | + Server(socket_path=/tmp/tmux-.../default) |
492 | 525 | """ |
493 | 526 | if session_name is not None: |
494 | 527 | session_check_name(session_name) |
@@ -548,11 +581,27 @@ def new_session( |
548 | 581 |
|
549 | 582 | @property |
550 | 583 | def sessions(self) -> QueryList[Session]: |
551 | | - """Return a :class:`QueryList` of all :class:`Session` objects in this server. |
| 584 | + """Return list of sessions. |
552 | 585 |
|
553 | | - Access advanced filtering and retrieval with: |
554 | | - :meth:`.sessions.get() <libtmux._internal.query_list.QueryList.get()>` and |
555 | | - :meth:`.sessions.filter() <libtmux._internal.query_list.QueryList.filter()>` |
| 586 | + Examples |
| 587 | + -------- |
| 588 | + >>> server = Server() |
| 589 | + >>> # Clean up any existing test sessions first |
| 590 | + >>> for name in ["test1", "test2"]: |
| 591 | + ... if server.has_session(name): |
| 592 | + ... server.kill_session(name) |
| 593 | + >>> # Create some test sessions |
| 594 | + >>> session1 = server.new_session(session_name="test1") |
| 595 | + >>> session2 = server.new_session(session_name="test2") |
| 596 | + >>> len(server.sessions) >= 2 # May have other sessions |
| 597 | + True |
| 598 | + >>> sorted([s.name for s in server.sessions if s.name in ["test1", "test2"]]) |
| 599 | + ['test1', 'test2'] |
| 600 | + >>> # Clean up |
| 601 | + >>> server.kill_session("test1") |
| 602 | + Server(socket_path=/tmp/tmux-.../default) |
| 603 | + >>> server.kill_session("test2") |
| 604 | + Server(socket_path=/tmp/tmux-.../default) |
556 | 605 | """ |
557 | 606 | sessions: list[Session] = [] |
558 | 607 | with contextlib.suppress(Exception): |
|
0 commit comments