Skip to content

Commit d5ccb94

Browse files
committed
!squash more libtmux dataclasses session
1 parent f7f7c34 commit d5ccb94

File tree

1 file changed

+158
-0
lines changed

1 file changed

+158
-0
lines changed

src/libtmux/neo.py

+158
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,155 @@ def cmd(self, *args: t.Any, **kwargs: t.Any) -> tmux_cmd:
325325
#
326326
# Commands (tmux-like)
327327
#
328+
def set_option(
329+
self, option: str, value: t.Union[str, int], _global: bool = False
330+
) -> "_Session":
331+
"""
332+
Set option ``$ tmux set-option <option> <value>``.
333+
334+
Parameters
335+
----------
336+
option : str
337+
the window option. such as 'default-shell'.
338+
value : str, int, or bool
339+
True/False will turn in 'on' and 'off'. You can also enter 'on' or
340+
'off' directly.
341+
_global : bool, optional
342+
check for option globally across all servers (-g)
343+
344+
Raises
345+
------
346+
:exc:`exc.OptionError`, :exc:`exc.UnknownOption`,
347+
:exc:`exc.InvalidOption`, :exc:`exc.AmbiguousOption`
348+
349+
Notes
350+
-----
351+
.. todo::
352+
353+
Needs tests
354+
"""
355+
if isinstance(value, bool) and value:
356+
value = "on"
357+
elif isinstance(value, bool) and not value:
358+
value = "off"
359+
360+
tmux_args: t.Tuple[t.Union[str, int], ...] = tuple()
361+
362+
if _global:
363+
tmux_args += ("-g",)
364+
365+
assert isinstance(option, str)
366+
assert isinstance(value, (str, int))
367+
368+
tmux_args += (
369+
option,
370+
value,
371+
)
372+
373+
proc = self.cmd("set-option", *tmux_args)
374+
375+
if isinstance(proc.stderr, list) and len(proc.stderr):
376+
handle_option_error(proc.stderr[0])
377+
378+
return self
379+
380+
def show_options(
381+
self, _global: t.Optional[bool] = False
382+
) -> t.Dict[str, t.Union[str, int]]:
383+
"""
384+
Return a dict of options for the window.
385+
386+
For familiarity with tmux, the option ``option`` param forwards to pick
387+
a single option, forwarding to :meth:`Session.show_option`.
388+
389+
Parameters
390+
----------
391+
_global : bool, optional
392+
Pass ``-g`` flag for global variable (server-wide)
393+
394+
Returns
395+
-------
396+
:py:obj:`dict`
397+
398+
Notes
399+
-----
400+
Uses ``_global`` for keyword name instead of ``global`` to avoid
401+
colliding with reserved keyword.
402+
"""
403+
tmux_args: t.Tuple[str, ...] = tuple()
404+
405+
if _global:
406+
tmux_args += ("-g",)
407+
408+
tmux_args += ("show-options",)
409+
session_output = self.cmd(*tmux_args).stdout
410+
411+
session_options: t.Dict[str, t.Union[str, int]] = {}
412+
for item in session_output:
413+
key, val = item.split(" ", maxsplit=1)
414+
assert isinstance(key, str)
415+
assert isinstance(val, str)
416+
417+
if isinstance(val, str) and val.isdigit():
418+
session_options[key] = int(val)
419+
420+
return session_options
421+
422+
def show_option(
423+
self, option: str, _global: bool = False
424+
) -> t.Optional[t.Union[str, int, bool]]:
425+
"""Return a list of options for the window.
426+
427+
Parameters
428+
----------
429+
option : str
430+
option name
431+
_global : bool, optional
432+
use global option scope, same as ``-g``
433+
434+
Returns
435+
-------
436+
str, int, or bool
437+
438+
Raises
439+
------
440+
:exc:`exc.OptionError`, :exc:`exc.UnknownOption`,
441+
:exc:`exc.InvalidOption`, :exc:`exc.AmbiguousOption`
442+
443+
Notes
444+
-----
445+
Uses ``_global`` for keyword name instead of ``global`` to avoid
446+
colliding with reserved keyword.
447+
448+
Test and return True/False for on/off string.
449+
"""
450+
451+
tmux_args: t.Tuple[str, ...] = tuple()
452+
453+
if _global:
454+
tmux_args += ("-g",)
455+
456+
tmux_args += (option,)
457+
458+
cmd = self.cmd("show-options", *tmux_args)
459+
460+
if isinstance(cmd.stderr, list) and len(cmd.stderr):
461+
handle_option_error(cmd.stderr[0])
462+
463+
if not len(cmd.stdout):
464+
return None
465+
466+
value_raw: t.List[str] = [item.split(" ") for item in cmd.stdout][0]
467+
468+
assert isinstance(value_raw[0], str)
469+
assert isinstance(value_raw[1], str)
470+
471+
value: t.Union[str, int] = (
472+
int(value_raw[1]) if value_raw[1].isdigit() else value_raw[1]
473+
)
474+
475+
return value
476+
328477
def select_window(self, target_window: t.Union[str, int]) -> "_Window":
329478
"""
330479
Return :class:`Window` selected via ``$ tmux select-window``.
@@ -549,6 +698,15 @@ def kill_window(self, target_window: t.Optional[str] = None) -> None:
549698

550699
self.server._update_windows()
551700

701+
#
702+
# Computed properties
703+
#
704+
@property
705+
def attached_pane(self) -> t.Optional["_Pane"]:
706+
"""Return active :class:`Pane` object."""
707+
708+
return self.attached_window.attached_pane
709+
552710
#
553711
# Redundant stuff we want to remove
554712
#

0 commit comments

Comments
 (0)