7
7
8
8
libtmux.server
9
9
~~~~~~~~~~~~~~
10
+
11
+ Examples
12
+ --------
13
+ >>> server.is_alive() # Check if tmux server is running
14
+ True
15
+ >>> # Clean up any existing test session first
16
+ >>> if server.has_session("test_session"):
17
+ ... server.kill_session("test_session")
18
+ >>> new_session = server.new_session(session_name="test_session")
19
+ >>> new_session.name
20
+ 'test_session'
21
+ >>> server.has_session("test_session")
22
+ True
23
+ >>> server.kill_session("test_session") # Clean up
24
+ Server(socket_name=libtmux_test...)
10
25
"""
11
26
12
27
from __future__ import annotations
@@ -361,25 +376,33 @@ def attached_sessions(self) -> list[Session]:
361
376
return self .sessions .filter (session_attached__noeq = "1" )
362
377
363
378
def has_session (self , target_session : str , exact : bool = True ) -> bool :
364
- """Check if a session with the given name (or pattern) exists.
379
+ """Return True if session exists.
365
380
366
381
Parameters
367
382
----------
368
- target_session
369
- The session name or pattern to check.
370
- exact
371
- Whether to require an exact match (tmux 2.1+). If True, prepends
372
- ``=`` to the session name for an exact match.
373
-
374
- Raises
375
- ------
376
- exc.BadSessionName
377
- If the `target_session` is invalid.
383
+ target_session : str
384
+ Target session name to check
385
+ exact : bool, optional
386
+ If True, match the name exactly. Otherwise, match as a pattern.
378
387
379
- Returns
380
- -------
381
- bool
382
- True if the session exists, False otherwise.
388
+ Examples
389
+ --------
390
+ >>> # Clean up any existing test session
391
+ >>> if server.has_session("test_session"):
392
+ ... server.kill_session("test_session")
393
+ >>> server.new_session(session_name="test_session")
394
+ Session($... test_session)
395
+ >>> server.has_session("test_session")
396
+ True
397
+ >>> server.has_session("nonexistent")
398
+ False
399
+ >>> server.has_session("test_session", exact=True) # Exact match
400
+ True
401
+ >>> # Pattern matching (using tmux's pattern matching)
402
+ >>> server.has_session("test_sess*", exact=False) # Pattern match
403
+ True
404
+ >>> server.kill_session("test_session") # Clean up
405
+ Server(socket_name=libtmux_test...)
383
406
"""
384
407
session_check_name (target_session )
385
408
@@ -396,38 +419,38 @@ def kill(self) -> None:
396
419
397
420
Examples
398
421
--------
399
- >>> svr = Server(socket_name="testing")
400
- >>> svr.new_session()
422
+ >>> # Create a new server for testing kill()
423
+ >>> test_server = Server(socket_name="testing")
424
+ >>> test_server.new_session()
401
425
Session(...)
402
-
403
- >>> svr.is_alive()
426
+ >>> test_server.is_alive()
404
427
True
405
-
406
- >>> svr.kill()
407
-
408
- >>> svr.is_alive()
428
+ >>> test_server.kill()
429
+ >>> test_server.is_alive()
409
430
False
410
431
"""
411
432
self .cmd ("kill-server" )
412
433
413
434
def kill_session (self , target_session : str | int ) -> Server :
414
- """Kill a specific session on this server .
435
+ """Kill a session by name .
415
436
416
437
Parameters
417
438
----------
418
- target_session
419
- The session name or ID to kill. Note that tmux uses fnmatch(3),
420
- so 'asdf' might also match 'asdfasd'.
421
-
422
- Returns
423
- -------
424
- Server
425
- This server object (for chaining).
439
+ target_session : str or int
440
+ Name of the session or session ID to kill
426
441
427
- Raises
428
- ------
429
- exc.LibTmuxException
430
- If tmux reports an error (stderr output).
442
+ Examples
443
+ --------
444
+ >>> # Clean up any existing session first
445
+ >>> if server.has_session("temp"):
446
+ ... server.kill_session("temp")
447
+ >>> session = server.new_session(session_name="temp")
448
+ >>> server.has_session("temp")
449
+ True
450
+ >>> server.kill_session("temp")
451
+ Server(socket_name=libtmux_test...)
452
+ >>> server.has_session("temp")
453
+ False
431
454
"""
432
455
proc = self .cmd ("kill-session" , target = target_session )
433
456
if proc .stderr :
@@ -442,6 +465,22 @@ def switch_client(self, target_session: str) -> None:
442
465
target_session
443
466
The name or pattern of the target session.
444
467
468
+ Examples
469
+ --------
470
+ >>> # Create two test sessions
471
+ >>> for name in ["session1", "session2"]:
472
+ ... if server.has_session(name):
473
+ ... server.kill_session(name)
474
+ >>> session1 = server.new_session(session_name="session1")
475
+ >>> session2 = server.new_session(session_name="session2")
476
+ >>> # Note: switch_client() requires an interactive terminal
477
+ >>> # so we can't demonstrate it in doctests
478
+ >>> # Clean up
479
+ >>> server.kill_session("session1")
480
+ Server(socket_name=libtmux_test...)
481
+ >>> server.kill_session("session2")
482
+ Server(socket_name=libtmux_test...)
483
+
445
484
Raises
446
485
------
447
486
exc.BadSessionName
@@ -463,6 +502,18 @@ def attach_session(self, target_session: str | None = None) -> None:
463
502
The name or pattern of the target session. If None, attaches to
464
503
the most recently used session.
465
504
505
+ Examples
506
+ --------
507
+ >>> # Create a test session
508
+ >>> if server.has_session("test_attach"):
509
+ ... server.kill_session("test_attach")
510
+ >>> session = server.new_session(session_name="test_attach")
511
+ >>> # Note: attach_session() requires an interactive terminal
512
+ >>> # so we can't demonstrate it in doctests
513
+ >>> # Clean up
514
+ >>> server.kill_session("test_attach")
515
+ Server(socket_name=libtmux_test...)
516
+
466
517
Raises
467
518
------
468
519
exc.BadSessionName
@@ -489,58 +540,62 @@ def new_session(
489
540
* args : t .Any ,
490
541
** kwargs : t .Any ,
491
542
) -> Session :
492
- """Create a new tmux session and return the associated :class:`Session`.
493
-
494
- Uses ``-P -F#{session_id}`` to capture the session ID in the output.
543
+ """Create a new session.
495
544
496
545
Parameters
497
546
----------
498
547
session_name : str, optional
499
- The name to assign to the new session (``-s`` flag).
548
+ Name of the session
500
549
kill_session : bool, optional
501
- If True, kills any existing session with the same name.
550
+ Kill session if it exists
502
551
attach : bool, optional
503
- If True, creates the new session in the foreground (no ``-d`` flag).
552
+ Attach to session after creating it
504
553
start_directory : str, optional
505
- Working directory for the new session (``-c`` flag).
554
+ Working directory for the session
506
555
window_name : str, optional
507
- If given, creates the session's first window with this name (``-n``).
556
+ Name of the initial window
508
557
window_command : str, optional
509
- A shell command to run immediately in the new window; the window
510
- closes when the command exits.
558
+ Command to run in the initial window
511
559
x : int or "-", optional
512
- Forces the specified width for the new session if detached (``-x``).
560
+ Width of new window
513
561
y : int or "-", optional
514
- Forces the specified height for the new session if detached (``-y``).
515
- environment : dict of str to str, optional
516
- Environment variables to set for the session (tmux 3.2+).
517
-
518
- Returns
519
- -------
520
- Session
521
- The newly created :class:`Session`.
522
-
523
- Raises
524
- ------
525
- exc.TmuxSessionExists
526
- If a session with the given `session_name` already exists and
527
- `kill_session` is False.
528
- exc.BadSessionName
529
- If `session_name` is invalid.
530
- exc.LibTmuxException
531
- If tmux reports an error (stderr output).
562
+ Height of new window
563
+ environment : dict, optional
564
+ Dictionary of environment variables to set
532
565
533
566
Examples
534
567
--------
535
- Sessions can be created without a session name:
536
-
537
- >>> server.new_session()
538
- Session($2 2)
539
-
540
- With a session name:
568
+ >>> # Clean up any existing sessions first
569
+ >>> for name in ["basic", "custom", "env_test"]:
570
+ ... if server.has_session(name):
571
+ ... server.kill_session(name)
572
+ >>> # Create a basic session
573
+ >>> session1 = server.new_session(session_name="basic")
574
+ >>> session1.name
575
+ 'basic'
576
+
577
+ >>> # Create session with custom window name
578
+ >>> session2 = server.new_session(
579
+ ... session_name="custom",
580
+ ... window_name="editor"
581
+ ... )
582
+ >>> session2.windows[0].name
583
+ 'editor'
541
584
542
- >>> server.new_session(session_name='my session')
543
- Session($3 my session)
585
+ >>> # Create session with environment variables
586
+ >>> session3 = server.new_session(
587
+ ... session_name="env_test",
588
+ ... environment={"TEST_VAR": "test_value"}
589
+ ... )
590
+ >>> session3.name
591
+ 'env_test'
592
+
593
+ >>> # Clean up
594
+ >>> for name in ["basic", "custom", "env_test"]:
595
+ ... server.kill_session(name)
596
+ Server(socket_name=libtmux_test...)
597
+ Server(socket_name=libtmux_test...)
598
+ Server(socket_name=libtmux_test...)
544
599
"""
545
600
if session_name is not None :
546
601
session_check_name (session_name )
@@ -600,11 +655,26 @@ def new_session(
600
655
601
656
@property
602
657
def sessions (self ) -> QueryList [Session ]:
603
- """Return a :class:`QueryList` of all :class:`Session` objects in this server .
658
+ """Return list of sessions .
604
659
605
- Access advanced filtering and retrieval with:
606
- :meth:`.sessions.get() <libtmux._internal.query_list.QueryList.get()>` and
607
- :meth:`.sessions.filter() <libtmux._internal.query_list.QueryList.filter()>`
660
+ Examples
661
+ --------
662
+ >>> # Clean up any existing test sessions first
663
+ >>> for name in ["test1", "test2"]:
664
+ ... if server.has_session(name):
665
+ ... server.kill_session(name)
666
+ >>> # Create some test sessions
667
+ >>> session1 = server.new_session(session_name="test1")
668
+ >>> session2 = server.new_session(session_name="test2")
669
+ >>> len(server.sessions) >= 2 # May have other sessions
670
+ True
671
+ >>> sorted([s.name for s in server.sessions if s.name in ["test1", "test2"]])
672
+ ['test1', 'test2']
673
+ >>> # Clean up
674
+ >>> server.kill_session("test1")
675
+ Server(socket_name=libtmux_test...)
676
+ >>> server.kill_session("test2")
677
+ Server(socket_name=libtmux_test...)
608
678
"""
609
679
sessions : list [Session ] = []
610
680
with contextlib .suppress (Exception ):
@@ -623,6 +693,36 @@ def windows(self) -> QueryList[Window]:
623
693
624
694
This includes windows in all sessions.
625
695
696
+ Examples
697
+ --------
698
+ >>> # Clean up any existing test sessions
699
+ >>> for name in ["test_windows1", "test_windows2"]:
700
+ ... if server.has_session(name):
701
+ ... server.kill_session(name)
702
+ >>> # Create sessions with windows
703
+ >>> session1 = server.new_session(session_name="test_windows1")
704
+ >>> session2 = server.new_session(session_name="test_windows2")
705
+ >>> # Create additional windows
706
+ >>> _ = session1.new_window(window_name="win1") # Create window
707
+ >>> _ = session2.new_window(window_name="win2") # Create window
708
+ >>> # Each session should have 2 windows (default + new)
709
+ >>> len([w for w in server.windows if w.session.name == "test_windows1"])
710
+ 2
711
+ >>> len([w for w in server.windows if w.session.name == "test_windows2"])
712
+ 2
713
+ >>> # Verify window names
714
+ >>> wins1 = [w for w in server.windows if w.session.name == "test_windows1"]
715
+ >>> wins2 = [w for w in server.windows if w.session.name == "test_windows2"]
716
+ >>> sorted(w.name for w in wins1)
717
+ ['win1', ...]
718
+ >>> sorted(w.name for w in wins2)
719
+ ['win2', ...]
720
+ >>> # Clean up
721
+ >>> server.kill_session("test_windows1")
722
+ Server(socket_name=libtmux_test...)
723
+ >>> server.kill_session("test_windows2")
724
+ Server(socket_name=libtmux_test...)
725
+
626
726
Access advanced filtering and retrieval with:
627
727
:meth:`.windows.get() <libtmux._internal.query_list.QueryList.get()>` and
628
728
:meth:`.windows.filter() <libtmux._internal.query_list.QueryList.filter()>`
@@ -643,6 +743,24 @@ def panes(self) -> QueryList[Pane]:
643
743
644
744
This includes panes from all windows in all sessions.
645
745
746
+ Examples
747
+ --------
748
+ >>> # Clean up any existing test session
749
+ >>> if server.has_session("test_panes"):
750
+ ... server.kill_session("test_panes")
751
+ >>> # Create a session and split some panes
752
+ >>> session = server.new_session(session_name="test_panes")
753
+ >>> window = session.attached_window
754
+ >>> # Split into two panes
755
+ >>> window.split_window()
756
+ Pane(%... Window(@... 1:..., Session($... test_panes)))
757
+ >>> # Each window starts with 1 pane, split creates another
758
+ >>> len([p for p in server.panes if p.window.session.name == "test_panes"])
759
+ 2
760
+ >>> # Clean up
761
+ >>> server.kill_session("test_panes")
762
+ Server(socket_name=libtmux_test...)
763
+
646
764
Access advanced filtering and retrieval with:
647
765
:meth:`.panes.get() <libtmux._internal.query_list.QueryList.get()>` and
648
766
:meth:`.panes.filter() <libtmux._internal.query_list.QueryList.filter()>`
0 commit comments