|
10 | 10 | import warnings
|
11 | 11 | from typing import overload
|
12 | 12 |
|
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 | +) |
14 | 18 | from libtmux.neo import Obj, fetch_obj
|
15 | 19 |
|
16 | 20 | from . import exc
|
@@ -125,31 +129,99 @@ def cmd(self, cmd: str, *args: t.Any, **kwargs: t.Any) -> tmux_cmd:
|
125 | 129 | Commands (tmux-like)
|
126 | 130 | """
|
127 | 131 |
|
128 |
| - def resize_pane(self, *args: t.Any, **kwargs: t.Any) -> "Pane": |
| 132 | + def resize_pane( |
| 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": |
129 | 147 | """Resize tmux pane.
|
130 | 148 |
|
131 | 149 | Parameters
|
132 | 150 | ----------
|
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 |
135 | 154 |
|
136 |
| - Other Parameters |
137 |
| - ---------------- |
138 |
| - height : int |
| 155 | + height : int, optional |
139 | 156 | ``resize-pane -y`` dimensions
|
140 |
| - width : int |
| 157 | + width : int, optional |
141 | 158 | ``resize-pane -x`` dimensions
|
142 | 159 |
|
| 160 | + zoom : bool |
| 161 | + expand pane |
| 162 | +
|
| 163 | + mouse : bool |
| 164 | + resize via mouse |
| 165 | +
|
| 166 | + trim_below : bool |
| 167 | + trim below cursor |
| 168 | +
|
143 | 169 | Raises
|
144 | 170 | ------
|
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``. |
146 | 186 | """
|
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) |
153 | 225 |
|
154 | 226 | if proc.stderr:
|
155 | 227 | raise exc.LibTmuxException(proc.stderr)
|
|
0 commit comments