Skip to content

Commit cf047ac

Browse files
committed
feat(Session): Use OptionsMixin for Session.set_option, Session.show_option(s)
1 parent 6a6e85e commit cf047ac

File tree

1 file changed

+5
-150
lines changed

1 file changed

+5
-150
lines changed

src/libtmux/session.py

Lines changed: 5 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414
import warnings
1515

1616
from libtmux._internal.query_list import QueryList
17-
from libtmux.constants import WINDOW_DIRECTION_FLAG_MAP, WindowDirection
17+
from libtmux.common import tmux_cmd
18+
from libtmux.constants import WINDOW_DIRECTION_FLAG_MAP, OptionScope, WindowDirection
1819
from libtmux.formats import FORMAT_SEPARATOR
1920
from libtmux.neo import Obj, fetch_obj, fetch_objs
21+
from libtmux.options import OptionsMixin
2022
from libtmux.pane import Pane
2123
from libtmux.window import Window
2224

@@ -28,7 +30,6 @@
2830
has_version,
2931
session_check_name,
3032
)
31-
from .options import handle_option_error
3233

3334
if t.TYPE_CHECKING:
3435
import sys
@@ -49,7 +50,7 @@
4950

5051

5152
@dataclasses.dataclass()
52-
class Session(Obj, EnvironmentMixin):
53+
class Session(Obj, EnvironmentMixin, OptionsMixin):
5354
""":term:`tmux(1)` :term:`Session` [session_manual]_.
5455
5556
Holds :class:`Window` objects.
@@ -92,6 +93,7 @@ class Session(Obj, EnvironmentMixin):
9293
https://man.openbsd.org/tmux.1#DESCRIPTION. Accessed April 1st, 2018.
9394
"""
9495

96+
default_option_scope: OptionScope | None = None
9597
server: Server
9698

9799
def __enter__(self) -> Self:
@@ -239,153 +241,6 @@ def cmd(
239241
Commands (tmux-like)
240242
"""
241243

242-
def set_option(
243-
self,
244-
option: str,
245-
value: str | int,
246-
global_: bool = False,
247-
) -> Session:
248-
"""Set option ``$ tmux set-option <option> <value>``.
249-
250-
Parameters
251-
----------
252-
option : str
253-
the window option. such as 'default-shell'.
254-
value : str, int, or bool
255-
True/False will turn in 'on' and 'off'. You can also enter 'on' or
256-
'off' directly.
257-
_global : bool, optional
258-
check for option globally across all servers (-g)
259-
260-
Raises
261-
------
262-
:exc:`exc.OptionError`, :exc:`exc.UnknownOption`,
263-
:exc:`exc.InvalidOption`, :exc:`exc.AmbiguousOption`
264-
265-
Notes
266-
-----
267-
.. todo::
268-
269-
Needs tests
270-
"""
271-
if isinstance(value, bool) and value:
272-
value = "on"
273-
elif isinstance(value, bool) and not value:
274-
value = "off"
275-
276-
tmux_args: tuple[str | int, ...] = ()
277-
278-
if global_:
279-
tmux_args += ("-g",)
280-
281-
assert isinstance(option, str)
282-
assert isinstance(value, (str, int))
283-
284-
tmux_args += (
285-
option,
286-
value,
287-
)
288-
289-
proc = self.cmd("set-option", *tmux_args)
290-
291-
if isinstance(proc.stderr, list) and len(proc.stderr):
292-
handle_option_error(proc.stderr[0])
293-
294-
return self
295-
296-
def show_options(
297-
self,
298-
global_: bool | None = False,
299-
) -> dict[str, str | int]:
300-
"""Return dict of options for the session.
301-
302-
Parameters
303-
----------
304-
_global : bool, optional
305-
Pass ``-g`` flag for global variable (server-wide)
306-
307-
Returns
308-
-------
309-
:py:obj:`dict`
310-
311-
Notes
312-
-----
313-
Uses ``_global`` for keyword name instead of ``global`` to avoid
314-
colliding with reserved keyword.
315-
"""
316-
tmux_args: tuple[str, ...] = ()
317-
318-
if global_:
319-
tmux_args += ("-g",)
320-
321-
tmux_args += ("show-options",)
322-
session_output = self.cmd(*tmux_args).stdout
323-
324-
session_options: dict[str, str | int] = {}
325-
for item in session_output:
326-
key, val = item.split(" ", maxsplit=1)
327-
assert isinstance(key, str)
328-
assert isinstance(val, str)
329-
330-
if isinstance(val, str) and val.isdigit():
331-
session_options[key] = int(val)
332-
333-
return session_options
334-
335-
def show_option(
336-
self,
337-
option: str,
338-
global_: bool = False,
339-
) -> str | int | bool | None:
340-
"""Return option value for the target session.
341-
342-
Parameters
343-
----------
344-
option : str
345-
option name
346-
_global : bool, optional
347-
use global option scope, same as ``-g``
348-
349-
Returns
350-
-------
351-
str, int, or bool
352-
353-
Raises
354-
------
355-
:exc:`exc.OptionError`, :exc:`exc.UnknownOption`,
356-
:exc:`exc.InvalidOption`, :exc:`exc.AmbiguousOption`
357-
358-
Notes
359-
-----
360-
Uses ``_global`` for keyword name instead of ``global`` to avoid
361-
colliding with reserved keyword.
362-
363-
Test and return True/False for on/off string.
364-
"""
365-
tmux_args: tuple[str, ...] = ()
366-
367-
if global_:
368-
tmux_args += ("-g",)
369-
370-
tmux_args += (option,)
371-
372-
cmd = self.cmd("show-options", *tmux_args)
373-
374-
if isinstance(cmd.stderr, list) and len(cmd.stderr):
375-
handle_option_error(cmd.stderr[0])
376-
377-
if not len(cmd.stdout):
378-
return None
379-
380-
value_raw: list[str] = next(item.split(" ") for item in cmd.stdout)
381-
382-
assert isinstance(value_raw[0], str)
383-
assert isinstance(value_raw[1], str)
384-
385-
value: str | int = int(value_raw[1]) if value_raw[1].isdigit() else value_raw[1]
386-
387-
return value
388-
389244
def select_window(self, target_window: str | int) -> Window:
390245
"""Select window and return the selected window.
391246

0 commit comments

Comments
 (0)