Skip to content

Commit 534a870

Browse files
committed
feat(Pane): Add Pane.set_option, Pane.show_option(s)
1 parent b0b00f2 commit 534a870

File tree

1 file changed

+169
-1
lines changed

1 file changed

+169
-1
lines changed

src/libtmux/pane.py

Lines changed: 169 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,19 @@
77
"""
88
import dataclasses
99
import logging
10+
import shlex
1011
import typing as t
1112
import warnings
1213
from typing import overload
1314

14-
from libtmux.common import tmux_cmd
15+
from libtmux.common import handle_option_error, tmux_cmd
16+
from libtmux.constants import OPTION_TYPE_FLAG_MAP, OptionType
1517
from libtmux.neo import Obj, fetch_obj
1618

1719
from . import exc
1820

1921
if t.TYPE_CHECKING:
22+
from .common import PaneOptionDict
2023
from .server import Server
2124
from .session import Session
2225
from .window import Window
@@ -328,6 +331,171 @@ def split_window(
328331
percent=percent,
329332
)
330333

334+
def set_option(
335+
self,
336+
option: str,
337+
value: t.Union[int, str],
338+
format: t.Optional[bool] = None,
339+
unset: t.Optional[bool] = None,
340+
unset_panes: t.Optional[bool] = None,
341+
prevent_overwrite: t.Optional[bool] = None,
342+
suppress_warnings: t.Optional[bool] = None,
343+
append: t.Optional[bool] = None,
344+
g: t.Optional[bool] = None,
345+
option_type: t.Optional[OptionType] = None,
346+
) -> "Pane":
347+
"""Set option for tmux pane.
348+
349+
Wraps ``$ tmux set-option -p <option> <value>``.
350+
351+
Parameters
352+
----------
353+
option : str
354+
option to set, e.g. 'aggressive-resize'
355+
value : str
356+
window option value. True/False will turn in 'on' and 'off',
357+
also accepts string of 'on' or 'off' directly.
358+
359+
Raises
360+
------
361+
:exc:`exc.OptionError`, :exc:`exc.UnknownOption`,
362+
:exc:`exc.InvalidOption`, :exc:`exc.AmbiguousOption`
363+
"""
364+
flags: t.List[str] = []
365+
if isinstance(value, bool) and value:
366+
value = "on"
367+
elif isinstance(value, bool) and not value:
368+
value = "off"
369+
370+
if unset is not None and unset:
371+
assert isinstance(unset, bool)
372+
flags.append("-u")
373+
374+
if unset_panes is not None and unset_panes:
375+
assert isinstance(unset_panes, bool)
376+
flags.append("-U")
377+
378+
if format is not None and format:
379+
assert isinstance(format, bool)
380+
flags.append("-F")
381+
382+
if prevent_overwrite is not None and prevent_overwrite:
383+
assert isinstance(prevent_overwrite, bool)
384+
flags.append("-o")
385+
386+
if suppress_warnings is not None and suppress_warnings:
387+
assert isinstance(suppress_warnings, bool)
388+
flags.append("-q")
389+
390+
if append is not None and append:
391+
assert isinstance(append, bool)
392+
flags.append("-a")
393+
394+
if g is not None and g:
395+
assert isinstance(g, bool)
396+
flags.append("-g")
397+
398+
if option_type is not None:
399+
assert option_type in OPTION_TYPE_FLAG_MAP
400+
flags.append(
401+
OPTION_TYPE_FLAG_MAP[option_type],
402+
)
403+
404+
cmd = self.cmd(
405+
"set-option",
406+
f"-t{self.pane_id}",
407+
option,
408+
value,
409+
*flags,
410+
)
411+
412+
if isinstance(cmd.stderr, list) and len(cmd.stderr):
413+
handle_option_error(cmd.stderr[0])
414+
415+
return self
416+
417+
def show_options(
418+
self,
419+
g: t.Optional[bool] = False,
420+
option_type: t.Optional[OptionType] = None,
421+
) -> "PaneOptionDict":
422+
"""Return a dict of options for the pane.
423+
424+
For familiarity with tmux, the option ``option`` param forwards to
425+
pick a single option, forwarding to :meth:`Pane.show_option`.
426+
427+
Parameters
428+
----------
429+
g : str, optional
430+
Pass ``-g`` flag for global variable, default False.
431+
"""
432+
tmux_args: t.Tuple[str, ...] = ()
433+
434+
if g:
435+
tmux_args += ("-g",)
436+
437+
if option_type is not None:
438+
assert option_type in OPTION_TYPE_FLAG_MAP
439+
tmux_args += (OPTION_TYPE_FLAG_MAP[option_type],)
440+
441+
cmd = self.cmd("show-pane-options", *tmux_args)
442+
443+
output = cmd.stdout
444+
445+
pane_options: "PaneOptionDict" = {}
446+
for item in output:
447+
key, val = shlex.split(item)
448+
assert isinstance(key, str)
449+
assert isinstance(val, str)
450+
451+
if isinstance(val, str) and val.isdigit():
452+
pane_options[key] = int(val)
453+
454+
return pane_options
455+
456+
def show_option(
457+
self, option: str, g: bool = False
458+
) -> t.Optional[t.Union[str, int]]:
459+
"""Return a list of options for the pane.
460+
461+
todo: test and return True/False for on/off string
462+
463+
Parameters
464+
----------
465+
option : str
466+
g : bool, optional
467+
Pass ``-g`` flag, global. Default False.
468+
469+
Raises
470+
------
471+
:exc:`exc.OptionError`, :exc:`exc.UnknownOption`,
472+
:exc:`exc.InvalidOption`, :exc:`exc.AmbiguousOption`
473+
"""
474+
tmux_args: t.Tuple[t.Union[str, int], ...] = ()
475+
476+
if g:
477+
tmux_args += ("-g",)
478+
479+
tmux_args += (option,)
480+
481+
cmd = self.cmd("show-options", *tmux_args)
482+
483+
if len(cmd.stderr):
484+
handle_option_error(cmd.stderr[0])
485+
486+
pane_options_output = cmd.stdout
487+
488+
if not len(pane_options_output):
489+
return None
490+
491+
value_raw = next(shlex.split(item) for item in pane_options_output)
492+
493+
value: t.Union[str, int] = (
494+
int(value_raw[1]) if value_raw[1].isdigit() else value_raw[1]
495+
)
496+
497+
return value
498+
331499
"""
332500
Commands (helpers)
333501
"""

0 commit comments

Comments
 (0)