Skip to content

Commit b2a7923

Browse files
committed
Pane(feat[display_message]): add format, verbose, delay, notify, list, and style flags
why: display-message supports many useful flags for format queries and output control that were not exposed in the Python API. what: - Add format_string (-F), all_formats (-a), verbose (-v), no_expand (-I), target_client (-c), delay (-d), notify (-N), list_formats (-l, 3.4+), no_style (-C, 3.6+) parameters - Version-gate list_formats and no_style with has_gte_version - Fix cmd argument handling: only pass when non-empty - Add DisplayMessageCase NamedTuple parametrized tests
1 parent 6881d32 commit b2a7923

2 files changed

Lines changed: 181 additions & 3 deletions

File tree

src/libtmux/pane.py

Lines changed: 111 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -548,15 +548,49 @@ def display_message(
548548
self,
549549
cmd: str,
550550
get_text: t.Literal[True],
551+
*,
552+
format_string: str | None = ...,
553+
all_formats: bool | None = ...,
554+
verbose: bool | None = ...,
555+
no_expand: bool | None = ...,
556+
target_client: str | None = ...,
557+
delay: int | None = ...,
558+
notify: bool | None = ...,
559+
list_formats: bool | None = ...,
560+
no_style: bool | None = ...,
551561
) -> list[str]: ...
552562

553563
@t.overload
554-
def display_message(self, cmd: str, get_text: t.Literal[False]) -> None: ...
564+
def display_message(
565+
self,
566+
cmd: str,
567+
get_text: t.Literal[False] = ...,
568+
*,
569+
format_string: str | None = ...,
570+
all_formats: bool | None = ...,
571+
verbose: bool | None = ...,
572+
no_expand: bool | None = ...,
573+
target_client: str | None = ...,
574+
delay: int | None = ...,
575+
notify: bool | None = ...,
576+
list_formats: bool | None = ...,
577+
no_style: bool | None = ...,
578+
) -> None: ...
555579

556580
def display_message(
557581
self,
558582
cmd: str,
559583
get_text: bool = False,
584+
*,
585+
format_string: str | None = None,
586+
all_formats: bool | None = None,
587+
verbose: bool | None = None,
588+
no_expand: bool | None = None,
589+
target_client: str | None = None,
590+
delay: int | None = None,
591+
notify: bool | None = None,
592+
list_formats: bool | None = None,
593+
no_style: bool | None = None,
560594
) -> list[str] | None:
561595
"""Display message to pane.
562596
@@ -569,16 +603,90 @@ def display_message(
569603
get_text : bool, optional
570604
Returns only text without displaying a message in
571605
target-client status line.
606+
format_string : str, optional
607+
Format string for output (``-F`` flag).
608+
all_formats : bool, optional
609+
List all format variables (``-a`` flag).
610+
verbose : bool, optional
611+
Show format variable types (``-v`` flag).
612+
no_expand : bool, optional
613+
Suppress format expansion (``-I`` flag).
614+
target_client : str, optional
615+
Target client (``-c`` flag).
616+
delay : int, optional
617+
Display time in milliseconds (``-d`` flag).
618+
notify : bool, optional
619+
Do not wait for input (``-N`` flag).
620+
list_formats : bool, optional
621+
List format variables (``-l`` flag). Requires tmux 3.4+.
622+
623+
.. versionadded:: 0.45
624+
no_style : bool, optional
625+
Suppress style output (``-C`` flag). Requires tmux 3.6+.
626+
627+
.. versionadded:: 0.45
572628
573629
Returns
574630
-------
575631
list[str] | None
576632
Message output if get_text is True, otherwise None.
577633
"""
634+
import warnings
635+
636+
from libtmux.common import has_gte_version
637+
638+
tmux_args: tuple[str, ...] = ()
639+
640+
if get_text:
641+
tmux_args += ("-p",)
642+
643+
if all_formats:
644+
tmux_args += ("-a",)
645+
646+
if verbose:
647+
tmux_args += ("-v",)
648+
649+
if no_expand:
650+
tmux_args += ("-I",)
651+
652+
if notify:
653+
tmux_args += ("-N",)
654+
655+
if list_formats:
656+
if has_gte_version("3.4", tmux_bin=self.server.tmux_bin):
657+
tmux_args += ("-l",)
658+
else:
659+
warnings.warn(
660+
"list_formats requires tmux 3.4+, ignoring",
661+
stacklevel=2,
662+
)
663+
664+
if no_style:
665+
if has_gte_version("3.6", tmux_bin=self.server.tmux_bin):
666+
tmux_args += ("-C",)
667+
else:
668+
warnings.warn(
669+
"no_style requires tmux 3.6+, ignoring",
670+
stacklevel=2,
671+
)
672+
673+
if target_client is not None:
674+
tmux_args += ("-c", target_client)
675+
676+
if delay is not None:
677+
tmux_args += ("-d", str(delay))
678+
679+
if format_string is not None:
680+
tmux_args += ("-F", format_string)
681+
682+
if cmd:
683+
tmux_args += (cmd,)
684+
685+
proc = self.cmd("display-message", *tmux_args)
686+
578687
if get_text:
579-
return self.cmd("display-message", "-p", cmd).stdout
688+
return proc.stdout
580689

