Skip to content

Commit

Permalink
feat(Pane.resize): Add support for all arguments as of tmux 3.4
Browse files Browse the repository at this point in the history
  • Loading branch information
tony committed Feb 15, 2024
1 parent f197c93 commit c2df245
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 17 deletions.
137 changes: 122 additions & 15 deletions src/libtmux/pane.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
import warnings
from typing import overload

from libtmux.common import tmux_cmd
from libtmux.common import has_gte_version, tmux_cmd
from libtmux.constants import (
RESIZE_ADJUSTMENT_DIRECTION_FLAG_MAP,
ResizeAdjustmentDirection,
)
from libtmux.neo import Obj, fetch_obj

from . import exc
Expand Down Expand Up @@ -125,31 +129,99 @@ def cmd(self, cmd: str, *args: t.Any, **kwargs: t.Any) -> tmux_cmd:
Commands (tmux-like)
"""

def resize_pane(self, *args: t.Any, **kwargs: t.Any) -> "Pane":
def resize(
self,
# Adjustments
adjustment_direction: t.Optional[ResizeAdjustmentDirection] = None,
adjustment: t.Optional[int] = None,
# Manual
height: t.Optional[t.Union[str, int]] = None,
width: t.Optional[t.Union[str, int]] = None,
# Zoom
zoom: t.Optional[bool] = None,
# Mouse
mouse: t.Optional[bool] = None,
# Optional flags
trim_below: t.Optional[bool] = None,
) -> "Pane":
"""Resize tmux pane.
Parameters
----------
target_pane : str
``target_pane``, or ``-U``,``-D``, ``-L``, ``-R``.
adjustment_direction : ResizeAdjustmentDirection, optional
direction to adjust, ``Up``, ``Down``, ``Left``, ``Right``.
adjustment : ResizeAdjustmentDirection, optional
Other Parameters
----------------
height : int
height : int, optional
``resize-pane -y`` dimensions
width : int
width : int, optional
``resize-pane -x`` dimensions
zoom : bool
expand pane
mouse : bool
resize via mouse
trim_below : bool
trim below cursor
Raises
------
exc.LibTmuxException
:exc:`exc.LibTmuxException`,
:exc:`exc.PaneAdjustmentDirectionRequiresAdjustment`,
:exc:`exc.RequiresDigitOrPercentage`
Returns
-------
:class:`Pane`
Notes
-----
Three types of resizing are available:
1. Adjustments: ``adjustment_direction`` and ``adjustment``.
2. Manual resizing: ``height`` and / or ``width``.
3. Zoom / Unzoom: ``zoom``.
"""
if "height" in kwargs:
proc = self.cmd("resize-pane", "-y%s" % int(kwargs["height"]))
elif "width" in kwargs:
proc = self.cmd("resize-pane", "-x%s" % int(kwargs["width"]))
else:
proc = self.cmd("resize-pane", args[0])
tmux_args: t.Tuple[str, ...] = ()

## Adjustments
if adjustment_direction:
if adjustment is None:
raise exc.PaneAdjustmentDirectionRequiresAdjustment()
tmux_args += (
f"{RESIZE_ADJUSTMENT_DIRECTION_FLAG_MAP[adjustment_direction]}",
str(adjustment),
)
elif height or width:
## Manual resizing
if height:
if isinstance(height, str):
if height.endswith("%") and not has_gte_version("3.1"):
raise exc.VersionTooLow
if not height.isdigit() and not height.endswith("%"):
raise exc.RequiresDigitOrPercentage
tmux_args += (f"-y{height}",)

if width:
if isinstance(width, str):
if width.endswith("%") and not has_gte_version("3.1"):
raise exc.VersionTooLow
if not width.isdigit() and not width.endswith("%"):
raise exc.RequiresDigitOrPercentage

tmux_args += (f"-x{width}",)
elif zoom:
## Zoom / Unzoom
tmux_args += ("-Z",)
elif mouse:
tmux_args += ("-M",)

if trim_below:
tmux_args += ("-T",)

proc = self.cmd("resize-pane", *tmux_args)

if proc.stderr:
raise exc.LibTmuxException(proc.stderr)
Expand Down Expand Up @@ -463,3 +535,38 @@ def __getitem__(self, key: str) -> t.Any:
"""
warnings.warn(f"Item lookups, e.g. pane['{key}'] is deprecated", stacklevel=2)
return getattr(self, key)

def resize_pane(
self,
# Adjustments
adjustment_direction: t.Optional[ResizeAdjustmentDirection] = None,
adjustment: t.Optional[int] = None,
# Manual
height: t.Optional[t.Union[str, int]] = None,
width: t.Optional[t.Union[str, int]] = None,
# Zoom
zoom: t.Optional[bool] = None,
# Mouse
mouse: t.Optional[bool] = None,
# Optional flags
trim_below: t.Optional[bool] = None,
) -> "Pane":
"""Resize pane, deprecated by :meth:`Pane.resize`.
.. deprecated:: 0.28
Deprecated by :meth:`Pane.resize`.
"""
warnings.warn(
"Deprecated: Use Pane.resize() instead of Pane.resize_pane()",
stacklevel=2,
)
return self.resize(
adjustment_direction=adjustment_direction,
adjustment=adjustment,
height=height,
width=width,
zoom=zoom,
mouse=mouse,
trim_below=trim_below,
)
7 changes: 5 additions & 2 deletions tests/test_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
ObjectDoesNotExist,
QueryList,
)
from libtmux.constants import ResizeAdjustmentDirection
from libtmux.pane import Pane
from libtmux.server import Server
from libtmux.session import Session
Expand Down Expand Up @@ -59,8 +60,10 @@ def test_pane(

old_pane_size = pane.pane_height

pane.resize_pane("-D", 25)
pane.resize_pane("-R", 25)
pane.resize_pane(adjustment_direction=ResizeAdjustmentDirection.Down, adjustment=25)
pane.resize_pane(
adjustment_direction=ResizeAdjustmentDirection.Right, adjustment=25
)

assert old_pane_size != pane.pane_height
assert pane.pane_current_command is not None
Expand Down

0 comments on commit c2df245

Please sign in to comment.