Skip to content

Commit c2df245

Browse files
committed
feat(Pane.resize): Add support for all arguments as of tmux 3.4
1 parent f197c93 commit c2df245

File tree

2 files changed

+127
-17
lines changed

2 files changed

+127
-17
lines changed

src/libtmux/pane.py

Lines changed: 122 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
import warnings
1111
from typing import overload
1212

13-
from libtmux.common import tmux_cmd
13+
from libtmux.common import has_gte_version, tmux_cmd
14+
from libtmux.constants import (
15+
RESIZE_ADJUSTMENT_DIRECTION_FLAG_MAP,
16+
ResizeAdjustmentDirection,
17+
)
1418
from libtmux.neo import Obj, fetch_obj
1519

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

128-
def resize_pane(self, *args: t.Any, **kwargs: t.Any) -> "Pane":
132+
def resize(
133+
self,
134+
# Adjustments
135+
adjustment_direction: t.Optional[ResizeAdjustmentDirection] = None,
136+
adjustment: t.Optional[int] = None,
137+
# Manual
138+
height: t.Optional[t.Union[str, int]] = None,
139+
width: t.Optional[t.Union[str, int]] = None,
140+
# Zoom
141+
zoom: t.Optional[bool] = None,
142+
# Mouse
143+
mouse: t.Optional[bool] = None,
144+
# Optional flags
145+
trim_below: t.Optional[bool] = None,
146+
) -> "Pane":
129147
"""Resize tmux pane.
130148
131149
Parameters
132150
----------
133-
target_pane : str
134-
``target_pane``, or ``-U``,``-D``, ``-L``, ``-R``.
151+
adjustment_direction : ResizeAdjustmentDirection, optional
152+
direction to adjust, ``Up``, ``Down``, ``Left``, ``Right``.
153+
adjustment : ResizeAdjustmentDirection, optional
135154
136-
Other Parameters
137-
----------------
138-
height : int
155+
height : int, optional
139156
``resize-pane -y`` dimensions
140-
width : int
157+
width : int, optional
141158
``resize-pane -x`` dimensions
142159
160+
zoom : bool
161+
expand pane
162+
163+
mouse : bool
164+
resize via mouse
165+
166+
trim_below : bool
167+
trim below cursor
168+
143169
Raises
144170
------
145-
exc.LibTmuxException
171+
:exc:`exc.LibTmuxException`,
172+
:exc:`exc.PaneAdjustmentDirectionRequiresAdjustment`,
173+
:exc:`exc.RequiresDigitOrPercentage`
174+
175+
Returns
176+
-------
177+
:class:`Pane`
178+
179+
Notes
180+
-----
181+
Three types of resizing are available:
182+
183+
1. Adjustments: ``adjustment_direction`` and ``adjustment``.
184+
2. Manual resizing: ``height`` and / or ``width``.
185+
3. Zoom / Unzoom: ``zoom``.
146186
"""
147-
if "height" in kwargs:
148-
proc = self.cmd("resize-pane", "-y%s" % int(kwargs["height"]))
149-
elif "width" in kwargs:
150-
proc = self.cmd("resize-pane", "-x%s" % int(kwargs["width"]))
151-
else:
152-
proc = self.cmd("resize-pane", args[0])
187+
tmux_args: t.Tuple[str, ...] = ()
188+
189+
## Adjustments
190+
if adjustment_direction:
191+
if adjustment is None:
192+
raise exc.PaneAdjustmentDirectionRequiresAdjustment()
193+
tmux_args += (
194+
f"{RESIZE_ADJUSTMENT_DIRECTION_FLAG_MAP[adjustment_direction]}",
195+
str(adjustment),
196+
)
197+
elif height or width:
198+
## Manual resizing
199+
if height:
200+
if isinstance(height, str):
201+
if height.endswith("%") and not has_gte_version("3.1"):
202+
raise exc.VersionTooLow
203+
if not height.isdigit() and not height.endswith("%"):
204+
raise exc.RequiresDigitOrPercentage
205+
tmux_args += (f"-y{height}",)
206+
207+
if width:
208+
if isinstance(width, str):
209+
if width.endswith("%") and not has_gte_version("3.1"):
210+
raise exc.VersionTooLow
211+
if not width.isdigit() and not width.endswith("%"):
212+
raise exc.RequiresDigitOrPercentage
213+
214+
tmux_args += (f"-x{width}",)
215+
elif zoom:
216+
## Zoom / Unzoom
217+
tmux_args += ("-Z",)
218+
elif mouse:
219+
tmux_args += ("-M",)
220+
221+
if trim_below:
222+
tmux_args += ("-T",)
223+
224+
proc = self.cmd("resize-pane", *tmux_args)
153225

154226
if proc.stderr:
155227
raise exc.LibTmuxException(proc.stderr)
@@ -463,3 +535,38 @@ def __getitem__(self, key: str) -> t.Any:
463535
"""
464536
warnings.warn(f"Item lookups, e.g. pane['{key}'] is deprecated", stacklevel=2)
465537
return getattr(self, key)
538+
539+
def resize_pane(
540+
self,
541+
# Adjustments
542+
adjustment_direction: t.Optional[ResizeAdjustmentDirection] = None,
543+
adjustment: t.Optional[int] = None,
544+
# Manual
545+
height: t.Optional[t.Union[str, int]] = None,
546+
width: t.Optional[t.Union[str, int]] = None,
547+
# Zoom
548+
zoom: t.Optional[bool] = None,
549+
# Mouse
550+
mouse: t.Optional[bool] = None,
551+
# Optional flags
552+
trim_below: t.Optional[bool] = None,
553+
) -> "Pane":
554+
"""Resize pane, deprecated by :meth:`Pane.resize`.
555+
556+
.. deprecated:: 0.28
557+
558+
Deprecated by :meth:`Pane.resize`.
559+
"""
560+
warnings.warn(
561+
"Deprecated: Use Pane.resize() instead of Pane.resize_pane()",
562+
stacklevel=2,
563+
)
564+
return self.resize(
565+
adjustment_direction=adjustment_direction,
566+
adjustment=adjustment,
567+
height=height,
568+
width=width,
569+
zoom=zoom,
570+
mouse=mouse,
571+
trim_below=trim_below,
572+
)

tests/test_dataclasses.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
ObjectDoesNotExist,
1010
QueryList,
1111
)
12+
from libtmux.constants import ResizeAdjustmentDirection
1213
from libtmux.pane import Pane
1314
from libtmux.server import Server
1415
from libtmux.session import Session
@@ -59,8 +60,10 @@ def test_pane(
5960

6061
old_pane_size = pane.pane_height
6162

62-
pane.resize_pane("-D", 25)
63-
pane.resize_pane("-R", 25)
63+
pane.resize_pane(adjustment_direction=ResizeAdjustmentDirection.Down, adjustment=25)
64+
pane.resize_pane(
65+
adjustment_direction=ResizeAdjustmentDirection.Right, adjustment=25
66+
)
6467

6568
assert old_pane_size != pane.pane_height
6669
assert pane.pane_current_command is not None

0 commit comments

Comments
 (0)