Skip to content

Commit 8ac3024

Browse files
committed
feat(Window): Use OptionsMixin for Window.set_option, Window.show_option(s)
1 parent 2dbde47 commit 8ac3024

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
@@ -551,6 +551,7 @@ def set_option(
551551
_format: bool | None = None,
552552
prevent_overwrite: bool | None = None,
553553
ignore_errors: bool | None = None,
554+
suppress_warnings: bool | None = None,
554555
append: bool | None = None,
555556
g: bool | None = None,
556557
global_: bool | None = None,
@@ -631,6 +632,10 @@ def set_option(
631632
assert isinstance(ignore_errors, bool)
632633
flags.append("-q")
633634

635+
if suppress_warnings is not None and suppress_warnings:
636+
assert isinstance(suppress_warnings, bool)
637+
flags.append("-q")
638+
634639
if append is not None and append:
635640
assert isinstance(append, bool)
636641
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
@@ -49,7 +49,7 @@
4949

5050

5151
@dataclasses.dataclass()
52-
class Window(Obj):
52+
class Window(Obj, OptionsMixin):
5353
""":term:`tmux(1)` :term:`Window` [window_manual]_.
5454
5555
Holds :class:`Pane` objects.
@@ -104,6 +104,7 @@ class Window(Obj):
104104
https://man.openbsd.org/tmux.1#DESCRIPTION. Accessed April 1st, 2018.
105105
"""
106106

107+
default_option_scope: OptionScope | None = OptionScope.Window
107108
server: Server
108109

109110
def __enter__(self) -> Self:
@@ -441,230 +442,6 @@ def select_layout(self, layout: str | None = None) -> Window:
441442

442443
return self
443444

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

0 commit comments

Comments
 (0)