Skip to content

Commit 3ccbac6

Browse files
committed
!squash more
1 parent 7b6ad6d commit 3ccbac6

File tree

2 files changed

+45
-22
lines changed

2 files changed

+45
-22
lines changed

libtmux/pane.py

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
"""
88
import logging
99
import typing as t
10-
from typing import Dict
10+
from typing import Dict, overload
1111

1212
from libtmux.common import tmux_cmd
1313

1414
from . import exc
15-
from .common import TmuxMappingObject, TmuxRelationalObject
15+
from .common import PaneDict, TmuxMappingObject, TmuxRelationalObject
1616

1717
if t.TYPE_CHECKING:
1818
from .server import Server
@@ -61,22 +61,27 @@ class Pane(TmuxMappingObject, TmuxRelationalObject):
6161
server: "Server"
6262
""":class:`libtmux.Server` pane is linked to"""
6363

64-
def __init__(self, window: "Window", **kwargs) -> None:
64+
def __init__(
65+
self,
66+
window: "Window",
67+
# pane_id: t.Optional[t.Union[str, int]] = None,
68+
pane_id: t.Union[str, int],
69+
**kwargs: t.Any,
70+
) -> None:
6571
self.window = window
6672
self.session = self.window.session
6773
self.server = self.session.server
6874

69-
self._pane_id = kwargs["pane_id"]
75+
self._pane_id = pane_id
7076

7177
self.server._update_panes()
7278

7379
@property
74-
def _info(self) -> Dict[str, str]:
75-
80+
def _info(self) -> PaneDict:
7681
attrs = {"pane_id": self._pane_id}
7782

7883
# from https://github.com/serkanyersen/underscore.py
79-
def by(val) -> bool:
84+
def by(val: PaneDict) -> bool:
8085
for key in attrs.keys():
8186
try:
8287
if attrs[key] != val[key]:
@@ -85,11 +90,11 @@ def by(val) -> bool:
8590
return False
8691
return True
8792

88-
# TODO add type hint
89-
target_panes = list(filter(by, self.server._panes))
93+
target_panes = [s for s in self.server._panes if by(s)]
94+
9095
return target_panes[0]
9196

92-
def cmd(self, cmd: str, *args, **kwargs) -> tmux_cmd:
97+
def cmd(self, cmd: str, *args: t.Any, **kwargs: t.Any) -> tmux_cmd:
9398
"""Return :meth:`Server.cmd` defaulting to ``target_pane`` as target.
9499
95100
Send command to tmux with :attr:`pane_id` as ``target-pane``.
@@ -140,7 +145,19 @@ def send_keys(
140145
if enter:
141146
self.enter()
142147

143-
def display_message(self, cmd, get_text=False):
148+
@overload
149+
def display_message(
150+
self, cmd: str, get_text: t.Literal[True]
151+
) -> t.Union[str, t.List[str]]:
152+
...
153+
154+
@overload
155+
def display_message(self, cmd: str, get_text: t.Literal[False]) -> None:
156+
...
157+
158+
def display_message(
159+
self, cmd: str, get_text: bool = False
160+
) -> t.Optional[t.Union[str, t.List[str]]]:
144161
"""
145162
``$ tmux display-message`` to the pane.
146163
@@ -161,10 +178,11 @@ def display_message(self, cmd, get_text=False):
161178
"""
162179
if get_text:
163180
return self.cmd("display-message", "-p", cmd).stdout
164-
else:
165-
self.cmd("display-message", cmd)
166181

167-
def clear(self):
182+
self.cmd("display-message", cmd)
183+
return None
184+
185+
def clear(self) -> None:
168186
"""Clear pane."""
169187
self.send_keys("reset")
170188

@@ -174,7 +192,11 @@ def reset(self) -> None:
174192
self.cmd("send-keys", r"-R \; clear-history")
175193

176194
def split_window(
177-
self, attach=False, vertical=True, start_directory=None, percent=None
195+
self,
196+
attach: bool = False,
197+
vertical: bool = True,
198+
start_directory: t.Optional[str] = None,
199+
percent: t.Optional[int] = None,
178200
) -> "Pane":
179201
"""
180202
Split window at pane and return newly created :class:`Pane`.
@@ -224,7 +246,7 @@ def set_height(self, height: int) -> None:
224246
"""
225247
self.resize_pane(height=height)
226248

227-
def resize_pane(self, *args, **kwargs) -> "Pane":
249+
def resize_pane(self, *args: t.Any, **kwargs: t.Any) -> "Pane":
228250
"""
229251
``$ tmux resize-pane`` of pane and return ``self``.
230252
@@ -248,7 +270,6 @@ def resize_pane(self, *args, **kwargs) -> "Pane":
248270
------
249271
exc.LibTmuxException
250272
"""
251-
252273
if "height" in kwargs:
253274
proc = self.cmd("resize-pane", "-y%s" % int(kwargs["height"]))
254275
elif "width" in kwargs:
@@ -270,7 +291,7 @@ def enter(self) -> None:
270291
"""
271292
self.cmd("send-keys", "Enter")
272293

273-
def capture_pane(self):
294+
def capture_pane(self) -> t.Union[str, t.List[str]]:
274295
"""
275296
Capture text from pane.
276297

libtmux/window.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ class Window(TmuxMappingObject, TmuxRelationalObject):
5959
session: "Session"
6060
""":class:`libtmux.Session` window is linked to"""
6161

62-
def __init__(self, session: "Session", window_id: str, **kwargs: t.Any) -> None:
62+
def __init__(
63+
self, session: "Session", window_id: t.Union[int, str], **kwargs: t.Any
64+
) -> None:
6365
self.session = session
6466
self.server = self.session.server
6567

@@ -396,12 +398,12 @@ def last_pane(self) -> t.Optional[Pane]:
396398

397399
def split_window(
398400
self,
399-
target: None = None,
400-
start_directory: None = None,
401+
target: Optional[t.Union[int, str]] = None,
402+
start_directory: Optional[str] = None,
401403
attach: bool = True,
402404
vertical: bool = True,
403405
shell: Optional[str] = None,
404-
percent: None = None,
406+
percent: Optional[int] = None,
405407
) -> Pane:
406408
"""
407409
Split window and return the created :class:`Pane`.

0 commit comments

Comments
 (0)