1- # flake8: NOQA: W605
21"""Pythonization of the :ref:`tmux(1)` pane.
32
43libtmux.pane
1110import warnings
1211from typing import overload
1312
14- 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+ )
1518from libtmux .neo import Obj , fetch_obj
1619
1720from . import exc
@@ -71,7 +74,11 @@ class Pane(Obj):
7174 def refresh (self ) -> None :
7275 """Refresh pane attributes from tmux."""
7376 assert isinstance (self .pane_id , str )
74- return super ()._refresh (obj_key = "pane_id" , obj_id = self .pane_id )
77+ return super ()._refresh (
78+ obj_key = "pane_id" ,
79+ obj_id = self .pane_id ,
80+ list_extra_args = ("-a" ,),
81+ )
7582
7683 @classmethod
7784 def from_pane_id (cls , server : "Server" , pane_id : str ) -> "Pane" :
@@ -122,31 +129,99 @@ def cmd(self, cmd: str, *args: t.Any, **kwargs: t.Any) -> tmux_cmd:
122129 Commands (tmux-like)
123130 """
124131
125- 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" :
126147 """Resize tmux pane.
127148
128149 Parameters
129150 ----------
130- target_pane : str
131- ``target_pane``, or ``-U``,``-D``, ``-L``, ``-R``.
151+ adjustment_direction : ResizeAdjustmentDirection, optional
152+ direction to adjust, ``Up``, ``Down``, ``Left``, ``Right``.
153+ adjustment : ResizeAdjustmentDirection, optional
132154
133- Other Parameters
134- ----------------
135- height : int
155+ height : int, optional
136156 ``resize-pane -y`` dimensions
137- width : int
157+ width : int, optional
138158 ``resize-pane -x`` dimensions
139159
160+ zoom : bool
161+ expand pane
162+
163+ mouse : bool
164+ resize via mouse
165+
166+ trim_below : bool
167+ trim below cursor
168+
140169 Raises
141170 ------
142- 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``.
143186 """
144- if "height" in kwargs :
145- proc = self .cmd ("resize-pane" , "-y%s" % int (kwargs ["height" ]))
146- elif "width" in kwargs :
147- proc = self .cmd ("resize-pane" , "-x%s" % int (kwargs ["width" ]))
148- else :
149- 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 )
150225
151226 if proc .stderr :
152227 raise exc .LibTmuxException (proc .stderr )
@@ -442,7 +517,7 @@ def get(self, key: str, default: t.Optional[t.Any] = None) -> t.Any:
442517
443518 .. deprecated:: 0.16
444519
445- Deprecated by attribute lookup. e.g. ``pane['window_name']`` is now
520+ Deprecated by attribute lookup, e.g. ``pane['window_name']`` is now
446521 accessed via ``pane.window_name``.
447522
448523 """
@@ -460,3 +535,38 @@ def __getitem__(self, key: str) -> t.Any:
460535 """
461536 warnings .warn (f"Item lookups, e.g. pane['{ key } '] is deprecated" , stacklevel = 2 )
462537 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+ )
0 commit comments