Skip to content

Commit 0b5490e

Browse files
committed
!squash
1 parent 0de934c commit 0b5490e

File tree

1 file changed

+38
-36
lines changed

1 file changed

+38
-36
lines changed

libtmux/session.py

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from . import exc, formats
1616
from .common import (
1717
EnvironmentMixin,
18+
SessionDict,
1819
TmuxMappingObject,
1920
TmuxRelationalObject,
2021
WindowDict,
@@ -69,11 +70,10 @@ def __init__(self, server: "Server", session_id: str, **kwargs: t.Any) -> None:
6970
self.server._update_windows()
7071

7172
@property
72-
def _info(self) -> Dict[str, str]:
73-
73+
def _info(self) -> t.Optional[SessionDict]:
7474
attrs = {"session_id": str(self._session_id)}
7575

76-
def by(val) -> bool:
76+
def by(val: SessionDict) -> bool:
7777
for key in attrs.keys():
7878
try:
7979
if attrs[key] != val[key]:
@@ -83,13 +83,15 @@ def by(val) -> bool:
8383
return True
8484

8585
# TODO add type hint
86-
target_sessions = list(filter(by, self.server._sessions))
86+
target_sessions = [s for s in self.server._sessions if by(s)]
87+
# target_sessions = list(filter(by, self.server._sessions))
8788
try:
8889
return target_sessions[0]
8990
except IndexError as e:
9091
logger.error(e)
92+
return None
9193

92-
def cmd(self, *args, **kwargs) -> tmux_cmd:
94+
def cmd(self, *args: t.Any, **kwargs: t.Any) -> tmux_cmd:
9395
"""
9496
Return :meth:`server.cmd`.
9597
@@ -106,13 +108,17 @@ def cmd(self, *args, **kwargs) -> tmux_cmd:
106108
# if -t is not set in any arg yet
107109
if not any("-t" in str(x) for x in args):
108110
# insert -t immediately after 1st arg, as per tmux format
109-
new_args = [args[0]]
110-
new_args += ["-t", self.id]
111-
new_args += args[1:]
111+
new_args: t.Tuple[str, ...] = tuple()
112+
new_args += (args[0],)
113+
new_args += (
114+
"-t",
115+
self.id,
116+
)
117+
new_args += tuple(args[1:])
112118
args = new_args
113119
return self.server.cmd(*args, **kwargs)
114120

115-
def attach_session(self):
121+
def attach_session(self) -> None:
116122
"""Return ``$ tmux attach-session`` aka alias: ``$ tmux attach``."""
117123
proc = self.cmd("attach-session", "-t%s" % self.id)
118124

@@ -127,7 +133,7 @@ def kill_session(self) -> None:
127133
if proc.stderr:
128134
raise exc.LibTmuxException(proc.stderr)
129135

130-
def switch_client(self):
136+
def switch_client(self) -> None:
131137
"""
132138
Switch client to this session.
133139
@@ -216,7 +222,7 @@ def new_window(
216222
wformats = ["session_name", "session_id"] + formats.WINDOW_FORMATS
217223
tmux_formats = ["#{%s}" % f for f in wformats]
218224

219-
window_args: t.Tuple = tuple()
225+
window_args: t.Tuple[str, ...] = tuple()
220226

221227
if not attach:
222228
window_args += ("-d",)
@@ -243,18 +249,20 @@ def new_window(
243249
if window_shell:
244250
window_args += (window_shell,)
245251

246-
proc = self.cmd("new-window", *window_args)
252+
cmd = self.cmd("new-window", *window_args)
247253

248-
if proc.stderr:
249-
raise exc.LibTmuxException(proc.stderr)
254+
if cmd.stderr:
255+
raise exc.LibTmuxException(cmd.stderr)
250256

251-
window = proc.stdout[0]
257+
window_output = cmd.stdout[0]
252258

253-
window = dict(zip(wformats, window.split(formats.FORMAT_SEPARATOR)))
259+
window_formatters = dict(
260+
zip(wformats, window_output.split(formats.FORMAT_SEPARATOR))
261+
)
254262

255263
# clear up empty dict
256-
window = {k: v for k, v in window.items() if v}
257-
window = Window(session=self, **window)
264+
window_formatters_filtered = {k: v for k, v in window_formatters.items() if v}
265+
window = Window(session=self, **window_formatters_filtered)
258266

259267
self.server._update_windows()
260268

@@ -412,12 +420,18 @@ def set_option(
412420
elif isinstance(value, bool) and not value:
413421
value = "off"
414422

415-
tmux_args = tuple()
423+
tmux_args: t.Tuple[t.Union[str, int], ...] = tuple()
416424

417425
if _global:
418426
tmux_args += ("-g",)
419427

420-
tmux_args += (option, value)
428+
assert isinstance(option, str)
429+
assert isinstance(value, (str, int))
430+
431+
tmux_args += (
432+
option,
433+
value,
434+
)
421435

422436
proc = self.cmd("set-option", *tmux_args)
423437

@@ -447,34 +461,22 @@ def show_options(
447461
Uses ``_global`` for keyword name instead of ``global`` to avoid
448462
colliding with reserved keyword.
449463
"""
450-
tmux_args = tuple()
464+
tmux_args: t.Tuple[str, ...] = tuple()
451465

452466
if _global:
453467
tmux_args += ("-g",)
454468

455469
tmux_args += ("show-options",)
456470
session_output = self.cmd(*tmux_args).stdout
457471

458-
session_formatters: t.List[t.List[str, t.Union[str, int]]] = []
459-
472+
session_options: t.Dict[str, t.Union[str, int]] = {}
460473
for item in session_output:
461474
key, val = item.split(" ", maxsplit=1)
462-
# assert len(raw_formatter) == 2
463-
# assert isinstance(raw_formatter[0], str)
464-
# assert isinstance(raw_formatter[1], str)
465475
assert isinstance(key, str)
466476
assert isinstance(val, str)
467-
formatter: t.List[str, t.Union[str, int]] = [key, val]
468-
469-
session_formatters.append(formatter)
470-
471-
print(f"session_formatters: {session_formatters}")
472-
473-
session_options: t.Dict[str, t.Union[str, int]] = dict(session_formatters)
474477

475-
for key, value in session_options.items():
476-
if isinstance(value, str) and value.isdigit():
477-
session_options[key] = int(value)
478+
if isinstance(val, str) and val.isdigit():
479+
session_options[key] = int(val)
478480

479481
return session_options
480482

0 commit comments

Comments
 (0)