Skip to content

Commit fcaf66f

Browse files
committed
feat(Session): Use OptionsMixin for Session.set_option, Session.show_option(s)
1 parent 281626b commit fcaf66f

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
from libtmux.common import tmux_cmd
@@ -40,7 +41,7 @@
4041

4142

4243
@dataclasses.dataclass()
43-
class Session(Obj, EnvironmentMixin):
44+
class Session(Obj, EnvironmentMixin, OptionsMixin):
4445
""":term:`tmux(1)` :term:`Session` [session_manual]_.
4546
4647
Holds :class:`Window` objects.
@@ -76,6 +77,7 @@ class Session(Obj, EnvironmentMixin):
7677
https://man.openbsd.org/tmux.1#DESCRIPTION. Accessed April 1st, 2018.
7778
"""
7879

80+
default_option_scope: OptionScope | None = None
7981
server: Server
8082

8183
def refresh(self) -> None:
@@ -193,153 +195,6 @@ def cmd(
193195
Commands (tmux-like)
194196
"""
195197

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

0 commit comments

Comments
 (0)