Skip to content

Commit b789c5e

Browse files
committed
feat!(Window.resize_window): Add resize_window() for tmux 2.9+
1 parent 76fe89e commit b789c5e

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

src/libtmux/window.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313

1414
from libtmux._internal.query_list import QueryList
1515
from libtmux.common import has_gte_version, tmux_cmd
16+
from libtmux.constants import (
17+
RESIZE_ADJUSTMENT_DIRECTION_FLAG_MAP,
18+
ResizeAdjustmentDirection,
19+
)
1620
from libtmux.neo import Obj, fetch_obj, fetch_objs
1721
from libtmux.pane import Pane
1822

@@ -284,6 +288,87 @@ def split_window(
284288

285289
return Pane.from_pane_id(server=self.server, pane_id=pane_formatters["pane_id"])
286290

291+
def resize_window(
292+
self,
293+
# Adjustments
294+
adjustment_direction: t.Optional[ResizeAdjustmentDirection] = None,
295+
adjustment: t.Optional[int] = None,
296+
# Manual
297+
height: t.Optional[int] = None,
298+
width: t.Optional[int] = None,
299+
# Expand / Shrink
300+
expand: t.Optional[bool] = None,
301+
shrink: t.Optional[bool] = None,
302+
) -> "Window":
303+
"""Resize tmux window.
304+
305+
Parameters
306+
----------
307+
adjustment_direction : ResizeAdjustmentDirection, optional
308+
direction to adjust, ``Up``, ``Down``, ``Left``, ``Right``.
309+
adjustment : ResizeAdjustmentDirection, optional
310+
311+
height : int, optional
312+
``resize-window -y`` dimensions
313+
width : int, optional
314+
``resize-window -x`` dimensions
315+
316+
expand : bool
317+
expand window
318+
shrink : bool
319+
shrink window
320+
321+
Raises
322+
------
323+
:exc:`exc.LibTmuxException`,
324+
:exc:`exc.PaneAdjustmentDirectionRequiresAdjustment`
325+
326+
Returns
327+
-------
328+
:class:`Window`
329+
330+
Notes
331+
-----
332+
Three types of resizing are available:
333+
334+
1. Adjustments: ``adjustment_direction`` and ``adjustment``.
335+
2. Manual resizing: ``height`` and / or ``width``.
336+
3. Expand or shrink: ``expand`` or ``shrink``.
337+
"""
338+
if not has_gte_version("2.9"):
339+
warnings.warn("resize_window() requires tmux 2.9 or newer", stacklevel=2)
340+
return self
341+
342+
tmux_args: t.Tuple[str, ...] = ()
343+
344+
## Adjustments
345+
if adjustment_direction:
346+
if adjustment is None:
347+
raise exc.WindowAdjustmentDirectionRequiresAdjustment()
348+
tmux_args += (
349+
f"{RESIZE_ADJUSTMENT_DIRECTION_FLAG_MAP[adjustment_direction]}",
350+
str(adjustment),
351+
)
352+
elif height or width:
353+
## Manual resizing
354+
if height:
355+
tmux_args += (f"-y{int(height)}",)
356+
if width:
357+
tmux_args += (f"-x{int(width)}",)
358+
elif expand or shrink:
359+
if expand:
360+
tmux_args += ("-A",)
361+
elif shrink:
362+
tmux_args += ("-a",)
363+
364+
proc = self.cmd("resize-window", *tmux_args)
365+
366+
if proc.stderr:
367+
raise exc.LibTmuxException(proc.stderr)
368+
369+
self.refresh()
370+
return self
371+
287372
def last_pane(self) -> t.Optional["Pane"]:
288373
"""Return last pane."""
289374
return self.select_pane("-l")

0 commit comments

Comments
 (0)