Skip to content

Commit 6e2938d

Browse files
committed
feat(Window): scope param for Window.{set,show}_option
1 parent 8cbe6ca commit 6e2938d

File tree

1 file changed

+85
-10
lines changed

1 file changed

+85
-10
lines changed

src/libtmux/window.py

Lines changed: 85 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
from libtmux._internal.query_list import QueryList
1717
from libtmux.common import has_gte_version, tmux_cmd
1818
from libtmux.constants import (
19+
OPTION_SCOPE_FLAG_MAP,
1920
RESIZE_ADJUSTMENT_DIRECTION_FLAG_MAP,
21+
OptionScope,
2022
PaneDirection,
2123
ResizeAdjustmentDirection,
2224
WindowDirection,
@@ -398,6 +400,7 @@ def set_option(
398400
suppress_warnings: bool | None = None,
399401
append: bool | None = None,
400402
g: bool | None = None,
403+
scope: OptionScope | None = None,
401404
) -> Window:
402405
"""Set option for tmux window.
403406
@@ -450,20 +453,63 @@ def set_option(
450453
assert isinstance(g, bool)
451454
flags.append("-g")
452455

456+
if scope is not None:
457+
assert scope in OPTION_SCOPE_FLAG_MAP
458+
flags.append(
459+
OPTION_SCOPE_FLAG_MAP[scope],
460+
)
461+
453462
cmd = self.cmd(
454463
"set-option",
455464
"-w",
465+
*flags,
456466
option,
457467
value,
458-
*flags,
459468
)
460469

461470
if isinstance(cmd.stderr, list) and len(cmd.stderr):
462471
handle_option_error(cmd.stderr[0])
463472

464473
return self
465474

466-
def show_options(self, g: bool | None = False) -> WindowOptionDict:
475+
@t.overload
476+
def show_options(
477+
self,
478+
g: bool | None,
479+
scope: OptionScope | None,
480+
include_hooks: bool | None,
481+
include_parents: bool | None,
482+
values_only: t.Literal[True],
483+
) -> list[str]: ...
484+
485+
@t.overload
486+
def show_options(
487+
self,
488+
g: bool | None,
489+
scope: OptionScope | None,
490+
include_hooks: bool | None,
491+
include_parents: bool | None,
492+
values_only: None = None,
493+
) -> WindowOptionDict: ...
494+
495+
@t.overload
496+
def show_options(
497+
self,
498+
g: bool | None = None,
499+
scope: OptionScope | None = None,
500+
include_hooks: bool | None = None,
501+
include_parents: bool | None = None,
502+
values_only: t.Literal[False] = False,
503+
) -> WindowOptionDict: ...
504+
505+
def show_options(
506+
self,
507+
g: bool | None = False,
508+
scope: OptionScope | None = OptionScope.Window,
509+
include_hooks: bool | None = None,
510+
include_parents: bool | None = None,
511+
values_only: bool | None = False,
512+
) -> WindowOptionDict | list[str]:
467513
"""Return a dict of options for the window.
468514
469515
Parameters
@@ -476,11 +522,20 @@ def show_options(self, g: bool | None = False) -> WindowOptionDict:
476522
if g:
477523
tmux_args += ("-g",)
478524

479-
tmux_args += (
480-
"show-options",
481-
"-w",
482-
)
483-
cmd = self.cmd(*tmux_args)
525+
if scope is not None:
526+
assert scope in OPTION_SCOPE_FLAG_MAP
527+
tmux_args += (OPTION_SCOPE_FLAG_MAP[scope],)
528+
529+
if include_parents is not None and include_parents:
530+
tmux_args += ("-A",)
531+
532+
if include_hooks is not None and include_hooks:
533+
tmux_args += ("-H",)
534+
535+
if values_only is not None and values_only:
536+
tmux_args += ("-v",)
537+
538+
cmd = self.cmd("show-options", *tmux_args)
484539

485540
output = cmd.stdout
486541

@@ -506,6 +561,9 @@ def show_option(
506561
self,
507562
option: str,
508563
g: bool = False,
564+
scope: OptionScope | None = OptionScope.Window,
565+
include_hooks: bool | None = None,
566+
include_parents: bool | None = None,
509567
) -> str | int | None:
510568
"""Return option value for the target window.
511569
@@ -527,9 +585,19 @@ def show_option(
527585
if g:
528586
tmux_args += ("-g",)
529587

588+
if scope is not None:
589+
assert scope in OPTION_SCOPE_FLAG_MAP
590+
tmux_args += (OPTION_SCOPE_FLAG_MAP[scope],)
591+
592+
if include_parents is not None and include_parents:
593+
tmux_args += ("-A",)
594+
595+
if include_hooks is not None and include_hooks:
596+
tmux_args += ("-H",)
597+
530598
tmux_args += (option,)
531599

532-
cmd = self.cmd("show-options", "-w", *tmux_args)
600+
cmd = self.cmd("show-options", *tmux_args)
533601

534602
if len(cmd.stderr):
535603
handle_option_error(cmd.stderr[0])
@@ -995,7 +1063,10 @@ def show_window_options(self, g: bool | None = False) -> WindowOptionDict:
9951063
category=DeprecationWarning,
9961064
stacklevel=2,
9971065
)
998-
return self.show_options(g=g)
1066+
return self.show_options(
1067+
g=g,
1068+
scope=OptionScope.Window,
1069+
)
9991070

10001071
def show_window_option(
10011072
self,
@@ -1014,7 +1085,11 @@ def show_window_option(
10141085
category=DeprecationWarning,
10151086
stacklevel=2,
10161087
)
1017-
return self.show_option(option=option, g=g)
1088+
return self.show_option(
1089+
option=option,
1090+
g=g,
1091+
scope=OptionScope.Window,
1092+
)
10181093

10191094
def get(self, key: str, default: t.Any | None = None) -> t.Any:
10201095
"""Return key-based lookup. Deprecated by attributes.

0 commit comments

Comments
 (0)