13
13
14
14
from libtmux ._internal .query_list import QueryList
15
15
from libtmux .common import tmux_cmd
16
+ from libtmux .constants import WINDOW_DIRECTION_FLAG_MAP , WindowDirection
16
17
from libtmux .formats import FORMAT_SEPARATOR
17
18
from libtmux .neo import Obj , fetch_obj , fetch_objs
18
19
from libtmux .pane import Pane
@@ -541,6 +542,8 @@ def new_window(
541
542
window_index : str = "" ,
542
543
window_shell : t .Optional [str ] = None ,
543
544
environment : t .Optional [t .Dict [str , str ]] = None ,
545
+ direction : t .Optional [WindowDirection ] = None ,
546
+ target_window : t .Optional [str ] = None ,
544
547
) -> "Window" :
545
548
"""Create new window, returns new :class:`Window`.
546
549
@@ -566,10 +569,52 @@ def new_window(
566
569
useful for long-running processes where the closing of the
567
570
window upon completion is desired.
568
571
572
+ direction : WindowDirection, optional
573
+ Insert window before or after target window (tmux 3.2+).
574
+
575
+ target_window : str, optional
576
+ Used by :meth:`Window.new_window` to specify the target window.
577
+
569
578
.. versionchanged:: 0.28.0
570
579
571
580
``attach`` default changed from ``True`` to ``False``.
572
581
582
+ See Also
583
+ --------
584
+ :meth:`Window.new_window()`
585
+
586
+ Examples
587
+ --------
588
+ .. ::
589
+ >>> import pytest
590
+ >>> from libtmux.common import has_lt_version
591
+ >>> if has_lt_version('3.2'):
592
+ ... pytest.skip('direction doctests require tmux 3.2 or newer')
593
+ >>> window_initial = session.new_window(window_name='Example')
594
+ >>> window_initial
595
+ Window(@... 2:Example, Session($1 libtmux_...))
596
+ >>> window_initial.window_index
597
+ '2'
598
+
599
+ >>> window_before = session.new_window(
600
+ ... window_name='Window before', direction=WindowDirection.Before)
601
+ >>> window_initial.refresh()
602
+ >>> window_before
603
+ Window(@... 1:Window before, Session($1 libtmux_...))
604
+ >>> window_initial
605
+ Window(@... 3:Example, Session($1 libtmux_...))
606
+
607
+ >>> window_after = session.new_window(
608
+ ... window_name='Window after', direction=WindowDirection.After)
609
+ >>> window_initial.refresh()
610
+ >>> window_after.refresh()
611
+ >>> window_after
612
+ Window(@... 3:Window after, Session($1 libtmux_...))
613
+ >>> window_initial
614
+ Window(@... 4:Example, Session($1 libtmux_...))
615
+ >>> window_before
616
+ Window(@... 1:Window before, Session($1 libtmux_...))
617
+
573
618
Returns
574
619
-------
575
620
:class:`Window`
@@ -599,6 +644,22 @@ def new_window(
599
644
"Environment flag ignored, requires tmux 3.0 or newer." ,
600
645
)
601
646
647
+ if direction is not None :
648
+ if has_gte_version ("3.2" ):
649
+ window_args += (WINDOW_DIRECTION_FLAG_MAP [direction ],)
650
+ else :
651
+ logger .warning (
652
+ "Direction flag ignored, requires tmux 3.1 or newer." ,
653
+ )
654
+
655
+ if target_window :
656
+ if has_gte_version ("3.2" ):
657
+ window_args += (f"-t{ target_window } " ,)
658
+ else :
659
+ logger .warning (
660
+ "Window target ignored, requires tmux 3.1 or newer." ,
661
+ )
662
+
602
663
if window_shell :
603
664
window_args += (window_shell ,)
604
665
0 commit comments