Skip to content

Commit d7a7ff1

Browse files
committed
feat(Pane): Add Pane.set_option, Pane.show_option(s)
1 parent 33d09db commit d7a7ff1

File tree

1 file changed

+172
-1
lines changed

1 file changed

+172
-1
lines changed

src/libtmux/pane.py

+172-1
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_SCOPE_FLAG_MAP, OptionScope
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
@@ -326,6 +329,174 @@ def split_window(
326329
percent=percent,
327330
)
328331

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

0 commit comments

Comments
 (0)