Skip to content

Commit 445d467

Browse files
committed
feat(Window): Use OptionsMixin for Window.set_option, Window.show_option(s)
1 parent d53c300 commit 445d467

File tree

2 files changed

+78
-312
lines changed

2 files changed

+78
-312
lines changed

src/libtmux/options.py

Lines changed: 74 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@
6767
to differentiate them, run `show_options()` without ``include_inherited=True``.
6868
"""
6969

70+
from __future__ import annotations
71+
7072
import io
7173
import logging
7274
import re
@@ -96,9 +98,9 @@
9698
TerminalOverrides = dict[str, TerminalOverride]
9799
CommandAliases = dict[str, str]
98100

99-
OptionDict: "TypeAlias" = dict[str, t.Any]
100-
UntypedOptionsDict: "TypeAlias" = dict[str, t.Optional[str]]
101-
ExplodedUntypedOptionsDict: "TypeAlias" = dict[
101+
OptionDict: TypeAlias = dict[str, t.Any]
102+
UntypedOptionsDict: TypeAlias = dict[str, t.Optional[str]]
103+
ExplodedUntypedOptionsDict: TypeAlias = dict[
102104
str,
103105
t.Union[
104106
str,
@@ -110,7 +112,7 @@
110112
],
111113
],
112114
]
113-
ExplodedComplexUntypedOptionsDict: "TypeAlias" = dict[
115+
ExplodedComplexUntypedOptionsDict: TypeAlias = dict[
114116
str,
115117
t.Optional[
116118
t.Union[
@@ -179,8 +181,8 @@ def handle_option_error(error: str) -> type[exc.OptionError]:
179181

180182

181183
_V = t.TypeVar("_V")
182-
ConvertedValue: "TypeAlias" = t.Union[str, int, bool, None]
183-
ConvertedValues: "TypeAlias" = t.Union[
184+
ConvertedValue: TypeAlias = t.Union[str, int, bool, None]
185+
ConvertedValues: TypeAlias = t.Union[
184186
ConvertedValue,
185187
list[ConvertedValue],
186188
dict[str, ConvertedValue],
@@ -189,8 +191,8 @@ def handle_option_error(error: str) -> type[exc.OptionError]:
189191

190192

191193
def convert_value(
192-
value: t.Optional[_V],
193-
) -> t.Optional[t.Union[ConvertedValue, _V]]:
194+
value: _V | None,
195+
) -> ConvertedValue | _V | None:
194196
"""Convert raw option strings to python types.
195197
196198
Examples
@@ -224,8 +226,8 @@ def convert_value(
224226

225227

226228
def convert_values(
227-
value: t.Optional[_V],
228-
) -> t.Optional[t.Union["ConvertedValues", _V]]:
229+
value: _V | None,
230+
) -> ConvertedValues | _V | None:
229231
"""Recursively convert values to python types via :func:`convert_value`.
230232
231233
>>> convert_values(None)
@@ -265,7 +267,7 @@ def convert_values(
265267

266268
def parse_options_to_dict(
267269
stdout: t.IO[str],
268-
) -> "UntypedOptionsDict":
270+
) -> UntypedOptionsDict:
269271
r"""Process subprocess.stdout options or hook output to flat, naive, untyped dict.
270272
271273
Does not explode arrays or deep values.
@@ -323,7 +325,7 @@ def parse_options_to_dict(
323325
"""
324326
output: UntypedOptionsDict = {}
325327

326-
val: t.Optional[ConvertedValue] = None
328+
val: ConvertedValue | None = None
327329

328330
for item in stdout.readlines():
329331
if " " in item:
@@ -344,9 +346,9 @@ def parse_options_to_dict(
344346

345347

346348
def explode_arrays(
347-
_dict: "UntypedOptionsDict",
349+
_dict: UntypedOptionsDict,
348350
force_array: bool = False,
349-
) -> "ExplodedUntypedOptionsDict":
351+
) -> ExplodedUntypedOptionsDict:
350352
"""Explode flat, naive options dict's option arrays.
351353
352354
Examples
@@ -379,7 +381,7 @@ def explode_arrays(
379381
"""
380382
options: dict[str, t.Any] = {}
381383
for key, val in _dict.items():
382-
Default: type[t.Union[dict[t.Any, t.Any], SparseArray[str]]] = (
384+
Default: type[dict[t.Any, t.Any] | SparseArray[str]] = (
383385
dict if isinstance(key, str) and key == "terminal-features" else SparseArray
384386
)
385387
if "[" not in key:
@@ -417,8 +419,8 @@ def explode_arrays(
417419

418420

419421
def explode_complex(
420-
_dict: "ExplodedUntypedOptionsDict",
421-
) -> "ExplodedComplexUntypedOptionsDict":
422+
_dict: ExplodedUntypedOptionsDict,
423+
) -> ExplodedComplexUntypedOptionsDict:
422424
r"""Explode arrayed option's complex values.
423425
424426
Examples
@@ -548,27 +550,26 @@ def explode_complex(
548550
class OptionsMixin(CmdMixin):
549551
"""Mixin for managing tmux options based on scope."""
550552

551-
default_option_scope: t.Optional[OptionScope]
553+
default_option_scope: OptionScope | None
552554

553-
def __init__(self, default_option_scope: t.Optional[OptionScope] = None) -> None:
555+
def __init__(self, default_option_scope: OptionScope | None = None) -> None:
554556
"""When not a user (custom) option, scope can be implied."""
555557
if default_option_scope is not None:
556558
self.default_option_scope = default_option_scope
557559

558560
def set_option(
559561
self,
560562
option: str,
561-
value: t.Union[int, str],
562-
_format: t.Optional[bool] = None,
563-
prevent_overwrite: t.Optional[bool] = None,
564-
ignore_errors: t.Optional[bool] = None,
565-
append: t.Optional[bool] = None,
566-
g: t.Optional[bool] = None,
567-
global_: t.Optional[bool] = None,
568-
scope: t.Optional[
569-
t.Union[OptionScope, _DefaultOptionScope]
570-
] = DEFAULT_OPTION_SCOPE,
571-
) -> "Self":
563+
value: int | str,
564+
_format: bool | None = None,
565+
prevent_overwrite: bool | None = None,
566+
ignore_errors: bool | None = None,
567+
suppress_warnings: bool | None = None,
568+
append: bool | None = None,
569+
g: bool | None = None,
570+
global_: bool | None = None,
571+
scope: OptionScope | _DefaultOptionScope | None = DEFAULT_OPTION_SCOPE,
572+
) -> Self:
572573
"""Set option for tmux target.
573574
574575
Wraps ``$ tmux set-option <option> <value>``.
@@ -644,6 +645,10 @@ def set_option(
644645
assert isinstance(ignore_errors, bool)
645646
flags.append("-q")
646647

648+
if suppress_warnings is not None and suppress_warnings:
649+
assert isinstance(suppress_warnings, bool)
650+
flags.append("-q")
651+
647652
if append is not None and append:
648653
assert isinstance(append, bool)
649654
flags.append("-a")
@@ -677,13 +682,11 @@ def set_option(
677682
def unset_option(
678683
self,
679684
option: str,
680-
unset_panes: t.Optional[bool] = None,
681-
_global: t.Optional[bool] = None,
682-
ignore_errors: t.Optional[bool] = None,
683-
scope: t.Optional[
684-
t.Union[OptionScope, _DefaultOptionScope]
685-
] = DEFAULT_OPTION_SCOPE,
686-
) -> "Self":
685+
unset_panes: bool | None = None,
686+
_global: bool | None = None,
687+
ignore_errors: bool | None = None,
688+
scope: OptionScope | _DefaultOptionScope | None = DEFAULT_OPTION_SCOPE,
689+
) -> Self:
687690
"""Unset option for tmux target.
688691
689692
Wraps ``$ tmux set-option -u <option>`` / ``$ tmux set-option -U <option>``
@@ -765,14 +768,12 @@ def unset_option(
765768

766769
def _show_options_raw(
767770
self,
768-
g: t.Optional[bool] = False,
769-
_global: t.Optional[bool] = False,
770-
scope: t.Optional[
771-
t.Union[OptionScope, _DefaultOptionScope]
772-
] = DEFAULT_OPTION_SCOPE,
773-
include_hooks: t.Optional[bool] = None,
774-
include_inherited: t.Optional[bool] = None,
775-
) -> "tmux_cmd":
771+
g: bool | None = False,
772+
_global: bool | None = False,
773+
scope: OptionScope | _DefaultOptionScope | None = DEFAULT_OPTION_SCOPE,
774+
include_hooks: bool | None = None,
775+
include_inherited: bool | None = None,
776+
) -> tmux_cmd:
776777
"""Return a dict of options for the target.
777778
778779
Parameters
@@ -828,14 +829,12 @@ def _show_options_raw(
828829

829830
def _show_options_dict(
830831
self,
831-
g: t.Optional[bool] = False,
832-
_global: t.Optional[bool] = False,
833-
scope: t.Optional[
834-
t.Union[OptionScope, _DefaultOptionScope]
835-
] = DEFAULT_OPTION_SCOPE,
836-
include_hooks: t.Optional[bool] = None,
837-
include_inherited: t.Optional[bool] = None,
838-
) -> "UntypedOptionsDict":
832+
g: bool | None = False,
833+
_global: bool | None = False,
834+
scope: OptionScope | _DefaultOptionScope | None = DEFAULT_OPTION_SCOPE,
835+
include_hooks: bool | None = None,
836+
include_inherited: bool | None = None,
837+
) -> UntypedOptionsDict:
839838
"""Return dict of options for the target.
840839
841840
Parameters
@@ -879,14 +878,12 @@ def _show_options_dict(
879878

880879
def _show_options(
881880
self,
882-
g: t.Optional[bool] = False,
883-
_global: t.Optional[bool] = False,
884-
scope: t.Optional[
885-
t.Union[OptionScope, _DefaultOptionScope]
886-
] = DEFAULT_OPTION_SCOPE,
887-
include_hooks: t.Optional[bool] = None,
888-
include_inherited: t.Optional[bool] = None,
889-
) -> "ExplodedComplexUntypedOptionsDict":
881+
g: bool | None = False,
882+
_global: bool | None = False,
883+
scope: OptionScope | _DefaultOptionScope | None = DEFAULT_OPTION_SCOPE,
884+
include_hooks: bool | None = None,
885+
include_inherited: bool | None = None,
886+
) -> ExplodedComplexUntypedOptionsDict:
890887
"""Return a dict of options for the target.
891888
892889
Parameters
@@ -934,13 +931,11 @@ def _show_option_raw(
934931
option: str,
935932
_global: bool = False,
936933
g: bool = False,
937-
scope: t.Optional[
938-
t.Union[OptionScope, _DefaultOptionScope]
939-
] = DEFAULT_OPTION_SCOPE,
940-
ignore_errors: t.Optional[bool] = None,
941-
include_hooks: t.Optional[bool] = None,
942-
include_inherited: t.Optional[bool] = None,
943-
) -> "tmux_cmd":
934+
scope: OptionScope | _DefaultOptionScope | None = DEFAULT_OPTION_SCOPE,
935+
ignore_errors: bool | None = None,
936+
include_hooks: bool | None = None,
937+
include_inherited: bool | None = None,
938+
) -> tmux_cmd:
944939
"""Return raw option output for target.
945940
946941
Parameters
@@ -989,7 +984,7 @@ def _show_option_raw(
989984
if scope is DEFAULT_OPTION_SCOPE:
990985
scope = self.default_option_scope
991986

992-
flags: tuple[t.Union[str, int], ...] = ()
987+
flags: tuple[str | int, ...] = ()
993988

994989
if g:
995990
warnings.warn("g argument is deprecated in favor of _global", stacklevel=2)
@@ -1019,13 +1014,11 @@ def _show_option(
10191014
option: str,
10201015
_global: bool = False,
10211016
g: bool = False,
1022-
scope: t.Optional[
1023-
t.Union[OptionScope, _DefaultOptionScope]
1024-
] = DEFAULT_OPTION_SCOPE,
1025-
ignore_errors: t.Optional[bool] = None,
1026-
include_hooks: t.Optional[bool] = None,
1027-
include_inherited: t.Optional[bool] = None,
1028-
) -> t.Optional[ConvertedValue]:
1017+
scope: OptionScope | _DefaultOptionScope | None = DEFAULT_OPTION_SCOPE,
1018+
ignore_errors: bool | None = None,
1019+
include_hooks: bool | None = None,
1020+
include_inherited: bool | None = None,
1021+
) -> ConvertedValue | None:
10291022
"""Return option value for the target.
10301023
10311024
todo: test and return True/False for on/off string
@@ -1104,13 +1097,11 @@ def show_option(
11041097
option: str,
11051098
_global: bool = False,
11061099
g: bool = False,
1107-
scope: t.Optional[
1108-
t.Union[OptionScope, _DefaultOptionScope]
1109-
] = DEFAULT_OPTION_SCOPE,
1110-
ignore_errors: t.Optional[bool] = None,
1111-
include_hooks: t.Optional[bool] = None,
1112-
include_inherited: t.Optional[bool] = None,
1113-
) -> t.Optional[t.Any]:
1100+
scope: OptionScope | _DefaultOptionScope | None = DEFAULT_OPTION_SCOPE,
1101+
ignore_errors: bool | None = None,
1102+
include_hooks: bool | None = None,
1103+
include_inherited: bool | None = None,
1104+
) -> t.Any | None:
11141105
"""Return option value for the target.
11151106
11161107
Parameters

0 commit comments

Comments
 (0)