581-
self.cmd("display-message", cmd)
582690
return None
583691

584692
def kill(

tests/test_pane.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,3 +642,73 @@ def test_select_pane_disable_enable_input(session: Session) -> None:
642642
3,
643643
raises=True,
644644
)
645+
646+
647+
class DisplayMessageCase(t.NamedTuple):
648+
"""Test case for display_message() flag variations."""
649+
650+
test_id: str
651+
cmd: str
652+
kwargs: dict[str, t.Any]
653+
expected_in_output: str | None
654+
min_tmux_version: str | None
655+
656+
657+
DISPLAY_MESSAGE_CASES: list[DisplayMessageCase] = [
658+
DisplayMessageCase(
659+
test_id="format_string",
660+
cmd="",
661+
kwargs={"get_text": True, "format_string": "#{pane_id}"},
662+
expected_in_output="%",
663+
min_tmux_version=None,
664+
),
665+
DisplayMessageCase(
666+
test_id="all_formats",
667+
cmd="",
668+
kwargs={"get_text": True, "all_formats": True},
669+
expected_in_output="session_name",
670+
min_tmux_version=None,
671+
),
672+
DisplayMessageCase(
673+
test_id="verbose",
674+
cmd="",
675+
kwargs={"get_text": True, "verbose": True, "all_formats": True},
676+
expected_in_output="session_name",
677+
min_tmux_version=None,
678+
),
679+
DisplayMessageCase(
680+
test_id="list_formats",
681+
cmd="",
682+
kwargs={"get_text": True, "list_formats": True},
683+
expected_in_output=None,
684+
min_tmux_version="3.4",
685+
),
686+
]
687+
688+
689+
@pytest.mark.parametrize(
690+
list(DisplayMessageCase._fields),
691+
DISPLAY_MESSAGE_CASES,
692+
ids=[c.test_id for c in DISPLAY_MESSAGE_CASES],
693+
)
694+
def test_display_message_flags(
695+
test_id: str,
696+
cmd: str,
697+
kwargs: dict[str, t.Any],
698+
expected_in_output: str | None,
699+
min_tmux_version: str | None,
700+
session: Session,
701+
) -> None:
702+
"""Test display_message() with various flag combinations."""
703+
if min_tmux_version and not has_gte_version(min_tmux_version):
704+
pytest.skip(f"Requires tmux {min_tmux_version}+")
705+
706+
pane = session.active_window.active_pane
707+
assert pane is not None
708+
709+
result = pane.display_message(cmd, **kwargs)
710+
711+
if expected_in_output is not None:
712+
assert result is not None
713+
output = "\n".join(result)
714+
assert expected_in_output in output

0 commit comments

Comments
 (0)