Skip to content

Commit facacbd

Browse files
committed
common: Add OptionMixin
1 parent 76e3a33 commit facacbd

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

src/libtmux/common.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import typing as t
1414
from typing import Dict, Optional, Union
1515

16+
from libtmux.constants import OPTION_SCOPE_FLAG_MAP, OptionScope
17+
1618
from . import exc
1719
from ._compat import LooseVersion, console_to_str, str_from_console
1820

@@ -36,6 +38,111 @@
3638
PaneDict = t.Dict[str, t.Any]
3739

3840

41+
class CmdProtocol(t.Protocol):
42+
"""Command protocol for tmux command."""
43+
44+
def __call__(self, cmd: str, *args: t.Any, **kwargs: t.Any) -> "tmux_cmd":
45+
"""Wrap tmux_cmd."""
46+
...
47+
48+
49+
class CmdMixin:
50+
"""Command mixin for tmux command."""
51+
52+
cmd: CmdProtocol
53+
54+
55+
class OptionMixin(CmdMixin):
56+
"""Mixin for manager session and server level environment variables in tmux."""
57+
58+
default_scope: OptionScope
59+
60+
def __init__(self, default_scope: OptionScope) -> None:
61+
self.default_scope = default_scope
62+
63+
def set_option(
64+
self,
65+
option: str,
66+
value: t.Union[int, str],
67+
_format: t.Optional[bool] = None,
68+
unset: t.Optional[bool] = None,
69+
unset_panes: t.Optional[bool] = None,
70+
prevent_overwrite: t.Optional[bool] = None,
71+
suppress_warnings: t.Optional[bool] = None,
72+
append: t.Optional[bool] = None,
73+
g: t.Optional[bool] = None,
74+
scope: t.Optional[OptionScope] = None,
75+
) -> "t.Self":
76+
"""Set option for tmux window.
77+
78+
Wraps ``$ tmux set-option <option> <value>``.
79+
80+
Parameters
81+
----------
82+
option : str
83+
option to set, e.g. 'aggressive-resize'
84+
value : str
85+
window option value. True/False will turn in 'on' and 'off',
86+
also accepts string of 'on' or 'off' directly.
87+
88+
Raises
89+
------
90+
:exc:`exc.OptionError`, :exc:`exc.UnknownOption`,
91+
:exc:`exc.InvalidOption`, :exc:`exc.AmbiguousOption`
92+
"""
93+
flags: t.List[str] = []
94+
if isinstance(value, bool) and value:
95+
value = "on"
96+
elif isinstance(value, bool) and not value:
97+
value = "off"
98+
99+
if unset is not None and unset:
100+
assert isinstance(unset, bool)
101+
flags.append("-u")
102+
103+
if unset_panes is not None and unset_panes:
104+
assert isinstance(unset_panes, bool)
105+
flags.append("-U")
106+
107+
if _format is not None and _format:
108+
assert isinstance(_format, bool)
109+
flags.append("-F")
110+
111+
if prevent_overwrite is not None and prevent_overwrite:
112+
assert isinstance(prevent_overwrite, bool)
113+
flags.append("-o")
114+
115+
if suppress_warnings is not None and suppress_warnings:
116+
assert isinstance(suppress_warnings, bool)
117+
flags.append("-q")
118+
119+
if append is not None and append:
120+
assert isinstance(append, bool)
121+
flags.append("-a")
122+
123+
if g is not None and g:
124+
assert isinstance(g, bool)
125+
flags.append("-g")
126+
127+
if scope is not None:
128+
assert scope in OPTION_SCOPE_FLAG_MAP
129+
flags.append(
130+
OPTION_SCOPE_FLAG_MAP[scope],
131+
)
132+
133+
cmd = self.cmd(
134+
"set-option",
135+
*flags,
136+
option,
137+
value,
138+
)
139+
140+
if isinstance(cmd.stderr, list) and len(cmd.stderr):
141+
handle_option_error(cmd.stderr[0])
142+
143+
return self
144+
145+
39146
class EnvironmentMixin:
40147
"""Mixin for manager session and server level environment variables in tmux."""
41148

0 commit comments

Comments
 (0)