1
- # flake8: NOQA: W605
2
1
"""Pythonization of the :ref:`tmux(1)` pane.
3
2
4
3
libtmux.pane
11
10
import warnings
12
11
from typing import overload
13
12
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
+ )
15
18
from libtmux .neo import Obj , fetch_obj
16
19
17
20
from . import exc
@@ -71,7 +74,11 @@ class Pane(Obj):
71
74
def refresh (self ) -> None :
72
75
"""Refresh pane attributes from tmux."""
73
76
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
+ )
75
82
76
83
@classmethod
77
84
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:
122
129
Commands (tmux-like)
123
130
"""
124
131
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" :
126
147
"""Resize tmux pane.
127
148
128
149
Parameters
129
150
----------
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
132
154
133
- Other Parameters
134
- ----------------
135
- height : int
155
+ height : int, optional
136
156
``resize-pane -y`` dimensions
137
- width : int
157
+ width : int, optional
138
158
``resize-pane -x`` dimensions
139
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
+
140
169
Raises
141
170
------
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``.
143
186
"""
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 )
150
225
151
226
if proc .stderr :
152
227
raise exc .LibTmuxException (proc .stderr )
@@ -442,7 +517,7 @@ def get(self, key: str, default: t.Optional[t.Any] = None) -> t.Any:
442
517
443
518
.. deprecated:: 0.16
444
519
445
- Deprecated by attribute lookup. e.g. ``pane['window_name']`` is now
520
+ Deprecated by attribute lookup, e.g. ``pane['window_name']`` is now
446
521
accessed via ``pane.window_name``.
447
522
448
523
"""
@@ -460,3 +535,38 @@ def __getitem__(self, key: str) -> t.Any:
460
535
"""
461
536
warnings .warn (f"Item lookups, e.g. pane['{ key } '] is deprecated" , stacklevel = 2 )
462
537
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