Skip to content

Commit 52c8d79

Browse files
committed
!squash test: Use OptionMixin
1 parent 09ca652 commit 52c8d79

File tree

1 file changed

+4
-152
lines changed

1 file changed

+4
-152
lines changed

src/libtmux/session.py

Lines changed: 4 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
import warnings
1212

1313
from libtmux._internal.query_list import QueryList
14-
from libtmux.common import tmux_cmd
14+
from libtmux.common import OptionMixin, tmux_cmd
15+
from libtmux.constants import OptionScope
1516
from libtmux.formats import FORMAT_SEPARATOR
1617
from libtmux.neo import Obj, fetch_obj, fetch_objs
1718
from libtmux.pane import Pane
@@ -21,7 +22,6 @@
2122
from .common import (
2223
EnvironmentMixin,
2324
WindowDict,
24-
handle_option_error,
2525
has_gte_version,
2626
has_version,
2727
session_check_name,
@@ -35,7 +35,7 @@
3535

3636

3737
@dataclasses.dataclass()
38-
class Session(Obj, EnvironmentMixin):
38+
class Session(Obj, EnvironmentMixin, OptionMixin):
3939
""":term:`tmux(1)` :term:`Session` [session_manual]_.
4040
4141
Holds :class:`Window` objects.
@@ -71,6 +71,7 @@ class Session(Obj, EnvironmentMixin):
7171
https://man.openbsd.org/tmux.1#DESCRIPTION. Accessed April 1st, 2018.
7272
"""
7373

74+
default_scope: OptionScope = OptionScope.Session
7475
server: "Server"
7576

7677
def refresh(self) -> None:
@@ -170,155 +171,6 @@ def cmd(self, *args: t.Any, **kwargs: t.Any) -> tmux_cmd:
170171
Commands (tmux-like)
171172
"""
172173

173-
def set_option(
174-
self,
175-
option: str,
176-
value: t.Union[str, int],
177-
_global: bool = False,
178-
) -> "Session":
179-
"""Set option ``$ tmux set-option <option> <value>``.
180-
181-
Parameters
182-
----------
183-
option : str
184-
the window option. such as 'default-shell'.
185-
value : str, int, or bool
186-
True/False will turn in 'on' and 'off'. You can also enter 'on' or
187-
'off' directly.
188-
_global : bool, optional
189-
check for option globally across all servers (-g)
190-
191-
Raises
192-
------
193-
:exc:`exc.OptionError`, :exc:`exc.UnknownOption`,
194-
:exc:`exc.InvalidOption`, :exc:`exc.AmbiguousOption`
195-
196-
Notes
197-
-----
198-
.. todo::
199-
200-
Needs tests
201-
"""
202-
if isinstance(value, bool) and value:
203-
value = "on"
204-
elif isinstance(value, bool) and not value:
205-
value = "off"
206-
207-
tmux_args: t.Tuple[t.Union[str, int], ...] = ()
208-
209-
if _global:
210-
tmux_args += ("-g",)
211-
212-
assert isinstance(option, str)
213-
assert isinstance(value, (str, int))
214-
215-
tmux_args += (
216-
option,
217-
value,
218-
)
219-
220-
proc = self.cmd("set-option", *tmux_args)
221-
222-
if isinstance(proc.stderr, list) and len(proc.stderr):
223-
handle_option_error(proc.stderr[0])
224-
225-
return self
226-
227-
def show_options(
228-
self,
229-
_global: t.Optional[bool] = False,
230-
) -> t.Dict[str, t.Union[str, int]]:
231-
"""Return dict of options for the session.
232-
233-
Parameters
234-
----------
235-
_global : bool, optional
236-
Pass ``-g`` flag for global variable (server-wide)
237-
238-
Returns
239-
-------
240-
:py:obj:`dict`
241-
242-
Notes
243-
-----
244-
Uses ``_global`` for keyword name instead of ``global`` to avoid
245-
colliding with reserved keyword.
246-
"""
247-
tmux_args: t.Tuple[str, ...] = ()
248-
249-
if _global:
250-
tmux_args += ("-g",)
251-
252-
tmux_args += ("show-options",)
253-
session_output = self.cmd(*tmux_args).stdout
254-
255-
session_options: t.Dict[str, t.Union[str, int]] = {}
256-
for item in session_output:
257-
key, val = item.split(" ", maxsplit=1)
258-
assert isinstance(key, str)
259-
assert isinstance(val, str)
260-
261-
if isinstance(val, str) and val.isdigit():
262-
session_options[key] = int(val)
263-
264-
return session_options
265-
266-
def show_option(
267-
self,
268-
option: str,
269-
_global: bool = False,
270-
) -> t.Optional[t.Union[str, int, bool]]:
271-
"""Return option value for the target session.
272-
273-
Parameters
274-
----------
275-
option : str
276-
option name
277-
_global : bool, optional
278-
use global option scope, same as ``-g``
279-
280-
Returns
281-
-------
282-
str, int, or bool
283-
284-
Raises
285-
------
286-
:exc:`exc.OptionError`, :exc:`exc.UnknownOption`,
287-
:exc:`exc.InvalidOption`, :exc:`exc.AmbiguousOption`
288-
289-
Notes
290-
-----
291-
Uses ``_global`` for keyword name instead of ``global`` to avoid
292-
colliding with reserved keyword.
293-
294-
Test and return True/False for on/off string.
295-
"""
296-
tmux_args: t.Tuple[str, ...] = ()
297-
298-
if _global:
299-
tmux_args += ("-g",)
300-
301-
tmux_args += (option,)
302-
303-
cmd = self.cmd("show-options", *tmux_args)
304-
305-
if isinstance(cmd.stderr, list) and len(cmd.stderr):
306-
handle_option_error(cmd.stderr[0])
307-
308-
if not len(cmd.stdout):
309-
return None
310-
311-
value_raw: t.List[str] = next(item.split(" ") for item in cmd.stdout)
312-
313-
assert isinstance(value_raw[0], str)
314-
assert isinstance(value_raw[1], str)
315-
316-
value: t.Union[str, int] = (
317-
int(value_raw[1]) if value_raw[1].isdigit() else value_raw[1]
318-
)
319-
320-
return value
321-
322174
def select_window(self, target_window: t.Union[str, int]) -> "Window":
323175
"""Select window, return selected window.
324176

0 commit comments

Comments
 (0)