1313
1414from libtmux ._internal .query_list import QueryList
1515from libtmux .common import tmux_cmd
16+ from libtmux .constants import WINDOW_DIRECTION_FLAG_MAP , WindowDirection
1617from libtmux .formats import FORMAT_SEPARATOR
1718from libtmux .neo import Obj , fetch_obj , fetch_objs
1819from libtmux .pane import Pane
@@ -541,6 +542,8 @@ def new_window(
541542 window_index : str = "" ,
542543 window_shell : t .Optional [str ] = None ,
543544 environment : t .Optional [t .Dict [str , str ]] = None ,
545+ direction : t .Optional [WindowDirection ] = None ,
546+ target_window : t .Optional [str ] = None ,
544547 ) -> "Window" :
545548 """Create new window, returns new :class:`Window`.
546549
@@ -566,10 +569,52 @@ def new_window(
566569 useful for long-running processes where the closing of the
567570 window upon completion is desired.
568571
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+
569578 .. versionchanged:: 0.28.0
570579
571580 ``attach`` default changed from ``True`` to ``False``.
572581
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+
573618 Returns
574619 -------
575620 :class:`Window`
@@ -599,6 +644,22 @@ def new_window(
599644 "Environment flag ignored, requires tmux 3.0 or newer." ,
600645 )
601646
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+
602663 if window_shell :
603664 window_args += (window_shell ,)
604665
0 commit comments