Skip to content

Commit eef72da

Browse files
committed
feat(Window): Use OptionMixin for Window.set_option, Window.show_option(s)
1 parent 10c51c5 commit eef72da

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,12 +13,12 @@
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
21-
from .common import PaneDict, WindowOptionDict, handle_option_error
21+
from .common import OptionMixin, PaneDict, WindowOptionDict
2222
from .formats import FORMAT_SEPARATOR
2323

2424
if t.TYPE_CHECKING:
@@ -29,7 +29,7 @@
2929

3030

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

80+
default_scope: t.Optional[OptionScope] = OptionScope.Window
8081
server: "Server"
8182

8283
def refresh(self) -> None:
@@ -343,89 +344,6 @@ def set_window_option(
343344

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

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

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

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

0 commit comments

Comments
 (0)