Skip to content

Commit 186ebd1

Browse files
committed
Options,Env(feat): add quiet/values-only to show_options, format/hidden to set_environment
why: show-options supports -q (quiet) and -v (values only) flags, and set-environment supports -F (format expansion) and -h (hidden) flags that were not exposed in the Python API. what: - Add quiet (-q) and values_only (-v) to _show_options_raw() - Add expand_format (-F) and hidden (-h) to set_environment() - Add tests for quiet show_options, hidden env vars, and format expansion
1 parent 240c88e commit 186ebd1

3 files changed

Lines changed: 65 additions & 1 deletion

File tree

src/libtmux/common.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,14 @@ class EnvironmentMixin:
6060
def __init__(self, add_option: str | None = None) -> None:
6161
self._add_option = add_option
6262

63-
def set_environment(self, name: str, value: str) -> None:
63+
def set_environment(
64+
self,
65+
name: str,
66+
value: str,
67+
*,
68+
expand_format: bool | None = None,
69+
hidden: bool | None = None,
70+
) -> None:
6471
"""Set environment ``$ tmux set-environment <name> <value>``.
6572
6673
Parameters
@@ -69,6 +76,14 @@ def set_environment(self, name: str, value: str) -> None:
6976
The environment variable name, e.g. 'PATH'.
7077
value : str
7178
Environment value.
79+
expand_format : bool, optional
80+
Expand tmux format strings in the value (``-F`` flag).
81+
82+
.. versionadded:: 0.45
83+
hidden : bool, optional
84+
Mark the variable as hidden (``-h`` flag).
85+
86+
.. versionadded:: 0.45
7287
7388
Raises
7489
------
@@ -79,6 +94,12 @@ def set_environment(self, name: str, value: str) -> None:
7994
if self._add_option:
8095
args += [self._add_option]
8196

97+
if expand_format:
98+
args += ["-F"]
99+
100+
if hidden:
101+
args += ["-h"]
102+
82103
args += [name, value]
83104

84105
cmd = self.cmd(*args)

src/libtmux/options.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,8 @@ def _show_options_raw(
807807
scope: OptionScope | _DefaultOptionScope | None = DEFAULT_OPTION_SCOPE,
808808
include_hooks: bool | None = None,
809809
include_inherited: bool | None = None,
810+
quiet: bool | None = None,
811+
values_only: bool | None = None,
810812
) -> tmux_cmd:
811813
"""Return a dict of options for the target.
812814
@@ -867,6 +869,12 @@ def _show_options_raw(
867869
if include_hooks is not None and include_hooks:
868870
flags += ("-H",)
869871

872+
if quiet is not None and quiet:
873+
flags += ("-q",)
874+
875+
if values_only is not None and values_only:
876+
flags += ("-v",)
877+
870878
return self.cmd("show-options", *flags)
871879

872880
def _show_options_dict(

tests/test_options.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1494,3 +1494,38 @@ def test_explode_arrays_preserves_inherited_marker(
14941494
assert array_value[idx] == expected_val, (
14951495
f"Value at index {idx}: expected '{expected_val}', got '{array_value[idx]}'"
14961496
)
1497+
1498+
1499+
def test_show_options_quiet(server: Server) -> None:
1500+
"""Test _show_options_raw with quiet flag suppresses errors."""
1501+
from libtmux.constants import OptionScope
1502+
1503+
# Query with quiet — should not raise even with unusual options
1504+
result = server._show_options_raw(
1505+
scope=OptionScope.Server,
1506+
quiet=True,
1507+
)
1508+
assert isinstance(result.stdout, list)
1509+
1510+
1511+
def test_set_environment_hidden(session: Session) -> None:
1512+
"""Test set_environment with hidden flag."""
1513+
session.set_environment("HIDDEN_TEST_VAR", "secret", hidden=True)
1514+
1515+
# Normal show_environment should NOT show hidden vars
1516+
env = session.show_environment()
1517+
assert "HIDDEN_TEST_VAR" not in env
1518+
1519+
1520+
def test_set_environment_expand_format(session: Session) -> None:
1521+
"""Test set_environment with expand_format flag."""
1522+
session.set_environment(
1523+
"FORMAT_TEST_VAR",
1524+
"#{session_name}",
1525+
expand_format=True,
1526+
)
1527+
1528+
env = session.show_environment()
1529+
# The value should have been expanded (not the raw format string)
1530+
assert "FORMAT_TEST_VAR" in env
1531+
assert env["FORMAT_TEST_VAR"] != "#{session_name}"

0 commit comments

Comments
 (0)