Skip to content

Commit de1efac

Browse files
committed
feat(Window): Use OptionsMixin for Window.set_option, Window.show_option(s)
1 parent e0fead4 commit de1efac

File tree

2 files changed

+9
-229
lines changed

2 files changed

+9
-229
lines changed

src/libtmux/options.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,7 @@ def set_option(
564564
_format: bool | None = None,
565565
prevent_overwrite: bool | None = None,
566566
ignore_errors: bool | None = None,
567+
suppress_warnings: bool | None = None,
567568
append: bool | None = None,
568569
g: bool | None = None,
569570
global_: bool | None = None,
@@ -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")

src/libtmux/window.py

Lines changed: 4 additions & 229 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
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,
2019
RESIZE_ADJUSTMENT_DIRECTION_FLAG_MAP,
2120
OptionScope,
2221
PaneDirection,
@@ -27,7 +26,8 @@
2726
from libtmux.pane import Pane
2827

2928
from . import exc
30-
from .options import handle_option_error
29+
from .common import PaneDict, WindowOptionDict
30+
from .options import OptionsMixin
3131

3232
if t.TYPE_CHECKING:
3333
import sys
@@ -47,7 +47,7 @@
4747

4848

4949
@dataclasses.dataclass()
50-
class Window(Obj):
50+
class Window(Obj, OptionsMixin):
5151
""":term:`tmux(1)` :term:`Window` [window_manual]_.
5252
5353
Holds :class:`Pane` objects.
@@ -102,6 +102,7 @@ class Window(Obj):
102102
https://man.openbsd.org/tmux.1#DESCRIPTION. Accessed April 1st, 2018.
103103
"""
104104

105+
default_option_scope: OptionScope | None = OptionScope.Window
105106
server: Server
106107

107108
def __enter__(self) -> Self:
@@ -439,230 +440,6 @@ def select_layout(self, layout: str | None = None) -> Window:
439440

440441
return self
441442

442-
def set_option(
443-
self,
444-
option: str,
445-
value: int | str,
446-
_format: bool | None = None,
447-
unset: bool | None = None,
448-
unset_panes: bool | None = None,
449-
prevent_overwrite: bool | None = None,
450-
suppress_warnings: bool | None = None,
451-
append: bool | None = None,
452-
g: bool | None = None,
453-
scope: OptionScope | None = None,
454-
) -> Window:
455-
"""Set option for tmux window.
456-
457-
Wraps ``$ tmux set-option <option> <value>``.
458-
459-
Parameters
460-
----------
461-
option : str
462-
option to set, e.g. 'aggressive-resize'
463-
value : str
464-
window option value. True/False will turn in 'on' and 'off',
465-
also accepts string of 'on' or 'off' directly.
466-
467-
Raises
468-
------
469-
:exc:`exc.OptionError`, :exc:`exc.UnknownOption`,
470-
:exc:`exc.InvalidOption`, :exc:`exc.AmbiguousOption`
471-
"""
472-
flags: list[str] = []
473-
if isinstance(value, bool) and value:
474-
value = "on"
475-
elif isinstance(value, bool) and not value:
476-
value = "off"
477-
478-
if unset is not None and unset:
479-
assert isinstance(unset, bool)
480-
flags.append("-u")
481-
482-
if unset_panes is not None and unset_panes:
483-
assert isinstance(unset_panes, bool)
484-
flags.append("-U")
485-
486-
if _format is not None and _format:
487-
assert isinstance(_format, bool)
488-
flags.append("-F")
489-
490-
if prevent_overwrite is not None and prevent_overwrite:
491-
assert isinstance(prevent_overwrite, bool)
492-
flags.append("-o")
493-
494-
if suppress_warnings is not None and suppress_warnings:
495-
assert isinstance(suppress_warnings, bool)
496-
flags.append("-q")
497-
498-
if append is not None and append:
499-
assert isinstance(append, bool)
500-
flags.append("-a")
501-
502-
if g is not None and g:
503-
assert isinstance(g, bool)
504-
flags.append("-g")
505-
506-
if scope is not None:
507-
assert scope in OPTION_SCOPE_FLAG_MAP
508-
flags.append(
509-
OPTION_SCOPE_FLAG_MAP[scope],
510-
)
511-
512-
cmd = self.cmd(
513-
"set-option",
514-
"-w",
515-
*flags,
516-
option,
517-
value,
518-
)
519-
520-
if isinstance(cmd.stderr, list) and len(cmd.stderr):
521-
handle_option_error(cmd.stderr[0])
522-
523-
return self
524-
525-
@t.overload
526-
def show_options(
527-
self,
528-
g: bool | None,
529-
scope: OptionScope | None,
530-
include_hooks: bool | None,
531-
include_parents: bool | None,
532-
values_only: t.Literal[True],
533-
) -> list[str]: ...
534-
535-
@t.overload
536-
def show_options(
537-
self,
538-
g: bool | None,
539-
scope: OptionScope | None,
540-
include_hooks: bool | None,
541-
include_parents: bool | None,
542-
values_only: None = None,
543-
) -> WindowOptionDict: ...
544-
545-
@t.overload
546-
def show_options(
547-
self,
548-
g: bool | None = None,
549-
scope: OptionScope | None = None,
550-
include_hooks: bool | None = None,
551-
include_parents: bool | None = None,
552-
values_only: t.Literal[False] = False,
553-
) -> WindowOptionDict: ...
554-
555-
def show_options(
556-
self,
557-
g: bool | None = False,
558-
scope: OptionScope | None = OptionScope.Window,
559-
include_hooks: bool | None = None,
560-
include_parents: bool | None = None,
561-
values_only: bool | None = False,
562-
) -> WindowOptionDict | list[str]:
563-
"""Return a dict of options for the window.
564-
565-
Parameters
566-
----------
567-
g : str, optional
568-
Pass ``-g`` flag for global variable, default False.
569-
"""
570-
tmux_args: tuple[str, ...] = ()
571-
572-
if g:
573-
tmux_args += ("-g",)
574-
575-
if scope is not None:
576-
assert scope in OPTION_SCOPE_FLAG_MAP
577-
tmux_args += (OPTION_SCOPE_FLAG_MAP[scope],)
578-
579-
if include_parents is not None and include_parents:
580-
tmux_args += ("-A",)
581-
582-
if include_hooks is not None and include_hooks:
583-
tmux_args += ("-H",)
584-
585-
if values_only is not None and values_only:
586-
tmux_args += ("-v",)
587-
588-
cmd = self.cmd("show-options", *tmux_args)
589-
590-
output = cmd.stdout
591-
592-
# The shlex.split function splits the args at spaces, while also
593-
# retaining quoted sub-strings.
594-
# shlex.split('this is "a test"') => ['this', 'is', 'a test']
595-
596-
window_options: WindowOptionDict = {}
597-
for item in output:
598-
try:
599-
key, val = shlex.split(item)
600-
except ValueError:
601-
logger.exception(f"Error extracting option: {item}")
602-
assert isinstance(key, str)
603-
assert isinstance(val, str)
604-
605-
if isinstance(val, str) and val.isdigit():
606-
window_options[key] = int(val)
607-
608-
return window_options
609-
610-
def show_option(
611-
self,
612-
option: str,
613-
g: bool = False,
614-
scope: OptionScope | None = OptionScope.Window,
615-
include_hooks: bool | None = None,
616-
include_parents: bool | None = None,
617-
) -> str | int | None:
618-
"""Return option value for the target window.
619-
620-
todo: test and return True/False for on/off string
621-
622-
Parameters
623-
----------
624-
option : str
625-
g : bool, optional
626-
Pass ``-g`` flag, global. Default False.
627-
628-
Raises
629-
------
630-
:exc:`exc.OptionError`, :exc:`exc.UnknownOption`,
631-
:exc:`exc.InvalidOption`, :exc:`exc.AmbiguousOption`
632-
"""
633-
tmux_args: tuple[str | int, ...] = ()
634-
635-
if g:
636-
tmux_args += ("-g",)
637-
638-
if scope is not None:
639-
assert scope in OPTION_SCOPE_FLAG_MAP
640-
tmux_args += (OPTION_SCOPE_FLAG_MAP[scope],)
641-
642-
if include_parents is not None and include_parents:
643-
tmux_args += ("-A",)
644-
645-
if include_hooks is not None and include_hooks:
646-
tmux_args += ("-H",)
647-
648-
tmux_args += (option,)
649-
650-
cmd = self.cmd("show-options", *tmux_args)
651-
652-
if len(cmd.stderr):
653-
handle_option_error(cmd.stderr[0])
654-
655-
window_options_output = cmd.stdout
656-
657-
if not len(window_options_output):
658-
return None
659-
660-
value_raw = next(shlex.split(item) for item in window_options_output)
661-
662-
value: str | int = int(value_raw[1]) if value_raw[1].isdigit() else value_raw[1]
663-
664-
return value
665-
666443
def rename_window(self, new_name: str) -> Window:
667444
"""Rename window.
668445
@@ -681,8 +458,6 @@ def rename_window(self, new_name: str) -> Window:
681458
>>> window.rename_window('New name')
682459
Window(@1 1:New name, Session($1 ...))
683460
"""
684-
import shlex
685-
686461
lex = shlex.shlex(new_name)
687462
lex.escape = " "
688463
lex.whitespace_split = False

0 commit comments

Comments
 (0)