Skip to content

Commit 6ee4abd

Browse files
committed
feat(Window): Use OptionMixin for Window.set_option, Window.show_option(s)
1 parent 0ccacec commit 6ee4abd

File tree

1 file changed

+4
-234
lines changed

1 file changed

+4
-234
lines changed

src/libtmux/window.py

Lines changed: 4 additions & 234 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313

1414
from libtmux._internal.query_list import QueryList
1515
from libtmux.common import has_gte_version, tmux_cmd
16-
from libtmux.constants import OPTION_SCOPE_FLAG_MAP, OptionScope
16+
from libtmux.constants import OptionScope
1717
from libtmux.neo import Obj, fetch_obj, fetch_objs
1818
from libtmux.pane import Pane
1919

2020
from . import exc
2121
from .common import PaneDict, WindowOptionDict
2222
from .formats import FORMAT_SEPARATOR
23-
from .options import handle_option_error
23+
from .options import OptionMixin
2424

2525
if t.TYPE_CHECKING:
2626
from .server import Server
@@ -30,7 +30,7 @@
3030

3131

3232
@dataclasses.dataclass()
33-
class Window(Obj):
33+
class Window(Obj, OptionMixin):
3434
""":term:`tmux(1)` :term:`Window` [window_manual]_.
3535
3636
Holds :class:`Pane` objects.
@@ -78,6 +78,7 @@ class Window(Obj):
7878
https://man.openbsd.org/tmux.1#DESCRIPTION. Accessed April 1st, 2018.
7979
"""
8080

81+
default_scope: t.Optional[OptionScope] = OptionScope.Window
8182
server: "Server"
8283

