Skip to content

Commit e338862

Browse files
committed
feat(Session): Use OptionsMixin for Session.set_option, Session.show_option(s)
1 parent d7bd5e7 commit e338862

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
@@ -48,7 +49,7 @@
4849

4950

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

95+
default_option_scope: OptionScope | None = None
9496
server: Server
9597

9698
def __enter__(self) -> Self:
@@ -238,153 +240,6 @@ def cmd(
238240
Commands (tmux-like)
239241
"""
240242

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

0 commit comments

Comments
 (0)