| 1 |
"""Pythonization of the :term:`tmux(1)` window. |
| 2 |
|
| 3 |
libtmux.window |
| 4 |
~~~~~~~~~~~~~~ |
| 5 |
|
| 6 |
""" |
| 7 |
|
| 8 |
from __future__ import annotations |
| 9 |
|
| 10 |
import dataclasses |
| 11 |
import logging |
| 12 |
import pathlib |
| 13 |
import shlex |
| 14 |
import typing as t |
| 15 |
import warnings |
| 16 |
|
| 17 |
from libtmux._internal.query_list import QueryList |
| 18 |
from libtmux.common import has_gte_version, raise_if_stderr, tmux_cmd |
| 19 |
from libtmux.constants import ( |
| 20 |
RESIZE_ADJUSTMENT_DIRECTION_FLAG_MAP, |
| 21 |
OptionScope, |
| 22 |
PaneDirection, |
| 23 |
ResizeAdjustmentDirection, |
| 24 |
WindowDirection, |
| 25 |
) |
| 26 |
from libtmux.hooks import HooksMixin |
| 27 |
from libtmux.neo import Obj, fetch_obj, fetch_objs |
| 28 |
from libtmux.pane import Pane |
| 29 |
|
| 30 |
from . import exc |
| 31 |
from .common import PaneDict, WindowOptionDict |
| 32 |
from .options import OptionsMixin |
| 33 |
|
| 34 |
if t.TYPE_CHECKING: |
| 35 |
import sys |
| 36 |
import types |
| 37 |
|
| 38 |
from libtmux._internal.types import StrPath |
| 39 |
|
| 40 |
from .common import PaneDict, WindowOptionDict |
| 41 |
from .server import Server |
| 42 |
from .session import Session |
| 43 |
|
| 44 |
if sys.version_info >= (3, 11): |
| 45 |
from typing import Self |
| 46 |
else: |
| 47 |
from typing_extensions import Self |
| 48 |
|
| 49 |
|
| 50 |
logger = logging.getLogger(__name__) |
| 51 |
|
| 52 |
|
| 53 |
@dataclasses.dataclass() |
| 54 |
class Window( |
| 55 |
Obj, |
| 56 |
OptionsMixin, |
| 57 |
HooksMixin, |
| 58 |
): |
| 59 |
""":term:`tmux(1)` :term:`Window` [window_manual]_. |
| 60 |
|
| 61 |
Holds :class:`Pane` objects. |
| 62 |
|
| 63 |
Parameters |
| 64 |
---------- |
| 65 |
session : :class:`Session` |
| 66 |
|
| 67 |
Examples |
| 68 |
-------- |
| 69 |
>>> window = session.new_window('My project', attach=True) |
| 70 |
|
| 71 |
>>> window |
| 72 |
Window(@2 2:My project, Session($... ...)) |
| 73 |
|
| 74 |
Windows have panes: |
| 75 |
|
| 76 |
>>> window.panes |
| 77 |
[Pane(...)] |
| 78 |
|
| 79 |
>>> window.active_pane |
| 80 |
Pane(...) |
| 81 |
|
| 82 |
Relations moving up: |
| 83 |
|
| 84 |
>>> window.session |
| 85 |
Session(...) |
| 86 |
|
| 87 |
>>> window.window_id == session.active_window.window_id |
| 88 |
True |
| 89 |
|
| 90 |
>>> window == session.active_window |
| 91 |
True |
| 92 |
|
| 93 |
>>> window in session.windows |
| 94 |
True |
| 95 |
|
| 96 |
The window can be used as a context manager to ensure proper cleanup: |
| 97 |
|
| 98 |
>>> with session.new_window() as window: |
| 99 |
... pane = window.split() |
| 100 |
... # Do work with the pane |
| 101 |
... # Window will be killed automatically when exiting the context |
| 102 |
|
| 103 |
References |
| 104 |
---------- |
| 105 |
.. [window_manual] tmux window. openbsd manpage for TMUX(1). |
| 106 |
"Each session has one or more windows linked to it. A window |
| 107 |
occupies the entire screen and may be split into rectangular |
| 108 |
panes..." |
| 109 |
|
| 110 |
https://man.openbsd.org/tmux.1#DESCRIPTION. Accessed April 1st, 2018. |
| 111 |
""" |
| 112 |
|
| 113 |
default_option_scope: OptionScope | None = OptionScope.Window |
| 114 |
default_hook_scope: OptionScope | None = OptionScope.Window |
| 115 |
server: Server |
| 116 |
|
| 117 |
def __enter__(self) -> Self: |
| 118 |
"""Enter the context, returning self. |
| 119 |
|
| 120 |
Returns |
| 121 |
------- |
| 122 |
:class:`Window` |
| 123 |
The window instance |
| 124 |
""" |
| 125 |
return self |
| 126 |
|
| 127 |
def __exit__( |
| 128 |
self, |
| 129 |
exc_type: type[BaseException] | None, |
| 130 |
exc_value: BaseException | None, |
| 131 |
exc_tb: types.TracebackType | None, |
| 132 |
) -> None: |
| 133 |
"""Exit the context, killing the window if it exists. |
| 134 |
|
| 135 |
Parameters |
| 136 |
---------- |
| 137 |
exc_type : type[BaseException] | None |
| 138 |
The type of the exception that was raised |
| 139 |
exc_value : BaseException | None |
| 140 |
The instance of the exception that was raised |
| 141 |
exc_tb : types.TracebackType | None |
| 142 |
The traceback of the exception that was raised |
| 143 |
""" |
| 144 |
if ( |
| 145 |
self.window_id is not None |
| 146 |
and len(self.session.windows.filter(window_id=self.window_id)) > 0 |
| 147 |
): |
| 148 |
self.kill() |
| 149 |
|
| 150 |
def refresh(self) -> None: |
| 151 |
"""Refresh window attributes from tmux. |
| 152 |
|
| 153 |
Raises |
| 154 |
------ |
| 155 |
ValueError |
| 156 |
When ``window_id`` is unset. Surfaces a clear error under |
| 157 |
``python -O``, where an ``assert`` would be stripped. |
| 158 |
""" |
| 159 |
if self.window_id is None: |
| 160 |
msg = "Window must have a window_id to refresh" |
| 161 |
raise ValueError(msg) |
| 162 |
return super()._refresh( |
| 163 |
obj_key="window_id", |
| 164 |
obj_id=self.window_id, |
| 165 |
list_cmd="list-windows", |
| 166 |
list_extra_args=("-a",), |
| 167 |
) |
| 168 |
|
| 169 |
@classmethod |
| 170 |
def from_window_id(cls, server: Server, window_id: str) -> Window: |
| 171 |
"""Create Window from existing window_id.""" |
| 172 |
window = fetch_obj( |
| 173 |
obj_key="window_id", |
| 174 |
obj_id=window_id, |
| 175 |
server=server, |
| 176 |
list_cmd="list-windows", |
| 177 |
list_extra_args=("-a",), |
| 178 |
) |
| 179 |
return cls(server=server, **window) |
| 180 |
|
| 181 |
@property |
| 182 |
def session(self) -> Session: |
| 183 |
"""Parent session of window.""" |
| 184 |
assert isinstance(self.session_id, str) |
| 185 |
from libtmux.session import Session |
| 186 |
|
| 187 |
return Session.from_session_id(server=self.server, session_id=self.session_id) |
| 188 |
|
| 189 |
@property |
| 190 |
def panes(self) -> QueryList[Pane]: |
| 191 |
"""Panes contained by window. |
| 192 |
|
| 193 |
Can be accessed via |
| 194 |
:meth:`.panes.get() <libtmux._internal.query_list.QueryList.get()>` and |
| 195 |
:meth:`.panes.filter() <libtmux._internal.query_list.QueryList.filter()>` |
| 196 |
""" |
| 197 |
panes: list[Pane] = [ |
| 198 |
Pane(server=self.server, **obj) |
| 199 |
for obj in fetch_objs( |
| 200 |
list_cmd="list-panes", |
| 201 |
list_extra_args=["-t", str(self.window_id)], |
| 202 |
server=self.server, |
| 203 |
) |
| 204 |
if obj.get("window_id") == self.window_id |
| 205 |
] |
| 206 |
|
| 207 |
return QueryList(panes) |
| 208 |
|
| 209 |
def search_panes( |
| 210 |
self, |
| 211 |
*, |
| 212 |
filter: str | None = None, |
| 213 |
) -> QueryList[Pane]: |
| 214 |
"""Panes in this window, optionally filtered by tmux. |
| 215 |
|
| 216 |
Like :attr:`Window.panes` but with a ``filter`` kwarg passed to |
| 217 |
``$ tmux list-panes -t <window> -f <filter>``. |
| 218 |
|
| 219 |
Parameters |
| 220 |
---------- |
| 221 |
filter : str, optional |
| 222 |
tmux format expression (``-f`` flag). |
| 223 |
|
| 224 |
.. warning:: |
| 225 |
|
| 226 |
tmux silently expands a malformed filter (unclosed |
| 227 |
``#{...}``, unknown format token) to empty, which the |
| 228 |
filter treats as false — every row is suppressed and no |
| 229 |
stderr is emitted. A bad filter is |
| 230 |
indistinguishable from "filter matched nothing"; verify |
| 231 |
filter syntax against the FORMATS section of ``tmux(1)``. |
| 232 |
|
| 233 |
.. versionadded:: 0.57 |
| 234 |
|
| 235 |
See Also |
| 236 |
-------- |
| 237 |
:attr:`Window.panes` : unfiltered :class:`QueryList` of every |
| 238 |
pane in this window (Python-side ``.filter()`` runs against |
| 239 |
this). |
| 240 |
:ref:`native-filtering` : when to pick ``search_*`` over |
| 241 |
``QueryList.filter()``. |
| 242 |
|
| 243 |
Examples |
| 244 |
-------- |
| 245 |
>>> target_pane = window.split() |
| 246 |
>>> matches = window.search_panes( |
| 247 |
... filter=f'#{{m:{target_pane.pane_id},#{{pane_id}}}}' |
| 248 |
... ) |
| 249 |
>>> [p.pane_id for p in matches] == [target_pane.pane_id] |
| 250 |
True |
| 251 |
""" |
| 252 |
panes: list[Pane] = [ |
| 253 |
Pane(server=self.server, **obj) |
| 254 |
for obj in fetch_objs( |
| 255 |
list_cmd="list-panes", |
| 256 |
list_extra_args=["-t", str(self.window_id)], |
| 257 |
server=self.server, |
| 258 |
filter=filter, |
| 259 |
) |
| 260 |
if obj.get("window_id") == self.window_id |
| 261 |
] |
| 262 |
|
| 263 |
return QueryList(panes) |
| 264 |
|
| 265 |
""" |
| 266 |
Commands (pane-scoped) |
| 267 |
""" |
| 268 |
|
| 269 |
def cmd( |
| 270 |
self, |
| 271 |
cmd: str, |
| 272 |
*args: t.Any, |
| 273 |
target: str | int | None = None, |
| 274 |
) -> tmux_cmd: |
| 275 |
"""Execute tmux subcommand within window context. |
| 276 |
|
| 277 |
Automatically binds target by adding ``-t`` for object's window ID to the |
| 278 |
command. Pass ``target`` to keyword arguments to override. |
| 279 |
|
| 280 |
Examples |
| 281 |
-------- |
| 282 |
Create a pane from a window: |
| 283 |
|
| 284 |
>>> window.cmd('split-window', '-P', '-F#{pane_id}').stdout[0] |
| 285 |
'%...' |
| 286 |
|
| 287 |
Magic, directly to a `Pane`: |
| 288 |
|
| 289 |
>>> Pane.from_pane_id(pane_id=session.cmd( |
| 290 |
... 'split-window', '-P', '-F#{pane_id}').stdout[0], server=session.server) |
| 291 |
Pane(%... Window(@... ...:..., Session($1 libtmux_...))) |
| 292 |
|
| 293 |
Parameters |
| 294 |
---------- |
| 295 |
target : str, optional |
| 296 |
Optional custom target override. By default, the target is the window ID. |
| 297 |
|
| 298 |
Returns |
| 299 |
------- |
| 300 |
:meth:`server.cmd` |
| 301 |
""" |
| 302 |
if target is None: |
| 303 |
target = self.window_id |
| 304 |
|
| 305 |
return self.server.cmd(cmd, *args, target=target) |
| 306 |
|
| 307 |
""" |
| 308 |
Commands (tmux-like) |
| 309 |
""" |
| 310 |
|
| 311 |
def select_pane(self, target_pane: str | int) -> Pane | None: |
| 312 |
"""Select pane and return selected :class:`Pane`. |
| 313 |
|
| 314 |
``$ tmux select-pane``. |
| 315 |
|
| 316 |
Parameters |
| 317 |
---------- |
| 318 |
target_pane : str |
| 319 |
'target_pane', '-U' ,'-D', '-L', '-R', or '-l'. |
| 320 |
|
| 321 |
Returns |
| 322 |
------- |
| 323 |
:class:`Pane` |
| 324 |
""" |
| 325 |
if target_pane in {"-l", "-U", "-D", "-L", "-R"}: |
| 326 |
proc = self.cmd("select-pane", target_pane) |
| 327 |
else: |
| 328 |
proc = self.cmd("select-pane", target=target_pane) |
| 329 |
|
| 330 |
raise_if_stderr(proc, "select-pane") |
| 331 |
|
| 332 |
return self.active_pane |
| 333 |
|
| 334 |
def split( |
| 335 |
self, |
| 336 |
/, |
| 337 |
target: int | str | None = None, |
| 338 |
start_directory: StrPath | None = None, |
| 339 |
attach: bool = False, |
| 340 |
direction: PaneDirection | None = None, |
| 341 |
full_window_split: bool | None = None, |
| 342 |
zoom: bool | None = None, |
| 343 |
shell: str | None = None, |
| 344 |
size: str | int | None = None, |
| 345 |
percentage: int | None = None, |
| 346 |
environment: dict[str, str] | None = None, |
| 347 |
empty: bool | None = None, |
| 348 |
style: str | None = None, |
| 349 |
active_border_style: str | None = None, |
| 350 |
inactive_border_style: str | None = None, |
| 351 |
message: str | None = None, |
| 352 |
keep: bool | None = None, |
| 353 |
) -> Pane: |
| 354 |
"""Split window on active pane and return the created :class:`Pane`. |
| 355 |
|
| 356 |
Parameters |
| 357 |
---------- |
| 358 |
attach : bool, optional |
| 359 |
Make new window the current window after creating it, default |
| 360 |
True. |
| 361 |
start_directory : str or PathLike, optional |
| 362 |
Specifies the working directory in which the new window is created. |
| 363 |
direction : PaneDirection, optional |
| 364 |
Split in direction. If none is specified, assume down. |
| 365 |
full_window_split : bool, optional |
| 366 |
Split across full window width or height, rather than active pane. |
| 367 |
zoom : bool, optional |
| 368 |
Expand pane. |
| 369 |
shell : str, optional |
| 370 |
Execute a command on splitting the window. The pane will close |
| 371 |
when the command exits. |
| 372 |
|
| 373 |
NOTE: When this command exits the pane will close. This feature |
| 374 |
is useful for long-running processes where the closing of the |
| 375 |
window upon completion is desired. |
| 376 |
size : int, optional |
| 377 |
Cell/row count to occupy with respect to current window. |
| 378 |
percentage : int, optional |
| 379 |
Percentage (0-100) of the window to occupy (``-p`` flag). |
| 380 |
Mutually exclusive with *size*. |
| 381 |
|
| 382 |
.. versionadded:: 0.56 |
| 383 |
environment : dict, optional |
| 384 |
Environmental variables for new pane. Passthrough to ``-e``. |
| 385 |
empty : bool, optional |
| 386 |
Create an empty pane with no command (``-E`` flag) instead of |
| 387 |
spawning the default shell. Requires tmux 3.7+. If used with |
| 388 |
tmux < 3.7, a warning is issued and the flag is ignored. |
| 389 |
style : str, optional |
| 390 |
Style for the new pane (``-s``). Requires tmux 3.7+. |
| 391 |
active_border_style : str, optional |
| 392 |
Active-pane border style (``-S``). Requires tmux 3.7+. |
| 393 |
inactive_border_style : str, optional |
| 394 |
Inactive-pane border style (``-R``). Requires tmux 3.7+. |
| 395 |
message : str, optional |
| 396 |
Keep the pane open (until a key is pressed) after exit, showing this |
| 397 |
``remain-on-exit-format`` message (``-m``). Requires tmux 3.7+. |
| 398 |
keep : bool, optional |
| 399 |
Keep the pane open until a key is pressed after exit (``-k``). |
| 400 |
Requires tmux 3.7+. These 3.7 flags warn and are ignored below 3.7. |
| 401 |
|
| 402 |
Returns |
| 403 |
------- |
| 404 |
:class:`Pane` |
| 405 |
The newly created pane. |
| 406 |
""" |
| 407 |
active_pane = self.active_pane or self.panes[0] |
| 408 |
return active_pane.split( |
| 409 |
target=target, |
| 410 |
start_directory=start_directory, |
| 411 |
attach=attach, |
| 412 |
direction=direction, |
| 413 |
full_window_split=full_window_split, |
| 414 |
zoom=zoom, |
| 415 |
shell=shell, |
| 416 |
size=size, |
| 417 |
percentage=percentage, |
| 418 |
environment=environment, |
| 419 |
empty=empty, |
| 420 |
style=style, |
| 421 |
active_border_style=active_border_style, |
| 422 |
inactive_border_style=inactive_border_style, |
| 423 |
message=message, |
| 424 |
keep=keep, |
| 425 |
) |
| 426 |
|
| 427 |
def new_pane( |
| 428 |
self, |
| 429 |
/, |
| 430 |
target: int | str | None = None, |
| 431 |
start_directory: StrPath | None = None, |
| 432 |
attach: bool = False, |
| 433 |
shell: str | None = None, |
| 434 |
environment: dict[str, str] | None = None, |
| 435 |
width: int | None = None, |
| 436 |
height: int | None = None, |
| 437 |
x: int | None = None, |
| 438 |
y: int | None = None, |
| 439 |
zoom: bool | None = None, |
| 440 |
empty: bool | None = None, |
| 441 |
style: str | None = None, |
| 442 |
active_border_style: str | None = None, |
| 443 |
inactive_border_style: str | None = None, |
| 444 |
message: str | None = None, |
| 445 |
keep: bool | None = None, |
| 446 |
) -> Pane: |
| 447 |
"""Create a floating :class:`Pane` in this window (``$ tmux new-pane``). |
| 448 |
|
| 449 |
Use :meth:`split` for a tiled pane. Floating panes require tmux 3.7+; |
| 450 |
delegates to :meth:`Pane.new_pane` on the active pane. |
| 451 |
|
| 452 |
Parameters |
| 453 |
---------- |
| 454 |
target : int or str, optional |
| 455 |
Custom *target-pane*. |
| 456 |
start_directory : str or PathLike, optional |
| 457 |
Working directory for the new pane (``-c``). |
| 458 |
attach : bool, optional |
| 459 |
Make the new pane active, default False (``-d`` otherwise). |
| 460 |
shell : str, optional |
| 461 |
Command to run; the pane closes when it exits. |
| 462 |
environment : dict, optional |
| 463 |
Environment variables for the new pane (``-e``). |
| 464 |
width : int, optional |
| 465 |
Width in cells (``-x``). |
| 466 |
height : int, optional |
| 467 |
Height in cells (``-y``). |
| 468 |
x : int, optional |
| 469 |
X position in cells (``-X``). |
| 470 |
y : int, optional |
| 471 |
Y position in cells (``-Y``). |
| 472 |
zoom : bool, optional |
| 473 |
Zoom the pane (``-Z``). |
| 474 |
empty : bool, optional |
| 475 |
Create an empty pane with no command (``-E``). |
| 476 |
style : str, optional |
| 477 |
Style for the floating pane (``-s``). |
| 478 |
active_border_style : str, optional |
| 479 |
Border style when the pane is active (``-S``). |
| 480 |
inactive_border_style : str, optional |
| 481 |
Border style when the pane is inactive (``-R``). |
| 482 |
message : str, optional |
| 483 |
Keep the pane open (until a key is pressed) after the command |
| 484 |
exits, showing this ``remain-on-exit-format`` message (``-m``). |
| 485 |
keep : bool, optional |
| 486 |
Keep the pane open until a key is pressed after exit (``-k``), |
| 487 |
using the default ``remain-on-exit-format``. |
| 488 |
|
| 489 |
Returns |
| 490 |
------- |
| 491 |
:class:`Pane` |
| 492 |
The newly created floating pane. |
| 493 |
|
| 494 |
Examples |
| 495 |
-------- |
| 496 |
>>> from libtmux.common import has_gte_version |
| 497 |
>>> if has_gte_version("3.7"): |
| 498 |
... floating = window.new_pane(width=40, height=10, shell="sleep 5") |
| 499 |
... is_floating = floating.pane_floating_flag |
| 500 |
... else: |
| 501 |
... is_floating = "1" |
| 502 |
>>> is_floating |
| 503 |
'1' |
| 504 |
""" |
| 505 |
active_pane = self.active_pane or self.panes[0] |
| 506 |
return active_pane.new_pane( |
| 507 |
target=target, |
| 508 |
start_directory=start_directory, |
| 509 |
attach=attach, |
| 510 |
shell=shell, |
| 511 |
environment=environment, |
| 512 |
width=width, |
| 513 |
height=height, |
| 514 |
x=x, |
| 515 |
y=y, |
| 516 |
zoom=zoom, |
| 517 |
empty=empty, |
| 518 |
style=style, |
| 519 |
active_border_style=active_border_style, |
| 520 |
inactive_border_style=inactive_border_style, |
| 521 |
message=message, |
| 522 |
keep=keep, |
| 523 |
) |
| 524 |
|
| 525 |
def resize( |
| 526 |
self, |
| 527 |
/, |
| 528 |
|
| 529 |
adjustment_direction: ResizeAdjustmentDirection | None = None, |
| 530 |
adjustment: int | None = None, |
| 531 |
|
| 532 |
height: int | None = None, |
| 533 |
width: int | None = None, |
| 534 |
|
| 535 |
expand: bool | None = None, |
| 536 |
shrink: bool | None = None, |
| 537 |
) -> Window: |
| 538 |
"""Resize tmux window. |
| 539 |
|
| 540 |
Parameters |
| 541 |
---------- |
| 542 |
adjustment_direction : ResizeAdjustmentDirection, optional |
| 543 |
direction to adjust, ``Up``, ``Down``, ``Left``, ``Right``. |
| 544 |
adjustment : ResizeAdjustmentDirection, optional |
| 545 |
|
| 546 |
height : int, optional |
| 547 |
``resize-window -y`` dimensions |
| 548 |
width : int, optional |
| 549 |
``resize-window -x`` dimensions |
| 550 |
|
| 551 |
expand : bool |
| 552 |
expand window |
| 553 |
shrink : bool |
| 554 |
shrink window |
| 555 |
|
| 556 |
Raises |
| 557 |
------ |
| 558 |
:exc:`exc.LibTmuxException`, |
| 559 |
:exc:`exc.PaneAdjustmentDirectionRequiresAdjustment` |
| 560 |
|
| 561 |
Returns |
| 562 |
------- |
| 563 |
:class:`Window` |
| 564 |
|
| 565 |
Notes |
| 566 |
----- |
| 567 |
Three types of resizing are available: |
| 568 |
|
| 569 |
1. Adjustments: ``adjustment_direction`` and ``adjustment``. |
| 570 |
2. Manual resizing: ``height`` and / or ``width``. |
| 571 |
3. Expand or shrink: ``expand`` or ``shrink``. |
| 572 |
""" |
| 573 |
tmux_args: tuple[str, ...] = () |
| 574 |
|
| 575 |
|
| 576 |
if adjustment_direction: |
| 577 |
if adjustment is None: |
| 578 |
raise exc.WindowAdjustmentDirectionRequiresAdjustment |
| 579 |
tmux_args += ( |
| 580 |
f"{RESIZE_ADJUSTMENT_DIRECTION_FLAG_MAP[adjustment_direction]}", |
| 581 |
str(adjustment), |
| 582 |
) |
| 583 |
elif height or width: |
| 584 |
|
| 585 |
if height: |
| 586 |
tmux_args += (f"-y{int(height)}",) |
| 587 |
if width: |
| 588 |
tmux_args += (f"-x{int(width)}",) |
| 589 |
elif expand or shrink: |
| 590 |
if expand: |
| 591 |
tmux_args += ("-A",) |
| 592 |
elif shrink: |
| 593 |
tmux_args += ("-a",) |
| 594 |
|
| 595 |
proc = self.cmd("resize-window", *tmux_args) |
| 596 |
|
| 597 |
raise_if_stderr(proc, "resize-window") |
| 598 |
|
| 599 |
self.refresh() |
| 600 |
return self |
| 601 |
|
| 602 |
def last_pane( |
| 603 |
self, |
| 604 |
*, |
| 605 |
disable_input: bool | None = None, |
| 606 |
enable_input: bool | None = None, |
| 607 |
keep_zoom: bool | None = None, |
| 608 |
) -> Pane | None: |
| 609 |
"""Select the last (previously active) pane via ``$ tmux last-pane``. |
| 610 |
|
| 611 |
Parameters |
| 612 |
---------- |
| 613 |
disable_input : bool, optional |
| 614 |
Disable input to the pane (``-d`` flag). |
| 615 |
|
| 616 |
.. versionadded:: 0.56 |
| 617 |
enable_input : bool, optional |
| 618 |
Enable input to the pane (``-e`` flag). |
| 619 |
|
| 620 |
.. versionadded:: 0.56 |
| 621 |
keep_zoom : bool, optional |
| 622 |
Keep the window zoomed if zoomed (``-Z`` flag). |
| 623 |
|
| 624 |
.. versionadded:: 0.56 |
| 625 |
|
| 626 |
Returns |
| 627 |
------- |
| 628 |
:class:`Pane` or None |
| 629 |
The selected pane, or None if no last pane exists. |
| 630 |
|
| 631 |
Examples |
| 632 |
-------- |
| 633 |
>>> pane1 = window.active_pane |
| 634 |
>>> pane2 = window.split() |
| 635 |
>>> pane2.select() |
| 636 |
Pane(...) |
| 637 |
>>> result = window.last_pane() |
| 638 |
""" |
| 639 |
tmux_args: tuple[str, ...] = () |
| 640 |
|
| 641 |
if disable_input: |
| 642 |
tmux_args += ("-d",) |
| 643 |
|
| 644 |
if enable_input: |
| 645 |
tmux_args += ("-e",) |
| 646 |
|
| 647 |
if keep_zoom: |
| 648 |
tmux_args += ("-Z",) |
| 649 |
|
| 650 |
proc = self.cmd("last-pane", *tmux_args) |
| 651 |
|
| 652 |
raise_if_stderr(proc, "last-pane") |
| 653 |
|
| 654 |
return self.active_pane |
| 655 |
|
| 656 |
def select_layout( |
| 657 |
self, |
| 658 |
layout: str | None = None, |
| 659 |
*, |
| 660 |
spread: bool | None = None, |
| 661 |
next_layout: bool | None = None, |
| 662 |
previous_layout: bool | None = None, |
| 663 |
) -> Window: |
| 664 |
"""Select layout for window. |
| 665 |
|
| 666 |
Wrapper for ``$ tmux select-layout <layout>``. |
| 667 |
|
| 668 |
Parameters |
| 669 |
---------- |
| 670 |
layout : str, optional |
| 671 |
String of the layout, 'even-horizontal', 'tiled', etc. Entering |
| 672 |
None (leaving this blank) is same as ``select-layout`` with no |
| 673 |
layout. In recent tmux versions, it picks the most recently |
| 674 |
set layout. |
| 675 |
|
| 676 |
'even-horizontal' |
| 677 |
Panes are spread out evenly from left to right across the |
| 678 |
window. |
| 679 |
'even-vertical' |
| 680 |
Panes are spread evenly from top to bottom. |
| 681 |
'main-horizontal' |
| 682 |
A large (main) pane is shown at the top of the window and the |
| 683 |
remaining panes are spread from left to right in the leftover |
| 684 |
space at the bottom. |
| 685 |
'main-vertical' |
| 686 |
Similar to main-horizontal but the large pane is placed on the |
| 687 |
left and the others spread from top to bottom along the right. |
| 688 |
'tiled' |
| 689 |
Panes are spread out as evenly as possible over the window in |
| 690 |
both rows and columns. |
| 691 |
'custom' |
| 692 |
Custom dimensions (see :term:`tmux(1)` manpages). |
| 693 |
spread : bool, optional |
| 694 |
Spread panes out evenly (``-E`` flag). |
| 695 |
|
| 696 |
.. versionadded:: 0.56 |
| 697 |
next_layout : bool, optional |
| 698 |
Move to the next layout (``-n`` flag). |
| 699 |
|
| 700 |
.. versionadded:: 0.56 |
| 701 |
previous_layout : bool, optional |
| 702 |
Move to the previous layout (``-p`` flag). |
| 703 |
|
| 704 |
.. versionadded:: 0.56 |
| 705 |
|
| 706 |
Returns |
| 707 |
------- |
| 708 |
:class:`Window` |
| 709 |
Self, for method chaining. |
| 710 |
|
| 711 |
Raises |
| 712 |
------ |
| 713 |
:exc:`libtmux.exc.LibTmuxException` |
| 714 |
If tmux returns an error. |
| 715 |
ValueError |
| 716 |
If both *layout* and a flag (*spread*, *next_layout*, |
| 717 |
*previous_layout*) are specified. |
| 718 |
""" |
| 719 |
flags = (spread, next_layout, previous_layout) |
| 720 |
if layout and any(flags): |
| 721 |
msg = "Cannot specify both layout and spread/next_layout/previous_layout" |
| 722 |
raise ValueError(msg) |
| 723 |
|
| 724 |
cmd = ["select-layout"] |
| 725 |
|
| 726 |
if spread: |
| 727 |
cmd.append("-E") |
| 728 |
|
| 729 |
if next_layout: |
| 730 |
cmd.append("-n") |
| 731 |
|
| 732 |
if previous_layout: |
| 733 |
cmd.append("-p") |
| 734 |
|
| 735 |
if layout: |
| 736 |
cmd.append(layout) |
| 737 |
|
| 738 |
proc = self.cmd(*cmd) |
| 739 |
|
| 740 |
raise_if_stderr(proc, "select-layout") |
| 741 |
|
| 742 |
return self |
| 743 |
|
| 744 |
def next_layout(self) -> Window: |
| 745 |
"""Cycle to the next layout via ``$ tmux next-layout``. |
| 746 |
|
| 747 |
>>> pane1 = window.active_pane |
| 748 |
>>> pane2 = window.split() |
| 749 |
>>> window.next_layout() |
| 750 |
Window(...) |
| 751 |
""" |
| 752 |
proc = self.cmd("next-layout") |
| 753 |
|
| 754 |
raise_if_stderr(proc, "next-layout") |
| 755 |
|
| 756 |
return self |
| 757 |
|
| 758 |
def previous_layout(self) -> Window: |
| 759 |
"""Cycle to the previous layout via ``$ tmux previous-layout``. |
| 760 |
|
| 761 |
>>> pane1 = window.active_pane |
| 762 |
>>> pane2 = window.split() |
| 763 |
>>> window.previous_layout() |
| 764 |
Window(...) |
| 765 |
""" |
| 766 |
proc = self.cmd("previous-layout") |
| 767 |
|
| 768 |
raise_if_stderr(proc, "previous-layout") |
| 769 |
|
| 770 |
return self |
| 771 |
|
| 772 |
def link( |
| 773 |
self, |
| 774 |
target_session: str | Session, |
| 775 |
*, |
| 776 |
target_index: str | None = None, |
| 777 |
kill_existing: bool | None = None, |
| 778 |
after: bool | None = None, |
| 779 |
before: bool | None = None, |
| 780 |
detach: bool | None = None, |
| 781 |
) -> None: |
| 782 |
"""Link this window to another session via ``$ tmux link-window``. |
| 783 |
|
| 784 |
Parameters |
| 785 |
---------- |
| 786 |
target_session : str or Session |
| 787 |
Target session to link the window to. |
| 788 |
target_index : str, optional |
| 789 |
Target window index in the destination session. |
| 790 |
kill_existing : bool, optional |
| 791 |
Kill target window if it exists (``-k`` flag). |
| 792 |
after : bool, optional |
| 793 |
Insert after the target window (``-a`` flag). |
| 794 |
before : bool, optional |
| 795 |
Insert before the target window (``-b`` flag). |
| 796 |
detach : bool, optional |
| 797 |
Do not make the linked window active (``-d`` flag). |
| 798 |
|
| 799 |
Examples |
| 800 |
-------- |
| 801 |
>>> w = session.new_window(window_name='link_test') |
| 802 |
>>> s2 = server.new_session(session_name='link_target') |
| 803 |
>>> w.link(s2) |
| 804 |
""" |
| 805 |
tmux_args: tuple[str, ...] = () |
| 806 |
|
| 807 |
if kill_existing: |
| 808 |
tmux_args += ("-k",) |
| 809 |
|
| 810 |
if after: |
| 811 |
tmux_args += ("-a",) |
| 812 |
|
| 813 |
if before: |
| 814 |
tmux_args += ("-b",) |
| 815 |
|
| 816 |
if detach: |
| 817 |
tmux_args += ("-d",) |
| 818 |
|
| 819 |
|
| 820 |
tmux_args += ("-s", f"{self.session_id}:{self.window_index}") |
| 821 |
|
| 822 |
|
| 823 |
from libtmux.session import Session |
| 824 |
|
| 825 |
session_id = ( |
| 826 |
target_session.session_id |
| 827 |
if isinstance(target_session, Session) |
| 828 |
else target_session |
| 829 |
) |
| 830 |
target = f"{session_id}:{target_index}" if target_index else str(session_id) |
| 831 |
tmux_args += ("-t", target) |
| 832 |
|
| 833 |
proc = self.server.cmd("link-window", *tmux_args) |
| 834 |
|
| 835 |
raise_if_stderr(proc, "link-window") |
| 836 |
|
| 837 |
def unlink(self, *, kill_if_last: bool | None = None) -> None: |
| 838 |
"""Unlink this window from the current session via ``$ tmux unlink-window``. |
| 839 |
|
| 840 |
Parameters |
| 841 |
---------- |
| 842 |
kill_if_last : bool, optional |
| 843 |
Kill the window if it is the last window in the session (``-k``). |
| 844 |
|
| 845 |
Examples |
| 846 |
-------- |
| 847 |
>>> w = session.new_window(window_name='unlink_test') |
| 848 |
>>> s2 = server.new_session(session_name='unlink_s2') |
| 849 |
>>> w.link(s2) |
| 850 |
>>> linked = [x for x in s2.windows if x.window_name == 'unlink_test'] |
| 851 |
>>> linked[0].unlink() |
| 852 |
""" |
| 853 |
tmux_args: tuple[str, ...] = () |
| 854 |
|
| 855 |
if kill_if_last: |
| 856 |
tmux_args += ("-k",) |
| 857 |
|
| 858 |
proc = self.cmd("unlink-window", *tmux_args) |
| 859 |
|
| 860 |
raise_if_stderr(proc, "unlink-window") |
| 861 |
|
| 862 |
def rotate( |
| 863 |
self, |
| 864 |
*, |
| 865 |
upward: bool | None = None, |
| 866 |
downward: bool | None = None, |
| 867 |
keep_zoom: bool | None = None, |
| 868 |
) -> Window: |
| 869 |
"""Rotate pane positions in the window via ``$ tmux rotate-window``. |
| 870 |
|
| 871 |
Parameters |
| 872 |
---------- |
| 873 |
upward : bool, optional |
| 874 |
Rotate upward (``-U`` flag). |
| 875 |
downward : bool, optional |
| 876 |
Rotate downward (``-D`` flag). |
| 877 |
keep_zoom : bool, optional |
| 878 |
Keep the window zoomed if zoomed (``-Z`` flag). |
| 879 |
|
| 880 |
Returns |
| 881 |
------- |
| 882 |
:class:`Window` |
| 883 |
Self, for method chaining. |
| 884 |
|
| 885 |
Examples |
| 886 |
-------- |
| 887 |
>>> pane1 = window.active_pane |
| 888 |
>>> pane2 = window.split() |
| 889 |
>>> window.rotate() |
| 890 |
Window(...) |
| 891 |
""" |
| 892 |
tmux_args: tuple[str, ...] = () |
| 893 |
|
| 894 |
if upward: |
| 895 |
tmux_args += ("-U",) |
| 896 |
|
| 897 |
if downward: |
| 898 |
tmux_args += ("-D",) |
| 899 |
|
| 900 |
if keep_zoom: |
| 901 |
tmux_args += ("-Z",) |
| 902 |
|
| 903 |
proc = self.cmd("rotate-window", *tmux_args) |
| 904 |
|
| 905 |
raise_if_stderr(proc, "rotate-window") |
| 906 |
|
| 907 |
return self |
| 908 |
|
| 909 |
def respawn( |
| 910 |
self, |
| 911 |
*, |
| 912 |
shell: str | None = None, |
| 913 |
start_directory: StrPath | None = None, |
| 914 |
environment: dict[str, str] | None = None, |
| 915 |
kill: bool | None = None, |
| 916 |
) -> None: |
| 917 |
"""Respawn the window process via ``$ tmux respawn-window``. |
| 918 |
|
| 919 |
Parameters |
| 920 |
---------- |
| 921 |
shell : str, optional |
| 922 |
Shell command to run in the respawned window. |
| 923 |
start_directory : str or PathLike, optional |
| 924 |
Working directory for the respawned window (``-c`` flag). |
| 925 |
environment : dict, optional |
| 926 |
Environment variables (``-e`` flag). |
| 927 |
kill : bool, optional |
| 928 |
Kill the current process before respawning (``-k`` flag). |
| 929 |
Required if the window is still active. |
| 930 |
|
| 931 |
Examples |
| 932 |
-------- |
| 933 |
>>> window = session.new_window(window_name='respawn_test') |
| 934 |
>>> window.respawn(kill=True, shell='sh') |
| 935 |
""" |
| 936 |
tmux_args: tuple[str, ...] = () |
| 937 |
|
| 938 |
if kill: |
| 939 |
tmux_args += ("-k",) |
| 940 |
|
| 941 |
if start_directory is not None: |
| 942 |
start_path = pathlib.Path(start_directory).expanduser() |
| 943 |
tmux_args += (f"-c{start_path}",) |
| 944 |
|
| 945 |
if environment: |
| 946 |
for k, v in environment.items(): |
| 947 |
tmux_args += (f"-e{k}={v}",) |
| 948 |
|
| 949 |
if shell: |
| 950 |
tmux_args += (shell,) |
| 951 |
|
| 952 |
proc = self.cmd("respawn-window", *tmux_args) |
| 953 |
|
| 954 |
raise_if_stderr(proc, "respawn-window") |
| 955 |
|
| 956 |
def swap( |
| 957 |
self, |
| 958 |
target: str | Window, |
| 959 |
*, |
| 960 |
detach: bool | None = None, |
| 961 |
) -> None: |
| 962 |
"""Swap this window with another via ``$ tmux swap-window``. |
| 963 |
|
| 964 |
Parameters |
| 965 |
---------- |
| 966 |
target : str or Window |
| 967 |
Target window to swap with. Can be a window ID string or Window. |
| 968 |
detach : bool, optional |
| 969 |
Do not change the active window (``-d`` flag). |
| 970 |
|
| 971 |
Examples |
| 972 |
-------- |
| 973 |
>>> w1 = session.new_window(window_name='swap_a') |
| 974 |
>>> w2 = session.new_window(window_name='swap_b') |
| 975 |
>>> w1_idx = w1.window_index |
| 976 |
>>> w2_idx = w2.window_index |
| 977 |
>>> w1.swap(w2) |
| 978 |
>>> w1.window_index == w2_idx |
| 979 |
True |
| 980 |
>>> w2.window_index == w1_idx |
| 981 |
True |
| 982 |
""" |
| 983 |
tmux_args: tuple[str, ...] = () |
| 984 |
|
| 985 |
if detach: |
| 986 |
tmux_args += ("-d",) |
| 987 |
|
| 988 |
target_id = target.window_id if isinstance(target, Window) else target |
| 989 |
tmux_args += ("-s", str(target_id)) |
| 990 |
|
| 991 |
proc = self.cmd("swap-window", *tmux_args) |
| 992 |
|
| 993 |
raise_if_stderr(proc, "swap-window") |
| 994 |
|
| 995 |
self.refresh() |
| 996 |
if isinstance(target, Window): |
| 997 |
target.refresh() |
| 998 |
|
| 999 |
@t.overload |
| 1000 |
def display_message( |
| 1001 |
self, |
| 1002 |
cmd: str, |
| 1003 |
get_text: t.Literal[True], |
| 1004 |
*, |
| 1005 |
format_string: str | None = ..., |
| 1006 |
all_formats: bool | None = ..., |
| 1007 |
verbose: bool | None = ..., |
| 1008 |
no_expand: bool | None = ..., |
| 1009 |
target_client: str | None = ..., |
| 1010 |
delay: int | None = ..., |
| 1011 |
notify: bool | None = ..., |
| 1012 |
) -> list[str]: ... |
| 1013 |
|
| 1014 |
@t.overload |
| 1015 |
def display_message( |
| 1016 |
self, |
| 1017 |
cmd: str, |
| 1018 |
get_text: t.Literal[False] = ..., |
| 1019 |
*, |
| 1020 |
format_string: str | None = ..., |
| 1021 |
all_formats: bool | None = ..., |
| 1022 |
verbose: bool | None = ..., |
| 1023 |
no_expand: bool | None = ..., |
| 1024 |
target_client: str | None = ..., |
| 1025 |
delay: int | None = ..., |
| 1026 |
notify: bool | None = ..., |
| 1027 |
) -> None: ... |
| 1028 |
|
| 1029 |
def display_message( |
| 1030 |
self, |
| 1031 |
cmd: str, |
| 1032 |
get_text: bool = False, |
| 1033 |
*, |
| 1034 |
format_string: str | None = None, |
| 1035 |
all_formats: bool | None = None, |
| 1036 |
verbose: bool | None = None, |
| 1037 |
no_expand: bool | None = None, |
| 1038 |
target_client: str | None = None, |
| 1039 |
delay: int | None = None, |
| 1040 |
notify: bool | None = None, |
| 1041 |
) -> list[str] | None: |
| 1042 |
"""Display message at window scope via ``$ tmux display-message``. |
| 1043 |
|
| 1044 |
Like :meth:`Pane.display_message` but auto-injects ``-t @<window-id>`` |
| 1045 |
instead of a pane id. Window-scoped format reads such as |
| 1046 |
``#{window_zoomed_flag}`` or ``#{window_active_clients}`` no longer |
| 1047 |
require dropping to :meth:`Window.cmd`. |
| 1048 |
|
| 1049 |
Parameters |
| 1050 |
---------- |
| 1051 |
cmd : str |
| 1052 |
Format string to display or evaluate (e.g. |
| 1053 |
``"#{window_zoomed_flag}"``). |
| 1054 |
|
| 1055 |
.. versionadded:: 0.57 |
| 1056 |
get_text : bool, optional |
| 1057 |
Return tmux's stdout instead of rendering to the status line |
| 1058 |
(``-p`` flag). |
| 1059 |
|
| 1060 |
.. versionadded:: 0.57 |
| 1061 |
format_string : str, optional |
| 1062 |
Alternative format template (``-F`` flag). |
| 1063 |
|
| 1064 |
.. versionadded:: 0.57 |
| 1065 |
all_formats : bool, optional |
| 1066 |
List all format variables (``-a`` flag). |
| 1067 |
|
| 1068 |
.. versionadded:: 0.57 |
| 1069 |
verbose : bool, optional |
| 1070 |
Show format variable types (``-v`` flag). |
| 1071 |
|
| 1072 |
.. versionadded:: 0.57 |
| 1073 |
no_expand : bool, optional |
| 1074 |
Output the literal string without format expansion (``-l`` flag). |
| 1075 |
Requires tmux 3.4+. |
| 1076 |
|
| 1077 |
.. versionadded:: 0.57 |
| 1078 |
target_client : str, optional |
| 1079 |
Target client (``-c`` flag). |
| 1080 |
|
| 1081 |
.. versionadded:: 0.57 |
| 1082 |
delay : int, optional |
| 1083 |
Display time in milliseconds (``-d`` flag). |
| 1084 |
|
| 1085 |
.. versionadded:: 0.57 |
| 1086 |
notify : bool, optional |
| 1087 |
Do not wait for input (``-N`` flag). |
| 1088 |
|
| 1089 |
.. versionadded:: 0.57 |
| 1090 |
|
| 1091 |
Returns |
| 1092 |
------- |
| 1093 |
list[str] | None |
| 1094 |
Message output if ``get_text`` is True, otherwise ``None``. |
| 1095 |
|
| 1096 |
Examples |
| 1097 |
-------- |
| 1098 |
Read the window's id format: |
| 1099 |
|
| 1100 |
>>> result = window.display_message("#{window_id}", get_text=True) |
| 1101 |
>>> result[0].startswith("@") |
| 1102 |
True |
| 1103 |
|
| 1104 |
Check zoom state (a common gap-#670 use case): |
| 1105 |
|
| 1106 |
>>> result = window.display_message( |
| 1107 |
... "#{window_zoomed_flag}", get_text=True |
| 1108 |
... ) |
| 1109 |
>>> result[0] in {"0", "1"} |
| 1110 |
True |
| 1111 |
|
| 1112 |
Notes |
| 1113 |
----- |
| 1114 |
Stderr from tmux is reported via :func:`warnings.warn`, not raised. |
| 1115 |
Callers that want to escalate to an exception can wrap the call in |
| 1116 |
:func:`warnings.catch_warnings` with ``filterwarnings("error")``. |
| 1117 |
|
| 1118 |
.. versionchanged:: 0.57 |
| 1119 |
Reports stderr via :func:`warnings.warn` instead of raising. |
| 1120 |
""" |
| 1121 |
tmux_args: tuple[str, ...] = () |
| 1122 |
|
| 1123 |
if get_text: |
| 1124 |
tmux_args += ("-p",) |
| 1125 |
|
| 1126 |
if all_formats: |
| 1127 |
tmux_args += ("-a",) |
| 1128 |
|
| 1129 |
if verbose: |
| 1130 |
tmux_args += ("-v",) |
| 1131 |
|
| 1132 |
if no_expand: |
| 1133 |
if has_gte_version("3.4", tmux_bin=self.server.tmux_bin): |
| 1134 |
tmux_args += ("-l",) |
| 1135 |
else: |
| 1136 |
warnings.warn( |
| 1137 |
"no_expand requires tmux 3.4+, ignoring", |
| 1138 |
stacklevel=2, |
| 1139 |
) |
| 1140 |
|
| 1141 |
if notify: |
| 1142 |
tmux_args += ("-N",) |
| 1143 |
|
| 1144 |
if target_client is not None: |
| 1145 |
tmux_args += ("-c", target_client) |
| 1146 |
|
| 1147 |
if delay is not None: |
| 1148 |
tmux_args += ("-d", str(delay)) |
| 1149 |
|
| 1150 |
if format_string is not None: |
| 1151 |
tmux_args += ("-F", format_string) |
| 1152 |
|
| 1153 |
if cmd: |
| 1154 |
tmux_args += (cmd,) |
| 1155 |
|
| 1156 |
proc = self.cmd("display-message", *tmux_args) |
| 1157 |
if proc.stderr: |
| 1158 |
warnings.warn( |
| 1159 |
f"display-message: {'; '.join(proc.stderr)}", |
| 1160 |
stacklevel=2, |
| 1161 |
) |
| 1162 |
|
| 1163 |
if get_text: |
| 1164 |
return proc.stdout |
| 1165 |
|
| 1166 |
return None |
| 1167 |
|
| 1168 |
def rename_window(self, new_name: str) -> Window: |
| 1169 |
"""Rename window. |
| 1170 |
|
| 1171 |
Parameters |
| 1172 |
---------- |
| 1173 |
new_name : str |
| 1174 |
name of the window |
| 1175 |
|
| 1176 |
Examples |
| 1177 |
-------- |
| 1178 |
>>> window = session.active_window |
| 1179 |
|
| 1180 |
>>> window.rename_window('My project') |
| 1181 |
Window(@1 1:My project, Session($1 ...)) |
| 1182 |
|
| 1183 |
>>> window.rename_window('New name') |
| 1184 |
Window(@1 1:New name, Session($1 ...)) |
| 1185 |
""" |
| 1186 |
lex = shlex.shlex(new_name) |
| 1187 |
lex.escape = " " |
| 1188 |
lex.whitespace_split = False |
| 1189 |
|
| 1190 |
proc = self.cmd("rename-window", new_name) |
| 1191 |
raise_if_stderr(proc, "rename-window") |
| 1192 |
|
| 1193 |
self.window_name = new_name |
| 1194 |
self.refresh() |
| 1195 |
|
| 1196 |
extra: dict[str, str] = { |
| 1197 |
"tmux_subcommand": "rename-window", |
| 1198 |
} |
| 1199 |
if self.window_name is not None: |
| 1200 |
extra["tmux_window"] = str(self.window_name) |
| 1201 |
if self.window_id is not None: |
| 1202 |
extra["tmux_target"] = str(self.window_id) |
| 1203 |
logger.info("window renamed", extra=extra) |
| 1204 |
|
| 1205 |
return self |
| 1206 |
|
| 1207 |
def kill( |
| 1208 |
self, |
| 1209 |
all_except: bool | None = None, |
| 1210 |
) -> None: |
| 1211 |
"""Kill :class:`Window`. |
| 1212 |
|
| 1213 |
``$ tmux kill-window``. |
| 1214 |
|
| 1215 |
Examples |
| 1216 |
-------- |
| 1217 |
Kill a window: |
| 1218 |
|
| 1219 |
>>> window_1 = session.new_window() |
| 1220 |
|
| 1221 |
>>> window_1 in session.windows |
| 1222 |
True |
| 1223 |
|
| 1224 |
>>> window_1.kill() |
| 1225 |
|
| 1226 |
>>> window_1 not in session.windows |
| 1227 |
True |
| 1228 |
|
| 1229 |
Kill all windows except the current one: |
| 1230 |
|
| 1231 |
>>> one_window_to_rule_them_all = session.new_window() |
| 1232 |
|
| 1233 |
>>> other_windows = session.new_window( |
| 1234 |
... ), session.new_window() |
| 1235 |
|
| 1236 |
>>> all([w in session.windows for w in other_windows]) |
| 1237 |
True |
| 1238 |
|
| 1239 |
>>> one_window_to_rule_them_all.kill(all_except=True) |
| 1240 |
|
| 1241 |
>>> all([w not in session.windows for w in other_windows]) |
| 1242 |
True |
| 1243 |
|
| 1244 |
>>> one_window_to_rule_them_all in session.windows |
| 1245 |
True |
| 1246 |
""" |
| 1247 |
flags: tuple[str, ...] = () |
| 1248 |
|
| 1249 |
if all_except: |
| 1250 |
flags += ("-a",) |
| 1251 |
|
| 1252 |
proc = self.cmd( |
| 1253 |
"kill-window", |
| 1254 |
*flags, |
| 1255 |
) |
| 1256 |
|
| 1257 |
raise_if_stderr(proc, "kill-window") |
| 1258 |
|
| 1259 |
msg = "other windows killed" if all_except else "window killed" |
| 1260 |
extra: dict[str, str] = { |
| 1261 |
"tmux_subcommand": "kill-window", |
| 1262 |
} |
| 1263 |
if self.window_name is not None: |
| 1264 |
extra["tmux_window"] = str(self.window_name) |
| 1265 |
if self.window_id is not None: |
| 1266 |
extra["tmux_target"] = str(self.window_id) |
| 1267 |
logger.info(msg, extra=extra) |
| 1268 |
|
| 1269 |
def move_window( |
| 1270 |
self, |
| 1271 |
destination: str = "", |
| 1272 |
session: str | None = None, |
| 1273 |
*, |
| 1274 |
after: bool | None = None, |
| 1275 |
before: bool | None = None, |
| 1276 |
no_select: bool | None = None, |
| 1277 |
kill_target: bool | None = None, |
| 1278 |
renumber: bool | None = None, |
| 1279 |
) -> Window: |
| 1280 |
"""Move current :class:`Window` object ``$ tmux move-window``. |
| 1281 |
|
| 1282 |
Parameters |
| 1283 |
---------- |
| 1284 |
destination : str, optional |
| 1285 |
The ``target window`` or index to move the window to, default: |
| 1286 |
empty string. |
| 1287 |
session : str, optional |
| 1288 |
The ``target session`` or index to move the window to, default: |
| 1289 |
current session. |
| 1290 |
after : bool, optional |
| 1291 |
Insert after the target window (``-a`` flag). |
| 1292 |
|
| 1293 |
.. versionadded:: 0.56 |
| 1294 |
before : bool, optional |
| 1295 |
Insert before the target window (``-b`` flag). |
| 1296 |
|
| 1297 |
.. versionadded:: 0.56 |
| 1298 |
no_select : bool, optional |
| 1299 |
Do not make the moved window the current window (``-d`` flag). |
| 1300 |
|
| 1301 |
.. versionadded:: 0.56 |
| 1302 |
kill_target : bool, optional |
| 1303 |
Kill the target window if it exists (``-k`` flag). |
| 1304 |
|
| 1305 |
.. versionadded:: 0.56 |
| 1306 |
renumber : bool, optional |
| 1307 |
Renumber all windows in sequential order (``-r`` flag). This is a |
| 1308 |
standalone operation — when used, no move is performed and other |
| 1309 |
parameters are ignored. |
| 1310 |
|
| 1311 |
.. versionadded:: 0.56 |
| 1312 |
|
| 1313 |
Returns |
| 1314 |
------- |
| 1315 |
:class:`Window` |
| 1316 |
Self, for method chaining. |
| 1317 |
|
| 1318 |
Raises |
| 1319 |
------ |
| 1320 |
:exc:`libtmux.exc.LibTmuxException` |
| 1321 |
If tmux returns an error. |
| 1322 |
""" |
| 1323 |
tmux_args: tuple[str, ...] = () |
| 1324 |
|
| 1325 |
if after: |
| 1326 |
tmux_args += ("-a",) |
| 1327 |
|
| 1328 |
if before: |
| 1329 |
tmux_args += ("-b",) |
| 1330 |
|
| 1331 |
if no_select: |
| 1332 |
tmux_args += ("-d",) |
| 1333 |
|
| 1334 |
if kill_target: |
| 1335 |
tmux_args += ("-k",) |
| 1336 |
|
| 1337 |
if renumber: |
| 1338 |
tmux_args += ("-r",) |
| 1339 |
|
| 1340 |
session = session or self.session_id |
| 1341 |
proc = self.cmd( |
| 1342 |
"move-window", |
| 1343 |
*tmux_args, |
| 1344 |
f"-s{self.session_id}:{self.window_index}", |
| 1345 |
target=f"{session}:{destination}", |
| 1346 |
) |
| 1347 |
|
| 1348 |
raise_if_stderr(proc, "move-window") |
| 1349 |
|
| 1350 |
self.refresh() |
| 1351 |
|
| 1352 |
return self |
| 1353 |
|
| 1354 |
def new_window( |
| 1355 |
self, |
| 1356 |
window_name: str | None = None, |
| 1357 |
*, |
| 1358 |
start_directory: None = None, |
| 1359 |
attach: bool = False, |
| 1360 |
window_index: str = "", |
| 1361 |
window_shell: str | None = None, |
| 1362 |
environment: dict[str, str] | None = None, |
| 1363 |
direction: WindowDirection | None = None, |
| 1364 |
) -> Window: |
| 1365 |
"""Create new window respective of current window's position. |
| 1366 |
|
| 1367 |
See Also |
| 1368 |
-------- |
| 1369 |
:meth:`Session.new_window()` |
| 1370 |
|
| 1371 |
Examples |
| 1372 |
-------- |
| 1373 |
>>> window_initial = session.new_window(window_name='Example') |
| 1374 |
>>> window_initial |
| 1375 |
Window(@... 2:Example, Session($1 libtmux_...)) |
| 1376 |
>>> window_initial.window_index |
| 1377 |
'2' |
| 1378 |
|
| 1379 |
>>> window_before = window_initial.new_window( |
| 1380 |
... window_name='Window before', direction=WindowDirection.Before) |
| 1381 |
>>> window_initial.refresh() |
| 1382 |
>>> window_before |
| 1383 |
Window(@... 2:Window before, Session($1 libtmux_...)) |
| 1384 |
>>> window_initial |
| 1385 |
Window(@... 3:Example, Session($1 libtmux_...)) |
| 1386 |
|
| 1387 |
>>> window_after = window_initial.new_window( |
| 1388 |
... window_name='Window after', direction=WindowDirection.After) |
| 1389 |
>>> window_initial.refresh() |
| 1390 |
>>> window_after.refresh() |
| 1391 |
>>> window_after |
| 1392 |
Window(@... 4:Window after, Session($1 libtmux_...)) |
| 1393 |
>>> window_initial |
| 1394 |
Window(@... 3:Example, Session($1 libtmux_...)) |
| 1395 |
>>> window_before |
| 1396 |
Window(@... 2:Window before, Session($1 libtmux_...)) |
| 1397 |
""" |
| 1398 |
return self.session.new_window( |
| 1399 |
window_name=window_name, |
| 1400 |
start_directory=start_directory, |
| 1401 |
attach=attach, |
| 1402 |
window_index=window_index, |
| 1403 |
window_shell=window_shell, |
| 1404 |
environment=environment, |
| 1405 |
direction=direction, |
| 1406 |
target_window=self.window_id, |
| 1407 |
) |
| 1408 |
|
| 1409 |
|
| 1410 |
|
| 1411 |
|
| 1412 |
def select(self) -> Window: |
| 1413 |
"""Select window. |
| 1414 |
|
| 1415 |
To select a window object asynchrously. If a ``window`` object exists |
| 1416 |
and is no longer the current window, ``w.select_window()`` |
| 1417 |
will make ``w`` the current window. |
| 1418 |
|
| 1419 |
Examples |
| 1420 |
-------- |
| 1421 |
>>> window = session.active_window |
| 1422 |
>>> new_window = session.new_window() |
| 1423 |
>>> session.refresh() |
| 1424 |
>>> active_windows = [w for w in session.windows if w.window_active == '1'] |
| 1425 |
|
| 1426 |
>>> new_window.window_active == '1' |
| 1427 |
False |
| 1428 |
|
| 1429 |
>>> new_window.select() |
| 1430 |
Window(...) |
| 1431 |
|
| 1432 |
>>> new_window.window_active == '1' |
| 1433 |
True |
| 1434 |
""" |
| 1435 |
proc = self.cmd("select-window") |
| 1436 |
|
| 1437 |
raise_if_stderr(proc, "select-window") |
| 1438 |
|
| 1439 |
self.refresh() |
| 1440 |
|
| 1441 |
return self |
| 1442 |
|
| 1443 |
|
| 1444 |
|
| 1445 |
|
| 1446 |
@property |
| 1447 |
def active_pane(self) -> Pane | None: |
| 1448 |
"""Return attached :class:`Pane`.""" |
| 1449 |
panes = self.panes.filter(pane_active="1") |
| 1450 |
if len(panes) > 0: |
| 1451 |
return panes[0] |
| 1452 |
return None |
| 1453 |
|
| 1454 |
|
| 1455 |
|
| 1456 |
|
| 1457 |
def __eq__(self, other: object) -> bool: |
| 1458 |
"""Equal operator for :class:`Window` object.""" |
| 1459 |
if isinstance(other, Window): |
| 1460 |
return self.window_id == other.window_id |
| 1461 |
return False |
| 1462 |
|
| 1463 |
def __repr__(self) -> str: |
| 1464 |
"""Representation of :class:`Window` object.""" |
| 1465 |
return ( |
| 1466 |
f"{self.__class__.__name__}({self.window_id} " |
| 1467 |
f"{self.window_index}:{self.window_name}, {self.session})" |
| 1468 |
) |
| 1469 |
|
| 1470 |
|
| 1471 |
|
| 1472 |
|
| 1473 |
@property |
| 1474 |
def id(self) -> str | None: |
| 1475 |
"""Alias of :attr:`Window.window_id`. |
| 1476 |
|
| 1477 |
>>> window.id |
| 1478 |
'@1' |
| 1479 |
|
| 1480 |
>>> window.id == window.window_id |
| 1481 |
True |
| 1482 |
""" |
| 1483 |
return self.window_id |
| 1484 |
|
| 1485 |
@property |
| 1486 |
def name(self) -> str | None: |
| 1487 |
"""Alias of :attr:`Window.window_name`. |
| 1488 |
|
| 1489 |
>>> window.name |
| 1490 |
'...' |
| 1491 |
|
| 1492 |
>>> window.name == window.window_name |
| 1493 |
True |
| 1494 |
""" |
| 1495 |
return self.window_name |
| 1496 |
|
| 1497 |
@property |
| 1498 |
def index(self) -> str | None: |
| 1499 |
"""Alias of :attr:`Window.window_index`. |
| 1500 |
|
| 1501 |
>>> window.index |
| 1502 |
'1' |
| 1503 |
|
| 1504 |
>>> window.index == window.window_index |
| 1505 |
True |
| 1506 |
""" |
| 1507 |
return self.window_index |
| 1508 |
|
| 1509 |
@property |
| 1510 |
def height(self) -> str | None: |
| 1511 |
"""Alias of :attr:`Window.window_height`. |
| 1512 |
|
| 1513 |
>>> window.height.isdigit() |
| 1514 |
True |
| 1515 |
|
| 1516 |
>>> window.height == window.window_height |
| 1517 |
True |
| 1518 |
""" |
| 1519 |
return self.window_height |
| 1520 |
|
| 1521 |
@property |
| 1522 |
def width(self) -> str | None: |
| 1523 |
"""Alias of :attr:`Window.window_width`. |
| 1524 |
|
| 1525 |
>>> window.width.isdigit() |
| 1526 |
True |
| 1527 |
|
| 1528 |
>>> window.width == window.window_width |
| 1529 |
True |
| 1530 |
""" |
| 1531 |
return self.window_width |
| 1532 |
|
| 1533 |
|
| 1534 |
|
| 1535 |
|
| 1536 |
def split_window( |
| 1537 |
self, |
| 1538 |
target: int | str | None = None, |
| 1539 |
start_directory: StrPath | None = None, |
| 1540 |
attach: bool = False, |
| 1541 |
vertical: bool = True, |
| 1542 |
shell: str | None = None, |
| 1543 |
size: str | int | None = None, |
| 1544 |
percent: int | None = None, |
| 1545 |
environment: dict[str, str] | None = None, |
| 1546 |
) -> Pane: |
| 1547 |
"""Split window and return the created :class:`Pane`. |
| 1548 |
|
| 1549 |
Notes |
| 1550 |
----- |
| 1551 |
.. deprecated:: 0.33.0 |
| 1552 |
|
| 1553 |
Deprecated in favor of :meth:`.split()`. |
| 1554 |
|
| 1555 |
.. versionchanged:: 0.28.0 |
| 1556 |
|
| 1557 |
``attach`` default changed from ``True`` to ``False``. |
| 1558 |
|
| 1559 |
.. deprecated:: 0.28.0 |
| 1560 |
|
| 1561 |
``percent=25`` deprecated in favor of ``size="25%"``. |
| 1562 |
""" |
| 1563 |
raise exc.DeprecatedError( |
| 1564 |
deprecated="Window.split_window()", |
| 1565 |
replacement="Window.split()", |
| 1566 |
version="0.33.0", |
| 1567 |
) |
| 1568 |
|
| 1569 |
@property |
| 1570 |
def attached_pane(self) -> Pane | None: |
| 1571 |
"""Return attached :class:`Pane`. |
| 1572 |
|
| 1573 |
Notes |
| 1574 |
----- |
| 1575 |
.. deprecated:: 0.31 |
| 1576 |
|
| 1577 |
Deprecated in favor of :meth:`.active_pane`. |
| 1578 |
""" |
| 1579 |
raise exc.DeprecatedError( |
| 1580 |
deprecated="Window.attached_pane", |
| 1581 |
replacement="Window.active_pane", |
| 1582 |
version="0.31.0", |
| 1583 |
) |
| 1584 |
|
| 1585 |
def select_window(self) -> Window: |
| 1586 |
"""Select window. |
| 1587 |
|
| 1588 |
Notes |
| 1589 |
----- |
| 1590 |
.. deprecated:: 0.30 |
| 1591 |
|
| 1592 |
Deprecated in favor of :meth:`.select()`. |
| 1593 |
""" |
| 1594 |
raise exc.DeprecatedError( |
| 1595 |
deprecated="Window.select_window()", |
| 1596 |
replacement="Window.select()", |
| 1597 |
version="0.30.0", |
| 1598 |
) |
| 1599 |
|
| 1600 |
def kill_window(self) -> None: |
| 1601 |
"""Kill the current :class:`Window` object. ``$ tmux kill-window``. |
| 1602 |
|
| 1603 |
Notes |
| 1604 |
----- |
| 1605 |
.. deprecated:: 0.30 |
| 1606 |
|
| 1607 |
Deprecated in favor of :meth:`.kill()`. |
| 1608 |
""" |
| 1609 |
raise exc.DeprecatedError( |
| 1610 |
deprecated="Window.kill_window()", |
| 1611 |
replacement="Window.kill()", |
| 1612 |
version="0.30.0", |
| 1613 |
) |
| 1614 |
|
| 1615 |
def set_window_option( |
| 1616 |
self, |
| 1617 |
option: str, |
| 1618 |
value: int | str, |
| 1619 |
) -> Window: |
| 1620 |
"""Set option for tmux window. Deprecated by :meth:`Window.set_option()`. |
| 1621 |
|
| 1622 |
.. deprecated:: 0.50 |
| 1623 |
|
| 1624 |
Deprecated by :meth:`Window.set_option()`. |
| 1625 |
|
| 1626 |
""" |
| 1627 |
warnings.warn( |
| 1628 |
"Window.set_window_option() is deprecated in favor of Window.set_option()", |
| 1629 |
category=DeprecationWarning, |
| 1630 |
stacklevel=2, |
| 1631 |
) |
| 1632 |
return self.set_option(option=option, value=value) |
| 1633 |
|
| 1634 |
def show_window_options(self, g: bool | None = False) -> WindowOptionDict: |
| 1635 |
"""Show options for tmux window. Deprecated by :meth:`Window.show_options()`. |
| 1636 |
|
| 1637 |
.. deprecated:: 0.50 |
| 1638 |
|
| 1639 |
Deprecated by :meth:`Window.show_options()`. |
| 1640 |
|
| 1641 |
""" |
| 1642 |
warnings.warn( |
| 1643 |
"Window.show_window_options() is deprecated" |
| 1644 |
" in favor of Window.show_options()", |
| 1645 |
category=DeprecationWarning, |
| 1646 |
stacklevel=2, |
| 1647 |
) |
| 1648 |
return self.show_options( |
| 1649 |
global_=g or False, |
| 1650 |
scope=OptionScope.Window, |
| 1651 |
) |
| 1652 |
|
| 1653 |
def show_window_option( |
| 1654 |
self, |
| 1655 |
option: str, |
| 1656 |
g: bool = False, |
| 1657 |
) -> str | int | None: |
| 1658 |
"""Return option for target window. Deprecated by :meth:`Window.show_option()`. |
| 1659 |
|
| 1660 |
.. deprecated:: 0.50 |
| 1661 |
|
| 1662 |
Deprecated by :meth:`Window.show_option()`. |
| 1663 |
|
| 1664 |
""" |
| 1665 |
warnings.warn( |
| 1666 |
"Window.show_window_option() is deprecated" |
| 1667 |
" in favor of Window.show_option()", |
| 1668 |
category=DeprecationWarning, |
| 1669 |
stacklevel=2, |
| 1670 |
) |
| 1671 |
return self.show_option( |
| 1672 |
option=option, |
| 1673 |
global_=g, |
| 1674 |
) |
| 1675 |
|
| 1676 |
def get(self, key: str, default: t.Any | None = None) -> t.Any: |
| 1677 |
"""Return key-based lookup. Deprecated by attributes. |
| 1678 |
|
| 1679 |
.. deprecated:: 0.17 |
| 1680 |
|
| 1681 |
Deprecated by attribute lookup.e.g. ``window['window_name']`` is now |
| 1682 |
accessed via ``window.window_name``. |
| 1683 |
|
| 1684 |
""" |
| 1685 |
raise exc.DeprecatedError( |
| 1686 |
deprecated="Window.get()", |
| 1687 |
replacement="direct attribute access (e.g., window.window_name)", |
| 1688 |
version="0.17.0", |
| 1689 |
) |
| 1690 |
|
| 1691 |
def __getitem__(self, key: str) -> t.Any: |
| 1692 |
"""Return item lookup by key. Deprecated in favor of attributes. |
| 1693 |
|
| 1694 |
.. deprecated:: 0.17 |
| 1695 |
|
| 1696 |
Deprecated in favor of attributes. e.g. ``window['window_name']`` is now |
| 1697 |
accessed via ``window.window_name``. |
| 1698 |
|
| 1699 |
""" |
| 1700 |
raise exc.DeprecatedError( |
| 1701 |
deprecated="Window[key] lookup", |
| 1702 |
replacement="direct attribute access (e.g., window.window_name)", |
| 1703 |
version="0.17.0", |
| 1704 |
) |
| 1705 |
|
| 1706 |
def get_by_id(self, pane_id: str) -> Pane | None: |
| 1707 |
"""Return pane by id. Deprecated in favor of :meth:`.panes.get()`. |
| 1708 |
|
| 1709 |
.. deprecated:: 0.16 |
| 1710 |
|
| 1711 |
Deprecated by :meth:`.panes.get()`. |
| 1712 |
|
| 1713 |
""" |
| 1714 |
raise exc.DeprecatedError( |
| 1715 |
deprecated="Window.get_by_id()", |
| 1716 |
replacement="Window.panes.get(pane_id=..., default=None)", |
| 1717 |
version="0.16.0", |
| 1718 |
) |
| 1719 |
|
| 1720 |
def where(self, kwargs: dict[str, t.Any]) -> list[Pane]: |
| 1721 |
"""Filter through panes, return list of :class:`Pane`. |
| 1722 |
|
| 1723 |
.. deprecated:: 0.17 |
| 1724 |
|
| 1725 |
Deprecated by :meth:`.panes.filter()`. |
| 1726 |
|
| 1727 |
""" |
| 1728 |
raise exc.DeprecatedError( |
| 1729 |
deprecated="Window.where()", |
| 1730 |
replacement="Window.panes.filter()", |
| 1731 |
version="0.17.0", |
| 1732 |
) |
| 1733 |
|
| 1734 |
def find_where(self, kwargs: dict[str, t.Any]) -> Pane | None: |
| 1735 |
"""Filter through panes, return first :class:`Pane`. |
| 1736 |
|
| 1737 |
.. deprecated:: 0.17 |
| 1738 |
|
| 1739 |
Slated to be removed in favor of :meth:`.panes.get()`. |
| 1740 |
|
| 1741 |
""" |
| 1742 |
raise exc.DeprecatedError( |
| 1743 |
deprecated="Window.find_where()", |
| 1744 |
replacement="Window.panes.get(default=None, **kwargs)", |
| 1745 |
version="0.17.0", |
| 1746 |
) |
| 1747 |
|
| 1748 |
def _list_panes(self) -> list[PaneDict]: |
| 1749 |
"""Return list of panes (deprecated in favor of :meth:`.panes`). |
| 1750 |
|
| 1751 |
.. deprecated:: 0.17 |
| 1752 |
|
| 1753 |
Slated to be removed in favor of :attr:`.panes`. |
| 1754 |
|
| 1755 |
""" |
| 1756 |
raise exc.DeprecatedError( |
| 1757 |
deprecated="Window._list_panes()", |
| 1758 |
replacement="Window.panes property", |
| 1759 |
version="0.17.0", |
| 1760 |
) |
| 1761 |
|
| 1762 |
@property |
| 1763 |
def _panes(self) -> list[PaneDict]: |
| 1764 |
"""Property / alias to return :meth:`~._list_panes`. |
| 1765 |
|
| 1766 |
.. deprecated:: 0.17 |
| 1767 |
|
| 1768 |
Slated to be removed in favor of :attr:`.panes`. |
| 1769 |
|
| 1770 |
""" |
| 1771 |
raise exc.DeprecatedError( |
| 1772 |
deprecated="Window._panes", |
| 1773 |
replacement="Window.panes property", |
| 1774 |
version="0.17.0", |
| 1775 |
) |
| 1776 |
|
| 1777 |
def list_panes(self) -> list[Pane]: |
| 1778 |
"""Return list of :class:`Pane` for the window. |
| 1779 |
|
| 1780 |
.. deprecated:: 0.17 |
| 1781 |
|
| 1782 |
Slated to be removed in favor of :attr:`.panes`. |
| 1783 |
|
| 1784 |
""" |
| 1785 |
raise exc.DeprecatedError( |
| 1786 |
deprecated="Window.list_panes()", |
| 1787 |
replacement="Window.panes property", |
| 1788 |
version="0.17.0", |
| 1789 |
) |
| 1790 |
|
| 1791 |
@property |
| 1792 |
def children(self) -> QueryList[Pane]: |
| 1793 |
"""Was used by TmuxRelationalObject (but that's longer used in this class). |
| 1794 |
|
| 1795 |
.. deprecated:: 0.17 |
| 1796 |
|
| 1797 |
Slated to be removed in favor of :attr:`.panes`. |
| 1798 |
|
| 1799 |
""" |
| 1800 |
raise exc.DeprecatedError( |
| 1801 |
deprecated="Window.children", |
| 1802 |
replacement="Window.panes property", |
| 1803 |
version="0.17.0", |
| 1804 |
) |