8384
def refresh(self) -> None:
@@ -344,89 +345,6 @@ def set_window_option(
344345

345346
return self.set_option(option=option, value=value)
346347

347-
def set_option(
348-
self,
349-
option: str,
350-
value: t.Union[int, str],
351-
_format: t.Optional[bool] = None,
352-
unset: t.Optional[bool] = None,
353-
unset_panes: t.Optional[bool] = None,
354-
prevent_overwrite: t.Optional[bool] = None,
355-
suppress_warnings: t.Optional[bool] = None,
356-
append: t.Optional[bool] = None,
357-
g: t.Optional[bool] = None,
358-
scope: t.Optional[OptionScope] = None,
359-
) -> "Window":
360-
"""Set option for tmux window.
361-
362-
Wraps ``$ tmux set-option <option> <value>``.
363-
364-
Parameters
365-
----------
366-
option : str
367-
option to set, e.g. 'aggressive-resize'
368-
value : str
369-
window option value. True/False will turn in 'on' and 'off',
370-
also accepts string of 'on' or 'off' directly.
371-
372-
Raises
373-
------
374-
:exc:`exc.OptionError`, :exc:`exc.UnknownOption`,
375-
:exc:`exc.InvalidOption`, :exc:`exc.AmbiguousOption`
376-
"""
377-
flags: t.List[str] = []
378-
if isinstance(value, bool) and value:
379-
value = "on"
380-
elif isinstance(value, bool) and not value:
381-
value = "off"
382-
383-
if unset is not None and unset:
384-
assert isinstance(unset, bool)
385-
flags.append("-u")
386-
387-
if unset_panes is not None and unset_panes:
388-
assert isinstance(unset_panes, bool)
389-
flags.append("-U")
390-
391-
if _format is not None and _format:
392-
assert isinstance(_format, bool)
393-
flags.append("-F")
394-
395-
if prevent_overwrite is not None and prevent_overwrite:
396-
assert isinstance(prevent_overwrite, bool)
397-
flags.append("-o")
398-
399-
if suppress_warnings is not None and suppress_warnings:
400-
assert isinstance(suppress_warnings, bool)
401-
flags.append("-q")
402-
403-
if append is not None and append:
404-
assert isinstance(append, bool)
405-
flags.append("-a")
406-
407-
if g is not None and g:
408-
assert isinstance(g, bool)
409-
flags.append("-g")
410-
411-
if scope is not None:
412-
assert scope in OPTION_SCOPE_FLAG_MAP
413-
flags.append(
414-
OPTION_SCOPE_FLAG_MAP[scope],
415-
)
416-
417-
cmd = self.cmd(
418-
"set-option",
419-
f"-t{self.session_id}:{self.window_index}",
420-
*flags,
421-
option,
422-
value,
423-
)
424-
425-
if isinstance(cmd.stderr, list) and len(cmd.stderr):
426-
handle_option_error(cmd.stderr[0])
427-
428-
return self
429-
430348
def show_window_options(self, g: t.Optional[bool] = False) -> "WindowOptionDict":
431349
"""Show options for tmux window. Deprecated by :meth:`Window.show_options()`.
432350
@@ -441,94 +359,6 @@ def show_window_options(self, g: t.Optional[bool] = False) -> "WindowOptionDict"
441359
scope=OptionScope.Window,
442360
)
443361

444-
@t.overload
445-
def show_options(
446-
self,
447-
g: t.Optional[bool],
448-
scope: t.Optional[OptionScope],
449-
include_hooks: t.Optional[bool],
450-
include_parents: t.Optional[bool],
451-
values_only: t.Literal[True],
452-
) -> t.List[str]:
453-
...
454-
455-
@t.overload
456-
def show_options(
457-
self,
458-
g: t.Optional[bool],
459-
scope: t.Optional[OptionScope],
460-
include_hooks: t.Optional[bool],
461-
include_parents: t.Optional[bool],
462-
values_only: t.Literal[None] = None,
463-
) -> "WindowOptionDict":
464-
...
465-
466-
@t.overload
467-
def show_options(
468-
self,
469-
g: t.Optional[bool] = None,
470-
scope: t.Optional[OptionScope] = None,
471-
include_hooks: t.Optional[bool] = None,
472-
include_parents: t.Optional[bool] = None,
473-
values_only: t.Literal[False] = False,
474-
) -> "WindowOptionDict":
475-
...
476-
477-
def show_options(
478-
self,
479-
g: t.Optional[bool] = False,
480-
scope: t.Optional[OptionScope] = OptionScope.Window,
481-
include_hooks: t.Optional[bool] = None,
482-
include_parents: t.Optional[bool] = None,
483-
values_only: t.Optional[bool] = False,
484-
) -> t.Union["WindowOptionDict", t.List[str]]:
485-
"""Return a dict of options for the window.
486-
487-
Parameters
488-
----------
489-
g : str, optional
490-
Pass ``-g`` flag for global variable, default False.
491-
"""
492-
tmux_args: t.Tuple[str, ...] = ()
493-
494-
if g:
495-
tmux_args += ("-g",)
496-
497-
if scope is not None:
498-
assert scope in OPTION_SCOPE_FLAG_MAP
499-
tmux_args += (OPTION_SCOPE_FLAG_MAP[scope],)
500-
501-
if include_parents is not None and include_parents:
502-
tmux_args += ("-A",)
503-
504-
if include_hooks is not None and include_hooks:
505-
tmux_args += ("-H",)
506-
507-
if values_only is not None and values_only:
508-
tmux_args += ("-v",)
509-
510-
cmd = self.cmd("show-options", *tmux_args)
511-
512-
output = cmd.stdout
513-
514-
# The shlex.split function splits the args at spaces, while also
515-
# retaining quoted sub-strings.
516-
# shlex.split('this is "a test"') => ['this', 'is', 'a test']
517-
518-
window_options: "WindowOptionDict" = {}
519-
for item in output:
520-
try:
521-
key, val = shlex.split(item)
522-
except ValueError:
523-
logger.exception(f"Error extracting option: {item}")
524-
assert isinstance(key, str)
525-
assert isinstance(val, str)
526-
527-
if isinstance(val, str) and val.isdigit():
528-
window_options[key] = int(val)
529-
530-
return window_options
531-
532362
def show_window_option(
533363
self,
534364
option: str,
@@ -548,64 +378,6 @@ def show_window_option(
548378
scope=OptionScope.Window,
549379
)
550380

551-
def show_option(
552-
self,
553-
option: str,
554-
g: bool = False,
555-
scope: t.Optional[OptionScope] = OptionScope.Window,
556-
include_hooks: t.Optional[bool] = None,
557-
include_parents: t.Optional[bool] = None,
558-
) -> t.Optional[t.Union[str, int]]:
559-
"""Return option value for the target window.
560-
561-
todo: test and return True/False for on/off string
562-
563-
Parameters
564-
----------
565-
option : str
566-
g : bool, optional
567-
Pass ``-g`` flag, global. Default False.
568-
569-
Raises
570-
------
571-
:exc:`exc.OptionError`, :exc:`exc.UnknownOption`,
572-
:exc:`exc.InvalidOption`, :exc:`exc.AmbiguousOption`
573-
"""
574-
tmux_args: t.Tuple[t.Union[str, int], ...] = ()
575-
576-
if g:
577-
tmux_args += ("-g",)
578-
579-
if scope is not None:
580-
assert scope in OPTION_SCOPE_FLAG_MAP
581-
tmux_args += (OPTION_SCOPE_FLAG_MAP[scope],)
582-
583-
if include_parents is not None and include_parents:
584-
tmux_args += ("-A",)
585-
586-
if include_hooks is not None and include_hooks:
587-
tmux_args += ("-H",)
588-
589-
tmux_args += (option,)
590-
591-
cmd = self.cmd("show-options", *tmux_args)
592-
593-
if len(cmd.stderr):
594-
handle_option_error(cmd.stderr[0])
595-
596-
window_options_output = cmd.stdout
597-
598-
if not len(window_options_output):
599-
return None
600-
601-
value_raw = next(shlex.split(item) for item in window_options_output)
602-
603-
value: t.Union[str, int] = (
604-
int(value_raw[1]) if value_raw[1].isdigit() else value_raw[1]
605-
)
606-
607-
return value
608-
609381
def rename_window(self, new_name: str) -> "Window":
610382
"""Rename window.
611383
@@ -624,8 +396,6 @@ def rename_window(self, new_name: str) -> "Window":
624396
>>> window.rename_window('New name')
625397
Window(@1 1:New name, Session($1 ...))
626398
"""
627-
import shlex
628-
629399
lex = shlex.shlex(new_name)
630400
lex.escape = " "
631401
lex.whitespace_split = False

0 commit comments

Comments
 (0)