Skip to content

Commit 419afd7

Browse files
committed
feat(Session): Use OptionsMixin for Session.set_option, Session.show_option(s)
1 parent 1db720e commit 419afd7

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
@@ -13,9 +13,10 @@
1313

1414
from libtmux._internal.query_list import QueryList
1515
from libtmux.common import tmux_cmd
16-
from libtmux.constants import WINDOW_DIRECTION_FLAG_MAP, WindowDirection
16+
from libtmux.constants import WINDOW_DIRECTION_FLAG_MAP, OptionScope, WindowDirection
1717
from libtmux.formats import FORMAT_SEPARATOR
1818
from libtmux.neo import Obj, fetch_obj, fetch_objs
19+
from libtmux.options import OptionsMixin
1920
from libtmux.pane import Pane
2021
from libtmux.window import Window
2122

@@ -27,7 +28,6 @@
2728
has_version,
2829
session_check_name,
2930
)
30-
from .options import handle_option_error
3131

3232
if t.TYPE_CHECKING:
3333
from .server import Server
@@ -37,7 +37,7 @@
3737

3838

3939
@dataclasses.dataclass()
40-
class Session(Obj, EnvironmentMixin):
40+
class Session(Obj, EnvironmentMixin, OptionsMixin):
4141
""":term:`tmux(1)` :term:`Session` [session_manual]_.
4242
4343
Holds :class:`Window` objects.
@@ -73,6 +73,7 @@ class Session(Obj, EnvironmentMixin):
7373
https://man.openbsd.org/tmux.1#DESCRIPTION. Accessed April 1st, 2018.
7474
"""
7575

76+
default_option_scope: t.Optional[OptionScope] = None
7677
server: "Server"
7778

7879
def refresh(self) -> None:
@@ -190,155 +191,6 @@ def cmd(
190191
Commands (tmux-like)
191192
"""
192193

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

0 commit comments

Comments
 (0)