Skip to content

Commit

Permalink
!squash more libtmux dataclasses session
Browse files Browse the repository at this point in the history
  • Loading branch information
tony committed Sep 17, 2022
1 parent f7f7c34 commit d5ccb94
Showing 1 changed file with 158 additions and 0 deletions.
158 changes: 158 additions & 0 deletions src/libtmux/neo.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,155 @@ def cmd(self, *args: t.Any, **kwargs: t.Any) -> tmux_cmd:
#
# Commands (tmux-like)
#
def set_option(
self, option: str, value: t.Union[str, int], _global: bool = False
) -> "_Session":
"""
Set option ``$ tmux set-option <option> <value>``.
Parameters
----------
option : str
the window option. such as 'default-shell'.
value : str, int, or bool
True/False will turn in 'on' and 'off'. You can also enter 'on' or
'off' directly.
_global : bool, optional
check for option globally across all servers (-g)
Raises
------
:exc:`exc.OptionError`, :exc:`exc.UnknownOption`,
:exc:`exc.InvalidOption`, :exc:`exc.AmbiguousOption`
Notes
-----
.. todo::
Needs tests
"""
if isinstance(value, bool) and value:
value = "on"
elif isinstance(value, bool) and not value:
value = "off"

tmux_args: t.Tuple[t.Union[str, int], ...] = tuple()

if _global:
tmux_args += ("-g",)

assert isinstance(option, str)
assert isinstance(value, (str, int))

tmux_args += (
option,
value,
)

proc = self.cmd("set-option", *tmux_args)

if isinstance(proc.stderr, list) and len(proc.stderr):
handle_option_error(proc.stderr[0])

return self

def show_options(
self, _global: t.Optional[bool] = False
) -> t.Dict[str, t.Union[str, int]]:
"""
Return a dict of options for the window.
For familiarity with tmux, the option ``option`` param forwards to pick
a single option, forwarding to :meth:`Session.show_option`.
Parameters
----------
_global : bool, optional
Pass ``-g`` flag for global variable (server-wide)
Returns
-------
:py:obj:`dict`
Notes
-----
Uses ``_global`` for keyword name instead of ``global`` to avoid
colliding with reserved keyword.
"""
tmux_args: t.Tuple[str, ...] = tuple()

if _global:
tmux_args += ("-g",)

tmux_args += ("show-options",)
session_output = self.cmd(*tmux_args).stdout

session_options: t.Dict[str, t.Union[str, int]] = {}
for item in session_output:
key, val = item.split(" ", maxsplit=1)
assert isinstance(key, str)
assert isinstance(val, str)

if isinstance(val, str) and val.isdigit():
session_options[key] = int(val)

return session_options

def show_option(
self, option: str, _global: bool = False
) -> t.Optional[t.Union[str, int, bool]]:
"""Return a list of options for the window.
Parameters
----------
option : str
option name
_global : bool, optional
use global option scope, same as ``-g``
Returns
-------
str, int, or bool
Raises
------
:exc:`exc.OptionError`, :exc:`exc.UnknownOption`,
:exc:`exc.InvalidOption`, :exc:`exc.AmbiguousOption`
Notes
-----
Uses ``_global`` for keyword name instead of ``global`` to avoid
colliding with reserved keyword.
Test and return True/False for on/off string.
"""

tmux_args: t.Tuple[str, ...] = tuple()

if _global:
tmux_args += ("-g",)

tmux_args += (option,)

cmd = self.cmd("show-options", *tmux_args)

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

if not len(cmd.stdout):
return None

value_raw: t.List[str] = [item.split(" ") for item in cmd.stdout][0]

assert isinstance(value_raw[0], str)
assert isinstance(value_raw[1], str)

value: t.Union[str, int] = (
int(value_raw[1]) if value_raw[1].isdigit() else value_raw[1]
)

return value

def select_window(self, target_window: t.Union[str, int]) -> "_Window":
"""
Return :class:`Window` selected via ``$ tmux select-window``.
Expand Down Expand Up @@ -549,6 +698,15 @@ def kill_window(self, target_window: t.Optional[str] = None) -> None:

self.server._update_windows()

#
# Computed properties
#
@property
def attached_pane(self) -> t.Optional["_Pane"]:
"""Return active :class:`Pane` object."""

return self.attached_window.attached_pane

#
# Redundant stuff we want to remove
#
Expand Down

0 comments on commit d5ccb94

Please sign in to comment.